-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.hpp
799 lines (705 loc) · 19.9 KB
/
list.hpp
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aisraely <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/08 13:15:25 by aisraely #+# #+# */
/* Updated: 2022/01/08 13:15:25 by aisraely ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_HPP
# define LIST_HPP
# include "memory.hpp"
# include "iterator.hpp"
# include "algorithm.hpp"
# include "type_traits.hpp"
# include "utility.hpp"
# include "functional.hpp"
namespace ft
{
// util structures for list
struct list_node_base
{
list_node_base() : next(), prev() { }
list_node_base *next;
list_node_base *prev;
};
template <class T>
struct list_node : public list_node_base
{
T data;
};
// list iterators
template <class T>
class list_const_iterator;
template <class T>
class list_iterator
{
private:
template <class T1, class Alloc1>
friend class list;
template <class T1>
friend bool operator==(const list_iterator<T1> &lhs, const list_iterator<T1> &rhs);
template <class T1>
friend class list_const_iterator;
template <class T1>
friend bool operator==(const list_iterator<T1> &lhs, const list_const_iterator<T1> &rhs);
template <class T1>
friend bool operator==(const list_const_iterator<T1> &lhs, const list_iterator<T1> &rhs);
// member variables
list_node_base *_node;
public:
// member types
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T& reference;
typedef T* pointer;
typedef std::bidirectional_iterator_tag iterator_category;
list_iterator() : _node() { }
list_iterator(list_node_base *_node) : _node(_node) { }
list_iterator(const list_iterator ©) : _node(copy._node) { }
~list_iterator() { }
list_iterator &operator=(const list_iterator &rhs)
{
this->_node = rhs._node;
return (*this);
}
reference operator*() const
{
return (static_cast<list_node<T>*>(this->_node)->data);
}
list_iterator &operator++()
{
this->_node = this->_node->next;
return (*this);
}
list_iterator operator++(int)
{
list_iterator temp(*this);
operator++();
return (temp);
}
list_iterator &operator--()
{
this->_node = this->_node->prev;
return (*this);
}
list_iterator operator--(int)
{
list_iterator temp(*this);
operator--();
return (temp);
}
pointer operator->() const
{
return (ft::addressof(operator*()));
}
};
template <class T>
bool operator==(const list_iterator<T> &lhs, const list_iterator<T> &rhs)
{
return (lhs._node == rhs._node);
}
template <class T>
bool operator!=(const list_iterator<T> &lhs, const list_iterator<T> &rhs)
{
return (!(lhs == rhs));
}
template <class T>
class list_const_iterator
{
private:
template <class T1, class Alloc1>
friend class list;
template <class T1>
friend bool operator==(const list_const_iterator<T1> &lhs, const list_const_iterator<T1> &rhs);
template <class T1>
friend bool operator==(const list_iterator<T1> &lhs, const list_const_iterator<T1> &rhs);
template <class T1>
friend bool operator==(const list_const_iterator<T1> &lhs, const list_iterator<T1> &rhs);
// member variables
const list_node_base *_node;
public:
// member types
typedef list_iterator<T> iterator;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef const T& reference;
typedef const T* pointer;
typedef std::bidirectional_iterator_tag iterator_category;
list_const_iterator() : _node() { }
list_const_iterator(const list_node_base *_node) : _node(_node) { }
list_const_iterator(const iterator ©) : _node(copy._node) { }
~list_const_iterator() { }
list_const_iterator &operator=(const list_const_iterator &rhs)
{
this->_node = rhs._node;
return (*this);
}
list_const_iterator &operator++()
{
this->_node = this->_node->next;
return (*this);
}
list_const_iterator operator++(int)
{
list_const_iterator temp(*this);
operator++();
return (temp);
}
list_const_iterator &operator--()
{
this->_node = this->_node->prev;
return (*this);
}
list_const_iterator operator--(int)
{
list_const_iterator temp(*this);
operator--();
return (temp);
}
reference operator*() const
{
return (static_cast<const list_node<T>*>(this->_node)->data);
}
pointer operator->() const
{
return (ft::addressof(operator*()));
}
};
template <class T>
bool operator==(const list_const_iterator<T> &lhs, const list_const_iterator<T> &rhs)
{
return (lhs._node == rhs._node);
}
template <class T>
bool operator!=(const list_const_iterator<T> &lhs, const list_const_iterator<T> &rhs)
{
return (!(lhs == rhs));
}
template <class T>
bool operator!=(const list_iterator<T> &lhs, const list_const_iterator<T> &rhs)
{
return (!(lhs == rhs));
}
template <class T>
bool operator!=(const list_const_iterator<T> &lhs, const list_iterator<T> &rhs)
{
return (!(lhs == rhs));
}
template <class T>
bool operator==(const list_iterator<T> &lhs, const list_const_iterator<T> &rhs)
{
return (lhs._node == rhs._node);
}
template <class T>
bool operator==(const list_const_iterator<T> &lhs, const list_iterator<T> &rhs)
{
return (lhs._node == rhs._node);
}
template <class T, class Alloc = std::allocator<T> >
class list
{
protected:
typedef list_node<T> node;
typedef typename Alloc::template rebind<node>::other node_allocator_type;
public:
// member types
typedef T value_type;
typedef Alloc allocator_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef list_iterator<value_type> iterator;
typedef list_const_iterator<value_type> const_iterator;
typedef typename iterator_traits<iterator>::difference_type difference_type;
typedef size_t size_type;
typedef typename ft::reverse_iterator<iterator> reverse_iterator;
typedef typename ft::reverse_iterator<const_iterator> const_reverse_iterator;
protected:
node_allocator_type _alloc;
list_node_base _header;
list_node_base _trailer;
size_type _size;
public:
// (constructor)
explicit list(const allocator_type &alloc = allocator_type()) : _alloc(alloc), _header(), _trailer(), _size(0)
{
this->_header.next = &this->_trailer;
this->_trailer.prev = &this->_header;
}
explicit list(size_type n, const value_type &val = value_type(),
const allocator_type &alloc = allocator_type()) : _alloc(alloc), _header(), _trailer(), _size(0)
{
this->_header.next = &this->_trailer;
this->_trailer.prev = &this->_header;
fill_assign(n, val);
}
template <class InputIterator>
list(InputIterator first, InputIterator last, const allocator_type &alloc = allocator_type()) : _alloc(alloc), _header(), _trailer(), _size(0)
{
this->_header.next = &this->_trailer;
this->_trailer.prev = &this->_header;
constructor_dispatch(first, last, typename ft::is_integral<InputIterator>::type());
}
private:
template <class InputIterator>
void constructor_dispatch(InputIterator first, InputIterator last, false_type)
{
// template argument was an iterator, proceed
range_assign(first, last);
}
template <typename Integral>
void constructor_dispatch(Integral n, Integral val, true_type)
{
fill_assign(n, val);
}
public:
list(const list &x) : _alloc(x._alloc), _header(), _trailer(), _size(0)
{
this->_header.next = &this->_trailer;
this->_trailer.prev = &this->_header;
range_assign(x.begin(), x.end());
}
list &operator=(const list& x)
{
if (&x != this)
range_assign(x.begin(), x.end());
return (*this);
}
// (destructor)
~list()
{
clear();
}
// iterators
iterator begin()
{
return (iterator(this->_header.next));
}
const_iterator begin() const
{
return (const_iterator(this->_header.next));
}
iterator end()
{
return (iterator(&this->_trailer));
}
const_iterator end() const
{
return (const_iterator(&this->_trailer));
}
reverse_iterator rbegin()
{
return (reverse_iterator(end()));
}
const_reverse_iterator rbegin() const
{
return (const_reverse_iterator(end()));
}
reverse_iterator rend()
{
return (reverse_iterator(begin()));
}
const_reverse_iterator rend() const
{
return (const_reverse_iterator(begin()));
}
// capacity
bool empty() const
{
return (!this->_size);
}
size_type size() const
{
return (this->_size);
}
size_type max_size() const
{
return (this->_alloc.max_size());
}
// element access
reference front()
{
return (*begin());
}
const_reference front() const
{
return (*begin());
}
reference back()
{
return (*(--end()));
}
const_reference back() const
{
return (*(--end()));
}
// modifiers
void assign(size_type n, const value_type &val)
{
fill_assign(n, val);
}
template <class InputIterator>
void assign(InputIterator first, InputIterator last)
{
assign_dispatch(first, last, typename ft::is_integral<InputIterator>::type());
}
private:
template <class InputIterator>
void assign_dispatch(InputIterator first, InputIterator last, false_type)
{
range_assign(first, last);
}
template <class Integral>
void assign_dispatch(Integral n, Integral val, true_type)
{
fill_assign(n, val);
}
template <class InputIterator>
void range_assign(InputIterator first, InputIterator last)
{
iterator begin = this->begin();
while (first != last and begin != end())
{
*begin = *first;
first++;
begin++;
}
// if this < than range, insert the rest at the end,
if (begin == end())
insert(end(), first, last);
// else if range is shorter, erase the rest till end
else
erase(begin, end());
}
void fill_assign(size_type n, const value_type &val)
{
size_type i = 0;
iterator begin = this->begin();
while (i < n and begin != end())
{
*begin = val;
i++;
begin++;
}
// if this < than range, insert the rest at the end,
if (begin == end())
insert(end(), n - i, val);
// else if range is shorter, erase the rest from end
else
erase(begin, end());
}
public:
void push_front(const value_type &val)
{
insert(begin(), val);
}
void pop_front()
{
erase(begin());
}
void push_back(const value_type &val)
{
insert(end(), val);
}
void pop_back()
{
erase(--end());
}
iterator insert(iterator position, const value_type &val)
{
put_before(position, val);
this->_size++;
return (--position);
}
void insert(iterator position, size_type n, const value_type &val)
{
fill_insert(position, n, val);
}
template <class InputIterator>
void insert(iterator position, InputIterator first, InputIterator last)
{
insert_dispatch(position, first, last, typename ft::is_integral<InputIterator>::type());
}
private:
template <class InputIterator>
void insert_dispatch(iterator position, InputIterator first, InputIterator last, false_type)
{
range_insert(position, first, last);
}
template <class Integral>
void insert_dispatch(iterator position, Integral n, Integral val, true_type)
{
fill_insert(position, n, val);
}
void fill_insert(iterator position, size_type n, const value_type &val)
{
for (size_type i = 0; i < n; i++)
insert(position, val);
}
template <class InputIterator>
void range_insert(iterator position, InputIterator first, InputIterator last)
{
while (first != last)
{
insert(position, *first);
first++;
}
}
public:
iterator erase(iterator position)
{
return (erase(position, position._node->next));
}
iterator erase(iterator first, iterator last)
{
size_type n = 0;
list_node_base *first_prev = first._node->prev;
list_node_base *curr = first._node;
while (curr != last._node)
{
curr = destroy_position(curr);
n++;
}
link_nodes(first_prev, last._node);
this->_size -= n;
return (last._node);
}
void swap(list &x)
{
ft::swap(this->_size, x._size);
if (!empty() and !x.empty())
{
list_node_base *this_header_next = this->_header.next;
link_nodes(&this->_header, x._header.next);
link_nodes(&x._header, this_header_next);
list_node_base *this_trailer_prev = this->_trailer.prev;
link_nodes(x._trailer.prev, &this->_trailer);
link_nodes(this_trailer_prev, &x._trailer);
}
else if (!x.empty())
{
link_nodes(&this->_header, x._header.next);
link_nodes(x._trailer.prev, &this->_trailer);
link_nodes(&x._header, &x._trailer);
}
else if (!empty())
{
link_nodes(&x._header, this->_header.next);
link_nodes(this->_trailer.prev, &x._trailer);
link_nodes(&this->_header, &this->_trailer);
}
}
void resize(size_type n, value_type val = value_type())
{
if (n > this->_size)
insert(end(), n - this->_size, val);
else if (n < this->_size)
{
n = this->_size - n;
for (size_type i = 0; i < n; i++)
pop_back();
}
}
void clear()
{
while (!empty())
pop_front();
}
// operations
void splice(iterator position, list &x)
{
this->_size += x._size;
x._size = 0;
list_node_base *first = x.begin()._node;
list_node_base *last = x.end()._node;
list_node_base *last_prev = last->prev;
link_nodes(first->prev, last);
link_nodes(position._node->prev, first);
link_nodes(last_prev, position._node);
}
void splice(iterator position, list &x, iterator i)
{
splice(position, x, i, iterator(i._node->next));
}
void splice(iterator position, list &x, iterator first, iterator last)
{
size_type n = ft::distance(first, last);
this->_size += n;
x._size -= n;
list_node_base *_first = first._node;
list_node_base *_last = last._node;
list_node_base *_last_prev = _last->prev;
link_nodes(_first->prev, _last);
link_nodes(position._node->prev, _first);
link_nodes(_last_prev, position._node);
}
void remove(const value_type &val)
{
for (iterator it = begin(); it != end(); ++it)
if (*it == val)
it = --erase(it);
}
template <class Predicate>
void remove_if(Predicate pred)
{
for (iterator it = begin(); it != end(); ++it)
if (pred(*it))
it = --erase(it);
}
void unique()
{
unique(ft::equal_to<value_type>());
}
template <class BinaryPredicate>
void unique(BinaryPredicate binary_pred)
{
for (iterator it = begin(); it != --end() and it != end();)
if (binary_pred(*it, *iterator(it._node->next)))
it = --erase(++it);
else
++it;
}
void merge(list &x)
{
merge(x, ft::less<value_type>());
}
template <class Compare>
void merge(list &x, Compare comp)
{
if (&x == this)
return ;
iterator this_it = begin();
for (iterator x_it = x.begin(); x_it != x.end() and this_it != end();)
{
if (comp(*x_it, *this_it))
splice(this_it, x, x_it++);
else
++this_it;
}
if (this_it == end())
splice (this_it, x);
}
void sort()
{
sort(ft::less<value_type>());
}
template <class Compare>
void sort(Compare comp)
{
mergesort(*this, comp);
}
private:
template <class Compare>
void mergesort(list &x, Compare comp)
{
// base case; trivially sorted
if (x.size() < 2) return ;
// split list into two equal parts
list left, right;
iterator mid = x.begin();
ft::advance(mid, x.size() / 2);
right.splice(right.end(), x, mid, x.end());
left.splice(left.end(), x);
// recur for each part
mergesort(left, comp);
mergesort(right, comp);
// add elements back and merge
x.splice(x.end(), left);
x.merge(right, comp);
}
public:
void reverse()
{
if (this->_size > 1)
{
size_type i = 0;
iterator curr = --(--end());
while (i < this->_size - 1)
{
iterator curr_prev(curr._node->prev);
splice(end(), *this, curr);
curr = curr_prev;
i++;
}
}
}
// observers
allocator_type get_allocator() const
{
return (allocator_type(this->_alloc));
}
private:
list_node_base *destroy_position(iterator position)
{
allocator_type alloc(this->_alloc);
list_node_base *position_next = position._node->next;
// downcast, so that `data` can be accessed because base has only prev/next pointers
alloc.destroy(&static_cast<node*>(position._node)->data);
this->_alloc.deallocate(static_cast<node*>(position._node), 1);
return (position_next);
}
void put_before(iterator position, const value_type &val)
{
node *new_node = this->_alloc.allocate(1);
try
{
allocator_type alloc(this->_alloc);
alloc.construct(&new_node->data, val);
}
catch (...)
{
this->_alloc.deallocate(new_node, 1);
throw ;
}
link_nodes(position._node->prev, new_node);
link_nodes(new_node, position._node);
}
// forms relationship a->b and a<-b
void link_nodes(list_node_base *a, list_node_base *b)
{
a->next = b;
b->prev = a;
}
};
template <class T, class Alloc>
void swap(list<T, Alloc> &x, list<T, Alloc> &y)
{
x.swap(y);
}
template <class T, class Alloc>
bool operator==(const ft::list<T, Alloc> &lhs, const ft::list<T, Alloc> &rhs)
{
return (lhs.size() == rhs.size() and ft::equal(lhs.begin(), lhs.end(), rhs.begin()));
}
template <class T, class Alloc>
bool operator!=(const ft::list<T, Alloc> &lhs, const ft::list<T, Alloc> &rhs)
{
return (!(lhs == rhs));
}
template <class T, class Alloc>
bool operator<(const ft::list<T, Alloc> &lhs, const ft::list<T, Alloc> &rhs)
{
return (ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()));
}
template <class T, class Alloc>
bool operator<=(const ft::list<T, Alloc> &lhs, const ft::list<T, Alloc> &rhs)
{
return (!(rhs < lhs));
}
template <class T, class Alloc>
bool operator>(const ft::list<T, Alloc> &lhs, const ft::list<T, Alloc> &rhs)
{
return (rhs < lhs);
}
template <class T, class Alloc>
bool operator>=(const ft::list<T, Alloc> &lhs, const ft::list<T, Alloc> &rhs)
{
return (!(lhs < rhs));
}
}
#endif