-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteratorBase.h
More file actions
203 lines (162 loc) · 4.05 KB
/
Copy pathIteratorBase.h
File metadata and controls
203 lines (162 loc) · 4.05 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
#ifndef BaseLib_Templates_IteratorBase_h_Included
#define BaseLib_Templates_IteratorBase_h_Included
#include"BaseLib/Templates/BaseTypes.h"
namespace BaseLib { namespace Templates
{
/**
* Iterators can be used to iterate over aggregates, for example, sets, lists, maps, etc.
* This means it can be combined with many other software patterns:
* - Interceptors, Memento, ObjectManager/Cache, etc.
*/
template <typename T>
class Iterator
{
public:
/**
* @brief HasNext
* @return
*/
virtual bool HasNext() const = 0;
/**
* @brief Next
* @return
*/
virtual T Next() = 0;
/**
* @brief Remove
*/
virtual void Remove() = 0;
/**
* @brief Rewind
*/
virtual void Rewind() = 0;
};
/**
* Iterable constructs an interator.
*
* Implemented by object that wishes to support "iterate internal data externally".
*/
template <typename T>
class Iterable
{
public:
/**
* @brief Iterator
* @return
*/
virtual Templates::Iterator<T> Iterator() = 0;
};
namespace details
{
template <typename Collection, typename T>
class IteratorBase : public Templates::Iterator<T>
{
public:
IteratorBase() : Templates::Iterator<T>()
{}
virtual ~IteratorBase()
{}
protected:
static typename Collection::iterator nextIterator(const typename Collection::iterator &curr)
{
typename Collection::iterator next = curr;
++next;
return next;
}
static bool hasNext(const Collection &collection, const typename Collection::iterator &curr)
{
return nextIterator(curr) != collection.end();
}
static bool isAtEnd(const Collection &collection, const typename Collection::iterator &curr)
{
return curr == collection.end();
}
};
template <typename Collection, typename T>
class IteratorStdList : public details::IteratorBase<Collection, T>
{
public:
IteratorStdList(Collection &collection)
: details::IteratorBase<Collection, T>()
, collection_(collection)
, curr_(collection.begin())
{}
virtual ~IteratorStdList()
{}
virtual bool HasNext() const
{
return !IteratorBase<Collection,T>::isAtEnd(collection_, curr_) && IteratorBase<Collection,T>::hasNext(collection_, curr_);
}
virtual T Next()
{
if(curr_ != collection_.begin())
++curr_;
return *curr_;
}
virtual void Remove()
{
if(IteratorBase<Collection,T>::isAtEnd(collection_, curr_)) return;
typename Collection::iterator removeIterator = curr_;
if(curr_ != collection_.begin())
--curr_;
else
++curr_;
collection_.erase(removeIterator);
}
virtual void Rewind()
{
curr_ = collection_.begin();
}
private:
Collection &collection_;
typename Collection::iterator curr_;
};
/**
*
*/
template <typename MapCollection, typename T>
class IteratorStdMap : public details::IteratorBase<MapCollection, T>
{
public:
IteratorStdMap(MapCollection &collection)
: details::IteratorBase<MapCollection, T>()
, collection_(collection)
, curr_(collection.begin())
{}
virtual ~IteratorStdMap()
{}
virtual bool HasNext() const
{
return !IteratorBase<MapCollection,T>::isAtEnd(collection_, curr_) && IteratorBase<MapCollection,T>::hasNext(collection_, curr_);
}
// virtual MapCollection::iterator Next()
// {
// if(curr_ != collection.begin())
// ++curr_;
// return curr_;
// }
virtual T Next()
{
return T();
}
virtual void Remove()
{
if(IteratorBase<MapCollection,T>::isAtEnd(collection_, curr_)) return;
typename MapCollection::iterator removeIterator = curr_;
if(curr_ != collection_.begin())
--curr_;
else
++curr_;
collection_.erase(removeIterator);
}
virtual void Rewind()
{
curr_ = collection_.begin();
}
private:
MapCollection &collection_;
typename MapCollection::iterator curr_;
};
}
}}
#endif