-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDataStructure.h
294 lines (250 loc) · 7.37 KB
/
DataStructure.h
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
#pragma once
#include <cassert>
#include <ctime>
#include <set>
#include <map>
#include <queue>
#include <functional>
#include <boost/weak_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/random.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include "PlatformDepends.h"
#include "MetaProgram.h"
#include "Delegate.h"
namespace Scan
{
/**
@brief 迭代器接口
*/
template<typename ValueType>
struct IIterator
{
virtual bool isExistMore() const = 0;
virtual ValueType& getNext() = 0;
virtual ValueType& peekNext() const = 0;
virtual IIterator& move(int step = 1) = 0;
virtual IIterator* copy() const = 0;
virtual ~IIterator() = 0 {}
};
/**
@brief 遍历指定类型容器或数组的迭代器
要求容器支持stl标准
*/
template<typename ContainerType, bool reverse>
class ContainerIterator:
public IIterator<typename MPL::ContainerTraits<ContainerType, reverse>::ValueType>
{
public:
typedef MPL::ContainerTraits<ContainerType, reverse> HelperType;
typedef typename HelperType::IterType IterType;
typedef typename HelperType::ValueType ValueType;
public:
ContainerIterator(ContainerType& c): m_con(c)
{
m_iter = HelperType::begin(m_con);
m_end = HelperType::end(m_con);
}
virtual bool isExistMore() const
{
return m_iter != m_end;
}
virtual ValueType& getNext()
{
ValueType &r = *m_iter;
HelperType::move(m_iter);
return r;
}
virtual ValueType& peekNext() const
{
ValueType &r = *m_iter;
return r;
}
virtual IIterator* copy() const
{
return new ContainerIterator(*this);
}
virtual IIterator& move(int step = 1)
{
HelperType::move(m_iter, step);
return *this;
}
template<typename NewValueType>
IIterator<NewValueType>* castTo()
{
SCAN_ASSERT_TYPECAST(NewValueType, ValueType);
return reinterpret_cast<IIterator<NewValueType>*>(this);
}
private:
ContainerType &m_con;
IterType m_iter;
IterType m_end;
};
/**
@brief 能够遍历各种类型stl容器和数组的迭代器
*/
template<typename ValueType, bool reverse = false>
class Iterator:
IIterator<ValueType>
{
private:
typedef IIterator<ValueType>* IterPtr;
public:
template<typename ContainerType>
explicit Iterator(ContainerType& c)
{
constructDispatch(c);
}
Iterator()
{
m_iter = NULL;
}
~Iterator()
{
safe_delete(m_iter);
}
Iterator(const Iterator& o)
{
copyConstruct(o);
}
Iterator& operator = (const Iterator& o)
{
if (this != &o)
{
safe_delete(m_iter);
if (o.m_iter != NULL)
{
m_iter = o.m_iter->copy();
}
}
return *this;
}
/**
@brief 是否存在更多的元素
*/
virtual bool isExistMore() const
{
return m_iter->isExistMore();
}
/**
@brief 获取下一个元素并移动迭代器
*/
virtual ValueType& getNext()
{
return m_iter->getNext();
}
/**
@brief 获取下一个元素但不移动迭代器
*/
virtual ValueType& peekNext() const
{
return m_iter->peekNext();
}
/**
@brief 拷贝迭代器
*/
virtual IIterator *copy() const
{
return new Iterator(*this);
}
/**
@brief 移动迭代器
*/
virtual IIterator& move(int step = 1)
{
m_iter->move(step);
return *this;
}
/**
@brief 将迭代器重新绑定到容器
*/
template<typename ContainerType>
void rebind(ContainerType& c)
{
m_iter =
(new ContainerIterator<ContainerType, reverse>(c))->castTo<ValueType>();
}
operator Iterator<const ValueType> ()
{
return *reinterpret_cast<Iterator<const ValueType>*>(this);
}
private:
template<typename T>
void constructDispatch(T& v)
{
rebind(v);
}
typedef typename MPL::ToNoConst<ValueType>::Type RawValueType;
template<>
void constructDispatch<Iterator<RawValueType>>(Iterator<RawValueType>& v)
{
copyConstruct(v);
}
template<>
void constructDispatch<Iterator<const RawValueType>>(Iterator<const RawValueType>& v)
{
copyConstruct(v);
}
void copyConstruct(const Iterator& o)
{
m_iter = NULL;
*this = o;
}
private:
IterPtr m_iter;
};
/**
@brief 观察者管理器
*/
template<
typename ListenerType,
typename MutexType = EmptyMutex,
typename SetType = std::set<ListenerType*>>
class ListenerManager
{
public:
typedef DelegateN<boost::function<void(ListenerType*)>> Delegate;
public:
void addListener(ListenerType *p)
{
SingleLocker locker(m_mutex);
m_set.insert(p);
}
void removeListener(ListenerType *p)
{
SingleLocker locker(m_mutex);
m_set.erase(p);
}
bool isEmpty() const
{
SingleLocker locker(m_mutex);
return m_set.empty();
}
void clear()
{
SingleLocker locker(m_mutex);
m_set.clear();
}
/**
@brief 调用所有观察者的某个函数
*/
void foreach(const Delegate& d) const
{
SetType tempSet;
{
SingleLocker locker(m_mutex);
tempSet = m_set;
}
typename SetType::const_iterator iter = tempSet.begin();
while (iter != tempSet.end())
{
d(*iter);
++iter;
}
}
private:
mutable MutexType m_mutex;
SetType m_set;
};
}