-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
1186 lines (1049 loc) · 33.2 KB
/
list.h
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by gy gao on 11/7/22.
//
#ifndef MYOWNSTL_LIST_H
#define MYOWNSTL_LIST_H
// 这个头文件包含了一个模板类 list
// list : 双向链表
// notes:
//
// 异常保证:
// myownstl::list<T> 满足基本异常保证,部分函数无异常保证,并对以下等函数做强异常安全保证:
// * emplace_front
// * emplace_back
// * emplace
// * push_front
// * push_back
// * insert
#include <initializer_list>
#include "iterator.h"
#include "memory.h"
#include "functional.h"
#include "util.h"
#include "exceptdef.h"
namespace myownstl
{
template<class T> struct list_node_base;
template<class T> struct list_node;
template<class T>
struct node_traits
{
typedef list_node_base<T>* base_ptr;
typedef list_node<T>* node_ptr;
};
// list的节点结构
template<class T>
struct list_node_base
{
typedef typename node_traits<T>::base_ptr base_ptr;
typedef typename node_traits<T>::node_ptr node_ptr;
base_ptr prev; // 前驱节点
base_ptr next; // 后继节点
list_node_base() = default;
base_ptr self()
{
return static_cast<base_ptr>(&*this);
}
node_ptr as_node()
{
return static_cast<node_ptr>(self());
}
void unlink()
{
prev = next = self();
}
};
template<class T>
struct list_node : public list_node_base<T>
{
typedef typename node_traits<T>::base_ptr base_ptr;
typedef typename node_traits<T>::node_ptr node_ptr;
T value; // 数据域
list_node() = default;
list_node(const T& v) : value(v) {}
list_node(T&& v) : value(myownstl::move(v)) {}
base_ptr as_node()
{
return static_cast<base_ptr>(&*this);
}
node_ptr self()
{
return static_cast<node_ptr>(&*this);
}
};
// list迭代器设计
template<class T>
struct list_iterator : public iterator<bidirectional_iterator_tag, T>
{
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef typename node_traits<T>::base_ptr base_ptr;
typedef typename node_traits<T>::node_ptr node_ptr;
typedef list_iterator<T> self;
base_ptr node_; // 指向当前节点
// 构造函数
list_iterator() = default;
list_iterator(base_ptr x) : node_(x) {}
list_iterator(node_ptr x) : node_(x->as_node()) {}
list_iterator(const list_iterator& rhs) : node_(rhs.node_) {}
// 重载操作符
reference operator*() const {return node_->as_node()->value;}
pointer operator->() const {return &(operator*());}
self& operator++()
{
MYOWNSTL_DEBUG(node_ != nullptr);
node_ = node_->next;
return *this;
}
self operator++(int)
{
self tmp = *this;
++*this;
return tmp;
}
self& operator--()
{
MYOWNSTL_DEBUG(node_ != nullptr);
node_ = node_->prev;
return *this;
}
self operator--(int)
{
self tmp = *this;
--*this;
return tmp;
}
// 重载比较操作符
bool operator==(const self& rhs) const {return node_ == rhs.node_;}
bool operator!=(const self& rhs) const {return node_ != rhs.node_;}
};
template<class T>
struct list_const_iterator : public iterator<bidirectional_iterator_tag, T>
{
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
typedef typename node_traits<T>::base_ptr base_ptr;
typedef typename node_traits<T>::node_ptr node_ptr;
typedef list_const_iterator<T> self;
base_ptr node_;
list_const_iterator() = default;
list_const_iterator(base_ptr x) : node_(x) {}
list_const_iterator(node_ptr x) : node_(x->as_base()) {}
list_const_iterator(const list_iterator<T>& rhs) : node_(rhs.node_) {}
list_const_iterator(const list_const_iterator& rhs) : node_(rhs.node_) {}
reference operator*() const {return node_->as_node()->value;}
pointer operator->() const {return &(operator*());}
self& operator++()
{
MYOWNSTL_DEBUG(node_ != nullptr);
node_ = node_->next;
return *this;
}
self operator++(int)
{
self tmp = *this;
++*this;
return tmp;
}
self& operator--()
{
MYOWNSTL_DEBUG(node_ != nullptr);
node_ = node_->prev;
return *this;
}
self operator--(int)
{
self tmp = *this;
--*this;
return tmp;
}
// 重载比较操作符
bool operator==(const self& rhs) const {return node_ == rhs.node_;}
bool operator!=(const self& rhs) const {return node_ != rhs.node_;}
};
// 模板类:list
// 模板参数 T 代表数据类型
template<class T>
class list
{
public:
// list的嵌套型别定义
typedef myownstl::allocator<T> allocator_type;
typedef myownstl::allocator<T> data_allocator;
typedef myownstl::allocator<list_node_base<T>> base_allocator;
typedef myownstl::allocator<list_node<T>> node_allocator;
typedef typename allocator_type::value_type value_type;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef list_iterator<T> iterator;
typedef list_const_iterator<T> const_iterator;
typedef myownstl::reverse_iterator<iterator> reverse_iterator;
typedef myownstl::reverse_iterator<const_iterator> const_reverse_iterator;
typedef typename node_traits<T>::base_ptr base_ptr;
typedef typename node_traits<T>::node_ptr node_ptr;
allocator_type get_allocator() {return node_allocator();}
private:
base_ptr node_; // 指向末尾节点
size_type size_; // 大小
public:
// 构造、复制、移动、析构函数
list() {fill_init(0, value_type());}
explicit list(size_type n) {fill_init(n, value_type());}
list(size_type n, const T& value) { fill_init(n, value); }
template<class Iter, typename std::enable_if<
myownstl::is_input_iterator<Iter>::value, int>::type = 0>
list(Iter first, Iter last) {copy_init(first, last);}
list(std::initializer_list<T> ilist) {copy_init(ilist.begin(), ilist.end());}
list(const list& rhs) {copy_init(rhs.begin(), rhs.end());}
list(list&& rhs) noexcept : node_(rhs.node_), size_(rhs.size_)
{
rhs.node_ = nullptr;
rhs.size = 0;
}
list& operator=(const list& rhs)
{
if (this != &rhs)
{
assign(rhs.begin(), rhs.end());
}
return *this;
}
list& operator=(list&& rhs) noexcept
{
clear();
splice(end(), rhs);
return *this;
}
list& operator=(std::initializer_list<T> ilist)
{
list tmp(ilist.begin(), ilist.end());
swap(tmp);
return *this;
}
~list()
{
if (node_)
{
clear();
base_allocator::deallocate(node_);
node_ = nullptr;
size_ = 0;
}
}
public:
// 迭代器相关操作(双向链表)
iterator begin() noexcept
{ return node_->next; }
const_iterator begin() const noexcept
{ return node_->next; }
iterator end() noexcept
{ return node_; }
const_iterator end() const noexcept
{ return node_; }
reverse_iterator rbegin() noexcept
{ return reverse_iterator(end()); }
const_reverse_iterator rbegin() const noexcept
{ return reverse_iterator(end()); }
reverse_iterator rend() noexcept
{ return reverse_iterator(begin()); }
const_reverse_iterator rend() const noexcept
{ return reverse_iterator(begin()); }
const_iterator cbegin() const noexcept
{ return begin(); }
const_iterator cend() const noexcept
{ return end(); }
const_reverse_iterator crbegin() const noexcept
{ return rbegin(); }
const_reverse_iterator crend() const noexcept
{ return rend(); }
// 1. 容器相关操作
bool empty() const noexcept
{ return node_->next == node_; }
size_type size() const noexcept
{ return size_; }
size_type max_size() const noexcept
{ return static_cast<size_type>(-1); }
// 2. 访问元素相关操作
reference front()
{
MYOWNSTL_DEBUG(!empty());
return *begin();
}
const_reference front() const
{
MYOWNSTL_DEBUG(!empty());
return *begin();
}
reference back()
{
MYOWNSTL_DEBUG(!empty());
return *(--end());
}
const_reference back() const
{
MYOWNSTL_DEBUG(!empty());
return *(--end());
}
// 3. 调整容器相关操作
// a) assign
void assign(size_type n, const value_type& value)
{ fill_assign(n, value); }
template<class Iter, typename std::enable_if<
myownstl::is_input_iterator<Iter>::value, int>::type = 0>
void assign(Iter first, Iter last)
{ copy_assign(first, last); }
void assign(std::initializer_list<T> ilist)
{ copy_assign(ilist.begin(), ilist.end()); }
// b) emplace_front / emplace_back / emplace
template<class ...Args>
void emplace_front(Args&& ...args)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - 1, "list<T>'s size too big");
auto link_node = create_node(myownstl::forward<Args>(args)...);
link_nodes_at_front(link_node->as_base(), link_node->as_base());
++size_;
}
template<class ...Args>
void emplace_back(Args&& ...args)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - 1, "list<T>'s size too big");
auto link_node = create_node(myownstl::forward<Args>(args)...);
link_nodes_at_back(link_node->as_base(), link_node->as_base());
++size_;
}
template<class ...Args>
iterator emplace(const_iterator pos, Args&& ...args)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - 1, "list<T>'s size too big");
auto link_node = create_node(myownstl::forward<Args>(args)...);
link_nodes(pos.node_, link_node->as_base(), link_node->as_base());
++size_;
return iterator(link_node);
}
// c) insert
iterator insert(const_iterator pos, const value_type& value)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - 1, "list<T>'s size too big");
auto link_node = create_node(value);
++size_;
return link_iter_node(pos, link_node->as_base());
}
iterator insert(const_iterator pos, value_type&& value)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - 1, "list<T>'s size too big");
auto link_node = create_node(myownstl::move(value));
++size_;
return link_iter_node(pos, link->as_base());
}
iterator insert(const_iterator pos, size_type n, const value_type& value)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - n, "list<T>'s size too big");
return fill_insert(pos, n, value);
}
template<class Iter, typename std::enable_if<
myownstl::is_input_iterator<Iter>::value, int>::type = 0>
iterator insert(const_iterator pos, Iter first, Iter last)
{
size_type n = myownstl::distance(first, last);
THROW_LENGTH_ERROR_IF(size_ > max_size() - n, "list<T>'s size too big");
return copy_insert(pos, n, first);
}
// d) push_front / push_back
void push_front(const value_type& value)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - 1, "list<T>'s size too big");
auto link_node = create_node(value);
link_nodes_at_front(link_node->as_base(), link_node->as_base());
++size_;
}
void push_front(value_type&& value)
{
emplace_front(myownstl::move(value));
}
void push_back(const value_type& value)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - 1, "list<T>'s size too big");
auto link_node = create_node(value);
link_nodes_at_back(link_node->as_base(), link_node->as_base());
++size_;
}
void push_back(value_type&& value)
{
emplace_back(myownstl::move(value));
}
// e) pop_front / pop_back
void pop_front()
{
MYOWNSTL_DEBUG(!empty());
auto n = node_->next;
unlink_nodes(n, n);
destroy_node(n->as_node());
--size_;
}
void pop_back()
{
MYOWNSTL_DEBUG(!empty());
auto n = node_->prev;
unlink_nodes(n, n);
destroy_node(n->as_node());
--size_;
}
// f) erase / clear
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
void clear();
// g) resize
void resize(size_type new_size) { resize(new_size, value_type()); }
void resize(size_type new_size, const value_type& value);
void swap(list& rhs) noexcept
{
myownstl::swap(node_, rhs.node_);
myownstl::swap(node_, rhs.size_);
}
// list 相关操作
// 1. splice
void splice(const_iterator pos, list& other);
void splice(const_iterator pos, list& other, const_iterator it);
void splice(const_iterator pos, list& other, const_iterator first, const_iterator last);
// 2. remove
void remove(const value_type& value)
{ remove_if([&](const value_type& v) {return v == value; }); }
template<class UnaryPredicate>
void remove_if(UnaryPredicate pred);
// 3. unique
void unique()
{unique(myownstl::equal_to<T>());}
template<class BinaryPredicate>
void unique(BinaryPredicate pred);
// 4. merge
void merge(list& x)
{ merge(x, myownstl::less<T>());}
template<class Compare>
void merge(list& x, Compare comp);
// 5. sort
void sort()
{ list_sort(begin(), end(), size(), myownstl::less<T>()); }
template<class Compared>
void sort(Compared comp)
{ list_sort(begin(), end(), size(), comp); }
// 6. reverse
void reverse();
private:
// helper function
// 1. create/destroy node
template<class ...Args>
node_ptr create_node(Args&& ...args);
void destroy_node(node_ptr p);
// 2. initialize
void fill_init(size_type n, const value_type& value);
template<class Iter>
void copy_init(Iter first, Iter last);
// 3. link/unlink
iterator link_iter_node(const_iterator pos, base_ptr node);
void link_nodes(base_ptr p, base_ptr first, base_ptr last);
void link_nodes_at_front(base_ptr first, base_ptr last);
void link_nodes_at_back(base_ptr first, base_ptr last);
void unlink_nodes(base_ptr f, base_ptr l);
// 4. assign
void fill_assign(size_type n, const value_type& value);
template<class Iter>
void copy_assign(Iter first, Iter last);
// 5. insert
iterator fill_insert(const_iterator pos, size_type n, const value_type& value);
template<class Iter>
iterator copy_insert(const_iterator pos, size_type n, Iter first);
// 6. sort
template<class Compared>
iterator list_sort(iterator first, iterator last, size_type n, Compared comp);
};
// 删除pos处的元素
template<class T>
typename list<T>::iterator
list<T>::erase(list::const_iterator pos)
{
MYOWNSTL_DEBUG(pos != cend());
auto n = pos.node_;
auto next = n->next;
unlink_nodes(n, n);
destroy_node(n->as_node());
--size_;
return iterator(next);
}
// 删除[first, last)内的元素
template<class T>
typename list<T>::iterator
list<T>::erase(list::const_iterator first, list::const_iterator last)
{
if (first != last)
{
unlink_nodes(first.node_, last.node_->prev);
while (first != last)
{
auto cur = first.node_;
++first;
destroy_node(cur->as_node());
--size_;
}
}
return iterator(last.node_);
}
// 清空list
template<class T>
void list<T>::clear()
{
if (size_ != 0)
{
auto cur = node_->next;
for (base_ptr next = cur->next; cur != node_; cur = next, next = cur->next)
{
destroy_node(cur->as_node());
}
node_->unlink();
size_ = 0;
}
}
// 重置容器大小
template<class T>
void list<T>::resize(list::size_type new_size, const value_type &value)
{
auto i = begin();
size_type len = 0;
while (i != end() && len < new_size)
{
++i;
++len;
}
if (len == new_size)
{
erase(i, node_);
}
else
{
insert(node_, new_size - len, value);
}
}
// 将 list x 接合于 pos 之前
template<class T>
void list<T>::splice(list::const_iterator pos, list &x)
{
MYOWNSTL_DEBUG(this != &x);
if (!x.empty())
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - x.size_, "list<T>'s size too big");
auto f = x.node_->next;
auto l = x.node_->prev;
x.unlink_nodes(f, l);
link_nodes(pos.node_, f, l);
size_ += x.size_;
x.size_ = 0;
}
}
// 将迭代器所指的节点接合于 pos 之前
template<class T>
void list<T>::splice(list::const_iterator pos, list &x, list::const_iterator it)
{
if (pos.node_ != it.node_ && pos.node_ != it.node_->next)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - 1, "list<T>'s size too big");
auto f = it.node_;
x.unlink_nodes(f, f);
link_nodes(pos.node_, f, f);
++size_;
--x.size_;
}
}
// 将 list x 的 [first, last) 内的节点接合于 pos 之前
template<class T>
void list<T>::splice(list::const_iterator pos, list &x, list::const_iterator first, list::const_iterator last)
{
if (first != last && this != &x)
{
size_type n = myownstl::distance(first, last);
THROW_LENGTH_ERROR_IF(size_ > max_size() - n, "list<T>'s size too big");
auto f = first.node_;
auto l = last.node_->prev;
x.unlink_nodes(f, l);
link_nodes(pos.node_, f, l);
size_ += n;
x.size_ -= n;
}
}
// 将另一元操作 pred 为 true 的所有元素移除
template<class T>
template<class UnaryPredicate>
void list<T>::remove_if(UnaryPredicate pred)
{
auto f = begin();
auto l = end();
for (auto next = f; f != l; f = next)
{
++next;
if (pred(*f))
{
erase(f);
}
}
}
// 移除 list 中满足 pred 为 true 重复元素
template<class T>
template<class BinaryPredicate>
void list<T>::unique(BinaryPredicate pred)
{
auto i = begin();
auto e = end();
auto j = i;
++j;
while (j != e)
{
if (pred(*i, *j))
{
erase(j);
}
else
{
i = j;
}
j = i;
++j;
}
}
// 与另一个 list 合并,按照 comp 为 true 的顺序
template<class T>
template<class Compare>
void list<T>::merge(list &x, Compare comp)
{
if (this != &x)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - x.size_, "list<T>'s size too big");
auto f1 = begin();
auto l1 = end();
auto f2 = x.begin();
auto l2 = x.end();
while (f1 != l1 && f2 != l2)
{
if (comp(*f2, *f1))
{
// 使 comp 为 true 的一段区间
auto next = f2;
++next;
for (; next != l2 && comp(*next, *f1); ++next)
;
auto f = f2.node_;
auto l = next.node_->prev;
f2 = next;
// link node
x.unlink_nodes(f, l);
link_nodes(f1.node_, f, l);
++f1;
}
else
{
++f1;
}
}
// 连接剩余部分
if (f2 != l2)
{
auto f = f2.node_;
auto l = l2.node_->prev;
x.unlink_nodes(f, l);
link_nodes(l1.node_, f, l);
}
size_ += x.size_;
x.size_ = 0;
}
}
// 将 list 反转
template<class T>
void list<T>::reverse()
{
if (size_ <= 1)
{
return;
}
auto i = begin();
auto e = end();
while (i.node_ != e.node_)
{
myownstl::swap(i.node_->prev, i.node_->next);
i.node_ = i.node_->prev;
}
myownstl::swap(e.node_->prev, e.node_->next);
}
/*****************************************************************************************/
// helper function
// 1. create/destroy node
template<class T>
template<class... Args>
typename list<T>::node_ptr
list<T>::create_node(Args&& ...args)
{
node_ptr p = node_allocator::allocate(1);
try
{
data_allocator::construct(myownstl::address_of(p->value), myownstl::forward<Args>(args)...);
p->prev = nullptr;
p->next = nullptr;
}
catch (...)
{
node_allocator::deallocate(p);
throw;
}
return p;
}
template<class T>
void list<T>::destroy_node(list::node_ptr p)
{
data_allocator::destroy(myownstl::address_of(p->value));
node_allocator::deallocate(p);
}
// 2. initialize
// 用n个元素初始化容器
template<class T>
void list<T>::fill_init(list::size_type n, const value_type& value)
{
node = base_allocator::allocate(1);
node_->unlink();
size_ = n;
try
{
for (; n > 0; --n)
{
auto node = create_node(value);
link_nodes_at_back(node->as_base(), node->as_base());
}
}
catch (...)
{
clear();
base_allocator::deallocate(node_);
node_ = nullptr;
throw;
}
}
// 以 [first, last) 初始化容器
template<class T>
template<class Iter>
void list<T>::copy_init(Iter first, Iter last)
{
node_ = base_allocator::allocate(1);
node_->unlink();
size_type n = myownstl::distance(first, last);
size_ = n;
try
{
for (; n > 0; --n, ++first)
{
auto node = create_node(*first);
link_nodes_at_back(node->as_base(), node->as_base());
}
}
catch (...)
{
clear();
base_allocator::deallocate(node_);
node_ = nullptr;
throw;
}
}
// 3. link/unlink
// 在 pos 处连接一个节点
template<class T>
typename list<T>::iterator
list<T>::link_iter_node(list::const_iterator pos, list::base_ptr node)
{
if (pos == node_->next)
{
link_nodes_at_front(link_node, link_node);
}
else if (pos == node_)
{
link_nodes_at_back(link_node, link_node);
}
else
{
link_nodes(pos.node_, link_node, link_node);
}
return iterator(link_node);
}
// 在 pos 处连接 [first, last] 的结点
template<class T>
void list<T>::link_nodes(list::base_ptr pos, list::base_ptr first, list::base_ptr last)
{
pos->prev->next = first;
first->prev = pos->prev;
pos->prev = last;
last->next = pos;
}
// 在头部连接 [first, last] 结点
template<class T>
void list<T>::link_nodes_at_front(list::base_ptr first, list::base_ptr last)
{
first->prev = node_;
last->next = node_->next;
last->next->prev = last;
node_->next = first;
}
// 在尾部连接 [first, last] 结点
template<class T>
void list<T>::link_nodes_at_back(list::base_ptr first, list::base_ptr last)
{
last->next = node_;
first->prev = node_->prev;
first->prev->next = first;
node_->prev = last;
}
// 容器与 [first, last] 结点断开连接
template<class T>
void list<T>::unlink_nodes(list::base_ptr first, list::base_ptr last)
{
first->prev->next = last->next;
last->next->prev = first->prev;
}
// 4. assign
// 用 n 个元素为容器赋值
template<class T>
void list<T>::fill_assign(list::size_type n, const value_type& value)
{
auto i = begin();
auto e = end();
for (; n > 0 && i != e; --n, ++i)
{
*i = value;
}
if (n > 0)
{
insert(e, n, value);
}
else
{
erase(i, e);
}
}
// 复制[f2, l2)为容器赋值
template<class T>
template<class Iter>
void list<T>::copy_assign(Iter first, Iter last)
{
auto f1 = begin();
auto l1 = end();
for (; f1 != l1 && f2 != l2; ++f1, ++f2)
{
*f1 = *f2;
}
if (f2 == l2)
{
erase(f1, l1);
}
else
{
insert(l1, f2, l2);
}
}
// 5. insert
// 在 pos 处插入 n 个元素
template<class T>
typename list<T>::iterator
list<T>::fill_insert(list::const_iterator pos, list::size_type n, const value_type &value)
{
iterator r(pos.node_);
if (n != 0)
{
const auto add_size = n;
auto node = create_node(value);
node->prev = nullptr;
r = iterator(node);
iterator end = r;
try
{
for (--n; n > 0; --n, ++end)
{
auto next = create_node(value);
end.node_->next = next->as_base();
next->prev = end.node_;
}
size_ += add_size;
}
catch (...)
{
auto enode = end.node_;
while (true)