-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstVectorIterator.cpp
118 lines (100 loc) · 3.89 KB
/
ConstVectorIterator.cpp
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
#include "ConstVectorIterator.h"
#include "BaseVector.h"
Matrix::ConstVectorIterator::ConstVectorIterator(const ConstBaseVector *vector, int pos){
//create a ConstVectorIterator over the specified vector starting at the specified position
this->vector = vector;
this->pos = pos;
}
const Matrix::ConstVectorIterator::value_type& Matrix::ConstVectorIterator::operator*() const {
// returns a reference to the element you are at
// at() returns a reference to the element at pos n in the vector
return this->vector->at(this->pos);
}
const Matrix::ConstVectorIterator::value_type& Matrix::ConstVectorIterator::operator[](int offset) const {
//return a reference to the element offset past your current position
return this->vector->at(this->pos + offset);
}
Matrix::ConstVectorIterator& Matrix::ConstVectorIterator::operator++() {
// move to the next element
// pre fix increment
this->pos++;
return *this;
}
const Matrix::ConstVectorIterator Matrix::ConstVectorIterator::operator++(int) {
// move to the next element
// post fix increment
const ConstVectorIterator currentIterator = *this;
this->pos++;
return currentIterator;
}
Matrix::ConstVectorIterator& Matrix::ConstVectorIterator::operator--() {
// move to the previous element
// pre fix decrement
this->pos--;
return *this;
}
const Matrix::ConstVectorIterator Matrix::ConstVectorIterator::operator--(int) {
// move to the previous element
// post fix decrement
const ConstVectorIterator currentIterator = *this;
this->pos--;
return currentIterator;
}
Matrix::ConstVectorIterator& Matrix::ConstVectorIterator::operator+=(int amount) {
// move forward amount of elements
// gets position and adds amount
// returns new pos
this->pos += amount;
return *this;
}
Matrix::ConstVectorIterator Matrix::ConstVectorIterator::operator+(int amount) const {
// moves forward amount elements
// doesn't increment, plain adds
const int index = this->pos + amount;
return this[index];
}
Matrix::ConstVectorIterator& Matrix::ConstVectorIterator::operator-=(int amount) {
// moves backward amount elements
// gets position and subtracts amount
// returns new pos
this->pos -= amount;
return *this;
}
Matrix::ConstVectorIterator Matrix::ConstVectorIterator::operator-(int amount) const {
// moves backward amount elements
// doesn't decrement, plain subtracts
const int index = this->pos - amount;
return this[index];
}
Matrix::ConstVectorIterator::difference_type Matrix::ConstVectorIterator::operator-(const ConstVectorIterator& rhs) {
//return the number of elements between yourself and rhs
//for example if you were at element 10 and rhs was at element 7
//the difference would be 3
return rhs.pos - this->pos;
}
bool Matrix::ConstVectorIterator::operator==(const ConstVectorIterator& rhs) const {
//you are equal to rhs if you are over the same vector
//and at the same position
return rhs.vector == this->vector && rhs.pos == this->pos;
}
bool Matrix::ConstVectorIterator::operator!=(const ConstVectorIterator& rhs) const {
//you are equal to rhs if you are over the same vector
//and at the same position
return !(rhs.vector == this->vector && rhs.pos == this->pos);
}
bool Matrix::ConstVectorIterator::operator<(const ConstVectorIterator& rhs) const {
//are you at an element before rhs?
return this->pos < rhs.pos;
}
bool Matrix::ConstVectorIterator::operator<=(const ConstVectorIterator& rhs) const {
//are you at an element before rhs?
return this->pos <= rhs.pos;
}
bool Matrix::ConstVectorIterator::operator>(const ConstVectorIterator& rhs) const {
//are you at an element after rhs?
return this->pos > rhs.pos;
}
bool Matrix::ConstVectorIterator::operator>=(const ConstVectorIterator& rhs) const {
//are you at an element after rhs?
return this->pos >= rhs.pos;
}