summaryrefslogtreecommitdiff
path: root/vector.hpp
blob: b43d9941dad007339f0ed4d9080aafba56f5990a (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#pragma once
#include <memory>
#include <limits>
#include <cstddef>
#include <stdexcept>

namespace ft {
	template <
		class T,
		class Allocator = std::allocator<T>
	> class vector {
	public:
		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 vector* iterator;
		typedef vector* const_iterator;
		typedef vector* reverse_iterator;
		typedef vector* const_reverse_iterator;
	private:
		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;
			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) : _data(NULL)
		{
			*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();
			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)
		{
			*this = vector(count, value);
		}

		template<class InputIt>
		void assign(InputIt first, InputIt last)
		{
			/* TODO */
		}

		/* get_allocator */
		allocator_type get_allocator() const
		{
			return _alloc;
		}

		/* ELEMENT ACCESS */
		/* at */
		reference at(size_type pos)
		{
			if (pos >= _size)
				throw std::out_of_range("pos out of bounds");
			return _data[pos];
		}

		const_reference at(size_type pos) const
		{
			if (pos >= _size)
				throw std::out_of_range("pos out of bounds");
			return _data[pos];
		}

		/* operator[] */
		reference operator[](size_type pos)
		{
			return _data[pos];
		}

		const_reference operator[](size_type pos) const
		{
			return _data[pos];
		}

		/* front */
		reference front()
		{
			return _data[0];
		}

		const_reference front() const
		{
			return _data[0];
		}

		/* back */
		reference back()
		{
			return _data[_size - 1];
		}

		const_reference back() const
		{
			return _data[_size - 1];
		}

		/* data */
		T* data()
		{
			return _data;
		}

		const T* data() const
		{
			return _data;
		}

		/* 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)
		{
			if (new_cap <= _capacity)
				return;
			if (new_cap > max_size())
				throw std::length_error("new_cap > max_size()");
			T *const new_data (_alloc.allocate(new_cap));
			for (size_type i = 0; i < _size; i++) {
				_alloc.construct(new_data + i, _data[i]);
				_alloc.destroy(_data + i);
			}
			_alloc.deallocate(_data, _capacity);
			_capacity = new_cap;
			_data = new_data;
		}

		size_type capacity() const
		{
			return _capacity;
		}

		/* MODIFIERS */
		/* clear */
		void clear()
		{
			for (size_type i = 0; i < _size; i++)
				_alloc.destroy(_data + i);
			_size = 0;
		}

		/* 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)
		{
			if (_size == _capacity)
				reserve(_size * 2);
			_alloc.construct(_data + _size, value);
			_size += 1;
		}

		/* pop_back */
		void pop_back()
		{
			_alloc.destroy(_data + _size - 1);
			_size -= 1;
		}

		/* resize */
		void resize(size_type count)
		{
			return resize(count, T());
		}

		void resize(size_type count, const value_type& value)
		{
			if (count < _size) {
				for (size_type i = count; i < _size; i++)
					_alloc.destroy(_data + i);
				_size = count;
				return;
			}
			reserve(count);
			for (size_type i = _size; i < count; i++)
				_alloc.construct(_data + i, value);
			_size = count;
		}

		/* swap */
		void swap(vector& other)
		{
			const size_type tmp_size (_size);
			const size_type tmp_capacity (_capacity);
			const T *tmp_data (_data);
			const allocator_type tmp_alloc (_alloc);
			_size = other._size;
			_capacity = other._capacity;
			_data = other._data;
			_alloc = other._alloc;
			other._size = tmp_size;
			other._capacity = tmp_capacity;
			other._data = tmp_data;
			other._alloc = tmp_alloc;
		}
	};
	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);
}