diff --git a/td/td3/include/deque.h b/td/td3/include/deque.h index e93b7ba..c2e2bf7 100644 --- a/td/td3/include/deque.h +++ b/td/td3/include/deque.h @@ -19,7 +19,7 @@ public: void RemoveBack(void); void ShrinkToFit(void); private: - size_t _frontIndex; + int64_t _frontIndex; }; template @@ -59,7 +59,7 @@ Deque Deque::GetFront(void) 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++) + for(int64_t i=0; i< this->_frontIndex+1 ; i++) returnDeque._values[i] = this->_values[i]; return returnDeque; } diff --git a/td/td4/include/deque.h b/td/td4/include/deque.h new file mode 100644 index 0000000..c2e2bf7 --- /dev/null +++ b/td/td4/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: + int64_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(int64_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/td4/include/smart_vector.h b/td/td4/include/smart_vector.h new file mode 100644 index 0000000..fd60fd0 --- /dev/null +++ b/td/td4/include/smart_vector.h @@ -0,0 +1,47 @@ +#ifndef HEADER_SMART_VECTOR +#define HEADER_SMART_VECTOR + +#include "vector.h" +#include + +template +class Smart_Vector : public Vector { +public: + Smart_Vector() : Vector() {}; + Smart_Vector(size_t capacity): Vector(capacity) {}; + Smart_Vector(size_t capacity, T value): Vector(capacity,value) {}; + Smart_Vector(const Vector& other) : Vector(other) {}; + Smart_Vector operator+(const Smart_Vector& other); + Smart_Vector& operator+=(const Smart_Vector& other); +}; + +template +Smart_Vector Smart_Vector::operator+(const Smart_Vector& other) +{ + // Gathering the vector with the greater size. + auto& maxVector = (this->_size > other._size ? *this : other); + Smart_Vector returnVector(maxVector._size); + returnVector._size = returnVector._capacity; + size_t i(0); + + // Adding both vectors from 0 to the minimum size + for(i=0; i_size,other._size); i++) + returnVector._values[i] = this->_values[i] + other._values[i]; + // Then simply copying the max vector values onto the return vector. + for(size_t j=i; j +Smart_Vector& Smart_Vector::operator+=(const Smart_Vector& other) +{ + size_t i(0), minimumSize(std::min(this->_size,other._size)); + for(i = 0; i_values[i] += other._values[i]; + for(size_t j=i; jPushBack(other._values[j]); + return *this; +} + +#endif // HEADER_SMART_VECTOR \ No newline at end of file diff --git a/td/td4/include/vector.h b/td/td4/include/vector.h new file mode 100644 index 0000000..8a68376 --- /dev/null +++ b/td/td4/include/vector.h @@ -0,0 +1,179 @@ +#ifndef HEADER_VECTOR +#define HEADER_VECTOR + +#include +#include + +template +class Vector { +public: + Vector(); + Vector(size_t capacity); + Vector(size_t capacity, T value); + Vector(const Vector& other); + ~Vector(); + size_t Size(); + size_t Capacity(); + void PushBack(T value); + template + friend std::ostream& operator<<(std::ostream& stream, const Vector& vec); + template + friend std::istream& operator>>(std::istream& stream, Vector& vec); + Vector& operator=(const Vector& other); + Vector& operator()(const Vector& other); + T& operator[](const int indice); + Vector operator+(const Vector& other); + Vector& operator+=(const Vector& other); + +protected: + size_t _capacity; + size_t _size; + T* _values; +}; + +template +Vector::Vector() + : _capacity(1) + , _size(0) + , _values(new T[1]) +{ +} + +template +Vector::Vector(size_t capacity) + : _capacity(capacity) + , _size(0) +{ + // Allocating the heap with the given capacity + _values = new T[capacity]; +} + +template +Vector::Vector(size_t capacity, T value) + : _capacity(capacity) + , _size(capacity) +{ + // Allocating the heap and setting the value to each element. + _values = new T[capacity]; + for (size_t i = 0; i < capacity; i++) + _values[i] = value; +} + +template +Vector::Vector(const Vector& other) + : _capacity(other._capacity) + , _size(other._size) + , _values(new T[other._capacity]) +{ + for (size_t i = 0; i < other._size; i++) + _values[i] = other._values[i]; +} + +template +Vector::~Vector() +{ + delete[] _values; +} + +template +Vector& Vector::operator=(const Vector& other) +{ + if (this != &other) { + this->_capacity = other._capacity; + this->_size = other._size; + delete[] this->_values; + this->_values = new T[other._capacity]; + for (size_t i = 0; i < other._size; i++) + this->_values[i] = other._values[i]; + } + return *this; +} + +template +Vector& Vector::operator()(const Vector& other) +{ + *this = other; + return *this; +} + +template +size_t Vector::Capacity() { return _capacity; } +template +size_t Vector::Size() { return _size; } + +template +void Vector::PushBack(T value) +{ + // if the next size will overload the heap + if (_capacity < _size + 1) { + // Keep a track of the old pointer + T* oldPtr = _values; + // Creating a new pointer by resizing its capacity by factor 2. + _values = new T[_size * 2]; + _capacity *= 2; + // Copying the old array onto the new + for (size_t i = 0; i < _size; i++) + _values[i] = oldPtr[i]; + // Deallocating the old array + delete[] oldPtr; + } + // Setting the given value and incrementing the size + _values[_size] = value; + _size++; +} + +// methode get +template +std::ostream& operator<<(std::ostream& stream, const Vector& vec) +{ + for (size_t i = 0; i < vec._size; i++) + stream << vec._values[i] << ", "; + return stream; +} + +// methode put +template +std::istream& operator>>(std::istream& stream, Vector& vec) +{ + for (size_t i = 0; i < vec._capacity; i++) + stream >> vec._values[i]; + vec._size = vec._capacity; + return stream; +} + +template +T& Vector::operator[](const int indice) +{ + if (static_cast(indice) > _size) + throw "Indice from the vector is not reachable"; + else + return _values[indice]; +} + +template +Vector Vector::operator+(const Vector& other) +{ + if (_size != other._size) + throw "Cannot add two vectors with different sizes together"; + else { + Vector returnVector(_size); + returnVector._size = _size; + for (size_t i = 0; i < _size; i++) + returnVector._values[i] = _values[i] + other._values[i]; + return returnVector; + } +} + +template +Vector& Vector::operator+=(const Vector& other) +{ + if (_size != other._size) + throw "Cannot add two vectors with different sizes together"; + else { + for (size_t i = 0; i < _size; i++) + _values[i] += other._values[i]; + return *this; + } +} + +#endif // HEADER_VECTOR \ No newline at end of file diff --git a/td/td4/src/main.cpp b/td/td4/src/main.cpp new file mode 100644 index 0000000..d6db6b9 --- /dev/null +++ b/td/td4/src/main.cpp @@ -0,0 +1,50 @@ +#include "../include/vector.h" +#include "../include/smart_vector.h" +#include "../include/deque.h" +// Joker : slice.h +// Joker : vector_iteraor.h +#include + +int main(void) +{ + Smart_Vector smartInt, test(10,6); + for(int i=0; i<6; i++) + smartInt.PushBack(i); + + std::cout << smartInt << std::endl; + std::cout << test << std::endl; + + std::cout << smartInt + test << std::endl; + std::cout << smartInt + smartInt << std::endl; + + Smart_Vector little(5,1), big(10,5); + little += big; + std::cout << little << std::endl; + 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