forked from apanangadan/CSUF-CPSC_131
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFixedVectorWithIterators.h
95 lines (78 loc) · 2.26 KB
/
FixedVectorWithIterators.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
#pragma once
#include <stdexcept>
using namespace std;
template <typename ELT>
class FixedVector {
private:
int _length;
ELT *_array;
public:
// Iterator is a nested class within FixedVector<ELT>
class Iterator { // an iterator for the list
public:
ELT& get() { // reference to the element
return _vector->get(_index);
}
ELT& operator*() { // reference to the element
return _vector[index];
}
bool operator==(const Iterator& p) const { // compare positions
return _index == p._index;
}
bool operator!=(const Iterator& p) const {
return _index != p._index;
}
Iterator& operator++() { // move to next position
_index++; return *this;
}
friend class FixedVector; // give FixedVector access
private:
int _index; // index to current element
FixedVector<ELT> *_vector; // pointer to the data structure
Iterator(int u, FixedVector<ELT> *fixedArray) { // constructor from int
_index = u;
_vector = fixedArray;
}
};
public:
// length copies of default_value
FixedVector(int length) {
if (length < 0) throw invalid_argument("length < 0");
_length = length;
_array = new ELT[length];
}
// default constructor: empty vector
FixedVector() {
_length = 0;
_array = new ELT[0];
}
// copy constructor
FixedVector(const FixedVector& a) {
_length = a._length;
_array = new ELT[length];
for (int i = 0; i < length; i++)
_array[i] = a._array[i];
}
~FixedVector() { delete[] _array; }
int length() { return _length; }
bool is_empty() { return (_length == 0); }
ELT& get(int index) {
if ((index < 0) || (index >= _length)) throw range_error("index out of bounds");
return _array[index];
}
void set(int index, ELT value) {
if (is_empty()) throw invalid_argument("array is empty");
if ((index < 0) || (index >= _length)) throw range_error("index out of bounds");
_array[index] = value;
}
ELT& operator[](int index) {
if ((index < 0) || (index >= _length)) throw range_error("index out of bounds");
return get(index);
}
Iterator begin() { // beginning position is first item
return Iterator(0, this);
}
Iterator end() { // beginning position is first item
return Iterator(_length, this);
}
};