-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_vector.h
609 lines (469 loc) · 13.8 KB
/
ml_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
/**
* @file ml_vector.h
* @brief
* @author malin [email protected]
* @date 2015年04月06日 星期一 10时58分11秒
* @version
* @note
*/
#ifndef __ML_INTERNAL_VECTOR_H
#define __ML_INTERNAL_VECTOR_H
#include "ml_config.h"
#include "ml_alloc.h"
#include "ml_exception.h"
#include "ml_algobase.h"
#include "ml_construct.h"
#include "ml_uninitialized.h"
#include "ml_iterator_base.h"
__ML_BEGIN_NAMESPACE
template<typename T, typename Alloc = alloc>
class vector
{
public:
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef Alloc allocator_type;
public:
// construct functions
explicit vector(const allocator_type &a = allocator_type()) : start(0), finish(0), end_of_storage(0) {}
explicit vector(size_type n, const value_type &value = value_type(), const allocator_type &_a = allocator_type())
{
start = data_allocator::allocate(n);
end_of_storage = finish = uninitialized_fill_n(start, n, value);
}
template<typename InputIterator>
vector(InputIterator first, InputIterator last)
{
typename iterator_traits<InputIterator>::iterator_category cat;
// dispatched by iterator tag
range_initialize(first, last, cat);
}
vector(const vector &rhs)
{
start = allocate_and_copy(rhs.size(), rhs.begin(), rhs.end());
finish = start + rhs.size(); end_of_storage = finish;
}
/*
* assignment operator
*
* version1: invoke clear() and copy_aux(), but this method is not efficient, because unnecessaried memory raleasing
* and allcaoting may be caused
*
* version2: implement optimzation according to the old size and capacity sa well as new size
*
*/
vector& operator = (const vector &rhs)
{
if (this != &rhs)
{
size_type new_size = rhs.size();
if (new_size > capacity())
{
destroy_and_deallocate();
start = allocate_and_copy(new_size, rhs.begin(), rhs.end());
finish = start + new_size; end_of_storage = finish;
}
else if (new_size >= size())
{
uninitialized_copy(rhs.begin() + size(), rhs.end(), copy(rhs.begin(), rhs.begin() + size(), start));
finish = start + new_size;
}
else
{
copy(rhs.begin(), rhs.end(), start);
destroy(start + new_size, finish);
finish = start + new_size;
}
}
return *this;
}
// unmutable functions
size_type size() const {return end() - begin();}
size_type capacity() const {return size_type(end_of_storage - start);}
bool empty() const {return begin() == end();}
iterator begin() const {return start;}
iterator end() const {return finish;}
const_reference front() const {return *begin();}
const_reference back() const {return *(end() - 1);}
const_reference operator [] (size_type index) const {return *(begin() + index);}
const_reference at(size_type index) const
{
range_check(index);
return operator[](index);
}
// capacity mutable functions
void resize(size_type n, const value_type &x = value_type())
{
if (size() > n) erase(start + n, finish);
else insert(finish, n - size(), x);
}
void reserve(size_type n)
{
if (n <= size()) return;
iterator new_start = allocate_and_copy(n, start, finish);
destroy_and_deallocate();
start = new_start; finish = start + size(); end_of_storage = start + n;
}
void assign(size_type n, const value_type &x)
{
if (capacity() >= n)
{
size_type old_size = size();
if (old_size <= n)
{
fill(start, finish, x);
finish = uninitialized_fill_n(finish, n - old_size, x);
}
else
{
fill_n(start, n, x); destroy(start + n, finish);
}
}
else
{
iterator new_start = data_allocator::allocate(n);
try
{
uninitialized_fill_n(new_start, n, x);
}
catch (...)
{
data_allocator::deallocate(new_start, n);
throw;
}
destroy_and_deallocate();
start = new_start; finish = start + n; end_of_storage = finish;
}
}
template<typename InputIterator>
void assign(InputIterator first, InputIterator last)
{
typename iterator_traits<InputIterator>::iterator_category cat;
assign_dispatch(first, last, cat);
}
// mutable function, the memory may be modified
reference front() {return *begin();}
reference back() {return *(end() - 1);}
reference operator [] (size_type index) {return *(begin() + index);}
reference at(size_type index)
{
range_check();
return operator[](index);
}
void push_back(const value_type &x)
{
// needn't special treatment for the exception which may be rised by the constructor, the user code
// should handle the exception correctly
if (finish < end_of_storage) construct(finish++, x);
else elem_insert_aux(end(), x);
}
void pop_back()
{
if (empty()) throw underflow_error("vector is empty!");
destroy(--finish);
}
iterator insert(iterator position, const value_type &x)
{
// needn't special treatment for the exception which may be rised by the constructor, the user code
// should handle the exception correctly
if (finish < end_of_storage && position == finish) construct(finish++, x);
else
{
size_type n = position - start;
elem_insert_aux(position, x);
return start + n;
}
}
void insert(iterator position, size_type n, const value_type& x)
{
if (n == 0) return;
if (size_type(end_of_storage - finish) >= n)
{
size_type elems_after = finish - position;
iterator old_finish = finish;
if (elems_after < n)
{
finish += n - elems_after;
uninitialized_fill(old_finish, finish, x);
uninitialized_copy(position, old_finish, finish);
fill(position, old_finish, x);
finish += elems_after;
}
else
{
value_type tmp = x;
finish += n;
uninitialized_copy(old_finish - n, old_finish, old_finish);
// must use copy_abckward instead of copy
copy_backward(position, old_finish - n, old_finish);
fill_n(position, n, tmp);
}
}
else
{
// must allocate a bigger memory
size_type old_size = size(), new_size = old_size + max(old_size, n);
iterator new_start = data_allocator::allocate(new_size), new_finish = new_start;
try
{
value_type tmp = x;
new_finish = uninitialized_copy(start, position, new_finish);
new_finish = uninitialized_fill_n(new_finish, n, tmp);
new_finish = uninitialized_copy(position, finish, new_finish);
}
catch (...)
{
destroy(new_start, new_finish);
data_allocator::deallocate(new_start, new_size);
throw;
}
// destroy and release the old vector
destroy_and_deallocate();
start = new_start; finish = new_finish; end_of_storage = start + new_size;
}
}
template<typename InputIterator>
void insert(iterator position, InputIterator first, InputIterator last)
{
if (first == last) return;
// dispatch
typename iterator_traits<InputIterator>::iterator_category cat;
insert_dispatch(position, first, last, cat);
}
iterator erase(iterator position)
{
if (position + 1 != end()) copy(position + 1, end(), position);
destroy(--finish);
return position;
}
iterator erase(iterator first, iterator last)
{
if (last != end()) copy(last, end(), first);
size_type n = (last - first);
destroy(end() - n, end());
finish -= n;
return first;
}
void clear()
{
destroy(begin(), end());
data_allocator::deallocate(begin(), size());
start = 0; finish = 0; end_of_storage = 0;
}
void swap(vector &x)
{
ml::swap(start, x.start);
ml::swap(finish, x.finish);
ml::swap(end_of_storage, x.end_of_storage);
}
private:
void range_check(size_type n) const
{
if (n >= size()) throw out_of_range("vector's subscript is out of range!");
}
// destroy and release memroy
void destroy_and_deallocate()
{
destroy(start, finish);
data_allocator::deallocate(start, size());
}
//used for internal construct
template<typename RandomAccessIterator>
void range_initialize(RandomAccessIterator first, RandomAccessIterator last, random_access_iterator_tag)
{
typename iterator_traits<RandomAccessIterator>::difference_type n = last - first;
start = allocate_and_copy(n, first, last);
end_of_storage = finish = start + n;
}
template<typename InputIterator>
void range_initialize(InputIterator first, InputIterator last, input_iterator_tag)
{
while (first != last) push_back(*first++);
}
// allocate memory and fill it by coping from ainternal
template<typename InputIterator>
inline iterator allocate_and_copy(size_type n, InputIterator first, InputIterator last)
{
iterator result = data_allocator::allocate(n);
try {uninitialized_copy(first, last, result);}
catch (...) {data_allocator::deallocate(result, n); throw;}
return result;
}
// this function is used for insert an element in the vector
void elem_insert_aux(iterator position, const T &x)
{
if (finish < end_of_storage)
{
construct(finish, *(finish - 1));
value_type tmp = x; // as x may be an element of the vector, we must reserve a temporary
copy_backward(position, finish, finish + 1);
*position = tmp;
++finish;
}
else
{
size_type old_size = size(), new_size = old_size == 0 ? 1 : 2 * old_size;
iterator new_start = data_allocator::allocate(new_size), new_finish = new_start;
try
{
// transform the first half
new_finish = uninitialized_copy(start, position, new_finish);
construct(new_finish++, x);
// transform the second half
new_finish = uninitialized_copy(position, finish, new_finish);
}
catch (...)
{
// roll back
destroy(new_start, new_finish);
data_allocator::deallocate(new_start, new_size);
throw;
}
destroy(start, finish);
data_allocator::deallocate(start, old_size);
start = new_start; finish = new_finish; end_of_storage = start + new_size;
}
}
// dispatch funvtion for interval insert
template<typename InputIterator>
void insert_dispatch(iterator position, InputIterator first, InputIterator last, input_iterator_tag)
{
// if the input iterator's category is input_iterator_tag, we can only insert them one by one
for (; first != last; ++first) position = insert(position, *first) + 1;
}
template<typename ForwardIterator>
void insert_dispatch(iterator position, ForwardIterator first, ForwardIterator last, forward_iterator_tag)
{
size_type n = distance(first, last);
if (end_of_storage - finish >= n)
{
size_type elems_after = finish - position;
iterator old_finish = finish;
if (elems_after < n)
{
finish += n - elems_after;
uninitialized_copy(position, old_finish, finish);
ForwardIterator mid = first;
advance(mid, elems_after);
copy(first, mid, position);
uninitialized_copy(mid, last, old_finish);
finish += elems_after;
}
else
{
finish += n;
uninitialized_copy(old_finish - n, old_finish, old_finish);
copy_backward(position, old_finish - n, old_finish);
copy(first, last, position);
}
}
else
{
size_type old_size = size(), new_size = old_size + max(old_size, n);
iterator new_start = data_allocator::allocate(new_size), new_finish = new_start;
try
{
new_finish = uninitialized_copy(start, position, new_finish);
new_finish = uninitialized_copy(first, last, new_finish);
new_finish = uninitialized_copy(position, finish, new_finish);
}
catch (...)
{
destroy(new_start, new_finish);
data_allocator::deallocate(new_start, new_size);
throw;
}
destroy(start, finish);
data_allocator::deallocate(start, old_size);
start = new_start; finish = new_finish; end_of_storage = new_start + new_size;
}
}
// interval assign function for input_iterator_tag
template<typename InputIterator>
void assign_dispatch(InputIterator first, InputIterator last, input_iterator_tag)
{
iterator cur = start;
for (; cur != finish, first != last; ++first, ++cur) *cur = *first;
if (cur != finish) erase(cur, finish);
else insert(finish, first, last);
}
// interval assign funcion for forward_iterator_tag
template<typename ForwardIterator>
void assign_dispatch(ForwardIterator first, ForwardIterator last, forward_iterator_tag)
{
size_type n = distance(first, last);
if (capacity() >= n)
{
size_type old_size = size();
if (old_size < n)
{
ForwardIterator mid;
advance(mid, old_size);
copy(first, mid, start);
finish = uninitialized_copy(mid, last, finish);
}
else
{
iterator new_finish = copy(first, last, start);
destroy(new_finish, finish);
finish = new_finish;
}
}
else
{
iterator new_start = allocate_and_copy(n, first, last);
destroy_and_deallocate();
start = new_start; finish = start + n; end_of_storage = finish;
}
}
private:
typedef simple_alloc<value_type, Alloc> data_allocator;
iterator start;
iterator finish;
iterator end_of_storage;
};
// relation operators
template<typename T, typename Alloc>
inline bool operator < (const vector<T, Alloc> &x, const vector<T, Alloc> &y)
{
return lexicgraphical_compare(x.begin(), x.end(), y.begin(), y.end());
}
template<typename T, typename Alloc>
bool operator <= (const vector<T, Alloc> &x, const vector<T, Alloc> &y)
{
return !(y < x);
}
template<typename T, typename Alloc>
bool operator > (const vector<T, Alloc> &x, const vector<T, Alloc> &y)
{
return y < x;
}
template<typename T, typename Alloc>
bool operator >= (const vector<T, Alloc> &x, const vector<T, Alloc> &y)
{
return !(x < y);
}
template<typename T, typename Alloc>
bool operator == (const vector<T, Alloc> &x, vector<T, Alloc> &y)
{
return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
}
template<typename T, typename Alloc>
bool operator != (const vector<T, Alloc> &x, const vector<T, Alloc> &y)
{
return !(x == y);
}
template<typename T, typename Alloc>
inline void swap(vector<T, Alloc> &x, vector<T, Alloc> &y)
{
x.swap(y);
}
__ML_END_NAMESPACE
#endif // __ML_INTERNAL_VECTOR_H