00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef QOLYESTER_UTL_ITERATOR_HH
00020 # define QOLYESTER_UTL_ITERATOR_HH 1
00021
00022 # include <cassert>
00023 # include <iterator>
00024
00025 namespace olsr {
00026
00027 namespace utl {
00028
00029
00030
00031 template <class Set, class Iter = typename Set::iterator>
00032 struct DefaultPredicate {
00033 bool operator()(Set&, const Iter& pos) const {
00034 return pos->is_valid();
00035 }
00036 };
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 template <class Set, class Iterator = typename Set::iterator>
00055 struct NoAction {
00056 void operator()(Set&, const Iterator&) const {}
00057 };
00058
00059
00060
00061
00062
00063
00064
00065
00066 template <class T>
00067 struct type_traits {
00068 typedef T mutable_val;
00069 typedef const T const_val;
00070 typedef T& mutable_ref;
00071 typedef const T& const_ref;
00072 typedef T* mutable_ptr;
00073 typedef const T* const_ptr;
00074 };
00075
00076 template <class T>
00077 struct type_traits<const T> {
00078 typedef T mutable_val;
00079 typedef const T const_val;
00080 typedef T& mutable_ref;
00081 typedef const T& const_ref;
00082 typedef T* mutable_ptr;
00083 typedef const T* const_ptr;
00084 };
00085
00086 template <typename T>
00087 struct type_traits<T&> {
00088 typedef typename type_traits<T>::mutable_val mutable_val;
00089 typedef typename type_traits<T>::const_val const_val;
00090 typedef typename type_traits<T>::mutable_ref mutable_ref;
00091 typedef typename type_traits<T>::const_ref const_ref;
00092 typedef typename type_traits<T>::mutable_ptr mutable_ptr;
00093 typedef typename type_traits<T>::const_ptr const_ptr;
00094 };
00095
00096 template <typename T>
00097 struct type_traits<T*> {
00098 typedef typename type_traits<T>::mutable_val mutable_val;
00099 typedef typename type_traits<T>::const_val const_val;
00100 typedef typename type_traits<T>::mutable_ref mutable_ref;
00101 typedef typename type_traits<T>::const_ref const_ref;
00102 typedef typename type_traits<T>::mutable_ptr mutable_ptr;
00103 typedef typename type_traits<T>::const_ptr const_ptr;
00104 };
00105
00106 template <class Iter>
00107 class DeconstIterator : public Iter {
00108 typedef DeconstIterator<Iter> This;
00109 typedef Iter Super;
00110 typedef typename std::iterator_traits<Iter>::value_type Elem;
00111 typedef typename type_traits<Elem>::mutable_ref MRef;
00112 typedef typename type_traits<Elem>::mutable_ptr MPtr;
00113 public:
00114 DeconstIterator() : Super() {}
00115 DeconstIterator(const This& other) : Super(other) {}
00116
00117 MRef operator*() const {
00118 return const_cast<MRef>(*static_cast<const Super&>(*this));
00119 }
00120
00121 MPtr operator->() const {
00122 return const_cast<MPtr>(&*static_cast<const Super&>(*this));
00123 }
00124
00125 This& operator++() {
00126 ++static_cast<Super&>(*this);
00127 return *this;
00128 }
00129
00130 This operator++(int) {
00131 return This::build(static_cast<Super&>(*this)++);
00132 }
00133
00134 static This build(const Super& other) {
00135 return This(static_cast<const This&>(other));
00136 }
00137
00138 static std::pair<This, bool> build(const std::pair<Super, bool>& p) {
00139 return std::pair<This, bool>(This(static_cast<const This&>(p.first)),
00140 p.second);
00141 }
00142 };
00143
00144 template <class Iter>
00145 class DerefIterator : public Iter {
00146 typedef DerefIterator This;
00147 typedef Iter Super;
00148 typedef typename std::iterator_traits<Iter>::value_type Elem;
00149 typedef typename std::iterator_traits<Elem>::value_type Value;
00150 typedef typename std::iterator_traits<Elem>::reference Ref;
00151 typedef typename std::iterator_traits<Elem>::pointer Ptr;
00152 public:
00153 DerefIterator() : Super() {}
00154 DerefIterator(const This& other) : Super(other) {}
00155
00156 Ref operator*() const {
00157 return **static_cast<const Super&>(*this);
00158 }
00159
00160 Ptr operator->() const {
00161 return &**static_cast<const Super&>(*this);
00162 }
00163
00164 This& operator++() {
00165 ++static_cast<Super&>(*this);
00166 return *this;
00167 }
00168
00169 This operator++(int) {
00170 return This::build(static_cast<Super&>(*this)++);
00171 }
00172
00173 const Super& deref_super() const { return *this; }
00174 Super& deref_super() { return *this; }
00175
00176 static This build(const Super& other) {
00177 return This(static_cast<const This&>(other));
00178 }
00179 };
00180
00181 template <class Key, class Iter>
00182 class KeyedIterator : public Iter {
00183 typedef KeyedIterator<Key, Iter> This;
00184 typedef Iter Super;
00185 public:
00186 KeyedIterator(const Super& other, const Key& k)
00187 : Super(other),
00188 _key(k)
00189 {}
00190
00191 KeyedIterator(const This& other)
00192 : Super(other),
00193 _key(other._key)
00194 {}
00195
00196 const Key& key() const {
00197 return _key;
00198 }
00199 private:
00200 Key _key;
00201 };
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 template <class Set,
00222 class Action = NoAction<Set>,
00223 class Predicate = DefaultPredicate<Set>,
00224 class ActionSet = Set>
00225 class MaskIterator {
00226 typedef typename Set::iterator Super;
00227 typedef typename std::iterator_traits<Super>::value_type Elem;
00228 typedef MaskIterator<Set, Action, Predicate, ActionSet> This;
00229 public:
00230 operator Super() const {
00231 return _super;
00232 }
00233
00234 const Super& mask_super() const { return _super; }
00235 Super& mask_super() { return _super; }
00236
00237 MaskIterator()
00238 : _super(),
00239 _instance(0),
00240 _action_instance(0)
00241 {}
00242
00243 MaskIterator(const Super& other, Set& instance)
00244 : _super(other),
00245 _instance(&instance),
00246 _action_instance(&instance)
00247 {}
00248
00249 MaskIterator(const Super& other, Set& instance, ActionSet& ainstance)
00250 : _super(other),
00251 _instance(&instance),
00252 _action_instance(&ainstance)
00253 {}
00254
00255 const Elem& operator*() const {
00256 return *_super;
00257 }
00258
00259 const Elem* operator->() const {
00260 return &*_super;
00261 }
00262
00263 This& operator++() {
00264 ++_super;
00265 skip();
00266 return *this;
00267 }
00268
00269 This operator++(int) {
00270 This tmp(*this);
00271 operator++();
00272 return tmp;
00273 }
00274
00275 bool operator==(const This& rhs) const {
00276 return _super == rhs._super;
00277 }
00278
00279 bool operator!=(const This& rhs) const {
00280 return _super != rhs._super;
00281 }
00282
00283 bool is_valid() const {
00284
00285 return _super == _instance->end() ||
00286 Predicate()(*_action_instance, _super);
00287 }
00288
00289 This& skip() {
00290 while (!is_valid()) {
00291 This tmp(*this);
00292 ++_super;
00293 Action()(*_action_instance, tmp._super);
00294 }
00295 return *this;
00296 }
00297
00298 void act() {
00299 Action()(*_action_instance, _super);
00300 _super = _instance->end();
00301 }
00302
00303 This& check() {
00304 assert(_instance != 0);
00305 if (!is_valid())
00306 act();
00307 return *this;
00308 }
00309
00310 private:
00311 Super _super;
00312 Set* _instance;
00313 ActionSet* _action_instance;
00314 };
00315
00316 template <class> struct MSAdapt;
00317
00318 template <class Set, class Action, class Predicate, class ActionSet>
00319 class MaskIterator<MSAdapt<Set>, Action, Predicate, ActionSet> {
00320 typedef typename Set::iterator Super;
00321 typedef typename std::iterator_traits<Super>::value_type Elem;
00322 typedef typename Set::key_type Key;
00323 typedef MaskIterator<MSAdapt<Set>, Action, Predicate, ActionSet> This;
00324 public:
00325 operator Super() const {
00326 return _super;
00327 }
00328
00329 const Super& mask_super() const { return _super; }
00330 Super& mask_super() { return _super; }
00331
00332 MaskIterator()
00333 : _super(),
00334 _instance(0),
00335 _action_instance(0),
00336 _key()
00337 {}
00338
00339 MaskIterator(const Super& other, Set& instance, const Key& key)
00340 : _super(other),
00341 _instance(&instance),
00342 _action_instance(&instance),
00343 _key(key)
00344 {}
00345
00346 MaskIterator(const Super& other, Set& instance,
00347 ActionSet& ainstance, const Key& key)
00348 : _super(other),
00349 _instance(&instance),
00350 _action_instance(&ainstance),
00351 _key(key)
00352 {}
00353
00354 const Elem& operator*() const {
00355 return *_super;
00356 }
00357
00358 const Elem* operator->() const {
00359 return &*_super;
00360 }
00361
00362 This& operator++() {
00363 ++_super;
00364 skip();
00365 return *this;
00366 }
00367
00368 This operator++(int) {
00369 This tmp(*this);
00370 operator++();
00371 return tmp;
00372 }
00373
00374 bool operator==(const This& rhs) const {
00375 return _super == rhs._super;
00376 }
00377
00378 bool operator!=(const This& rhs) const {
00379 return _super != rhs._super;
00380 }
00381
00382 bool is_valid() const {
00383
00384 return _super == _instance->end(_key) || Predicate()(*_action_instance,
00385 _super);
00386 }
00387
00388 This& skip() {
00389 while (!is_valid()) {
00390 This tmp(*this);
00391 ++_super;
00392 Action()(*_action_instance, tmp._super);
00393 }
00394 return *this;
00395 }
00396
00397 void destroy() {
00398 Action()(*_action_instance, _super);
00399 _super = _instance->end(_key);
00400 }
00401
00402 This& check() {
00403 assert(_instance != 0);
00404 if (!is_valid())
00405 destroy();
00406 return *this;
00407 }
00408
00409 private:
00410 Super _super;
00411 Set* _instance;
00412 ActionSet* _action_instance;
00413 Key _key;
00414 };
00415
00416 }
00417
00418 }
00419
00420 namespace std {
00421
00422 template <class Iter>
00423 struct iterator_traits<olsr::utl::DeconstIterator<Iter> > {
00424 typedef typename iterator_traits<Iter>::iterator_category
00425 iterator_category;
00426 typedef typename olsr::utl::type_traits<typename iterator_traits<Iter>::value_type>::mutable_val
00427 value_type;
00428 typedef typename iterator_traits<Iter>::difference_type
00429 difference_type;
00430 typedef typename olsr::utl::type_traits<typename iterator_traits<Iter>::reference>::mutable_ref
00431 reference;
00432 typedef typename olsr::utl::type_traits<typename iterator_traits<Iter>::pointer>::mutable_ptr
00433 pointer;
00434 };
00435
00436 template <class Iter>
00437 struct iterator_traits<olsr::utl::DerefIterator<Iter> > {
00438 typedef typename iterator_traits<Iter>::iterator_category
00439 iterator_category;
00440 typedef typename iterator_traits<Iter>::difference_type
00441 difference_type;
00442 typedef typename iterator_traits<typename iterator_traits<Iter>::value_type>::value_type value_type;
00443 typedef typename iterator_traits<typename iterator_traits<Iter>::value_type>::reference reference;
00444 typedef typename iterator_traits<typename iterator_traits<Iter>::value_type>::pointer pointer;
00445 };
00446
00447 template <class Key, class Iter>
00448 struct iterator_traits<olsr::utl::KeyedIterator<Key, Iter> > {
00449 typedef typename iterator_traits<Iter>::iterator_category
00450 iterator_category;
00451 typedef typename iterator_traits<Iter>::difference_type
00452 difference_type;
00453 typedef typename iterator_traits<Iter>::value_type value_type;
00454 typedef typename iterator_traits<Iter>::reference reference;
00455 typedef typename iterator_traits<Iter>::pointer pointer;
00456 };
00457
00458 template <class Set, class Action, class Predicate, class ActionSet>
00459 struct iterator_traits<olsr::utl::MaskIterator<Set, Action, Predicate, ActionSet> > {
00460 typedef forward_iterator_tag iterator_category;
00461 typedef typename iterator_traits<typename Set::iterator>::difference_type
00462 difference_type;
00463 typedef typename iterator_traits<typename Set::iterator>::value_type
00464 value_type;
00465 typedef typename iterator_traits<typename Set::iterator>::reference
00466 reference;
00467 typedef typename iterator_traits<typename Set::iterator>::pointer
00468 pointer;
00469 };
00470
00471 template <class Set, class Action, class Predicate, class ActionSet>
00472 struct iterator_traits<olsr::utl::MaskIterator<olsr::utl::MSAdapt<Set>, Action, Predicate, ActionSet> > {
00473 typedef forward_iterator_tag iterator_category;
00474 typedef typename iterator_traits<typename Set::iterator>::difference_type
00475 difference_type;
00476 typedef typename iterator_traits<typename Set::iterator>::value_type
00477 value_type;
00478 typedef typename iterator_traits<typename Set::iterator>::reference
00479 reference;
00480 typedef typename iterator_traits<typename Set::iterator>::pointer
00481 pointer;
00482 };
00483
00484 }
00485
00486 #endif // ! QOLYESTER_UTL_ITERATOR_HH