-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
1221 lines (607 loc) · 19 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
// vector standard header
#ifndef _NEWVECTOR_
#define _NEWVECTOR_
#include <climits>
#include <memory>
#include <stdexcept>
#include <xutility>
#ifdef _MSC_VER
#pragma pack(push,8)
#endif // _MSC_VER
_STD_BEGIN
// TEMPLATE CLASS vector
template<class _Ty, class _A = allocator<_Ty> >
class vector {
public:
typedef vector<_Ty, _A> _Myt;
typedef _A allocator_type;
typedef _A::size_type size_type;
typedef _A::difference_type difference_type;
typedef _A::pointer _Tptr;
typedef _A::const_pointer _Ctptr;
typedef _A::reference reference;
typedef _A::const_reference const_reference;
typedef _A::value_type value_type;
typedef _Tptr iterator;
typedef _Ctptr const_iterator;
typedef reverse_iterator<const_iterator, value_type,
const_reference, _Ctptr, difference_type>
const_reverse_iterator;
typedef reverse_iterator<iterator, value_type,
reference, _Tptr, difference_type>
reverse_iterator;
explicit vector(const _A& _Al = _A())
: allocator(_Al), _First(0), _Last(0), _End(0) {}
explicit vector(size_type _N, const _Ty& _V = _Ty(),
const _A& _Al = _A())
: allocator(_Al)
{_First = allocator.allocate(_N, (void *)0);
_Ufill(_First, _N, _V);
_Last = _First + _N;
_End = _Last; }
vector(const _Myt& _X)
: allocator(_X.allocator)
{_First = allocator.allocate(_X.size(), (void *)0);
_Last = _Ucopy(_X.begin(), _X.end(), _First);
_End = _Last; }
typedef const_iterator _It;
vector(_It _F, _It _L, const _A& _Al = _A())
: allocator(_Al), _First(0), _Last(0), _End(0)
{insert(begin(), _F, _L); }
~vector()
{_Destroy(_First, _Last);
allocator.deallocate(_First, _End - _First);
_First = 0, _Last = 0, _End = 0; }
_Myt& operator=(const _Myt& _X)
{if (this == &_X)
;
else if (_X.size() <= size())
{iterator _S = copy(_X.begin(), _X.end(), _First);
_Destroy(_S, _Last);
_Last = _First + _X.size(); }
else if (_X.size() <= capacity())
{const_iterator _S = _X.begin() + size();
copy(_X.begin(), _S, _First);
_Ucopy(_S, _X.end(), _Last);
_Last = _First + _X.size(); }
else
{_Destroy(_First, _Last);
allocator.deallocate(_First, _End - _First);
_First = allocator.allocate(_X.size(), (void *)0);
_Last = _Ucopy(_X.begin(), _X.end(),
_First);
_End = _Last; }
return (*this); }
void reserve(size_type _N)
{if (capacity() < _N)
{iterator _S = allocator.allocate(_N, (void *)0);
_Ucopy(_First, _Last, _S);
_Destroy(_First, _Last);
allocator.deallocate(_First, _End - _First);
_End = _S + _N;
_Last = _S + size();
_First = _S; }}
size_type capacity() const
{return (_First == 0 ? 0 : _End - _First); }
iterator begin()
{return (_First); }
const_iterator begin() const
{return ((const_iterator)_First); }
iterator end()
{return (_Last); }
const_iterator end() const
{return ((const_iterator)_Last); }
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())); }
void resize(size_type _N, const _Ty& _X = _Ty())
{if (size() < _N)
insert(end(), _N - size(), _X);
else if (_N < size())
verase(begin() + _N, end()); }
size_type size() const
{return (_First == 0 ? 0 : _Last - _First); }
size_type max_size() const
{return (allocator.max_size()); }
bool empty() const
{return (size() == 0); }
_A get_allocator() const
{return (allocator); }
const_reference at(size_type _P) const
{if (size() <= _P)
_Xran();
return (*(begin() + _P)); }
reference at(size_type _P)
{if (size() <= _P)
_Xran();
return (*(begin() + _P)); }
const_reference operator[](size_type _P) const
{return (*(begin() + _P)); }
reference operator[](size_type _P)
{return (*(begin() + _P)); }
reference front()
{return (*begin()); }
const_reference front() const
{return (*begin()); }
reference back()
{return (*(end() - 1)); }
const_reference back() const
{return (*(end() - 1)); }
void push_back(const _Ty& _X)
{insert(end(), _X); }
void pop_back()
{verase(end() - 1); }
void assign(_It _F, _It _L)
{verase(begin(), end());
insert(begin(), _F, _L); }
void assign(size_type _N, const _Ty& _X = _Ty())
{verase(begin(), end());
insert(begin(), _N, _X); }
iterator insert(iterator _P, const _Ty& _X = _Ty())
{size_type _O = _P - begin();
insert(_P, 1, _X);
return (begin() + _O); }
iterator insert(int i, const _Ty& _X = _Ty())
{return insert(begin()+i,_X);}
void insert(iterator _P, size_type _M, const _Ty& _X)
{if (_End - _Last < _M)
{size_type _N = size() + (_M < size() ? size() : _M);
iterator _S = allocator.allocate(_N, (void *)0);
iterator _Q = _Ucopy(_First, _P, _S);
_Ufill(_Q, _M, _X);
_Ucopy(_P, _Last, _Q + _M);
_Destroy(_First, _Last);
allocator.deallocate(_First, _End - _First);
_End = _S + _N;
_Last = _S + size() + _M;
_First = _S; }
else if (_Last - _P < _M)
{_Ucopy(_P, _Last, _P + _M);
_Ufill(_Last, _M - (_Last - _P), _X);
fill(_P, _Last, _X);
_Last += _M; }
else if (0 < _M)
{_Ucopy(_Last - _M, _Last, _Last);
copy_backward(_P, _Last - _M, _Last);
fill(_P, _P + _M, _X);
_Last += _M; }}
void insert(iterator _P, _It _F, _It _L)
{size_type _M = 0;
_Distance(_F, _L, _M);
if (_End - _Last < _M)
{size_type _N = size() + (_M < size() ? size() : _M);
iterator _S = allocator.allocate(_N, (void *)0);
iterator _Q = _Ucopy(_First, _P, _S);
_Q = _Ucopy(_F, _L, _Q);
_Ucopy(_P, _Last, _Q);
_Destroy(_First, _Last);
allocator.deallocate(_First, _End - _First);
_End = _S + _N;
_Last = _S + size() + _M;
_First = _S; }
else if (_Last - _P < _M)
{_Ucopy(_P, _Last, _P + _M);
_Ucopy(_F + (_Last - _P), _L, _Last);
copy(_F, _F + (_Last - _P), _P);
_Last += _M; }
else if (0 < _M)
{_Ucopy(_Last - _M, _Last, _Last);
copy_backward(_P, _Last - _M, _Last);
copy(_F, _L, _P);
_Last += _M; }}
iterator verase(iterator _P)
{copy(_P + 1, end(), _P);
_Destroy(_Last - 1, _Last);
--_Last;
return (_P); }
iterator verase(iterator _F, iterator _L)
{iterator _S = copy(_L, end(), _F);
_Destroy(_S, end());
_Last = _S;
return (_F); }
iterator verase(int i)
{return verase(begin()+i);}
void clear()
{verase(begin(), end()); }
bool operator==(const _Myt& _X) const
{return (size() == _X.size()
&& equal(begin(), end(), _X.begin())); }
bool operator!=(const _Myt& _X) const
{return (!(*this == _X)); }
bool operator<(const _Myt& _X) const
{return (lexicographical_compare(begin(), end(),
_X.begin(), _X.end())); }
bool operator>(const _Myt& _X) const
{return (_X < *this); }
bool operator<=(const _Myt& _X) const
{return (!(_X < *this)); }
bool operator>=(const _Myt& _X) const
{return (!(*this < _X)); }
void swap(_Myt& _X)
{if (allocator == _X.allocator)
{std::swap(_First, _X._First);
std::swap(_Last, _X._Last);
std::swap(_End, _X._End); }
else
{_Myt _Ts = *this; *this = _X, _X = _Ts; }}
friend void swap(_Myt& _X, _Myt& _Y)
{_X.swap(_Y); }
protected:
void _Destroy(iterator _F, iterator _L)
{for (; _F != _L; ++_F)
allocator.destroy(_F); }
iterator _Ucopy(const_iterator _F, const_iterator _L,
iterator _P)
{for (; _F != _L; ++_P, ++_F)
allocator.construct(_P, *_F);
return (_P); }
void _Ufill(iterator _F, size_type _N, const _Ty &_X)
{for (; 0 < _N; --_N, ++_F)
allocator.construct(_F, _X); }
void _Xran() const
{_THROW(out_of_range, "invalid vector<T> subscript"); }
_A allocator;
iterator _First, _Last, _End;
};
// CLASS vector<_Bool, allocator>
typedef unsigned int _Vbase;
const int _VBITS = CHAR_BIT * sizeof (_Vbase);
typedef allocator<_Vbase> _Bool_allocator;
class vector<_Bool, _Bool_allocator> {
public:
typedef _Bool_allocator _A;
typedef _Bool _Ty;
typedef vector<_Ty, _A> _Myt;
typedef vector<_Vbase, _A> _Vbtype;
typedef _A allocator_type;
typedef _A::size_type size_type;
typedef _A::difference_type difference_type;
// CLASS reference
class reference {
public:
reference()
: _Mask(0), _Ptr(0) {}
reference(size_t _O, _Vbase *_P)
: _Mask((_Vbase)1 << _O), _Ptr(_P) {}
reference& operator=(const reference& _X)
{return (*this = bool(_X)); }
reference& operator=(bool _V)
{if (_V)
*_Ptr |= _Mask;
else
*_Ptr &= ~_Mask;
return (*this); }
void flip()
{*_Ptr ^= _Mask; }
bool operator~() const
{return (!bool(*this)); }
operator bool() const
{return ((*_Ptr & _Mask) != 0); }
protected:
_Vbase _Mask, *_Ptr;
};
typedef const reference const_reference;
typedef bool value_type;
// CLASS iterator
class iterator : public _Ranit<_Bool, difference_type> {
public:
iterator()
: _Off(0), _Ptr(0) {}
iterator(size_t _O, _Vbase *_P)
: _Off(_O), _Ptr(_P) {}
reference operator*() const
{return (reference(_Off, _Ptr)); }
iterator& operator++()
{_Inc();
return (*this); }
iterator operator++(int)
{iterator _Tmp = *this;
_Inc();
return (_Tmp); }
iterator& operator--()
{_Dec();
return (*this); }
iterator operator--(int)
{iterator _Tmp = *this;
_Dec();
return (_Tmp); }
iterator& operator+=(difference_type _N)
{_Off += _N;
_Ptr += _Off / _VBITS;
_Off %= _VBITS;
return (*this); }
iterator& operator-=(difference_type _N)
{return (*this += -_N); }
iterator operator+(difference_type _N) const
{iterator _Tmp = *this;
return (_Tmp += _N); }
iterator operator-(difference_type _N) const
{iterator _Tmp = *this;
return (_Tmp -= _N); }
difference_type operator-(const iterator _X) const
{return (_VBITS * (_Ptr - _X._Ptr)
+ (difference_type)_Off
- (difference_type)_X._Off); }
reference operator[](difference_type _N) const
{return (*(*this + _N)); }
bool operator==(const iterator& _X) const
{return (_Ptr == _X._Ptr && _Off == _X._Off); }
bool operator!=(const iterator& _X) const
{return (!(*this == _X)); }
bool operator<(const iterator& _X) const
{return (_Ptr < _X._Ptr
|| _Ptr == _X._Ptr && _Off < _X._Off); }
bool operator>(const iterator& _X) const
{return (_X < *this); }
bool operator<=(const iterator& _X) const
{return (!(_X < *this)); }
bool operator>=(const iterator& _X) const
{return (!(*this < _X)); }
protected:
void _Dec()
{if (_Off != 0)
--_Off;
else
_Off = _VBITS - 1, --_Ptr; }
void _Inc()
{if (_Off < _VBITS - 1)
++_Off;
else
_Off = 0, ++_Ptr; }
size_t _Off;
_Vbase *_Ptr;
};
// CLASS const_iterator
class const_iterator : public iterator {
public:
const_iterator()
: iterator() {}
const_iterator(size_t _O, const _Vbase *_P)
: iterator(_O, (_Vbase *)_P) {}
const_iterator(const iterator& _X)
: iterator(_X) {}
const_reference operator*() const
{return (reference(_Off, _Ptr)); }
const_iterator& operator++()
{_Inc();
return (*this); }
const_iterator operator++(int)
{const_iterator _Tmp = *this;
_Inc();
return (_Tmp); }
const_iterator& operator--()
{_Dec();
return (*this); }
const_iterator operator--(int)
{const_iterator _Tmp = *this;
_Dec();
return (_Tmp); }
const_iterator& operator+=(difference_type _N)
{_Off += _N;
_Ptr += _Off / _VBITS;
_Off %= _VBITS;
return (*this); }
const_iterator& operator-=(difference_type _N)
{return (*this += -_N); }
const_iterator operator+(difference_type _N) const
{const_iterator _Tmp = *this;
return (_Tmp += _N); }
const_iterator operator-(difference_type _N) const
{const_iterator _Tmp = *this;
return (_Tmp -= _N); }
difference_type operator-(const const_iterator _X) const
{return (_VBITS * (_Ptr - _X._Ptr)
+ (difference_type)_Off
- (difference_type)_X._Off); }
const_reference operator[](difference_type _N) const
{return (*(*this + _N)); }
bool operator==(const const_iterator& _X) const
{return (_Ptr == _X._Ptr && _Off == _X._Off); }
bool operator!=(const const_iterator& _X) const
{return (!(*this == _X)); }
bool operator<(const const_iterator& _X) const
{return (_Ptr < _X._Ptr
|| _Ptr == _X._Ptr && _Off < _X._Off); }
bool operator>(const const_iterator& _X) const
{return (_X < *this); }
bool operator<=(const const_iterator& _X) const
{return (!(_X < *this)); }
bool operator>=(const const_iterator& _X) const
{return (!(*this < _X)); }
};
typedef reverse_iterator<const_iterator, value_type,
const_reference, const_reference *, difference_type>
const_reverse_iterator;
typedef reverse_iterator<iterator, value_type,
reference, reference *, difference_type>
reverse_iterator;
explicit vector(const _A& _Al = _A())
: _Size(0), _Vec(_Al) {}
explicit vector(size_type _N, const bool _V = false,
const _A& _Al = _A())
: _Vec(_Nw(_N), _V ? -1 : 0, _Al) {_Trim(_N); }
typedef const_iterator _It;
vector(_It _F, _It _L, const _A& _Al = _A())
: _Size(0), _Vec(_Al)
{insert(begin(), _F, _L); }
~vector()
{_Size = 0; }
void reserve(size_type _N)
{_Vec.reserve(_Nw(_N)); }
size_type capacity() const
{return (_Vec.capacity() * _VBITS); }
iterator begin()
{return (iterator(0, _Vec.begin())); }
const_iterator begin() const
{return (const_iterator(0, _Vec.begin())); }
iterator end()
{iterator _Tmp = begin();
if (0 < _Size)
_Tmp += _Size;
return (_Tmp); }
const_iterator end() const
{const_iterator _Tmp = begin();
if (0 < _Size)
_Tmp += _Size;
return (_Tmp); }
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())); }
void resize(size_type _N, bool _X = false)
{if (size() < _N)
insert(end(), _N - size(), _X);
else if (_N < size())
verase(begin() + _N, end()); }
size_type size() const
{return (_Size); }
size_type max_size() const
{return (_Vec.max_size() * _VBITS); }
bool empty() const
{return (size() == 0); }
_A get_allocator() const
{return (_Vec.get_allocator()); }
const_reference at(size_type _P) const
{if (size() <= _P)
_Xran();
return (*(begin() + _P)); }
reference at(size_type _P)
{if (size() <= _P)
_Xran();
return (*(begin() + _P)); }
const_reference operator[](size_type _P) const
{return (*(begin() + _P)); }
reference operator[](size_type _P)
{return (*(begin() + _P)); }
reference front()
{return (*begin()); }
const_reference front() const
{return (*begin()); }
reference back()
{return (*(end() - 1)); }
const_reference back() const
{return (*(end() - 1)); }
void push_back(const bool _X)
{insert(end(), _X); }
void pop_back()
{verase(end() - 1); }
void assign(_It _F, _It _L)
{verase(begin(), end());
insert(begin(), _F, _L); }
void assign(size_type _N, const bool _X = false)
{verase(begin(), end());
insert(begin(), _N, _X); }
iterator insert(iterator _P, const bool _X = false)
{size_type _O = _P - begin();
insert(_P, 1, _X);
return (begin() + _O); }
void insert(iterator _P, size_type _M, const bool _X)
{if (0 < _M)
{if (capacity() - size() < _M)
{size_type _O = _P - begin();
_Vec.resize(_Nw(size() + _M), 0);
_P = begin() + _O; }
copy_backward(_P, end(), end() + _M);
fill(_P, _P + _M, _X);
_Size += _M; }}
void insert(iterator _P, _It _F, _It _L)
{size_type _M = 0;
_Distance(_F, _L, _M);
if (0 < _M)
{if (capacity() - size() < _M)
{size_type _O = _P - begin();
_Vec.resize(_Nw(size() + _M), 0);
_P = begin() + _O; }
copy_backward(_P, end(), end() + _M);
copy(_F, _L, _P);
_Size += _M; }}
iterator verase(iterator _P)
{copy(_P + 1, end(), _P);
_Trim(_Size - 1);
return (_P); }
iterator verase(iterator _F, iterator _L)
{iterator _S = copy(_L, end(), _F);
_Trim(_S - begin());
return (_F); }
iterator verase(int i)
{return verase(begin()+i);}
void clear()
{verase(begin(), end()); }
void flip()
{for (_Vbtype::iterator _S = _Vec.begin();
_S != _Vec.end(); ++_S)
*_S = ~*_S;
_Trim(_Size); }
bool operator==(const _Myt& _X) const
{return (_Size == _X._Size && _Vec == _X._Vec); }
bool operator!=(const _Myt& _X) const
{return (!(*this == _X)); }
bool operator<(const _Myt& _X) const
{return (_Size < _X._Size
|| _Size == _X._Size && _Vec < _X._Vec); }
bool operator>(const _Myt& _X) const
{return (_X < *this); }
bool operator<=(const _Myt& _X) const
{return (!(_X < *this)); }
bool operator>=(const _Myt& _X) const
{return (!(*this < _X)); }
void swap(_Myt& _X)
{std::swap(_Size, _X._Size);
_Vec.swap(_X._Vec); }
friend void swap(_Myt& _X, _Myt& _Y)
{_X.swap(_Y); }
static void swap(reference _X, reference _Y)
{bool _V = _X;
_X = _Y;
_Y = _V; }
protected:
static size_type _Nw(size_type _N)
{return ((_N + _VBITS - 1) / _VBITS); }
void _Trim(size_type _N)
{size_type _M = _Nw(_N);
if (_M < _Vec.size())
_Vec.verase(_Vec.begin() + _M, _Vec.end());
_Size = _N;
_N %= _VBITS;
if (0 < _N)
_Vec[_M - 1] &= ((_Vbase)1 << _N) - 1; }
void _Xran() const
{_THROW(out_of_range, "invalid vector<bool> subscript"); }
size_type _Size;
_Vbtype _Vec;
};
typedef vector<_Bool, _Bool_allocator> _Bvector;
_STD_END
#ifdef _MSC_VER
#pragma pack(pop)
#endif // _MSC_VER
#endif // _NEWVECTOR_
/*
* Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
*/
/*
* This file is derived from software bearing the following
* restrictions:
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this
* software and its documentation for any purpose is hereby
* granted without fee, provided that the above copyright notice
* appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
* Hewlett-Packard Company makes no representations about the
* suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
*/