From ef7c7406c9931fec28c3c37ab4fd4f938f6f4487 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Mon, 25 Sep 2023 12:04:16 +0200 Subject: [PATCH] TD1 Question 1 --- td/td1/include/vector.h | 14 ++++++-- td/td1/src/main.cpp | 21 ++++++++++-- td/td1/src/vector.cpp | 73 +++++++++++++++++++++++++++++++++++------ 3 files changed, 93 insertions(+), 15 deletions(-) diff --git a/td/td1/include/vector.h b/td/td1/include/vector.h index 8da96bc..f4af047 100644 --- a/td/td1/include/vector.h +++ b/td/td1/include/vector.h @@ -1,13 +1,23 @@ #ifndef HEADER_VECTOR #define HEADER_VECTOR +#include + +template class Vector{ public: Vector(); + Vector(size_t capacity); + Vector(size_t capacity, T value); ~Vector(); - void Affiche(); + void Display(); + size_t Size(); + size_t Capacity(); + void PushBack(T value); private: - int _value; + T* _values; + size_t _capacity; + size_t _size; }; #endif //HEADER_VECTOR \ No newline at end of file diff --git a/td/td1/src/main.cpp b/td/td1/src/main.cpp index d6b8011..e14b9ce 100644 --- a/td/td1/src/main.cpp +++ b/td/td1/src/main.cpp @@ -3,8 +3,23 @@ int main(void) { - Vector coolVector; - coolVector.Affiche(); - std::cout << "Hello world!" << std::endl; + Vector coolVector; + std::cout << "Actual size:" << coolVector.Size() << "\tCapacity: " << coolVector.Capacity() << std::endl; + coolVector.Display(); + coolVector.PushBack(123); + coolVector.Display(); + std::cout << "Actual size:" << coolVector.Size() << "\tCapacity: " << coolVector.Capacity() << std::endl; + coolVector.PushBack(124); + coolVector.Display(); + std::cout << "Actual size:" << coolVector.Size() << "\tCapacity: " << coolVector.Capacity() << std::endl; + coolVector.PushBack(125); + coolVector.Display(); + std::cout << "Actual size:" << coolVector.Size() << "\tCapacity: " << coolVector.Capacity() << std::endl; + coolVector.PushBack(126); + coolVector.Display(); + std::cout << "Actual size:" << coolVector.Size() << "\tCapacity: " << coolVector.Capacity() << std::endl; + + Vector anotherCoolVector(10,1.2); + anotherCoolVector.Display(); return 0; } \ No newline at end of file diff --git a/td/td1/src/vector.cpp b/td/td1/src/vector.cpp index bb7dae0..9e26d0b 100644 --- a/td/td1/src/vector.cpp +++ b/td/td1/src/vector.cpp @@ -1,14 +1,67 @@ #include "../include/vector.h" +#include #include -Vector::Vector() - : _value(1) -{} - -Vector::~Vector() -{} - -void Vector::Affiche() +template +Vector::Vector() + : _capacity(1) + , _size(0) { - std::cout << "Vector: " << _value << std::endl; -} \ No newline at end of file + _values = new T[1]; +} + +template +Vector::Vector(size_t capacity) + : _capacity(capacity) + , _size(0) +{ + _values = new T[capacity]; +} + +template +Vector::Vector(size_t capacity, T value) + : _capacity(capacity) + , _size(capacity) +{ + _values = new T[capacity]; + for(size_t i = 0; i +Vector::~Vector() +{ + delete[] _values; +} + +template +void Vector::Display() +{ + for(size_t i = 0; i<_size; i++) + std::cout << _values[i] << std::endl; +} + +template +size_t Vector::Capacity() { return _capacity; } +template +size_t Vector::Size() { return _size; } + +template +void Vector::PushBack(T value) +{ + if (_capacity < _size+1) + { + T* oldPtr = _values; + _values = new T[_size*2]; + _capacity*=2; + for(size_t i=0; i<_size; i++) + _values[i] = oldPtr[i]; + delete[] oldPtr; + } + _values[_size] = value; + _size++; +} + +template class Vector; +template class Vector; \ No newline at end of file