-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.h
More file actions
586 lines (564 loc) · 17.2 KB
/
vector.h
File metadata and controls
586 lines (564 loc) · 17.2 KB
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
#ifndef SJTU_VECTOR_HPP
#define SJTU_VECTOR_HPP
#include "exceptions.h"
#include <climits>
#include <cstddef>
#define MAXINITIAL 128
namespace sjtu
{
/**
* a data container like std::vector
* store data in a successive memory and support random access.
*/
template<typename T>
class vector
{
public:
T *index;
int memnum;
int maxnum;
void Double(){
maxnum*=2;
}
/**
* TODO
* a type for actions of the elements of a vector, and you should write
* a class named const_iterator with same interfaces.
*/
/**
* you can see RandomAccessIterator at CppReference for help.
*/
class const_iterator;
class iterator
{
// The following code is written for the C++ type_traits library.
// Type traits is a C++ feature for describing certain properties of a type.
// For instance, for an iterator, iterator::value_type is the type that the
// iterator points to.
// STL algorithms and containers may use these type_traits (e.g. the following
// typedef) to work properly. In particular, without the following code,
// @code{std::sort(iter, iter1);} would not compile.
// See these websites for more information:
// https://en.cppreference.com/w/cpp/header/type_traits
// About value_type: https://blog.csdn.net/u014299153/article/details/72419713
// About iterator_category: https://en.cppreference.com/w/cpp/iterator
public:
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
using iterator_category = std::output_iterator_tag;
/**
* TODO add data members
* just add whatever you want.
*/
T *iter;
public:
int nownum;
iterator(){
iter= nullptr;
nownum=0;
}
/**
* return a new iterator which pointer n-next elements
* as well as operator-
*/
iterator operator+(const int &n) const
{
iterator temp;
temp.iter=iter;
temp.nownum=nownum+n;
return temp;
//TODO
}
iterator operator-(const int &n) const
{
if(nownum-n<0){
throw runtime_error();
}
iterator temp;
temp.iter=iter;
temp.nownum=nownum-n;
return temp;
//TODO
}
// return the distance between two iterators,
// if these two iterators point to different vectors, throw invaild_iterator.
int operator-(const iterator &rhs) const
{
return nownum-rhs.nownum;
//TODO
}
iterator& operator+=(const int &n)
{
nownum+=n;
return *this;
//TODO
}
iterator& operator-=(const int &n)
{
if(nownum-n<0)throw runtime_error();
nownum-=n;
return *this;
//TODO
}
/**
* TODO iter++
*/
iterator operator++(int) {
iterator temp;
temp.iter=iter;
temp.nownum=nownum;
nownum++;
return temp;
}
/**
* TODO ++iter
*/
iterator& operator++() {
nownum++;
return *this;
}
/**
* TODO iter--
*/
iterator operator--(int) {
if(nownum==0)throw runtime_error();
iterator temp;
temp.iter=iter;
temp.nownum=nownum;
nownum--;
return temp;
}
/**
* TODO --iter
*/
iterator& operator--() {
if(nownum==0)throw runtime_error();
nownum--;
return *this;
}
/**
* TODO *it
*/
T& operator*() const{
return iter[nownum];
}
/**
* a operator to check whether two iterators are same (pointing to the same memory address).
*/
bool operator==(const iterator &rhs) const {
if(iter!=rhs.iter)return false;
if(nownum!=rhs.nownum)return false;
return true;
}
bool operator==(const const_iterator &rhs) const {
if(iter!=rhs.iter)return false;
if(nownum!=rhs.nownum)return false;
return true;
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
if(iter!=rhs.iter)return true;
if(nownum!=rhs.nownum)return true;
return false;
}
bool operator!=(const const_iterator &rhs) const {
if(iter!=rhs.iter)return true;
if(nownum!=rhs.nownum)return true;
return false;
}
};
/**
* TODO
* has same function as iterator, just for a const object.
*/
class const_iterator
{
public:
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
using iterator_category = std::output_iterator_tag;
private:
/*TODO*/
public:T *iter;
int nownum;
const_iterator(){
iter= nullptr;
nownum=0;
}
public:
/**
* return a new iterator which pointer n-next elements
* as well as operator-
*/
const_iterator operator+(const int &n) const
{
const_iterator temp;
temp.iter=iter;
temp.nownum=nownum+n;
return temp;
//TODO
}
const_iterator operator-(const int &n) const
{
if(nownum-n<0){
throw runtime_error();
}
const_iterator temp;
temp.iter=iter;
temp.nownum=nownum-n;
return temp;
//TODO
}
// return the distance between two iterators,
// if these two iterators point to different vectors, throw invaild_iterator.
int operator-(const const_iterator &rhs) const
{
return nownum-rhs.nownum;
//TODO
}
const_iterator& operator+=(const int &n)
{
nownum+=n;
return *this;
//TODO
}
const_iterator& operator-=(const int &n)
{
if(nownum-n<0)throw runtime_error();
nownum-=n;
return *this;
//TODO
}
/**
* TODO iter++
*/
const_iterator operator++(int) {
const_iterator temp;
temp.iter=iter;
temp.nownum=nownum;
nownum++;
return temp;
}
/**
* TODO ++iter
*/
const_iterator& operator++() {
nownum++;
return *this;
}
/**
* TODO iter--
*/
const_iterator operator--(int) {
if(nownum==0)throw runtime_error();
const_iterator temp;
temp.iter=iter;
temp.nownum=nownum;
nownum--;
return temp;
}
/**
* TODO --iter
*/
const_iterator& operator--() {
if(nownum==0)throw runtime_error();
nownum--;
return *this;
}
/**
* TODO *it
*/
const T& operator*() const{
return iter[nownum];
}
/**
* a operator to check whether two iterators are same (pointing to the same memory address).
*/
bool operator==(const iterator &rhs) const {
if(iter!=rhs.iter)return false;
if(nownum!=rhs.nownum)return false;
return true;
}
bool operator==(const const_iterator &rhs) const {
if(iter!=rhs.iter)return false;
if(nownum!=rhs.nownum)return false;
return true;
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
if(iter!=rhs.iter)return true;
if(nownum!=rhs.nownum)return true;
return false;
}
bool operator!=(const const_iterator &rhs) const {
if(iter!=rhs.iter)return true;
if(nownum!=rhs.nownum)return true;
return false;
}
};
/**
* TODO Constructs
* At least two: default constructor, copy constructor
*/
vector(): memnum(0), maxnum(MAXINITIAL) {
index=(T*)malloc(MAXINITIAL*sizeof (T));
}
vector(const vector &other) : memnum(other.memnum), maxnum(other.maxnum){
if(other.index== nullptr)index= nullptr;
else{
index=(T*)malloc(maxnum*sizeof (T));
for(int i=0;i<memnum;i++){
index[i]=other.index[i];
}
}
}
/**
* TODO Destructor
*/
~vector() {
for(int i=0; i<memnum; ++i) {
index[i].~T();
}
free(index);
}
/**
* TODO Assignment operator
*/
vector &operator=(const vector &other) {
if (this != &other) {
maxnum = other.maxnum;
memnum = other.memnum;
std::free(index);
index = (T *) malloc(maxnum * sizeof(T));
for (int i = 0; i < memnum; i++) {
index[i] = other.index[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>memnum-1||pos<0)throw index_out_of_bound();
return index[pos];
}
const T & at(const size_t &pos) const {
if(pos>memnum-1||pos<0)throw index_out_of_bound();
return index[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 but I want you to do.
*/
T & operator[](const size_t &pos) {
if(pos>memnum-1||pos<0)throw index_out_of_bound();
return index[pos];
}
const T & operator[](const size_t &pos) const {
if(pos>memnum-1||pos<0)throw index_out_of_bound();
return index[pos];
}
/**
* access the first element.
* throw container_is_empty if size == 0
*/
const T & front() const {
if(memnum==0)throw container_is_empty();
else return index[0];
}
/**
* access the last element.
* throw container_is_empty if size == 0
*/
const T & back() const {
if(memnum==0)throw container_is_empty();
else return index[memnum-1];
}
/**
* returns an iterator to the beginning.
*/
iterator begin() {
iterator tmp;
tmp.iter=index;
return tmp;
}
const_iterator cbegin() const {
const_iterator tmp;
tmp.iter=index;
return tmp;
}
/**
* returns an iterator to the end.
*/
iterator end() {
iterator tmp;tmp.iter=index;
// tmp.iter= index[memnum];
tmp.nownum=memnum;
return tmp;
}
const_iterator cend() const {
const_iterator tmp;tmp.iter=index;
// tmp.iter= index[memnum];
tmp.nownum=memnum;
return tmp;
}
/**
* checks whether the container is empty
*/
bool empty() const {
if(memnum==0)return true;
return false;
}
/**
* returns the number of elements
*/
size_t size() const {
return memnum;
}
/**
* clears the contents
*/
void clear() {
memnum=0;
std::free(index);
index= nullptr;
}
/**
* inserts value before pos
* returns an iterator pointing to the inserted value.
*/
iterator insert(iterator pos, const T &value) {
if(memnum>=maxnum-1){
Double();
T *tmp=(T*)malloc(maxnum*sizeof(T));
for(int i=0;i<memnum;i++){
new(tmp+i)T(index[i]);
}
for(int i=0;i<memnum;i++){
index[i].~T();
}
free(index);
index=tmp;
}
memnum++;
new(index+memnum-1)T(index[memnum-2]);
iterator iter=this->begin();
int now=pos.nownum;
iter.nownum=now;
for(int i=memnum-2;i>now;i--){
index[i]=index[i-1];
}
index[now]=value;
return iter;
}
/**
* 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>memnum){
throw index_out_of_bound();
}
if(memnum>=maxnum-1){
Double();
T *tmp=(T*)malloc(maxnum*sizeof(T));
for(int i=0;i<memnum;i++){
new(tmp+i)T(index[i]);
}
for(int i=0;i<memnum;i++){
index[i].~T();
}
free(index);
index=tmp;
}
memnum++;
new(index+memnum-1)T(index[memnum-2]);
iterator iter=this->begin();
iter.nownum=ind;
for(int i=memnum-2;i>ind;i--){
index[i]=index[i-1];
}
index[ind]=value;
return iter;
}
/**
* 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) {
iterator iter=this->begin();
int now = pos.nownum;
iter.nownum=now;
memnum--;
index[memnum].~T();
for(int i=now;i<memnum;i++){
index[i]=index[i+1];
}
return iter;
}
/**
* 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>=memnum){
throw index_out_of_bound();
}
iterator iter=this->begin();
iter.nownum=ind;
memnum--;
index[memnum].~T();
for(int i=ind;i<memnum;i++){
index[i]=index[i+1];
}
return iter;
}
/**
* adds an element to the end.
*/
void push_back(const T &value) {
if(memnum>=maxnum-1){
Double();
T *tmp=(T*)malloc(maxnum*sizeof(T));
for(int i=0;i<memnum;i++){
new(tmp+i)T(index[i]);
}
for(int i=0;i<memnum;i++){
index[i].~T();
}
free(index);
index=tmp;
}
memnum++;
new(index+memnum-1)T(value);
}
/**
* remove the last element from the end.
* throw container_is_empty if size() == 0
*/
void pop_back() {
if(memnum==0)throw container_is_empty();
else {
index[memnum-1].~T();
memnum--;
}
}
};
}
#endif