summaryrefslogtreecommitdiff
path: root/vector.hpp
blob: ad8b716add5fc3af4d8a0b7d9b70b4bfcf6c1e70 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#pragma once
#include <memory>
#include <limits>
#include <cstddef>
#include <stdexcept>
#include <iostream>

namespace ft {
	template <
		class T,
		class Allocator = std::allocator<T>
	> class vector;
	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);
}

template <class T, class Allocator> class ft::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;
	class iterator {
	public:
		typedef iterator self_type;
		typedef reference reference_type;
		typedef T* pointer_type;
	private:
		pointer_type _ptr;
	public:
		iterator(pointer_type ptr) : _ptr(ptr)
		{
		}
		iterator(const self_type& src) : _ptr(src._ptr)
		{
		}
		reference_type operator*()
		{
			return *_ptr;
		}
		pointer_type operator->()
		{
			return _ptr;
		}
		self_type& operator++()
		{
			_ptr++;
			return *this;
		}
		self_type& operator++(int)
		{
			self_type tmp (*this);
			_ptr++;
			return tmp;
		}
		self_type operator--()
		{
			_ptr--;
			return *this;
		}
		self_type operator--(int)
		{
			self_type tmp (*this);
			_ptr--;
			return tmp;
		}
		self_type& operator+=(difference_type n)
		{
			*this = *this + n;
			return *this;
		}
		self_type operator+(difference_type n)
		{
			self_type tmp (*this);
			tmp += n;
			return tmp;
		}
		self_type operator-(difference_type n)
		{
			self_type tmp (*this);
			tmp -= n;
			return tmp;
		}
		self_type& operator-=(difference_type n)
		{
			*this = *this - n;
			return *this;
		}
		bool operator==(const self_type& other) const
		{
			return _ptr == other._ptr;
		}
		bool operator!=(const self_type& other) const
		{
			return !(*this == other);
		}
	};
	class const_iterator {
	private:
		typedef iterator self_type;
		typedef const_reference reference_type;
		typedef const T* pointer_type;
		pointer_type _ptr;
	public:
		const_iterator(pointer_type ptr) : _ptr(ptr)
		{
		}
		const_iterator(const self_type& src) : _ptr(src._ptr)
		{
		}
		reference_type operator*()
		{
			return *_ptr;
		}
		pointer_type operator->()
		{
			return _ptr;
		}
		self_type& operator++()
		{
			_ptr++;
			return *this;
		}
		self_type& operator++(int)
		{
			self_type tmp (*this);
			_ptr++;
			return tmp;
		}
		self_type operator--()
		{
			_ptr--;
			return *this;
		}
		self_type operator--(int)
		{
			self_type tmp (*this);
			_ptr--;
			return tmp;
		}
		self_type& operator+=(difference_type n)
		{
			*this = *this + n;
			return *this;
		}
		self_type operator+(difference_type n)
		{
			self_type tmp (*this);
			tmp += n;
			return tmp;
		}
		self_type operator-(difference_type n)
		{
			self_type tmp (*this);
			tmp -= n;
			return tmp;
		}
		self_type& operator-=(difference_type n)
		{
			*this = *this - n;
			return *this;
		}
		bool operator==(const self_type& other) const
		{
			return _ptr == other._ptr;
		}
		bool operator!=(const self_type& other) const
		{
			return !(*this == other);
		}
	};
	class reverse_iterator {
	private:
		typedef reverse_iterator self_type;
		typedef reference reference_type;
		typedef T* pointer_type;
		pointer_type _ptr;
	public:
		reverse_iterator(pointer_type ptr) : _ptr(ptr)
		{
		}
		reverse_iterator(const self_type& src) : _ptr(src._ptr)
		{
		}
		reference_type operator*()
		{
			return *_ptr;
		}
		pointer_type operator->()
		{
			return _ptr;
		}
		self_type& operator++()
		{
			_ptr--;
			return *this;
		}
		self_type& operator++(int)
		{
			self_type tmp (*this);
			_ptr--;
			return tmp;
		}
		self_type operator--()
		{
			_ptr++;
			return *this;
		}
		self_type operator--(int)
		{
			self_type tmp (*this);
			_ptr++;
			return tmp;
		}
		self_type& operator+=(difference_type n)
		{
			*this = *this - n;
			return *this;
		}
		self_type operator+(difference_type n)
		{
			self_type tmp (*this);
			tmp += n;
			return tmp;
		}
		self_type operator-(difference_type n)
		{
			self_type tmp (*this);
			tmp -= n;
			return tmp;
		}
		self_type& operator-=(difference_type n)
		{
			*this = *this + n;
			return *this;
		}
		bool operator==(const self_type& other) const
		{
			return _ptr == other._ptr;
		}
		bool operator!=(const self_type& other) const
		{
			return !(*this == other);
		}
	};
	class const_reverse_iterator {
	private:
		typedef const_reverse_iterator self_type;
		typedef const_reference reference_type;
		typedef const T* pointer_type;
		pointer_type _ptr;
	public:
		const_reverse_iterator(pointer_type ptr) : _ptr(ptr)
		{
		}
		const_reverse_iterator(const self_type& src) : _ptr(src._ptr)
		{
		}
		reference_type operator*()
		{
			return *_ptr;
		}
		pointer_type operator->()
		{
			return _ptr;
		}
		self_type& operator++()
		{
			_ptr--;
			return *this;
		}
		self_type& operator++(int)
		{
			self_type tmp (*this);
			_ptr--;
			return tmp;
		}
		self_type operator--()
		{
			_ptr++;
			return *this;
		}
		self_type operator--(int)
		{
			self_type tmp (*this);
			_ptr++;
			return tmp;
		}
		self_type& operator+=(difference_type n)
		{
			*this = *this - n;
			return *this;
		}
		self_type operator+(difference_type n)
		{
			self_type tmp (*this);
			tmp += n;
			return tmp;
		}
		self_type operator-(difference_type n)
		{
			self_type tmp (*this);
			tmp -= n;
			return tmp;
		}
		self_type& operator-=(difference_type n)
		{
			*this = *this + n;
			return *this;
		}
		bool operator==(const self_type& other) const
		{
			return _ptr == other._ptr;
		}
		bool operator!=(const self_type& other) const
		{
			return !(*this == other);
		}
	};

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())
	{
		_alloc = alloc;
		_size = 0;
		while (first + _size != last)
			_size += 1;
		_capacity = _size;
		if (_capacity > 0)
			_data = _alloc.allocate(_capacity);
		for (size_type i = 0; i < _size; i++)
			_alloc.construct(_data + i, *(first + i));
	}

	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, _alloc);
	}

	template<class InputIt>
	void assign(InputIt first, InputIt last)
	{
		*this = vector(first, last, _alloc);
		/* 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 */
	typename iterator::self_type begin()
	{
		return iterator(_data);
	}

	typename const_iterator::self_type begin() const
	{
		return const_iterator(_data);
	}

	typename iterator::self_type end()
	{
		return iterator(_data + _size);
	}

	typename const_iterator::self_type end() const
	{
		return const_iterator(_data + _size);
	}

	typename reverse_iterator::self_type rbegin()
	{
		return reverse_iterator(_data + _size - 1);
	}

	typename const_reverse_iterator::self_type rbegin() const
	{
		return const_reverse_iterator(_data + _size - 1);
	}

	typename reverse_iterator::self_type rend()
	{
		return reverse_iterator(_data - 1);
	}

	typename const_reverse_iterator::self_type rend() const
	{
		return const_reverse_iterator(_data - 1);
	}

	/* 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);
	/* TODO */
	/* 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 ft::swap(vector<T,Alloc>& lhs,
              vector<T,Alloc>& rhs)
{
	lhs.swap(rhs);
}

template<class T, class Alloc>
bool ft::operator==(const vector<T,Alloc>& lhs,
                    const vector<T,Alloc>& rhs)
{
	if (lhs.size() != rhs.size())
		return false;
	for (typename vector<T,Alloc>::size_type i = 0; i < lhs.size(); i++)
		if (lhs[i] != rhs[i])
			return false;
	return true;
}

template<class T, class Alloc>
bool ft::operator!=(const vector<T,Alloc>& lhs,
                    const vector<T,Alloc>& rhs)
{
	return !(lhs == rhs);
}

template<class T, class Alloc>
bool ft::operator<(const vector<T,Alloc>& lhs,
                   const vector<T,Alloc>& rhs)
{
	const typename vector<T,Alloc>::size_type end
		(lhs.size() < rhs.size() ? lhs.size() : rhs.size());
	for (typename vector<T,Alloc>::size_type i = 0; i < end; i++) {
		if (lhs[i] < rhs[i])
			return true;
	}
	return lhs.size() < rhs.size();
}

template<class T, class Alloc>
bool ft::operator<=(const vector<T,Alloc>& lhs,
                    const vector<T,Alloc>& rhs)
{
	return !(lhs > rhs);
}

template<class T, class Alloc>
bool ft::operator>(const vector<T,Alloc>& lhs,
                   const vector<T,Alloc>& rhs)
{
	const typename vector<T,Alloc>::size_type end
		(lhs.size() < rhs.size() ? lhs.size() : rhs.size());
	for (typename vector<T,Alloc>::size_type i = 0; i < end; i++) {
		if (lhs[i] > rhs[i])
			return true;
	}
	return lhs.size() > rhs.size();
}

template<class T, class Alloc>
bool ft::operator>=(const vector<T,Alloc>& lhs,
                    const vector<T,Alloc>& rhs)
{
	return !(lhs < rhs);
}