-
Notifications
You must be signed in to change notification settings - Fork 4
/
vector.h
569 lines (342 loc) · 9.03 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
#pragma once
#include<memory>
#include<cmath>
#include<stdexcept>
namespace tiny
{
typedef unsigned long long size_type;
//iterator class
//pre-declaration, needed in friend declaraion
template<typename T> class vector;
template<typename T> class Iterator;
template<typename T> Iterator<T>& operator+(Iterator<T> & iv, size_type pos);
template<typename T> bool operator==(Iterator<T> & iv1, Iterator<T> & iv2);
template<typename T> bool operator!= (Iterator<T> & iv1, Iterator<T> & iv2);
template<class T>
class Iterator
{
friend Iterator& operator+<T>(Iterator& iv, size_type pos);
friend bool operator==<T>(Iterator& iv1, Iterator& iv2);
friend bool operator!=<T>(Iterator& iv1, Iterator& iv2);
friend class vector<T>;
public:
//default constructor
Iterator(T* pTemp = 0) :pIterator(pTemp) {}
//copy constructor
Iterator(const Iterator & iv) :pIterator(iv.pIterator) {}
//assign operation
Iterator& operator= (const Iterator & iv)
{
pIterator = iv.pIterator;
return *this;
}
//operator ++
Iterator & operator++()
{
++pIterator;
return *this;
}
//operator+
Iterator& operator+(size_type pos)
{
pIterator += pos;
return *this;
}
//operator-
Iterator& operator-(size_type pos)
{
pIterator -= pos;
return *this;
}
// dereference operator
T& operator*()
{
return *pIterator;
}
// get the raw pointer
T* getPointer()
{
return pIterator;
}
private:
T * pIterator;
};
template<class T> Iterator<T>& operator+(Iterator<T> & iv, size_type pos)
{
iv.pIterator += pos ;
return iv;
}
template<class T> bool operator==(Iterator<T> & iv1, Iterator<T> & iv2)
{
return iv1.pIterator == iv2.pIterator;
}
template<class T> bool operator!= (Iterator<T> & iv1, Iterator<T> & iv2)
{
return !(iv1 == iv2);
}
//container class
template<class T>
class vector
{
public:
typedef Iterator<T> iterator;
//default constructor,construct a empty vector
vector() :p_first_element(0), p_first_free(0), p_end(0) {}
//constructor
vector(size_type count,const T& t):p_first_element(0), p_first_free(0), p_end(0)
{
for (size_type i = 0; i != count; ++i)
{
push_back(t);
}
}
//copy constructor
vector(const vector&);
//destructor
~vector();
//Element access interfaces
// get the first element
const T& front() const
{
return *p_first_element;
}
T& front()
{
return *p_first_element;
}
//get the last element
const T& back() const
{
return *(p_first_free - 1);
}
T& back()
{
return *(p_first_free - 1);
}
// get the pos element
const T& at(size_type pos) const
{
if (!(pos < size()) )
throw std::out_of_range("out of range");
return *(p_first_element + pos);
}
T& at(size_type pos)
{
if (!(pos < size()))
throw std::out_of_range("out of range");
return *(p_first_element + pos);
}
//overloaded operator[]
const T& operator[] (size_type pos) const
{
if (!(pos < size()))
throw std::out_of_range("out of range");
return *(p_first_element + pos);
}
T& operator[] (size_type pos)
{
if (!(pos < size()))
throw std::out_of_range("out of range");
return *(p_first_element + pos);
}
//iterator interface
iterator& begin()
{
iv.pIterator = p_first_element;
return iv;
}
iterator& end()
{
iv.pIterator = p_first_free;
return iv;
}
//Modifiers interfaces
//add a element to vector
void push_back(const T& element_t);
//remove the last element of vector
void pop_back();
//remove of element from container,leave the capacity() unchanged;
void clear();
//resize container to hold count element;
void resize(size_type count);
//erase the element at pos,return iterator following the removed element
iterator& erase(iterator& pos);
//insert element before pos
iterator& insert(iterator& pos, const T& value);
//capacity interface
//get size of vector<T>
size_type size() const
{
return p_first_free - p_first_element;
}
//get capacity of vector<T>
size_type capacity() const
{
return p_end - p_first_element;
}
//get max_size() of vector<T>
size_type max_size() const
{
if (4 == sizeof(p_first_element))
{
return std::exp2(32)/ sizeof(T) -1;
}
else
{
return std::exp2(64) / sizeof(T) - 1;
}
}
//check whether the vector is empty
bool empty()
{
return 0 == size();
}
//set the capacity of vector
void reserve(unsigned long arg_capacity);
//remove unused capacity
void shrink_to_fit();
private:
void reallocate(unsigned long arg_capacity); // reallocate memeory, allocate a new block memory size arg_capacity, copy elements of vector to the new memory block and free the old memory ;
static std::allocator<T> av; //default allocator of vector
static iterator iv;
T* p_first_element; // pointer to first element (pointer to start of allocated memory)
T* p_first_free; // pointer after the last element (pointer to the start of allocated but not constructed element)
T* p_end; //pointer to p_end of allocated memory
};
//inition of static member: av & iv
template<class T> std::allocator<T> vector<T>::av = std::allocator<T>();
template<class T> typename vector<T>::iterator vector<T>::iv = Iterator<T>(0);
//class definition
//copy constructor
template<class T> vector<T>::vector(const vector &vt):p_first_element(0),p_first_free(0),p_end(0)
{
size_type _size = vt.size();
size_type _capacity = vt.capacity();
T* ptemp = av.allocate(_size);
p_first_element = ptemp;
for (unsigned int i = 0; i < _size ; ++i)
{
av.construct(p_first_element + i, *(vt.p_first_element + i));
}
p_first_free = p_first_element + _size;
p_end = p_first_element + _size;
}
//destructor
template<class T> vector<T>::~vector()
{
T* ptemp = p_first_element;
//delete element in vector
for (size_type i = 0; i < size(); ++i)
{
av.destroy(ptemp + i);
}
//free memory
av.deallocate(p_first_element, capacity());
}
template<class T> void vector<T>::push_back(const T& element_t)
{
//if no memory has allocated ,allocate 1*sizeof(T) memory
if (0 == capacity())
{
p_first_element = av.allocate(1);
p_first_free = p_first_element;
p_end = p_first_element + 1;
}
// if no unused memory left, doubles the allocated memory
if (size() == capacity())
{
reallocate( 2*capacity() );
}
// add a element to vector
av.construct(p_first_free, element_t);
p_first_free = p_first_free + 1;
}
//remove the last element of vector<T>
template<class T> void vector<T>::pop_back()
{
av.destroy(p_first_free - 1);
p_first_free = p_first_free - 1;
}
template<class T> void vector<T>::clear()
{
size_type _size = size();
for (size_type i = 0; i < _size; ++i)
{
pop_back();
}
}
//resize container to hold count element;
template<class T> void vector<T>::resize(size_type count)
{
while (count != size())
{
if (count > size())
push_back( T() );
else pop_back();
}
}
template<class T> typename vector<T>::iterator& vector<T>::erase(iterator& pos)
{
T* ptemp = pos.getPointer()+1;
while (ptemp != p_first_free)
{
*(ptemp-1) = *ptemp ;
ptemp += 1;
}
pop_back();
return pos;
}
template<class T> typename vector<T>::iterator& vector<T>::insert(iterator& pos, const T& value)
{
T* ptemp = pos.getPointer();
//记录偏移量,因为容器元素个数发生变化时(重新分配内存),原来迭代器(指针)失效
size_type dif = ptemp - p_first_element;
//call push_back to add element , and double size vector if necessary
//push_back(value);
push_back(value);
//根据偏移量计算新的对应的指针
T* ptemp_new = p_first_element + dif;
T* index = p_first_free - 1;
while (index != ptemp_new)
{
*index = *(index -1);
index -= 1;
}
av.construct(ptemp_new, value);
return pos;
}
//allocate new memory block
template<class T> void vector<T>::reallocate(unsigned long arg_capacity)
{
size_type _size = size();
size_type _capacity = capacity();
//allocate new memory block
T* temp = av.allocate(arg_capacity);
//copy element to new memory block and delete element in old memory block
for (size_type i = 0; i < _size; ++i)
{
av.construct(temp + i, *(p_first_element + i));
av.destroy(p_first_element + i);
}
//free old memory block
av.deallocate(p_first_element, _capacity);
// adjust cursor pinters;
p_first_element = temp;
p_first_free = p_first_element + _size;
p_end = p_first_element + arg_capacity;
}
template<class T> void vector<T>::reserve(unsigned long arg_capacity)
{
//if parameter is no bigger than real capacity , do nothing
if (arg_capacity <= capacity())
return;
//re-allocate memory
reallocate(arg_capacity);
}
template<class T> void vector<T>::shrink_to_fit()
{
if (size() == capacity())
return;
reallocate(size());
}
}