-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblIterator.hpp
441 lines (351 loc) · 14.1 KB
/
blIterator.hpp
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
#ifndef BL_ITERATOR_HPP
#define BL_ITERATOR_HPP
//-------------------------------------------------------------------
// FILE: blIterator.hpp
// CLASS: blIterator
// BASE CLASS: None
//
// PURPOSE: This class provides a generic iterator which
// wraps a user specified container.
// The iterator is customizable through user-provided
// begin, end, advance and distance functors.
// Through the user-provided functors, the iterator
// can for example be made circular, be made to never
// go pass its end, be made to skip every other place
// while moving forward, be made into a reverse iterator
// or much more.
//
// AUTHOR: Vincenzo Barbato
// http://www.barbatolabs.com
// navyenzo@gmail.com
//
// LISENSE: MIT-LICENCE
// http://www.opensource.org/licenses/mit-license.php
//
// DEPENDENCIES:
//
// NOTES:
//
// DATE CREATED: Dec/02/2013
//
// DATE UPDATED:
//-------------------------------------------------------------------
//-------------------------------------------------------------------
template<typename blContainerType,
typename blAdvanceDistanceFunctorType,
typename blBeginEndFunctorType>
class blIterator : public std::iterator< std::random_access_iterator_tag,
typename std::iterator_traits<decltype(blBeginEndFunctorType::begin(std::declval<blContainerType&>()))>::value_type,
typename std::iterator_traits<decltype(blBeginEndFunctorType::begin(std::declval<blContainerType&>()))>::difference_type,
typename std::iterator_traits<decltype(blBeginEndFunctorType::begin(std::declval<blContainerType&>()))>::pointer,
typename std::iterator_traits<decltype(blBeginEndFunctorType::begin(std::declval<blContainerType&>()))>::reference >
{
public: // Public typedefs
typedef typename std::shared_ptr<blContainerType> blContainerPtr;
typedef typename std::iterator_traits<typename blContainerType::iterator>::value_type blDataType;
typedef typename std::iterator_traits<typename blContainerType::iterator>::pointer blDataTypePtr;
typedef typename std::iterator_traits<typename blContainerType::iterator>::reference blDataTypeRef;
typedef decltype(blBeginEndFunctorType::begin(std::declval<blContainerType&>())) iterator;
private: // Private variables
// The iterator
// to the data
// point
iterator m_ptr;
// The raw pointer
// to the dat Container
blContainerPtr m_containerPtr;
public: // Constructors and destructors
// Default constructors
blIterator() = default;
// Construct from container
blIterator(blContainerType& container)
{
m_containerPtr = get_shared_ptr(container);
if(m_containerPtr)
m_ptr = blBeginEndFunctorType::begin(*m_containerPtr);
}
// Construct from container
// pointer
blIterator(blContainerType* containerPtr)
{
m_containerPtr = get_shared_ptr(containerPtr);
if(m_containerPtr)
m_ptr = blBeginEndFunctorType::begin(*m_containerPtr);
}
// Construct from container
// shared pointer
blIterator(const blContainerPtr& containerPtr)
{
m_containerPtr = containerPtr;
if(m_containerPtr)
m_ptr = blBeginEndFunctorType::begin(*m_containerPtr);
}
// Construct from iterator
// and container shared pointer
blIterator(const iterator& ptr,
const blContainerPtr& containerPtr)
{
m_ptr = ptr;
m_containerPtr = containerPtr;
}
// Copy constructor
blIterator(const blIterator<blContainerType,blAdvanceDistanceFunctorType,blBeginEndFunctorType>&) = default;
// Move constructor
blIterator(blIterator<blContainerType,blAdvanceDistanceFunctorType,blBeginEndFunctorType>&&) = default;
// Destructor
~blIterator()
{
}
public: // Assignment operator
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType>& operator=(const blIterator<blContainerType,blAdvanceDistanceFunctorType,blBeginEndFunctorType>& iterator) = default;
public: // Dereferencing operators
blDataTypeRef operator*(){return (*m_ptr);}
const blDataTypeRef operator*()const{return (*m_ptr);}
iterator operator->(){return m_ptr;}
public: // Overloaded operators
// Equality and
// inequality
// operators
bool operator==(const blIterator<blContainerType,blAdvanceDistanceFunctorType,blBeginEndFunctorType>& iterator)const
{
return ( (m_ptr == iterator.getPtr()) &&
(m_containerPtr == iterator.getContainerPtr()) );
}
bool operator!=(const blIterator<blContainerType,blAdvanceDistanceFunctorType,blBeginEndFunctorType>& iterator)const
{
return ( (m_ptr != iterator.getPtr()) ||
(m_containerPtr != iterator.getContainerPtr()) );
}
// Bool operator
// so that this
// iterator can be
// used in if statements
explicit operator bool()const
{
if(this->m_containerPtr)
return true;
else
return false;
}
// Increment/decrement
// operators
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType>& operator++()
{
this->advance(1);
return (*this);
}
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType> operator++(int)
{
auto TempIter(*this);
this->advance(1);
return TempIter;
}
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType>& operator--()
{
this->advance(-1);
return (*this);
}
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType> operator--(int)
{
auto TempIter(*this);
this->advance(-1);
return TempIter;
}
// Operators used to
// advance a random
// access iterator
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType>& operator+=(const ptrdiff_t& Offset)
{
this->advance(Offset);
return (*this);
}
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType>& operator-=(const ptrdiff_t& Offset)
{
this->advance(-Offset);
return (*this);
}
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType> operator+(const ptrdiff_t& Offset)const
{
auto NewIter = (*this);
NewIter.advance(Offset);
return NewIter;
}
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType> operator-(const ptrdiff_t& Offset)const
{
auto NewIter = (*this);
NewIter.advance(-Offset);
return NewIter;
}
// Operator used
// to calculate
// the distance
// between two
// iterators
ptrdiff_t operator-(const blIterator<blContainerType,blAdvanceDistanceFunctorType,blBeginEndFunctorType>& iterator)const
{
return blAdvanceDistanceFunctorType::distance(this->getDistanceFromBeginToIter(),
iterator.getDistanceFromBeginToIter(),
this->getDistanceFromIterToEnd(),
iterator.getDistanceFromIterToEnd());
}
public: // Public functions
// Functions used to
// set the Container pointer
void setContainerPtr(blContainerType& container)
{
auto containerPtr = get_shared_ptr(container);
if(containerPtr != m_containerPtr)
{
m_containerPtr = containerPtr;
if(m_containerPtr)
{
(*this) = this->begin();
}
}
}
void setContainerPtr(const blContainerType& container)
{
auto containerPtr = get_shared_ptr(container);
if(containerPtr != m_containerPtr)
{
m_containerPtr = containerPtr;
if(m_containerPtr)
{
(*this) = this->begin();
}
}
}
void setContainerPtr(blContainerType* rawContainerPtr)
{
auto containerPtr = get_shared_ptr(rawContainerPtr);
if(containerPtr != m_containerPtr)
{
m_containerPtr = containerPtr;
if(m_containerPtr)
{
(*this) = this->begin();
}
}
}
// Functions used to
// get the raw pointer
// and iterator
const iterator& getPtr()const{return m_ptr;}
const blContainerPtr& getContainerPtr()const{return m_containerPtr;}
// Functions used to
// get the distance
// from the begin
// to the iterator
// and from the iterator
// to the end
ptrdiff_t getDistanceFromBeginToIter()const
{
if(m_containerPtr)
return std::distance(blBeginEndFunctorType::begin(*m_containerPtr),this->m_ptr);
else
return 0;
}
ptrdiff_t getDistanceFromIterToEnd()const
{
if(m_containerPtr)
return std::distance(this->m_ptr,blBeginEndFunctorType::end(*m_containerPtr));
else
return 0;
}
// Functions used to
// return iterators
// to the "begin" and
// "end" data points
// of the Container
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType> begin()const
{
if(m_containerPtr)
{
return blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType>(blBeginEndFunctorType::begin(*m_containerPtr),
m_containerPtr);
}
else
return blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType>(m_containerPtr);
}
blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType> end()const
{
if(m_containerPtr)
{
return blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType>(blBeginEndFunctorType::end(*m_containerPtr),
m_containerPtr);
}
else
return blIterator<blContainerType,
blAdvanceDistanceFunctorType,
blBeginEndFunctorType>(m_containerPtr);
}
// Operators used to
// access data elements
// from the container
// directly
//
// NOTE: These operators do
// not check for out of
// bound indices
blDataTypeRef operator[](const size_t& index){return (*(this->begin() + index));}
const blDataTypeRef operator[](const size_t& index)const{return (*(this->begin() + index));}
// Functions used
// to return the
// container's size
size_t size()const
{
if(m_containerPtr)
return m_containerPtr->size();
else
return 0;
}
size_t length()const{return this->size();}
size_t max_size()const{return this->size();}
bool empty()const{return (this->size() == 0);}
private: // Special functions
// Function used
// to advance the
// iterator
void advance(const ptrdiff_t& HowManyStepsToAdvanceIter)
{
if(m_containerPtr)
{
blAdvanceDistanceFunctorType::advance(m_ptr,
HowManyStepsToAdvanceIter,
this->begin().m_ptr,
this->end().m_ptr,
this->getDistanceFromBeginToIter(),
this->getDistanceFromIterToEnd());
}
}
};
//-------------------------------------------------------------------
#endif // BL_ITERATOR_HPP