-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathblIteratorFunctors.hpp
More file actions
208 lines (184 loc) · 7.85 KB
/
blIteratorFunctors.hpp
File metadata and controls
208 lines (184 loc) · 7.85 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
#ifndef BL_ITERATORFUNCTORS_HPP
#define BL_ITERATORFUNCTORS_HPP
//-------------------------------------------------------------------
// FILE: blIteratorFunctors.hpp
// CLASS: None
// BASE CLASS: None
//
// PURPOSE: Functors defined in this file are used
// by iterators in the blIteratorAPI library
// to "advance" and to get "begin" and "end"
// iterators.
//
// - The iterator itself
// - The container's "begin" and "end" iterators
//
// AUTHOR: Vincenzo Barbato
// http://www.barbatolabs.com
// navyenzo@gmail.com
//
// LISENSE: MIT-LICENCE
// http://www.opensource.org/licenses/mit-license.php
//
// DEPENDENCIES: - std::distance -- Used to calculate the
// distance from the beginning
// to the iterator and from the
// iterator to the end if needed
//
// - std::advance -- Used to actually advance
// the specified iterator
// once the algorithm has
// decided how much to advance
// it by.
//
// NOTES:
//
// DATE CREATED: Jan/08/2014
//
// DATE UPDATED:
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Define "begin" and "end" functors
// to use with smart iterators
//-------------------------------------------------------------------
struct blBeginEnd
{
template<typename blContainerType>
static auto begin(blContainerType& container)->decltype(container.begin()){return container.begin();}
template<typename blContainerType>
static auto end(blContainerType& container)->decltype(container.end()){return container.end();}
};
struct blcBeginEnd
{
template<typename blContainerType>
static auto begin(const blContainerType& container)->decltype(container.cbegin()){return container.cbegin();}
template<typename blContainerType>
static auto end(const blContainerType& container)->decltype(container.cend()){return container.cend();}
};
struct blrBeginEnd
{
template<typename blContainerType>
static auto begin(blContainerType& container)->decltype(container.rbegin()){return container.rbegin();}
template<typename blContainerType>
static auto end(blContainerType& container)->decltype(container.rend()){return container.rend();}
};
struct blcrBeginEnd
{
template<typename blContainerType>
static auto begin(const blContainerType& container)->decltype(container.crbegin()){return container.crbegin();}
template<typename blContainerType>
static auto end(const blContainerType& container)->decltype(container.crend()){return container.crend();}
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functor: - blAdvanceLinearly
//
// PURPOSE: - This functor advances an iterator
// forward or in reverse but stopping
// if it reaches the specified "end" or
// "begin" iterators.
//
// DEPENDENCIES: - blAdvance
//-------------------------------------------------------------------
struct blAdvanceLinearly
{
template<typename blIteratorType>
static void advance(blIteratorType& iter,
const ptrdiff_t& howManyStepsToAdvanceIter,
const blIteratorType& beginIter,
const blIteratorType& endIter,
const ptrdiff_t& distanceFromBeginToIter,
const ptrdiff_t& distanceFromIterToEnd)
{
if(howManyStepsToAdvanceIter > 0)
{
if(howManyStepsToAdvanceIter > distanceFromIterToEnd)
iter = endIter;
else
std::advance(iter,howManyStepsToAdvanceIter);
}
else if(howManyStepsToAdvanceIter < 0)
{
if((-howManyStepsToAdvanceIter) > distanceFromBeginToIter)
iter = beginIter;
else
std::advance(iter,howManyStepsToAdvanceIter);
}
}
template<typename blIteratorType>
static ptrdiff_t distance(const ptrdiff_t& distanceFromBeginToIter1,
const ptrdiff_t& distanceFromBeginToIter2,
const ptrdiff_t& distanceFromIterToEnd1,
const ptrdiff_t& distanceFromIterToEnd2)
{
return ( distanceFromBeginToIter2 - distanceFromBeginToIter1 );
}
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functor: - blAdvanceCircularly
//
// PURPOSE: - This functor advances an iterator
// forward or in reverse in a circular
// way, simulating a "ring buffer".
// - If the iterator is equal to the
// "end" iterator, then it is not advanced.
//
// DEPENDENCIES: - blAdvance
//-------------------------------------------------------------------
struct blAdvanceCircularly
{
template<typename blIteratorType>
static void advance(blIteratorType& iter,
const ptrdiff_t& howManyStepsToAdvanceIter,
const blIteratorType& beginIter,
const blIteratorType& endIter,
const ptrdiff_t& distanceFromBeginToIter,
const ptrdiff_t& distanceFromIterToEnd)
{
if(howManyStepsToAdvanceIter > 0)
{
if(howManyStepsToAdvanceIter < distanceFromIterToEnd)
std::advance(iter,howManyStepsToAdvanceIter);
else
{
iter = beginIter;
std::advance(
iter,
(howManyStepsToAdvanceIter - distanceFromIterToEnd) %
(distanceFromBeginToIter + distanceFromIterToEnd)
);
}
}
else if(howManyStepsToAdvanceIter < 0)
{
if((-howManyStepsToAdvanceIter) < distanceFromBeginToIter)
std::advance(iter,howManyStepsToAdvanceIter);
else
{
iter = beginIter;
std::advance(
iter,
(distanceFromBeginToIter + distanceFromIterToEnd) +
(howManyStepsToAdvanceIter - distanceFromIterToEnd) %
(distanceFromBeginToIter + distanceFromIterToEnd)
);
}
}
}
template<typename blIteratorType>
static ptrdiff_t distance(const ptrdiff_t& distanceFromBeginToIter1,
const ptrdiff_t& distanceFromBeginToIter2,
const ptrdiff_t& distanceFromIterToEnd1,
const ptrdiff_t& distanceFromIterToEnd2)
{
if(distanceFromBeginToIter1 <= distanceFromBeginToIter2)
return ( distanceFromBeginToIter2 - distanceFromBeginToIter1 );
else
{
return ( distanceFromIterToEnd1 + distanceFromBeginToIter2);
}
}
};
//-------------------------------------------------------------------
#endif // BL_ITERATORFUNCTORS_HPP