-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
961 lines (860 loc) · 22 KB
/
vector.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
#ifndef VECTOR_H
#define VECTOR_H
// 这个头文件包含一个模板类 vector
#include<initializer_list>
// TODO 建立自己的initializer_list
#include "iterator.h"
#include "memory.h"
#include "util.h"
#include "exceptdef.h"
namespace mystl
{
#ifdef max
#pragma message("#undefing marco max")
#undef max
#endif //max
#ifdef min
#pragma message("#undefing marco min")
#undef min
#endif // min
// 模板类vector
template <class T>
class vector
{
static_assert(!std::is_same<bool, T>::value, "vector<bool> is abandoned in mystl");
public:
// vector的嵌套型别定义
typedef mystl::allocator<T> alloactor_type;
typedef mystl::allocator<T> data_allocator;
typedef typename allocator_type::value_type value_type;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::cosnt_pointer const_pointer;
typedef typename allcoator_type::refernce reference;
typedef typename alloactor_type::const_reference const_reference;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef mystl::reverse_iterator<iterator> reverse_iterator;
typedef mystl::reverse_iterator<const_iterator> const_reverse_iterator;
allocator_type get_allocator() { return data_allocator(); }
private:
iterator begin_; // 表示目前使用空间的头
iterator end_; // 表示目前使用空间的尾
iterator cap_; // 表示当前可用空间的尾
public:
// 构造/复制/移动/析构函数
// 空白构造 比如 vector<int> res;
vector() noexcept
{
try_init();
}
// 直接申请若干长度的vector<int> res(6);
explicit vector(size_type n)
{
fill_init(n, value_type());
}
// 直接申请若干长度且值为value的vector<int> res(6,1);
vector(size_type n, const value_type& value)
{
fill_init(n, value);
}
// ?
template <class Iter, typename std::enable_if <
mystl::is_input_iterator<Iter>::value, int>::type = 0>
vector(Iter first, Iter last)
{
MYSTL_DEBUG(!(last < first));
range_init(first, last);
}
// 拷贝其他vector来构造
vector(const vector& rhs)
{
range_init(rhs.begin_, rhs.end_);
}
// ?
vector(vector&& rhs) noexcept
:begin_(rhs.begin_),
end_(rhs.end_),
cap_(rhs.cap_)
{
rhs.begin_ = nullptr;
rhs.end_ = nullptr;
rhs.cap_ = nullptr;
}
// 以其他容器初始化构造vector
vector(std::initializer_list<value_type> ilist)
{
range_init(ilist.begin(), ilist.end());
}
vector& operator=(const vector& rhs);
vector& operator=(vector&& rhs) noexcept;
// 重载 = 号 所以可以vector1 = vector2
vector& operator=(std::initializer_list<value_type> ilist)
{
vector tmp(ilist.begin(), ilist.end());
swap(tmp);
return *this;
}
~vector()
{
destroy_and_recover(begin_, end_, cap_ - begin_);
begin_ = end_ = cap_ = nullptr;
}
public:
// 迭代器相关操作
iterator begin() noexcept
{
return begin_;
}
const_iterator begin() const noexcept
{
return begin_;
}
iterator end() noexcept
{
return end_;
}
const_iterator end() const noexcept
{
return end_;
}
reverse_iterator rbegin() noexcept
{
return reverse_iterator(end());
}
const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator(end());
}
reverse_iterator rend() noexcept
{
return reverse_iterator(begin());
}
const_reverse_iterator rend() const noexcept
{
return const_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();
}
// 容量相关操作
bool empty() const noexcept
{
return begin_ == end_;
}
size_type size() const noexcept
{
return static_cast<size_type>(end_ - begin_);
}
size_type max_size() const noexcept
{
return static_cast<size_type>(-1) / sizeof(T);
}
size_type capacity() const noexcept
{
return static_cast<size_type>(cap_ - begin_);
}
void reserve(size_type n);
void shrink_to_fit();
// 访问元素相关操作
reference operator[](size_type n)
{
MYSTL_DEBUG(n < size());
return *(begin_ + n);
}
const_reference operator[](size_type n) const
{
MYSTL_DEBUG(n < size());
return *(begin_ + n);
}
reference at(size_type n)
{
THROW_OUT_OF_RANGE_IF(!(n < size()), "vector<T>::at() subscript out of range");
return (*this)[n];
}
const_reference at(size_type n) const
{
THROW_OUT_OF_RANGE_IF(!(n < size()), "vector<T>::at() subscript out of range");
return (*this)[n];
}
reference front()
{
MYSTL_DEBUG(!empty());
return *begin_;
}
const_reference front() const
{
MYSTL_DEBUG(!empty());
return *begin_;
}
reference back()
{
MYSTL_DEBUG(!empty());
return *(end_ - 1);
}
const_reference back() const
{
MYSTL_DEBUG(!empty());
return *(end_ - 1);
}
pointer data() noexcept { return begin_; }
const_pointer data() const noexcept { return begin_; }
// 修改容器相关操作
// assign
void assign(size_type n, const value_type& value)
{
fill_assign(n, value);
}
template <class Iter, typename std::enable_if<
mystl::is_input_iterator<Iter>::value, int>::type = 0>
void assign(Iter first, Iter last)
{
MYSTL_DEBUG(!(last < first));
copy_assign(first, last, iterator_category(first));
}
void assign(std::initializer_list<value_type> il)
{
copy_assign(il.begin(), il.end(), mystl::forward_iterator_tag{});
}
// emplace / emplace_back
template <class... Args>
iterator emplace(const_iterator pos, Args&& ...args);
template <class... Args>
void emplace_back(Args&& ...args);
// push_back / pop_back
void push_back(const value_type& value);
void push_back(value_type&& value)
{
emplace_back(mystl::move(value));
}
void pop_back();
// insert
iterator insert(const_iterator pos, const value_type& value);
iterator insert(const_iterator pos, value_type&& value)
{
return emplace(pos, mystl::move(value));
}
iterator insert(const_iterator pos, size_type n, const value_type& value)
{
MYSTL_DEBUG(pos >= begin() && pos <= end());
return fill_insert(const_cast<iterator>(pos), n, value);
}
template <class Iter, typename std::enable_if<
mystl::is_input_iterator<Iter>::value, int>::type = 0>
void insert(const_iterator pos, Iter first, Iter last)
{
MYSTL_DEBUG(pos >= begin() && pos <= end() && !(last < first));
copy_insert(const_cast<iterator>(pos), first, last);
}
// erase / clear
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
void clear() { erase(begin(), end()); }
// resize / reverse
void resize(size_type new_size) { return resize(new_size, value_type()); }
void resize(size_type new_size, const value_type& value);
void reverse() { mystl::reverse(begin(), end()); }
// swap
void swap(vector& rhs) noexcept;
private:
// helper functions
// initialize / destroy
void try_init() noexcept;
void init_space(size_type size, size_type cap);
void fill_init(size_type n, const value_type& value);
template <class Iter>
void range_init(Iter first, Iter last);
void destroy_and_recover(iterator first, iterator last, size_type n);
// calculate the growth size
size_type get_new_cap(size_type add_size);
// assign
void fill_assign(size_type n, const value_type& value);
template <class IIter>
void copy_assign(IIter first, IIter last, input_iterator_tag);
template <class FIter>
void copy_assign(FIter first, FIter last, forward_iterator_tag);
// reallocate
template <class... Args>
void reallocate_emplace(iterator pos, Args&& ...args);
void reallocate_insert(iterator pos, const value_type& value);
// insertt
iterator fill_insert(iterator pos, size_type n, const value_type& value);
template <class IIter>
void copy_insert(iterator pos, IIter first, IIter last);
// shrink_to_fit
void reinsert(size_type size);
};
/*****************************************************************************************/
// 函数实现
// 复制赋值操作符
template <class T>
vector<T>& vector<T>::operator=(const vector& rhs)
{
if (this != &rhs)
{
const auto len = rhs.size();
if (len > capacity())
{
vector tmp(rhs.begin(), rhs.end());
swap(tmp);
}
else if (size() >= len)
{
auto i = mystl::copy(rhs.begin(), rhs.end(), begin());
data_allocator::destroy(i, end_);
end_ = begin_ + len;
}
else
{
mystl::copy(rhs.begin(), rhs.begin() + size(), begin_);
mystl::uninitialized_copy(rhs.begin() + size(), rhs.end(), end_);
cap_ = end_ = begin_ + len;
}
}
return *this;
}
// 预留空间大小,当原容量小于要求大小时,才会重新分配
template <class T>
void vector<T>::reserve(size_type n)
{
if (capacity() < n)
{
THROW_LENGTH_ERROR_IF(n > max_size(),
"n can not larger than max_size() in vector<T>::reserve(n)");
const auto old_size = size();
auto tmp = data_allocator::allocate(n);
mystl::uninitialized_move(begin_, end_, tmp);
data_allocator::deallocate(begin_, cap_ - begin_);
begin_ = tmp;
end_ = tmp + old_size;
cap_ = begin_ + n;
}
}
// 放弃多余的容量
template <class T>
void vector<T>::shrink_to_fit()
{
if (end_ < cap_)
{
reinsert(size());
}
}
// 在 pos 位置就地构造元素,避免额外的复制或移动开销
template <class T>
template <class ...Args>
typename vector<T>::iterator
vector<T>::emplace(const_iterator pos, Args&& ...args)
{
MYSTL_DEBUG(pos >= begin() && pos <= end());
iterator xpos = const_cast<iterator>(pos);
const size_type n = xpos - begin_;
if (end_ != cap_ && xpos == end_)
{
data_allocator::construct(mystl::address_of(*end_), mystl::forward<Args>(args)...);
++end_;
}
else if (end_ != cap_)
{
auto new_end = end_;
data_allocator::construct(mystl::address_of(*end_), *(end_ - 1));
++new_end;
mystl::copy_backward(xpos, end_ - 1, end_);
*xpos = value_type(mystl::forward<Args>(args)...);
}
else
{
reallocate_emplace(xpos, mystl::forward<Args>(args)...);
}
return begin() + n;
}
// 在尾部就地构造元素,避免额外的复制或移动开销
template <class T>
template <class ...Args>
void vector<T>::emplace_back(Args&& ...args)
{
if (end_ < cap_)
{
data_allocator::construct(mystl::address_of(*end_), mystl::forward<Args>(args)...);
++end_;
}
else
{
reallocate_emplace(end_, mystl::forwarinserd<Args>(args)...);
}
}
// 在尾部插入元素
template <class T>
void vector<T>::push_back(const value_type& value)
{
if (end_ != cap_)
{
data_allocator::construct(mystl::address_of(*end_), value);
++end_;
}
else
{
reallocate_insert(end_, value);
}
}
// 弹出尾部元素
template <class T>
void vector<T>::pop_back()
{
MYSTL_DEBUG(!empty());
data_allocator::destroy(end_ - 1);
--end_;
}
// 在 pos 处插入元素
template <class T>
typename vector<T>::iterator
vector<T>::insert(const_iterator pos, const value_type& value)
{
MYSTL_DEBUG(pos >= begin() && pos <= end());
iterator xpos = const_cast<iterator>(pos);
const size_type n = pos - begin_;
if (end_ != cap_ && xpos == end_)
{
data_allocator::construct(mystl::address_of(*end_), value);
++end_;
}
else if (end_ != cap_)
{
auto new_end = end_;
data_allocator::construct(mystl::address_of(*end_), *(end_ - 1));
++new_end;
auto value_copy = value; // 避免元素因以下复制操作而被改变
mystl::copy_backward(xpos, end_ - 1, end_);
*xpos = mystl::move(value_copy);
end_ = new_end;
}
else
{
reallocate_insert(xpos, value);
}
return begin_ + n;
}
// 删除 pos 位置上的元素
template <class T>
typename vector<T>::iterator
vector<T>::erase(const_iterator pos)
{
MYSTL_DEBUG(pos >= begin() && pos < end());
iterator xpos = begin_ + (pos - begin());
mystl::move(xpos + 1, end_, xpos);
data_allocator::destroy(end_ - 1);
--end_;
return xpos;
}
// 删除[first, last)上的元素
template <class T>
typename vector<T>::iterator
vector<T>::erase(const_iterator first, const_iterator last)
{
MYSTL_DEBUG(first >= begin() && last <= end() && !(last < first));
const auto n = first - begin();
iterator r = begin_ + (first - begin());
data_allocator::destroy(mystl::move(r + (last - first), end_, r), end_);
end_ = end_ - (last - first);
return begin_ + n;
}
// 重置容器大小
template <class T>
void vector<T>::resize(size_type new_size, const value_type& value)
{
if (new_size < size())
{
erase(begin() + new_size, end());
}
else
{
insert(end(), new_size - size(), value);
}
}
// 与另一个 vector 交换
template <class T>
void vector<T>::swap(vector<T>& rhs) noexcept
{
if (this != &rhs)
{
mystl::swap(begin_, rhs.begin_);
mystl::swap(end_, rhs.end_);
mystl::swap(cap_, rhs.cap_);
}
}
/*****************************************************************************************/
// helper function
// try_init 函数,若分配失败则忽略,不抛出异常
template <class T>
void vector<T>::try_init() noexcept
{
try
{
begin_ = data_allocator::allocate(16);
end_ = begin_;
cap_ = begin_ + 16;
}
catch (...)
{
begin_ = nullptr;
end_ = nullptr;
cap_ = nullptr;
}
}
// init_space 函数
template <class T>
void vector<T>::init_space(size_type size, size_type cap)
{
try
{
begin_ = data_allocator::allocate(cap);
end_ = begin_ + size;
cap_ = begin_ + cap;
}
catch (...)
{
begin_ = nullptr;
end_ = nullptr;
cap_ = nullptr;
throw;
}
}
// fill_init 函数
template <class T>
void vector<T>::
fill_init(size_type n, const value_type& value)
{
const size_type init_size = mystl::max(static_cast<size_type>(16), n);
init_space(n, init_size);
mystl::uninitialized_fill_n(begin_, n, value);
}
// range_init 函数
template <class T>
template <class Iter>
void vector<T>::
range_init(Iter first, Iter last)
{
const size_type init_size = mystl::max(static_cast<size_type>(last - first),
static_cast<size_type>(16));
init_space(static_cast<size_type>(last - first), init_size);
mystl::uninitialized_copy(first, last, begin_);
}
// destroy_and_recover 函数
template <class T>
void vector<T>::
destroy_and_recover(iterator first, iterator last, size_type n)
{
data_allocator::destroy(first, last);
data_allocator::deallocate(first, n);
}
// get_new_cap 函数
template <class T>
typename vector<T>::size_type
vector<T>::
get_new_cap(size_type add_size)
{
const auto old_size = capacity();
THROW_LENGTH_ERROR_IF(old_size > max_size() - add_size,
"vector<T>'s size too big");
if (old_size > max_size() - old_size / 2)
{
return old_size + add_size > max_size() - 16
? old_size + add_size : old_size + add_size + 16;
}
const size_type new_size = old_size == 0
? mystl::max(add_size, static_cast<size_type>(16))
: mystl::max(old_size + old_size / 2, old_size + add_size);
return new_size;
}
// fill_assign 函数
template <class T>
void vector<T>::
fill_assign(size_type n, const value_type& value)
{
if (n > capacity())
{
vector tmp(n, value);
swap(tmp);
}
else if (n > size())
{
mystl::fill(begin(), end(), value);
end_ = mystl::uninitialized_fill_n(end_, n - size(), value);
}
else
{
erase(mystl::fill_n(begin_, n, value), end_);
}
}
// copy_assign 函数
template <class T>
template <class IIter>
void vector<T>::
copy_assign(IIter first, IIter last, input_iterator_tag)
{
auto cur = begin_;
for (; first != last && cur != end_; ++first, ++cur)
{
*cur = *first;
}
if (first == last)
{
erase(cur, end_);
}
else
{
insert(end_, first, last);
}
}
// 用 [first, last) 为容器赋值
template <class T>
template <class FIter>
void vector<T>::
copy_assign(FIter first, FIter last, forward_iterator_tag)
{
const size_type len = mystl::distance(first, last);
if (len > capacity())
{
vector tmp(first, last);
swap(tmp);
}
else if (size() >= len)
{
auto new_end = mystl::copy(first, last, begin_);
data_allocator::destroy(new_end, end_);
end_ = new_end;
}
else
{
auto mid = first;
mystl::advance(mid, size());
mystl::copy(first, mid, begin_);
auto new_end = mystl::uninitialized_copy(mid, last, end_);
end_ = new_end;
}
}
// 重新分配空间并在 pos 处就地构造元素
template <class T>
template <class ...Args>
void vector<T>::
reallocate_emplace(iterator pos, Args&& ...args)
{
const auto new_size = get_new_cap(1);
auto new_begin = data_allocator::allocate(new_size);
auto new_end = new_begin;
try
{
new_end = mystl::uninitialized_move(begin_, pos, new_begin);
data_allocator::construct(mystl::address_of(*new_end), mystl::forward<Args>(args)...);
++new_end;
new_end = mystl::uninitialized_move(pos, end_, new_end);
}
catch (...)
{
data_allocator::deallocate(new_begin, new_size);
throw;
}
destroy_and_recover(begin_, end_, cap_ - begin_);
begin_ = new_begin;
end_ = new_end;
cap_ = new_begin + new_size;
}
// 重新分配空间并在 pos 处插入元素
template <class T>
void vector<T>::reallocate_insert(iterator pos, const value_type& value)
{
const auto new_size = get_new_cap(1);
auto new_begin = data_allocator::allocate(new_size);
auto new_end = new_begin;
const value_type& value_copy = value;
try
{
new_end = mystl::uninitialized_move(begin_, pos, new_begin);
data_allocator::construct(mystl::address_of(*new_end), value_copy);
++new_end;
new_end = mystl::uninitialized_move(pos, end_, new_end);
}
catch (...)
{
data_allocator::deallocate(new_begin, new_size);
throw;
}
destroy_and_recover(begin_, end_, cap_ - begin_);
begin_ = new_begin;
end_ = new_end;
cap_ = new_begin + new_size;
}
// fill_insert 函数
template <class T>
typename vector<T>::iterator
vector<T>::
fill_insert(iterator pos, size_type n, const value_type& value)
{
if (n == 0)
return pos;
const size_type xpos = pos - begin_;
const value_type value_copy = value; // 避免被覆盖
if (static_cast<size_type>(cap_ - end_) >= n)
{ // 如果备用空间大于等于增加的空间
const size_type after_elems = end_ - pos;
auto old_end = end_;
if (after_elems > n)
{
mystl::uninitialized_copy(end_ - n, end_, end_);
end_ += n;
mystl::move_backward(pos, old_end - n, old_end);
mystl::uninitialized_fill_n(pos, n, value_copy);
}
else
{
end_ = mystl::uninitialized_fill_n(end_, n - after_elems, value_copy);
end_ = mystl::uninitialized_move(pos, old_end, end_);
mystl::uninitialized_fill_n(pos, after_elems, value_copy);
}
}
else
{ // 如果备用空间不足
const auto new_size = get_new_cap(n);
auto new_begin = data_allocator::allocate(new_size);
auto new_end = new_begin;
try
{
new_end = mystl::uninitialized_move(begin_, pos, new_begin);
new_end = mystl::uninitialized_fill_n(new_end, n, value);
new_end = mystl::uninitialized_move(pos, end_, new_end);
}
catch (...)
{
destroy_and_recover(new_begin, new_end, new_size);
throw;
}
data_allocator::deallocate(begin_, cap_ - begin_);
begin_ = new_begin;
end_ = new_end;
cap_ = begin_ + new_size;
}
return begin_ + xpos;
}
// copy_insert 函数
template <class T>
template <class IIter>
void vector<T>::
copy_insert(iterator pos, IIter first, IIter last)
{
if (first == last)
return;
const auto n = mystl::distance(first, last);
if ((cap_ - end_) >= n)
{ // 如果备用空间大小足够
const auto after_elems = end_ - pos;
auto old_end = end_;
if (after_elems > n)
{
end_ = mystl::uninitialized_copy(end_ - n, end_, end_);
mystl::move_backward(pos, old_end - n, old_end);
mystl::uninitialized_copy(first, last, pos);
}
else
{
auto mid = first;
mystl::advance(mid, after_elems);
end_ = mystl::uninitialized_copy(mid, last, end_);
end_ = mystl::uninitialized_move(pos, old_end, end_);
mystl::uninitialized_copy(first, mid, pos);
}
}
else
{ // 备用空间不足
const auto new_size = get_new_cap(n);
auto new_begin = data_allocator::allocate(new_size);
auto new_end = new_begin;
try
{
new_end = mystl::uninitialized_move(begin_, pos, new_begin);
new_end = mystl::uninitialized_copy(first, last, new_end);
new_end = mystl::uninitialized_move(pos, end_, new_end);
}
catch (...)
{
destroy_and_recover(new_begin, new_end, new_size);
throw;
}
data_allocator::deallocate(begin_, cap_ - begin_);
begin_ = new_begin;
end_ = new_end;
cap_ = begin_ + new_size;
}
}
// reinsert 函数
template <class T>
void vector<T>::reinsert(size_type size)
{
auto new_begin = data_allocator::allocate(size);
try
{
mystl::uninitialized_move(begin_, end_, new_begin);
}
catch (...)
{
data_allocator::deallocate(new_begin, size);
throw;
}
data_allocator::deallocate(begin_, cap_ - begin_);
begin_ = new_begin;
end_ = begin_ + size;
cap_ = begin_ + size;
}
/*****************************************************************************************/
// 重载比较操作符
template <class T>
bool operator==(const vector<T>& lhs, const vector<T>& rhs)
{
return lhs.size() == rhs.size() &&
mystl::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template <class T>
bool operator<(const vector<T>& lhs, const vector<T>& rhs)
{
return mystl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), lhs.end());
}
template <class T>
bool operator!=(const vector<T>& lhs, const vector<T>& rhs)
{
return !(lhs == rhs);
}
template <class T>
bool operator>(const vector<T>& lhs, const vector<T>& rhs)
{
return rhs < lhs;
}
template <class T>
bool operator<=(const vector<T>& lhs, const vector<T>& rhs)
{
return !(rhs < lhs);
}
template <class T>
bool operator>=(const vector<T>& lhs, const vector<T>& rhs)
{
return !(lhs < rhs);
}
// 重载 mystl 的 swap
template <class T>
void swap(vector<T>& lhs, vector<T>& rhs)
{
lhs.swap(rhs);
}
}
#endif // !VECTOR_H