generated from q4x3/CS1958-Lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.hpp
474 lines (470 loc) · 14.1 KB
/
vector.hpp
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
#ifndef SJTU_VECTOR_HPP
#define SJTU_VECTOR_HPP
#include "exceptions.hpp"
#include <climits>
#include <cstddef>
namespace sjtu {
/**
* a data container like std::vector
* store data in a successive memory and support random access.
* thanks to google.com
* thanks to RandomAccessIterator at CppReference.
* thanks to CSDN
* thanks to Shu Ze Chen, Alan Liang, Xiao Yang Lu and other classmates.
*/
template<typename T>
class vector {
public:
///current size of the vector
size_t vectorSize;
///current maximal capacity of the vector
size_t capacity;
///a secondary pointer which points to a pointer array
T **storage;
class const_iterator;
class iterator {
private:
///a pointer which points to the vector which the iterator belongs to
vector<T> *vectorStorage;
T **pointer;
public:
iterator(vector<T> *vec, T **ptr):vectorStorage(vec), pointer(ptr) {}
/**
* return a new iterator which pointer n-next elements
* as well as operator-
*/
iterator operator+(const int &n) const {
iterator tmp(vectorStorage, pointer + n);
return tmp;
}
iterator operator-(const int &n) const {
iterator tmp(vectorStorage, pointer - n);
return tmp;
}
/// return the distance between two iterators,
/// if these two iterators point to different vectors, throw invaild_iterator.
int operator-(const iterator &rhs) const {
if (vectorStorage != rhs.vectorStorage) throw invalid_iterator();
int tmp;
tmp = pointer - rhs.pointer;
return tmp;
}
iterator& operator+=(const int &n) {
pointer += n;
return *this;
}
iterator& operator-=(const int &n) {
pointer -= n;
return *this;
}
/**
* iter++
*/
iterator operator++(int) {
iterator tmp(vectorStorage, pointer);
++pointer;
return(tmp);
}
/**
* ++iter
*/
iterator& operator++() {
++pointer;
return *this;
}
/**
* iter--
*/
iterator operator--(int) {
iterator tmp(vectorStorage, pointer);
--pointer;
return(tmp);
}
/**
* --iter
*/
iterator& operator--() {
--pointer;
return *this;
}
/**
* *it
*/
T& operator*() const{return **pointer;}
/**
* a operator to check whether two iterators are same (pointing to the same memory address).
*/
bool operator==(const iterator &rhs) const {
return (pointer == rhs.pointer);
}
bool operator==(const const_iterator &rhs) const {
return (pointer == rhs.pointer);
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
return (pointer != rhs.pointer);
}
bool operator!=(const const_iterator &rhs) const {
return (pointer != rhs.pointer);
}
///the pointer of the iterator is needed in functions in the vector.
///notice that the outer class is not the friend class of the inner class.
friend class vector<T>;
///operater== and operator!= needs const_iterator
friend class const_iterator;
};
/**
* has same function as iterator, just for a const object.
*/
class const_iterator {
private:
const vector<T> *vectorStorage;
T **pointer;
public:
const_iterator(const vector<T> *vec, T **ptr):vectorStorage(vec), pointer(ptr) {}
/**
* return a new iterator which pointer n-next elements
* as well as operator-
*/
const_iterator operator+(const int &n) const {
const_iterator tmp(vectorStorage, pointer + n);
return tmp;
}
const_iterator operator-(const int &n) const {
const_iterator tmp(vectorStorage, pointer - n);
return tmp;
}
/// return the distance between two iterators,
/// if these two iterators point to different vectors, throw invaild_iterator.
int operator-(const const_iterator &rhs) const {
if (vectorStorage != rhs.vectorStorage) throw invalid_iterator();
int tmp;
tmp = pointer - rhs.pointer;
return tmp;
}
const_iterator& operator+=(const int &n) {
pointer += n;
return *this;
}
const_iterator& operator-=(const int &n) {
pointer -= n;
return *this;
}
/**
* iter++
*/
const_iterator operator++(int) {
const_iterator tmp(vectorStorage, pointer);
++pointer;
return(tmp);
}
/**
* ++iter
*/
const_iterator& operator++() {
++pointer;
return *this;
}
/**
* iter--
*/
const_iterator operator--(int) {
const_iterator tmp(vectorStorage, pointer);
--pointer;
return(tmp);
}
/**
* --iter
*/
const_iterator& operator--() {
--pointer;
return *this;
}
/**
* *it
*/
///cannot change the value of the object which the const_iterator points to
const T& operator*() const{return **pointer;}
/**
* a operator to check whether two iterators are same (pointing to the same memory address).
*/
bool operator==(const iterator &rhs) const {
return (pointer == rhs.pointer);
}
bool operator==(const const_iterator &rhs) const {
return (pointer == rhs.pointer);
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
return (pointer != rhs.pointer);
}
bool operator!=(const const_iterator &rhs) const {
return (pointer != rhs.pointer);
}
///the pointer of the iterator is needed in functions in the vector.
///notice that the outer class is not the friend class of the inner class.
friend class vector<T>;
///operater== and operator!= needs iterator
friend class iterator;
};
/**
* Constructor
* default constructor, copy constructor
*/
vector():vectorSize(0), capacity(8), storage(new T* [capacity]) {}
vector(const vector &other):vectorSize(other.vectorSize), capacity(other.capacity), storage(new T* [other.capacity]) {
for (size_t i = 0; i < vectorSize; ++i)
{
//下标运算符优先级高于*运算符
storage[i] = new T(*other.storage[i]);
}
}
/**
* Destructor
*/
~vector() {
for (size_t i = 0; i < vectorSize; ++i) delete storage[i];
if (storage)
{
delete [] storage;
storage = nullptr;
}
}
/**
* Assignment operator
*/
vector &operator=(const vector &other) {
if (this == &other) return *this;
for (size_t i = 0; i < vectorSize; ++i) delete storage[i];
if (storage) delete [] storage;
vectorSize = other.vectorSize;
capacity = other.capacity;
storage = new T* [capacity];
for (size_t i = 0; i < vectorSize; ++i)
{
storage[i] = new T(*other.storage[i]);
}
return *this;
}
/**
* assigns specified element with bounds checking
* throw index_out_of_bound if pos is not in [0, size)
*/
T & at(const size_t &pos) {
if (pos < 0 || pos >= vectorSize) throw index_out_of_bound();
return *storage[pos];
}
///used by const vector
//返回一个常量的引用,不能通过引用改变所引用对象的值
const T & at(const size_t &pos) const {
if (pos < 0 || pos >= vectorSize) throw index_out_of_bound();
return *storage[pos];
}
/**
* assigns specified element with bounds checking
* throw index_out_of_bound if pos is not in [0, size)
* !!! Pay attentions
* In STL this operator does not check the boundary.
*/
T & operator[](const size_t &pos) {
if (pos < 0 || pos >= vectorSize) throw index_out_of_bound();
return *storage[pos];
}
///used by const vector
const T & operator[](const size_t &pos) const {
if (pos < 0 || pos >= vectorSize) throw index_out_of_bound();
return *storage[pos];
}
/**
* access the first element.
* throw container_is_empty if size == 0
*/
const T & front() const {
if (vectorSize == 0) throw container_is_empty();
return *storage[0];
}
/**
* access the last element.
* throw container_is_empty if size == 0
*/
const T & back() const {
if (vectorSize == 0) throw container_is_empty();
return *storage[vectorSize - 1];
}
/**
* returns an iterator to the beginning.
*/
iterator begin() {
iterator tmp(this, storage);
return tmp;
}
///used by const vector
//这里是sjtu::vector的常量成员函数,不能修改其数据成员
//因此const_iterator中必须把数据成员都设为常量,这样就不可能修改了
const_iterator cbegin() const {
const_iterator tmp(this, storage);
return tmp;
}
/**
* returns an iterator to the end.
*/
iterator end() {
iterator tmp(this, storage + vectorSize);
return tmp;
}
///used by const vector
const_iterator cend() const {
const_iterator tmp(this, storage + vectorSize);
return tmp;
}
/**
* checks whether the container is empty
*/
bool empty() const {
if (vectorSize == 0) return true;
else return false;
}
/**
* returns the number of elements
*/
size_t size() const {
return vectorSize;
}
/**
* clears the contents
*/
void clear() {
for (size_t i = 0; i < vectorSize; ++i) delete storage[i];
vectorSize = 0;
}
/**
* inserts value before pos(valid)
* returns an iterator pointing to the inserted value.
*/
iterator insert(iterator pos, const T &value) {
long long ind = pos - begin();
if (full()) expand();
storage[vectorSize] = new T(*storage[vectorSize - 1]);
for (long long i = vectorSize - 2; i >= ind; --i)
{
*storage[i + 1] = *storage[i];
}
*storage[ind] = value;
++vectorSize;
iterator itr(this, storage + ind);
return itr;
}
/**
* inserts value at index ind.
* after inserting, this->at(ind) == value
* returns an iterator pointing to the inserted value.
* throw index_out_of_bound if ind > size (in this situation ind can be size because after inserting the size will increase 1.)
*/
iterator insert(const size_t &ind, const T &value) {
if (ind > vectorSize) throw index_out_of_bound();
if (full()) expand();
if (vectorSize == ind) {
storage[vectorSize] = new T(value);
++vectorSize;
} else {
storage[vectorSize] = new T(*storage[vectorSize - 1]);
//防止size_t到long long自动类型转唤出错
long long newind = ind;
for (long long i = vectorSize - 2; i >= newind; --i)
{
*storage[i + 1] = *storage[i];
}
*storage[ind] = value;
++vectorSize;
}
iterator itr(this, storage + ind);
return itr;
}
/**
* removes the element at pos.
* return an iterator pointing to the following element.
* If the iterator pos refers the last element, the end() iterator is returned.
*/
iterator erase(iterator pos) {
if (pos == end() - 1)
{
delete storage[vectorSize - 1];
--vectorSize;
return end();
}
size_t aryPos = pos - begin();
for (size_t i = aryPos; i < vectorSize - 1; ++i)
{
*storage[i] = *storage[i + 1];
}
delete storage[vectorSize - 1];
--vectorSize;
return pos;
}
/**
* removes the element with index ind.
* return an iterator pointing to the following element.
* throw index_out_of_bound if ind >= size
*/
iterator erase(const size_t &ind) {
if (ind >= vectorSize) throw index_out_of_bound();
if (ind == vectorSize - 1)
{
delete storage[vectorSize - 1];
--vectorSize;
return end();
}
for (size_t i = ind; i < vectorSize - 1; ++i)
{
*storage[i] = *storage[i + 1];
}
delete storage[vectorSize - 1];
--vectorSize;
iterator itr(this, storage + ind);
return itr;
}
/**
* adds an element to the end.
*/
void push_back(const T &value) {
if (full()) expand();
storage[vectorSize] = new T(value);
++vectorSize;
}
/**
* remove the last element from the end.
* throw container_is_empty if size() == 0
*/
void pop_back() {
if (size() == 0) throw container_is_empty();
delete storage[vectorSize - 1];
--vectorSize;
}
private:
///judge whether the currentsize is about to be full
inline bool full() {
return (vectorSize + 2 >= capacity);
}
///doublespace when the current size is close to the maximal capacity
void expand() {
capacity *= 2;
T **newStorage = new T* [capacity];
for (size_t i = 0; i < vectorSize; ++i)
{
newStorage[i] = new T(*storage[i]);
delete storage[i];
}
if (storage) delete [] storage;
storage = newStorage;
}
// the inner class is naturally the friend class of the outer class
// friend class iterator;
// friend class const_iterator;
};
///the end of namespace sjtu
}
#endif