From 0e13870efe85090d787678aa799c685204d61226 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Tue, 3 Oct 2023 21:43:18 +0200 Subject: [PATCH] TD3 Joker : Added Deque Inheritance. --- td/td3/include/deque.h | 104 +++++++++++++++++++++++++++++++++++++++++ td/td3/src/main.cpp | 26 +++++++++++ 2 files changed, 130 insertions(+) create mode 100644 td/td3/include/deque.h diff --git a/td/td3/include/deque.h b/td/td3/include/deque.h new file mode 100644 index 0000000..e93b7ba --- /dev/null +++ b/td/td3/include/deque.h @@ -0,0 +1,104 @@ +#ifndef HEADER_DEQUE +#define HEADER_DEQUE + +#include "vector.h" +#include +#include + +template +class Deque : public Vector { +public: + Deque() : Vector(), _frontIndex(-1) {}; + Deque(size_t capacity): Vector(capacity), _frontIndex(-1) {}; + Deque(size_t capacity, T value): Vector(capacity,value), _frontIndex(-1) {}; + Deque(const Vector& other) : Vector(other), _frontIndex(-1) {}; + void PushFront(T value); + Deque GetFront(void); + Deque GetBack(void); + void RemoveFront(void); + void RemoveBack(void); + void ShrinkToFit(void); +private: + size_t _frontIndex; +}; + +template +void Deque::PushFront(T value) +{ + // if the next size will overload the heap + // We create a new allocation, and shift each data + if (this->_capacity < this->_size + 1) { + auto oldPtr = this->_values; + this->_values = new T[this->_size * 2]; + this->_capacity *= 2; + for (size_t i = 0; i < this->_size; i++) + this->_values[i+1] = oldPtr[i]; + delete[] oldPtr; + } + // Else we only shift the data + else{ + // _values[0] will be overwritten, so it serves as a temporary value + // To store the overwritten index + auto tempActual(this->_values[0]); + for(size_t i=0; i< this->_size ; i++) + { + this->_values[0] = this->_values[i+1]; + this->_values[i+1] = tempActual; + tempActual = this->_values[0]; + } + } + this->_values[0] = value; + this->_size++; + _frontIndex++; +} + +template +Deque Deque::GetFront(void) +{ + if(this->_frontIndex == -1) + throw "This Deque doesn't have front elements"; + Deque returnDeque(this->_frontIndex+1); + returnDeque._size = this->_frontIndex+1; + for(size_t i=0; i< this->_frontIndex+1 ; i++) + returnDeque._values[i] = this->_values[i]; + return returnDeque; +} + +template +Deque Deque::GetBack(void) +{ + Deque returnDeque(this->_size-(this->_frontIndex+1)); + returnDeque._size = this->_size-(this->_frontIndex+1); + for(size_t i = this->_frontIndex+1, j=0 ; i_size ; i++, j++) + returnDeque._values[j] = this->_values[i]; + return returnDeque; +} + +template +void Deque::RemoveFront(void) +{ + for(size_t i = this->_frontIndex+1, j=0 ; i_size ; i++, j++) + this->_values[j] = this->_values[i]; + this->_size -= (this->_frontIndex+1); + this->_frontIndex = -1; +} + +// The Capacity is not shrunk during this process. +template +void Deque::RemoveBack(void) +{ + this->_size = this->_frontIndex+1; +} + +template +void Deque::ShrinkToFit(void) +{ + auto oldPtr = this->_values; + this->_values = new T[this->_size]; + for(size_t i=0; i_size; i++) + this->_values[i] = oldPtr[i]; + delete[] oldPtr; + this->_capacity = this->_size; +} + +#endif //HEADER_DEQUE \ No newline at end of file diff --git a/td/td3/src/main.cpp b/td/td3/src/main.cpp index 73bdc37..d6db6b9 100644 --- a/td/td3/src/main.cpp +++ b/td/td3/src/main.cpp @@ -1,5 +1,8 @@ #include "../include/vector.h" #include "../include/smart_vector.h" +#include "../include/deque.h" +// Joker : slice.h +// Joker : vector_iteraor.h #include int main(void) @@ -20,5 +23,28 @@ int main(void) little = Smart_Vector(5,1); big += little; std::cout << big << std::endl; + + // Deque test + Deque dequeInt; + for(int i=0; i<6; i++) + dequeInt.PushBack(i); + std::cout << "Deque push back: " << dequeInt << std::endl; + for(int i=1; i<=6; i++) + dequeInt.PushFront(i*10); + std::cout << "Deque push front: " << dequeInt << std::endl; + std::cout << "End capacity: " << dequeInt.Capacity() << std::endl; + + std::cout << "Deque Front Elements: " << dequeInt.GetFront() << std::endl; + std::cout << "Deque Back Elements: " << dequeInt.GetBack() << std::endl; + dequeInt.RemoveBack(); + std::cout << "After Removing Back: " << dequeInt << std::endl; + dequeInt.PushBack(56); + std::cout << "Pushing Back again: " << dequeInt << std::endl; + dequeInt.RemoveFront(); + std::cout << "After Removing Front: " << dequeInt << std::endl; + dequeInt.PushFront(10); + std::cout << "Front: " << dequeInt.GetFront() << "\tBack: " << dequeInt.GetBack() << "\tFull Deque: " << dequeInt << "\tWith Capacity: " << dequeInt.Capacity() << std::endl; + dequeInt.ShrinkToFit(); + std::cout << "After ShrinkToFit: " << dequeInt.Capacity() << "\tWith: " << dequeInt << std::endl; return 0; } \ No newline at end of file