-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.h
More file actions
226 lines (211 loc) · 7.95 KB
/
iterator.h
File metadata and controls
226 lines (211 loc) · 7.95 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
//Bedrich Pisl - Programming in C++, MFF, 2013/2014
#ifndef BEDAS_ITERATORS_H
#define BEDAS_ITERATORS_H
template <typename T, typename real_type> //this class should be abstract base class and "real_type" should always be type of derived iterator
class base_iterator : public std::iterator<std::random_access_iterator_tag, T>
{
public:
real_type& operator++ () //prefix
{
position += difference;
return *( (real_type*) this);
}
real_type operator++ (int) //postfix
{
real_type result(base, position);
position += difference;
return result;
}
friend real_type operator+ (real_type it, int distance)
{
real_type result = it;
result.position += result.difference * distance;
return result;
}
friend real_type operator+ (int distance, real_type it)
{
return it + distance;
}
real_type& operator+= (int distance)
{
position += distance * difference;
return *((real_type*) this);
}
real_type& operator-- ()
{
position -= difference;
return *this;
}
real_type operator-- (int)
{
real_type result(base, position);
position -= difference;
return result;
}
real_type& operator-= (int parameter)
{
return *this += (-parameter);
}
friend real_type operator- (real_type it, int distance)
{
it.position -= distance * it.difference;
return it;
}
friend int operator- (real_type first, real_type second)
{
return (first.position - second.position) / first.difference;
}
bool operator== (real_type other)
{
return position == other.position;
}
bool operator!= (real_type other)
{
return position != other.position;
}
bool operator< (real_type other)
{
return position < other.position;
}
bool operator> (real_type other)
{
return position > other.position;
}
bool operator>= (real_type other)
{
return position >= other.position;
}
bool operator<= (real_type other)
{
return position <= other.position;
}
protected:
base_iterator(std::shared_ptr<typename matrix_general<T>::matrix_base> base, int position, int difference) : base(base), position(position), difference(difference)
{}
std::shared_ptr<typename matrix_general<T>::matrix_base> base;
int position;
int difference;
};
//all elements iterator
template <typename T>
class matrix_general<T>::iterator : public base_iterator<T, typename matrix_general<T>::iterator>
{
public:
iterator(std::shared_ptr<matrix_base> base, int position) : base_iterator<T, typename matrix_general<T>::iterator>(base, position, 1) {}
T& operator* () {return *(this->base->data + this->position);}
T* operator-> () {return (this->base->data + this->position);}
T& operator[] (int index) {return *(this->base->data + this->position);}
};
//const version
template <typename T>
class matrix_general<T>::const_iterator : public base_iterator<T, typename matrix_general<T>::const_iterator>
{
public:
const_iterator(std::shared_ptr<matrix_base> base, int position) : base_iterator<T, typename matrix_general<T>::const_iterator>(base, position, 1) {}
const T& operator* () {return *(this->base->data + this->position);}
const T* operator-> () {return (this->base->data + this->position);}
const T& operator[] (int index) {return *(this->base->data + this->position);}
};
//elements in row iterator
template <typename T>
class matrix_general<T>::row::iterator : public base_iterator<T, typename matrix_general<T>::row::iterator>
{
public:
iterator(std::shared_ptr<matrix_base> base, int position) : base_iterator<T, typename matrix_general<T>::row::iterator>(base, position, 1) {}
T& operator* () {return *(this->base->data + this->position);}
T* operator-> () {return (this->base->data + this->position);}
T& operator[] (int index) {return *(this->base->data + this->position);}
};
//const version
template <typename T>
class matrix_general<T>::row::const_iterator : public base_iterator<T, typename matrix_general<T>::row::const_iterator>
{
public:
const_iterator(std::shared_ptr<matrix_base> base, int position) : base_iterator<T, typename matrix_general<T>::row::const_iterator>(base, position, 1) {}
const T& operator* () {return *(this->base->data + this->position);}
const T* operator-> () {return (this->base->data + this->position);}
const T& operator[] (int index) {return *(this->base->data + this->position);}
};
//elements in column iterator
template <typename T>
class matrix_general<T>::col::iterator : public base_iterator<T, typename matrix_general<T>::col::iterator>
{
public:
iterator(std::shared_ptr<matrix_base> base, int position) : base_iterator<T, typename matrix_general<T>::col::iterator>(base, position, base->row_length) {}
T& operator* () {return *(this->base->data + this->position);}
T* operator-> () {return (this->base->data + this->position);}
T& operator[] (int index) {return *(this->base->data + this->position);}
};
//const version
template <typename T>
class matrix_general<T>::col::const_iterator : public base_iterator<T, typename matrix_general<T>::col::const_iterator>
{
public:
const_iterator(std::shared_ptr<matrix_base> base, int position) : base_iterator<T, typename matrix_general<T>::col::const_iterator>(base, position, base->row_length) {}
const T& operator* () {return *(this->base->data + this->position);}
const T* operator-> () {return (this->base->data + this->position);}
const T& operator[] (int index) {return *(this->base->data + this->position);}
};
//rows iterator
template <typename T>
class matrix_general<T>::row_iterator : public base_iterator<T, typename matrix_general<T>::row_iterator>
{
public:
row_iterator(std::shared_ptr<matrix_base> base, int position) : base_iterator<T, typename matrix_general<T>::row_iterator>(base, position, base->row_length) {}
row operator* () {return row(this->base, this->position);}
row* operator-> ()
{
static void* aux_row = operator new(sizeof(row)); //allocates memory
aux_row = new (aux_row) row(this->base, this->position); //call constructor
return (matrix_general<T>::row*)aux_row;
}
row operator[] (int index) {return *(this->base->data + this->position);}
};
//const version
template <typename T>
class matrix_general<T>::row_const_iterator : public base_iterator<T, typename matrix_general<T>::row_const_iterator>
{
public:
row_const_iterator(std::shared_ptr<matrix_base> base, int position) : base_iterator<T, typename matrix_general<T>::row_const_iterator>(base, position, base->row_length) {}
const row operator* () {return row(this->base, this->position);}
const row* operator-> ()
{
static void* aux_row = operator new(sizeof(row)); //allocates memory
aux_row = new (aux_row) row(this->base, this->position); //call constructor
return (matrix_general<T>::row*)aux_row;
}
const row operator[] (int index) {return *(this->base->data + this->position);}
};
//columns iterator
template <typename T>
class matrix_general<T>::col_iterator : public base_iterator<T, typename matrix_general<T>::col_iterator>
{
public:
col_iterator(std::shared_ptr<matrix_base> base, int position) : base_iterator<T, typename matrix_general<T>::col_iterator>(base, position, 1)
{}
col operator* () {return col(this->base, this->position);}
col* operator-> ()
{
static void* aux_col = operator new(sizeof(col)); //allocates memory
aux_col = new (aux_col) col(this->base, this->position); //call constructor
return (matrix_general<T>::col*)aux_col;
}
col operator[] (int index) {return *(this->base->data + this->position);}
};
//const verison
template <typename T>
class matrix_general<T>::col_const_iterator : public base_iterator<T, typename matrix_general<T>::col_const_iterator>
{
public:
col_const_iterator(std::shared_ptr<matrix_base> base, int position) : base_iterator<T, typename matrix_general<T>::col_const_iterator>(base, position, 1)
{}
const col operator* () {return col(this->base, this->position);}
const col* operator-> ()
{
static void* aux_col = operator new(sizeof(col)); //allocates memory
aux_col = new (aux_col) col(this->base, this->position); //call constructor
return (matrix_general<T>::col*)aux_col;
}
const col operator[] (int index) {return *(this->base->data + this->position);}
};
#endif