summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kdx.42@42l.fr>2023-01-11 13:39:22 +0100
committerkdx <kdx.42@42l.fr>2023-01-11 13:39:22 +0100
commit87e96b663e3f006dc9222ad058024a37ec20acc7 (patch)
treec8463b1aa45baa4fbadc524e78714182c4abd9d4
parent62bb5a78215597890cbfd642387f97334fb4a6ec (diff)
download42-containers-87e96b663e3f006dc9222ad058024a37ec20acc7.tar.gz
c'est impossible de bosser avec les deux bozos
-rw-r--r--main.cpp5
-rw-r--r--vector.hpp124
2 files changed, 81 insertions, 48 deletions
diff --git a/main.cpp b/main.cpp
index bd7b6ec..d567012 100644
--- a/main.cpp
+++ b/main.cpp
@@ -2,7 +2,8 @@
int main()
{
- ft::vector<int> vec;
- (void)vec;
+ ft::vector<int> vec(10);
+ ft::vector<int> other(vec);
+ vec = other;
return 0;
}
diff --git a/vector.hpp b/vector.hpp
index 6c7143f..c8f7dfb 100644
--- a/vector.hpp
+++ b/vector.hpp
@@ -1,5 +1,6 @@
#pragma once
#include <memory>
+#include <limits>
#include <cstddef>
namespace ft {
@@ -26,23 +27,81 @@ namespace ft {
allocator_type _alloc;
public:
/* constructor */
- vector();
- explicit vector(const Allocator& alloc);
+ vector()
+ {
+ vector(size_type(0), T(), allocator_type());
+ }
+
+ explicit vector(const Allocator& alloc) {
+ vector(size_type(0), T(), alloc);
+ }
+
explicit vector(size_type count,
const T& value = T(),
- const Allocator& alloc = Allocator());
+ const Allocator& alloc = Allocator())
+ {
+ _alloc = alloc;
+ _size = count;
+ _capacity = count;
+ /* TODO check error */
+ if (_capacity > 0)
+ _data = _alloc.allocate(_capacity);
+ for (size_type i = 0; i < count; i++)
+ _alloc.construct(_data + i, value);
+ }
+
template<class InputIt>
vector(InputIt first, InputIt last,
- const Allocator&alloc = Allocator());
- vector(const vector& other);
+ const Allocator& alloc = Allocator())
+ {
+ /* TODO */
+ }
+
+ vector(const vector& other)
+ {
+ *this = other;
+ }
+
/* destructor */
- ~vector();
+ ~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;
+ }
+ }
+
/* operator= */
- vector& operator=(const vector& other);
+ vector& operator=(const vector& other)
+ {
+ 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;
+ }
+ _alloc = allocator_type();
+ _size = other.size();
+ _capacity = other.size();
+ /* TODO check error */
+ if (_capacity > 0)
+ _data = _alloc.allocate(_capacity);
+ for (size_type i = 0; i < other.size(); i++)
+ _alloc.construct(_data + i, other._data[i]);
+ return *this;
+ }
+
/* assign */
void assign(size_type count, const T& value);
template<class InputIt>
void assign(InputIt first, InputIt last);
+
/* get_allocator */
allocator_type get_allocator() const;
/* ELEMENT ACCESS */
@@ -63,9 +122,18 @@ namespace ft {
const T* data() const;
/* ITERATORS TODO */
/* CAPACITY */
- bool empty() const;
- size_type size() const;
- size_type max_size() const;
+ bool empty() const
+ {
+ return _size == 0;
+ }
+ size_type size() const
+ {
+ return _size;
+ }
+ size_type max_size() const
+ {
+ return std::numeric_limits<difference_type>::max();
+ }
void reserve(size_type new_cap);
size_type capacity() const;
/* MODIFIERS */
@@ -113,40 +181,4 @@ namespace ft {
template<class T, class Alloc>
bool operator>=(const vector<T,Alloc>& lhs,
const vector<T,Alloc>& rhs);
-
- template<class T, class Alloc>
- vector<T,Alloc>::vector() : _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;
- }
- }
}