-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
700 lines (580 loc) · 17.6 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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
* Vector.h
*
* Created on: Mar 15, 2015
* Author: Aurelia
*/
/*
* File: vector.h
* --------------
* This file exports the Vector class, which provides an efficient, safe,
* convenient replacement for the array type in C++.
*/
#ifndef _vector_h
#define _vector_h
#include <iterator>
#include <iostream>
#include <sstream>
#include <string>
#include "strlib.h"
/*
* Class: Vector<ValueType>
* ------------------------
* This class stores an ordered list of values similar to an array. It
* supports traditional array selection using square brackets, but also
* supports inserting and deleting elements. It is similar in function to
* the STL vector type, but is simpler both to use and to implement.
*/
template <typename ValueType>
class Vector {
public:
/*
* Constructor: Vector
* Usage: Vector<ValueType> vec;
* Vector<ValueType> vec(n, value);
* ---------------------------------------
* Initializes a new vector. The default constructor creates an empty
* vector. The second form creates an array with n elements, each of which
* is initialized to value; if value is missing, the elements are
* initialized to the default value for the type.
*/
Vector();
explicit Vector(int n, ValueType value = ValueType());
/*
* Destructor: ~Vector
* -------------------
* Frees any heap storage allocated by this vector.
*/
virtual ~Vector();
/*
* Method: size
* Usage: int nElems = vec.size();
* -------------------------------
* Returns the number of elements in this vector.
*/
int size() const;
/*
* Method: isEmpty
* Usage: if (vec.isEmpty()) ...
* -----------------------------
* Returns true if this vector contains no elements.
*/
bool isEmpty() const;
/*
* Method: clear
* Usage: vec.clear();
* -------------------
* Removes all elements from this vector.
*/
void clear();
/*
* Method: get
* Usage: ValueType val = vec.get(index);
* --------------------------------------
* Returns the element at the specified index in this vector. This method
* signals an error if the index is not in the array range.
*/
const ValueType & get(int index) const;
/*
* Method: set
* Usage: vec.set(index, value);
* -----------------------------
* Replaces the element at the specified index in this vector with a new
* value. The previous value at that index is overwritten. This method
* signals an error if the index is not in the array range.
*/
void set(int index, const ValueType & value);
/*
* Method: insert
* Usage: vec.insert(0, value);
* ----------------------------
* Inserts the element into this vector before the specified index. All
* subsequent elements are shifted one position to the right. This method
* signals an error if the index is outside the range from 0 up to and
* including the length of the vector.
*/
void insert(int index, ValueType value);
/*
* Method: remove
* Usage: vec.remove(index);
* -------------------------
* Removes the element at the specified index from this vector. All
* subsequent elements are shifted one position to the left. This method
* signals an error if the index is outside the array range.
*/
void remove(int index);
/*
* Method: add
* Usage: vec.add(value);
* ----------------------
* Adds a new value to the end of this vector. To ensure compatibility
* with the vector class in the Standard Template Library, this method is
* also called push_back.
*/
void add(ValueType value);
void push_back(ValueType value);
/*
* Operator: []
* Usage: vec[index]
* -----------------
* Overloads [] to select elements from this vector. This extension
* enables the use of traditional array notation to get or set individual
* elements. This method signals an error if the index is outside the
* array range. The file supports two versions of this operator, one for
* const vectors and one for mutable vectors.
*/
ValueType & operator[](int index);
const ValueType & operator[](int index) const;
/*
* Operator: +
* Usage: v1 + v2
* --------------
* Concatenates two vectors.
*/
Vector operator+(const Vector & v2) const;
/*
* Operator: +=
* Usage: v1 += v2;
* v1 += value;
* -------------------
* Adds all of the elements from v2 (or the single specified value) to v1.
* As a convenience, the Vector package also overloads the comma operator
* so that it is possible to initialize a vector like this:
*
* Vector<int> digits;
* digits += 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;
*/
Vector & operator+=(const Vector & v2);
Vector & operator+=(const ValueType & value);
/*
* Method: toString
* Usage: string str = vec.toString();
* -----------------------------------
* Converts the vector to a printable string representation.
*/
std::string toString();
/*
* Method: mapAll
* Usage: vec.mapAll(fn);
* ----------------------
* Calls the specified function on each element of the vector in ascending
* index order.
*/
void mapAll(void (*fn)(ValueType)) const;
void mapAll(void (*fn)(const ValueType &)) const;
template <typename FunctorType>
void mapAll(FunctorType fn) const;
/*
* Additional Vector operations
* ----------------------------
* In addition to the methods listed in this interface, the Vector class
* supports the following operations:
*
* - Stream I/O using the << and >> operators
* - Deep copying for the copy constructor and assignment operator
* - Iteration using the range-based for statement or STL iterators
*
* The iteration forms process the Vector in index order.
*/
/* Private section */
/**********************************************************************/
/* Note: Everything below this point in the file is logically part */
/* of the implementation and should not be of interest to clients. */
/**********************************************************************/
private:
/*
* Implementation notes: Vector data structure
* -------------------------------------------
* The elements of the Vector are stored in a dynamic array of the
* specified element type. If the space in the array is ever exhausted,
* the implementation doubles the array capacity.
*/
/* Instance variables */
ValueType *elements; /* A dynamic array of the elements */
int capacity; /* The allocated size of the array */
int count; /* The number of elements in use */
/* Private methods */
void expandCapacity();
void deepCopy(const Vector & src);
/*
* Hidden features
* ---------------
* The remainder of this file consists of the code required to support deep
* copying and iteration. Including these methods in the public interface
* would make that interface more difficult to understand for the average
* client.
*/
public:
/*
* Deep copying support
* --------------------
* This copy constructor and operator= are defined to make a deep copy,
* making it possible to pass or return vectors by value and assign from
* one vector to another.
*/
Vector(const Vector & src);
Vector & operator=(const Vector & src);
/*
* Operator: ,
* -----------
* Adds an element to the vector passed as the left-hand operatand. This
* form makes it easier to initialize vectors in old versions of C++.
*/
Vector & operator,(const ValueType & value);
/*
* Iterator support
* ----------------
* The classes in the StanfordCPPLib collection implement input iterators
* so that they work symmetrically with respect to the corresponding STL
* classes.
*/
class iterator :
public std::iterator<std::random_access_iterator_tag, ValueType> {
private:
const Vector *vp;
int index;
public:
iterator() {
this->vp = NULL;
}
iterator(const iterator & it) {
this->vp = it.vp;
this->index = it.index;
}
iterator(const Vector *vp, int index) {
this->vp = vp;
this->index = index;
}
iterator & operator++() {
index++;
return *this;
}
iterator operator++(int) {
iterator copy(*this);
operator++();
return copy;
}
iterator & operator--() {
index--;
return *this;
}
iterator operator--(int) {
iterator copy(*this);
operator--();
return copy;
}
bool operator==(const iterator & rhs) {
return vp == rhs.vp && index == rhs.index;
}
bool operator!=(const iterator & rhs) {
return !(*this == rhs);
}
bool operator<(const iterator & rhs) {
extern void error(std::string msg);
if (vp != rhs.vp) error("Iterators are in different vectors");
return index < rhs.index;
}
bool operator<=(const iterator & rhs) {
extern void error(std::string msg);
if (vp != rhs.vp) error("Iterators are in different vectors");
return index <= rhs.index;
}
bool operator>(const iterator & rhs) {
extern void error(std::string msg);
if (vp != rhs.vp) error("Iterators are in different vectors");
return index > rhs.index;
}
bool operator>=(const iterator & rhs) {
extern void error(std::string msg);
if (vp != rhs.vp) error("Iterators are in different vectors");
return index >= rhs.index;
}
iterator operator+(const int & rhs) {
return iterator(vp, index + rhs);
}
iterator operator+=(const int & rhs) {
index += rhs;
return *this;
}
iterator operator-(const int & rhs) {
return iterator(vp, index - rhs);
}
iterator operator-=(const int & rhs) {
index -= rhs;
return *this;
}
int operator-(const iterator & rhs) {
extern void error(std::string msg);
if (vp != rhs.vp) error("Iterators are in different vectors");
return index - rhs.index;
}
ValueType & operator*() {
return vp->elements[index];
}
ValueType *operator->() {
return &vp->elements[index];
}
ValueType & operator[](int k) {
return vp->elements[index + k];
}
};
iterator begin() const {
return iterator(this, 0);
}
iterator end() const {
return iterator(this, count);
}
};
/* Implementation section */
extern void error(std::string msg);
/*
* Implementation notes: Vector constructor and destructor
* -------------------------------------------------------
* The constructor allocates storage for the dynamic array and initializes
* the other fields of the object. The destructor frees the memory used
* for the array.
*/
template <typename ValueType>
Vector<ValueType>::Vector() {
count = capacity = 0;
elements = NULL;
}
template <typename ValueType>
Vector<ValueType>::Vector(int n, ValueType value) {
count = capacity = n;
elements = (n == 0) ? NULL : new ValueType[n];
for (int i = 0; i < n; i++) {
elements[i] = value;
}
}
template <typename ValueType>
Vector<ValueType>::~Vector() {
if (elements != NULL) delete[] elements;
}
/*
* Implementation notes: Vector methods
* ------------------------------------
* The basic Vector methods are straightforward and should require no
* detailed documentation.
*/
template <typename ValueType>
int Vector<ValueType>::size() const {
return count;
}
template <typename ValueType>
bool Vector<ValueType>::isEmpty() const {
return count == 0;
}
template <typename ValueType>
void Vector<ValueType>::clear() {
if (elements != NULL) delete[] elements;
count = capacity = 0;
elements = NULL;
}
template <typename ValueType>
const ValueType & Vector<ValueType>::get(int index) const {
if (index < 0 || index >= count) error("get: index out of range");
return elements[index];
}
template <typename ValueType>
void Vector<ValueType>::set(int index, const ValueType & value) {
if (index < 0 || index >= count) error("set: index out of range");
elements[index] = value;
}
/*
* Implementation notes: insert, remove, add
* -----------------------------------------
* These methods must shift the existing elements in the array to make room
* for a new element or to close up the space left by a deleted one.
*/
template <typename ValueType>
void Vector<ValueType>::insert(int index, ValueType value) {
if (count == capacity) expandCapacity();
if (index < 0 || index > count) {
error("insert: index out of range");
}
for (int i = count; i > index; i--) {
elements[i] = elements[i - 1];
}
elements[index] = value;
count++;
}
template <typename ValueType>
void Vector<ValueType>::remove(int index) {
if (index < 0 || index >= count) error("remove: index out of range");
for (int i = index; i < count - 1; i++) {
elements[i] = elements[i + 1];
}
count--;
}
template <typename ValueType>
void Vector<ValueType>::add(ValueType value) {
insert(count, value);
}
template <typename ValueType>
void Vector<ValueType>::push_back(ValueType value) {
insert(count, value);
}
/*
* Implementation notes: Vector selection
* --------------------------------------
* The following code implements traditional array selection using square
* brackets for the index.
*/
template <typename ValueType>
ValueType & Vector<ValueType>::operator[](int index) {
if (index < 0 || index >= count) error("Selection index out of range");
return elements[index];
}
template <typename ValueType>
const ValueType & Vector<ValueType>::operator[](int index) const {
if (index < 0 || index >= count) error("Selection index out of range");
return elements[index];
}
template <typename ValueType>
Vector<ValueType> Vector<ValueType>::operator+(const Vector & v2) const {
Vector<ValueType> vec = *this;
for (int i=0; i<v2.capacity;i++) {
v2.elements[i]= value;
vec.add(value);
}
return vec;
}
template <typename ValueType>
Vector<ValueType> & Vector<ValueType>::operator+=(const Vector & v2) {
for (int i= 0; i<v2.capacity;i++) {
v2.elements[i]= value;
*this += value;
}
return *this;
}
template <typename ValueType>
Vector<ValueType> & Vector<ValueType>::operator+=(const ValueType & value) {
this->add(value);
return *this;
}
template <typename ValueType>
std::string Vector<ValueType>::toString() {
ostringstream os;
os << *this;
return os.str();
}
/*
* Implementation notes: copy constructor and assignment operator
* --------------------------------------------------------------
* The constructor and assignment operators follow a standard paradigm, as
* described in the associated textbook.
*/
template <typename ValueType>
Vector<ValueType>::Vector(const Vector & src) {
deepCopy(src);
}
template <typename ValueType>
Vector<ValueType> & Vector<ValueType>::operator=(const Vector & src) {
if (this != &src) {
if (elements != NULL) delete[] elements;
deepCopy(src);
}
return *this;
}
template <typename ValueType>
void Vector<ValueType>::deepCopy(const Vector & src) {
count = capacity = src.count;
elements = (capacity == 0) ? NULL : new ValueType[capacity];
for (int i = 0; i < count; i++) {
elements[i] = src.elements[i];
}
}
/*
* Implementation notes: The , operator
* ------------------------------------
* The comma operator works adding the right operand to the vector and then
* returning the vector by reference so that it is set for the next value
* in the chain.
*/
template <typename ValueType>
Vector<ValueType> & Vector<ValueType>::operator,(const ValueType & value) {
this->add(value);
return *this;
}
/*
* Implementation notes: mapAll
* ----------------------------
* The various versions of the mapAll function apply the function or
* function object to each element in ascending index order.
*/
template <typename ValueType>
void Vector<ValueType>::mapAll(void (*fn)(ValueType)) const {
for (int i = 0; i < count; i++) {
fn(elements[i]);
}
}
template <typename ValueType>
void Vector<ValueType>::mapAll(void (*fn)(const ValueType &)) const {
for (int i = 0; i < count; i++) {
fn(elements[i]);
}
}
template <typename ValueType>
template <typename FunctorType>
void Vector<ValueType>::mapAll(FunctorType fn) const {
for (int i = 0; i < count; i++) {
fn(elements[i]);
}
}
/*
* Implementation notes: expandCapacity
* ------------------------------------
* This function doubles the array capacity, copies the old elements into
* the new array, and then frees the old one.
*/
template <typename ValueType>
void Vector<ValueType>::expandCapacity() {
capacity = max(1, capacity * 2);
ValueType *array = new ValueType[capacity];
for (int i = 0; i < count; i++) {
array[i] = elements[i];
}
if (elements != NULL) delete[] elements;
elements = array;
}
/*
* Implementation notes: << and >>
* -------------------------------
* The insertion and extraction operators use the template facilities in
* strlib.h to read and write generic values in a way that treats strings
* specially.
*/
template <typename ValueType>
std::ostream & operator<<(std::ostream & os, const Vector<ValueType> & vec) {
os << "{";
int len = vec.size();
for (int i = 0; i < len; i++) {
if (i > 0) os << ", ";
writeGenericValue(os, vec[i], true);
}
return os << "}";
}
template <typename ValueType>
std::istream & operator>>(std::istream & is, Vector<ValueType> & vec) {
char ch;
is >> ch;
if (ch != '{') error("operator >>: Missing {");
vec.clear();
is >> ch;
if (ch != '}') {
is.unget();
while (true) {
ValueType value;
readGenericValue(is, value);
vec += value;
is >> ch;
if (ch == '}') break;
if (ch != ',') {
error(std::string("operator >>: Unexpected character ") + ch);
}
}
}
return is;
}
#endif