summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kdx.42@42l.fr>2023-01-11 12:21:48 +0100
committerkdx <kdx.42@42l.fr>2023-01-11 12:21:48 +0100
commit62bb5a78215597890cbfd642387f97334fb4a6ec (patch)
tree52e1cb4de9be37616fbbc2282e13992a55087cb3
parenta0466bb648c096ed277ab2d662b2ef5c42f788bf (diff)
download42-containers-62bb5a78215597890cbfd642387f97334fb4a6ec.tar.gz
compile!
-rw-r--r--.gitignore1
-rw-r--r--vector.hpp38
2 files changed, 35 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index d7756c2..1fb08e4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
a.out
*.o
+containers_test/
diff --git a/vector.hpp b/vector.hpp
index a0f8adc..6c7143f 100644
--- a/vector.hpp
+++ b/vector.hpp
@@ -1,5 +1,6 @@
#pragma once
#include <memory>
+#include <cstddef>
namespace ft {
template <
@@ -22,6 +23,7 @@ namespace ft {
size_type _size;
size_type _capacity;
T *_data;
+ allocator_type _alloc;
public:
/* constructor */
vector();
@@ -113,10 +115,38 @@ namespace ft {
const vector<T,Alloc>& rhs);
template<class T, class Alloc>
- vector<T,Alloc>::vector()
+ vector<T,Alloc>::vector() : _data(NULL)
{
- _size = 0;
- _capacity = 0;
- _data = NULL;
+ vector(size_type(0), 0, allocator_type());
+ }
+ template<class T, class Alloc>
+ vector<T,Alloc>::vector(const Alloc& alloc) : _data(NULL)
+ {
+ vector(size_type(0), 0, alloc);
+ }
+ template<class T, class Alloc>
+ vector<T,Alloc>::vector(size_type count, const T& value,
+ const Alloc& alloc) : _data(NULL)
+ {
+ _alloc = alloc;
+ _size = count;
+ _capacity = count;
+ /* TODO check error */
+ if (count > 0)
+ _data = _alloc.allocate(count);
+ for (size_type i = 0; i < count; i++)
+ _alloc.construct(_data + i, value);
+ }
+ template<class T, class Alloc>
+ vector<T,Alloc>::~vector()
+ {
+ if (_data != NULL) {
+ for (size_type i = 0; i < _size; i++)
+ _alloc.destroy(_data + i);
+ _alloc.deallocate(_data, _capacity);
+ _size = 0;
+ _capacity = 0;
+ _data = NULL;
+ }
}
}