summaryrefslogtreecommitdiff
path: root/vector.hpp
blob: c8f7dfbf15b3ff5d5760133795dc7eba9779c321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#pragma once
#include <memory>
#include <limits>
#include <cstddef>

namespace ft {
	template <
		class T,
		class Allocator = std::allocator<T>
	> class vector {
	private:
		typedef T value_type;
		typedef Allocator allocator_type;
		typedef std::size_t size_type;
		typedef std::ptrdiff_t difference_type;
		typedef value_type& reference;
		typedef const value_type& const_reference;
		typedef typename Allocator::pointer pointer;
		/* TODO */
		typedef int iterator;
		typedef int const_iterator;
		typedef int reverse_iterator;
		typedef int const_reverse_iterator;
		size_type _size;
		size_type _capacity;
		T *_data;
		allocator_type _alloc;
	public:
		/* constructor */
		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())
		{
			_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())
		{
			/* TODO */
		}

		vector(const vector& other)
		{
			*this = other;
		}

		/* destructor */
		~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)
		{
			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 */
		/* at */
		reference at(size_type pos);
		const_reference at(size_type pos) const;
		/* operator[] */
		reference operator[](size_type pos);
		const_reference operator[](size_type pos) const;
		/* front */
		reference front();
		const_reference front() const;
		/* back */
		reference back();
		const_reference back() const;
		/* data */
		T* data();
		const T* data() const;
		/* ITERATORS TODO */
		/* CAPACITY */
		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 */
		/* clear */
		void clear();
		/* insert */
		void insert();
		iterator insert(const_iterator pos, const T& value);
		iterator insert(const_iterator pos, size_type count,
		                const T& value);
		template<class InputIt>
		iterator insert(const_iterator pos, InputIt first,
		                InputIt last);
		/* erase */
		iterator erase(iterator pos);
		iterator erase(iterator first, iterator last);
		/* push_back */
		void push_back(const T& value);
		/* pop_back */
		void pop_back();
		/* resize */
		void resize(size_type count);
		void resize(size_type count, const value_type& value);
		/* swap */
		void swap(vector& other);
	};
	template<class T, class Alloc>
	void swap(vector<T,Alloc>& lhs,
		  vector<T,Alloc>& rhs);
	template<class T, class Alloc>
	bool operator==(const vector<T,Alloc>& lhs,
	                const vector<T,Alloc>& rhs);
	template<class T, class Alloc>
	bool operator!=(const vector<T,Alloc>& lhs,
	                const vector<T,Alloc>& rhs);
	template<class T, class Alloc>
	bool operator<(const vector<T,Alloc>& lhs,
	               const vector<T,Alloc>& rhs);
	template<class T, class Alloc>
	bool operator<=(const vector<T,Alloc>& lhs,
	                const vector<T,Alloc>& rhs);
	template<class T, class Alloc>
	bool operator>(const vector<T,Alloc>& lhs,
	               const vector<T,Alloc>& rhs);
	template<class T, class Alloc>
	bool operator>=(const vector<T,Alloc>& lhs,
	                const vector<T,Alloc>& rhs);
}