COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/map_iterator.h @ 1138:f68cb8752d81

Last change on this file since 1138:f68cb8752d81 was 987:87f7c54892df, checked in by Alpar Juttner, 19 years ago

Naming changes:

File size: 18.6 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/lemon/map_iterator.h - Part of LEMON, a generic C++ optimization library
[906]3 *
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
[921]17#ifndef LEMON_MAP_ITERATOR_H
18#define LEMON_MAP_ITERATOR_H
[822]19
[844]20#include <iterator>
21
[921]22#include <lemon/extended_pair.h>
[822]23
[830]24///\ingroup graphmaps
25///\file
26///\brief Iterators on the maps.
27
[921]28namespace lemon {
[822]29
[830]30  /// \addtogroup graphmaps
31  /// @{
32
33  /** The base class all of the map iterators.
34   *  The class defines the typedefs of the iterators,
35   *  simple step functions and equality operators.
36   */
[822]37
38  template <typename Map>
[830]39  class MapIteratorBase {
[822]40
[830]41  public:
[844]42
[830]43    /// The key type of the iterator.
[987]44    typedef typename Map::Key Key;
[830]45    /// The iterator to iterate on the keys.
46    typedef typename Map::KeyIt KeyIt;
47
48    /// The value type of the iterator.
[987]49    typedef typename Map::Value Value;
[830]50    /// The reference type of the iterator.
[987]51    typedef typename Map::Reference Reference;
[830]52    /// The pointer type of the iterator.
[987]53    typedef typename Map::Pointer Pointer;
[830]54
55    /// The const value type of the iterator.
[987]56    typedef typename Map::ConstValue ConstValue;
[830]57    /// The const reference type of the iterator.
[987]58    typedef typename Map::ConstReference ConstReference;
[830]59    /// The pointer type of the iterator.
[987]60    typedef typename Map::ConstPointer ConstPointer;
[830]61
62  protected:
63
64    KeyIt it;
65
66    /// Default constructor.
67    MapIteratorBase() {}
68
69    /// KeyIt initialized MapIteratorBase constructor.
70    MapIteratorBase(const KeyIt pit) : it(pit) {}
71
72  public:
73
74    /// Stepping forward in the map.   
75    void increment() {
76      ++it;
77    }
78
79    /// The equality operator of the map.
80    bool operator==(const MapIteratorBase& pit) const {
81      return pit.it == it;
82    }
83       
84    /// The not-equality operator of the map.
85    bool operator!=(const MapIteratorBase& pit) const {
86      return !(*this == pit);
87    }
88  };
89
90  template <typename Map> class MapConstIterator;
[822]91
92  /** Compatible iterator with the stl maps' iterators.
[830]93   * It iterates on pairs of a key and a value.
[822]94   */
95  template <typename Map> 
[830]96  class MapIterator : public MapIteratorBase<Map> {
97
[822]98    friend class MapConstIterator<Map>;
99
[844]100
[822]101  public:
102
[844]103    /// The iterator base class.
104    typedef MapIteratorBase<Map> Base;
105
[822]106    /// The key type of the iterator.
[987]107    typedef typename Map::Key Key;
[822]108    /// The iterator to iterate on the keys.
109    typedef typename Map::KeyIt KeyIt;
110
111    /// The value type of the iterator.
[987]112    typedef typename Map::Value Value;
[822]113    /// The reference type of the iterator.
[987]114    typedef typename Map::Reference Reference;
[822]115    /// The pointer type of the iterator.
[987]116    typedef typename Map::Pointer Pointer;
[822]117
118    /// The const value type of the iterator.
[987]119    typedef typename Map::ConstValue ConstValue;
[822]120    /// The const reference type of the iterator.
[987]121    typedef typename Map::ConstReference ConstReference;
[822]122    /// The pointer type of the iterator.
[987]123    typedef typename Map::ConstPointer ConstPointer;
[822]124   
125  public:
126
[844]127    /// The value type of the iterator.
[987]128    typedef extended_pair<Key, const Key&,
129      Value, const Value&> PairValue;
[844]130
[830]131    /// The reference type of the iterator.
[987]132    typedef extended_pair<const Key&, const Key&,
133      Reference, Reference> PairReference;
[822]134
[830]135    /// Default constructor.
136    MapIterator() {}
137
138    /// Constructor to initalize the iterators returned
139    /// by the begin() and end().
[844]140    MapIterator(Map& pmap, const KeyIt& pit) : Base(pit), map(&pmap) {}
[830]141
142    /// Dereference operator for the iterator.
[987]143    PairReference operator*() {
144      return PairReference(Base::it, (*map)[Base::it]);
[822]145    }
146
[830]147    /// The pointer type of the iterator.
[987]148    class PairPointer {
[822]149      friend class MapIterator;
150    private:
[987]151      PairReference data;
152      PairPointer(const Key& key, Reference val)
[822]153        : data(key, val) {}
154    public:
[987]155      PairReference* operator->() {return &data;}
[822]156    };
157
[830]158    /// Arrow operator for the iterator.
[987]159    PairPointer operator->() {
160      return PairPointer(Base::it, ((*map)[Base::it]));
[822]161    }
[830]162       
163    /// The pre increment operator of the iterator.
[822]164    MapIterator& operator++() {
[844]165      Base::increment();
[822]166      return *this;
167    }
168
[830]169    /// The post increment operator of the iterator.
[822]170    MapIterator operator++(int) {
[844]171      MapIterator tmp(*this);
172      Base::increment();
[822]173      return tmp;
174    }
175
176  private:
177    Map* map;
[844]178
179  public:
180    // STL  compatibility typedefs.
181    typedef std::forward_iterator_tag iterator_category;
182    typedef int difference_type;
[987]183    typedef PairValue value_type;
184    typedef PairReference reference;
185    typedef PairPointer pointer;
[822]186  };
187
188  /** Compatible iterator with the stl maps' iterators.
189   *  It iterates on pairs of a key and a value.
190   */
191  template <typename Map>
[830]192  class MapConstIterator : public MapIteratorBase<Map> {
193   
[822]194  public:
195
[844]196    /// The iterator base class.
197    typedef MapIteratorBase<Map> Base;
198
[822]199    /// The key type of the iterator.
[987]200    typedef typename Map::Key Key;
[822]201    /// The iterator to iterate on the keys.
202    typedef typename Map::KeyIt KeyIt;
203
204    /// The value type of the iterator.
[987]205    typedef typename Map::Value Value;
[822]206    /// The reference type of the iterator.
[987]207    typedef typename Map::Reference Reference;
[822]208    /// The pointer type of the iterator.
[987]209    typedef typename Map::Pointer Pointer;
[822]210
211    /// The const value type of the iterator.
[987]212    typedef typename Map::ConstValue ConstValue;
[822]213    /// The const reference type of the iterator.
[987]214    typedef typename Map::ConstReference ConstReference;
[822]215    /// The pointer type of the iterator.
[987]216    typedef typename Map::ConstPointer ConstPointer;
[822]217
218  public:   
219
[830]220    /// Default constructor.
[822]221    MapConstIterator() {}
222
[830]223    /// Constructor to initalize the the iterators returned
224    ///  by the begin() and end().
225    MapConstIterator(const Map& pmap, const KeyIt& pit)
[844]226      : Base(pit), map(&pmap) {}
[830]227
228    /// Constructor to create const iterator from a non const.
229    MapConstIterator(const MapIterator<Map>& pit) {
[844]230      Base::it = pit.Base::it;
[830]231      map = pit.map;
232    }
233
[844]234    /// The value type of the iterator.
[987]235    typedef extended_pair<Key, const Key&,
236      Value, const Value&> PairValue;
[844]237
[830]238    /// The reference type of map.
[987]239    typedef extended_pair<const Key&, const Key&,
240      ConstReference, ConstReference> PairReference;
[822]241
[830]242    /// Dereference operator for the iterator.
[987]243    PairReference operator*() {
244      return PairReference(Base::it, (*map)[Base::it]);
[822]245    }
246
[830]247    /// The pointer type of the iterator.
[987]248    class PairPointer {
[822]249      friend class MapConstIterator;
250    private:
[987]251      PairReference data;
252      PairPointer(const Key& key, ConstReference val)
[822]253        : data(key, val) {}
254    public:
[987]255      PairReference* operator->() {return &data;}
[822]256    };
257
[830]258    /// Arrow operator for the iterator.
[987]259    PairPointer operator->() {
260      return PairPointer(Base::it, (*map)[Base::it]);
[822]261    }
262
[830]263    /// The pre increment operator of the iterator.
[822]264    MapConstIterator& operator++() {
[844]265      Base::increment();
[822]266      return *this;
267    }
268
[830]269    /// The post increment operator of the iterator.
[822]270    MapConstIterator operator++(int) {
[844]271      MapConstIterator tmp(*this);
272      Base::increment();
[822]273      return tmp;
274    }
275
[830]276  private:
277    const Map* map;
[844]278
279  public:
280    // STL  compatibility typedefs.
281    typedef std::input_iterator_tag iterator_category;
282    typedef int difference_type;
[987]283    typedef PairValue value_type;
284    typedef PairReference reference;
285    typedef PairPointer pointer;
[830]286  };
287
288  /** The class makes the KeyIt to an stl compatible iterator
289   *  with dereferencing operator.
290   */
291  template <typename Map>
292  class MapKeyIterator : public MapIteratorBase<Map> {
293
294  public:
295
[844]296    /// The iterator base class.
297    typedef MapIteratorBase<Map> Base;
298 
[830]299    /// The key type of the iterator.
[987]300    typedef typename Map::Key Key;
[830]301    /// The iterator to iterate on the keys.
302    typedef typename Map::KeyIt KeyIt;
303
304  public:
305
306    /// Default constructor.
307    MapKeyIterator() {}
308
309    /// KeyIt initialized iterator.
[844]310    MapKeyIterator(const KeyIt& pit) : Base(pit) {}
[830]311
312    /// The pre increment operator of the iterator.
313    MapKeyIterator& operator++() {
[844]314      Base::increment();
[830]315      return *this;
[822]316    }
317
[830]318    /// The post increment operator of the iterator.
319    MapKeyIterator operator++(int) {
320      MapKeyIterator tmp(*this);
[844]321      Base::increment();
[830]322      return tmp;
[822]323    }
[830]324
325    /// The dereferencing operator of the iterator.
[987]326    Key operator*() const {
327      return static_cast<Key>(Base::it);
[822]328    }
[844]329
330  public:
331    // STL  compatibility typedefs.
332    typedef std::input_iterator_tag iterator_category;
333    typedef int difference_type;
[987]334    typedef Key value_type;
335    typedef const Key& reference;
336    typedef const Key* pointer;
[830]337  };
338
339  template <typename Map> class MapConstValueIterator;
340
[844]341  /** MapValueIterator creates an stl compatible iterator
342   *  for the values.
343   */
[830]344  template <typename Map>
345  class MapValueIterator : public MapIteratorBase<Map> {
346
347    friend class MapConstValueIterator<Map>;
348
349  public:
350
[844]351    /// The iterator base class.
352    typedef MapIteratorBase<Map> Base;
353
[830]354    /// The key type of the iterator.
[987]355    typedef typename Map::Key Key;
[830]356    /// The iterator to iterate on the keys.
357    typedef typename Map::KeyIt KeyIt;
358
359
360    /// The value type of the iterator.
[987]361    typedef typename Map::Value Value;
[830]362    /// The reference type of the iterator.
[987]363    typedef typename Map::Reference Reference;
[830]364    /// The pointer type of the iterator.
[987]365    typedef typename Map::Pointer Pointer;
[830]366
367    /// The const value type of the iterator.
[987]368    typedef typename Map::ConstValue ConstValue;
[830]369    /// The const reference type of the iterator.
[987]370    typedef typename Map::ConstReference ConstReference;
[830]371    /// The pointer type of the iterator.
[987]372    typedef typename Map::ConstPointer ConstPointer;
[830]373
[822]374  private:
[830]375
376    Map* map;
377
378  public:
379
380    /// Default constructor.
381    MapValueIterator() {}
382
383    /// Map and KeyIt initialized iterator.
384    MapValueIterator(Map& pmap, const KeyIt& pit)
[844]385      : Base(pit), map(&pmap) {}
[830]386   
387
388    /// The pre increment operator of the iterator.
389    MapValueIterator& operator++() {
[844]390      Base::increment();
[830]391      return *this;
392    }
393
394    /// The post increment operator of the iterator.
395    MapValueIterator operator++(int) {
396      MapValueIterator tmp(*this);
[844]397      Base::increment();
[830]398      return tmp;
399    }
400
401    /// The dereferencing operator of the iterator.
[987]402    Reference operator*() const {
[844]403      return (*map)[Base::it];
[830]404    }
405
406    /// The arrow operator of the iterator.
[987]407    Pointer operator->() const {
[830]408      return &(operator*());
409    }
410
[844]411  public:
412    // STL  compatibility typedefs.
413    typedef std::forward_iterator_tag iterator_category;
414    typedef int difference_type;
[987]415    typedef Value value_type;
416    typedef Reference reference;
417    typedef Pointer pointer;
[830]418  };
419
[844]420  /** MapValueIterator creates an stl compatible iterator
421   *  for the const values.
422   */
[830]423
424  template <typename Map>
425  class MapConstValueIterator : public MapIteratorBase<Map> {
426
427  public:
428
[844]429    /// The iterator base class.
430    typedef MapIteratorBase<Map> Base;
431
[830]432    /// The key type of the iterator.
[987]433    typedef typename Map::Key Key;
[830]434    /// The iterator to iterate on the keys.
435    typedef typename Map::KeyIt KeyIt;
436
437    /// The value type of the iterator.
[987]438    typedef typename Map::Value Value;
[830]439    /// The reference type of the iterator.
[987]440    typedef typename Map::Reference Reference;
[830]441    /// The pointer type of the iterator.
[987]442    typedef typename Map::Pointer Pointer;
[830]443
444    /// The const value type of the iterator.
[987]445    typedef typename Map::ConstValue ConstValue;
[830]446    /// The const reference type of the iterator.
[987]447    typedef typename Map::ConstReference ConstReference;
[830]448    /// The pointer type of the iterator.
[987]449    typedef typename Map::ConstPointer ConstPointer;
[830]450
451  private:
452
[822]453    const Map* map;
[830]454
455  public:
456
457    /// Default constructor.
458    MapConstValueIterator() {}
459
460    /// Constructor to create const iterator from a non const.
461    MapConstValueIterator(const MapValueIterator<Map>& pit) {
[844]462      Base::it = pit.Base::it;
[830]463      map = pit.map;
464    }
465
466    /// Map and KeyIt initialized iterator.
467    MapConstValueIterator(const Map& pmap, const KeyIt& pit)
[844]468      : Base(pit), map(&pmap) {}
[830]469
470    /// The pre increment operator of the iterator.
471    MapConstValueIterator& operator++() {
[844]472      Base::increment();
[830]473      return *this;
474    }
475
476    /// The post increment operator of the iterator.
477    MapConstValueIterator operator++(int) {
478      MapConstValueIterator tmp(*this);
[844]479      Base::increment();
[830]480      return tmp;
481    }
482
483    /// The dereferencing operator of the iterator.
[987]484    ConstReference operator*() const {
[844]485      return (*map)[Base::it];
[830]486    }
487
488    /// The arrow operator of the iterator.
[987]489    ConstPointer operator->() const {
[830]490      return &(operator*());
491    }
492
[844]493  public:
494    // STL  compatibility typedefs.
495    typedef std::input_iterator_tag iterator_category;
496    typedef int difference_type;
[987]497    typedef Value value_type;
498    typedef ConstReference reference;
499    typedef ConstPointer pointer;
[822]500  };
501
[830]502
503  /** This class makes from a map an iteratable set
504   *  which contains all the keys of the map.
505   */
506  template <typename Map>
507  class MapConstKeySet {
508
509    const Map* map;
510
511  public:
512
513    /// The key type of the iterator.
[987]514    typedef typename Map::Key Key;
[830]515    /// The iterator to iterate on the keys.
516    typedef typename Map::KeyIt KeyIt;
517
[844]518
519    /// The value type of the iterator.
[987]520    typedef typename Map::Value Value;
[844]521    /// The reference type of the iterator.
[987]522    typedef typename Map::Reference Reference;
[844]523    /// The pointer type of the iterator.
[987]524    typedef typename Map::Pointer Pointer;
[844]525
526    /// The const value type of the iterator.
[987]527    typedef typename Map::ConstValue ConstValue;
[844]528    /// The const reference type of the iterator.
[987]529    typedef typename Map::ConstReference ConstReference;
[844]530    /// The pointer type of the iterator.
[987]531    typedef typename Map::ConstPointer ConstPointer;
[844]532
[830]533    /// The map initialized const key set.
534    MapConstKeySet(const Map& pmap) : map(&pmap) {}
535
536    /// The const iterator of the set.
537    typedef MapKeyIterator<Map> ConstIterator;
538
539    /// It gives back the const iterator pointed to the first element.
540    ConstIterator begin() const {
541      return ConstIterator(KeyIt(*map->getGraph()));
542    }
543           
544    /// It gives back the const iterator pointed to the first ivalid element.
545    ConstIterator end() const {
546      return ConstIterator(KeyIt(INVALID));
547    }
[844]548 
549  public:
550    // STL  compatibility typedefs.
[987]551    typedef Value value_type;
[844]552    typedef ConstIterator const_iterator;
[987]553    typedef ConstReference const_reference;
554    typedef ConstPointer const_pointer;
[844]555    typedef int difference_type;
[830]556  };
557
558  /** This class makes from a map an iteratable set
559   *  which contains all the values of the map.
560   *  The values cannot be modified.
561   */
562  template <typename Map>
563  class MapConstValueSet {
564
565    const Map* map;
566
567  public:
568
569    /// The key type of the iterator.
[987]570    typedef typename Map::Key Key;
[830]571    /// The iterator to iterate on the keys.
572    typedef typename Map::KeyIt KeyIt;
573
[844]574
575    /// The value type of the iterator.
[987]576    typedef typename Map::Value Value;
[844]577    /// The reference type of the iterator.
[987]578    typedef typename Map::Reference Reference;
[844]579    /// The pointer type of the iterator.
[987]580    typedef typename Map::Pointer Pointer;
[844]581
582    /// The const value type of the iterator.
[987]583    typedef typename Map::ConstValue ConstValue;
[844]584    /// The const reference type of the iterator.
[987]585    typedef typename Map::ConstReference ConstReference;
[844]586    /// The pointer type of the iterator.
[987]587    typedef typename Map::ConstPointer ConstPointer;
[844]588
[830]589    /// The map initialized const value set.
590    MapConstValueSet(const Map& pmap) : map(&pmap) {}
591
592    /// The const iterator of the set.
593    typedef MapConstValueIterator<Map> ConstIterator;
594
595    /// It gives back the const iterator pointed to the first element.
596    ConstIterator begin() const {
597      return ConstIterator(*map, KeyIt(*map->getGraph()));
598    }
599
600    /// It gives back the const iterator pointed to the first invalid element.
601    ConstIterator end() const {
602      return ConstIterator(*map, KeyIt(INVALID));
603    }
[844]604
605  public:
606    // STL  compatibility typedefs.
[987]607    typedef Value value_type;
[844]608    typedef ConstIterator const_iterator;
[987]609    typedef ConstReference const_reference;
610    typedef ConstPointer const_pointer;
[844]611    typedef int difference_type;
[830]612  };
613
614
615  /** This class makes from a map an iteratable set
616   *  which contains all the values of the map.
617   *  The values can be modified.
618   */
619  template <typename Map>
620  class MapValueSet {
621
622    Map* map;
623
624  public:
625
626    /// The key type of the iterator.
[987]627    typedef typename Map::Key Key;
[830]628    /// The iterator to iterate on the keys.
629    typedef typename Map::KeyIt KeyIt;
630
[844]631
632    /// The value type of the iterator.
[987]633    typedef typename Map::Value Value;
[844]634    /// The reference type of the iterator.
[987]635    typedef typename Map::Reference Reference;
[844]636    /// The pointer type of the iterator.
[987]637    typedef typename Map::Pointer Pointer;
[844]638
639    /// The const value type of the iterator.
[987]640    typedef typename Map::ConstValue ConstValue;
[844]641    /// The const reference type of the iterator.
[987]642    typedef typename Map::ConstReference ConstReference;
[844]643    /// The pointer type of the iterator.
[987]644    typedef typename Map::ConstPointer ConstPointer;
[844]645
[830]646    /// The map initialized value set.
647    MapValueSet(Map& pmap) : map(&pmap) {}
648
649    /// The const iterator of the set.
650    typedef MapConstValueIterator<Map> ConstIterator;
651
652    /// It gives back the const iterator pointed to the first element.
653    ConstIterator begin() const {
654      return ConstIterator(*map, KeyIt(*map->getGraph()));
655    }
656
657    /// It gives back the const iterator pointed to the first invalid element.
658    ConstIterator end() const {
659      return ConstIterator(*map, KeyIt(INVALID));
660    }
661
662    /// The iterator of the set.
663    typedef MapValueIterator<Map> Iterator;
664
665    /// It gives back the iterator pointed to the first element.
666    Iterator begin() {
667      return Iterator(*map, KeyIt(*map->getGraph()));
668    }
669
670    /// It gives back the iterator pointed to the first invalid element.
671    Iterator end() {
672      return Iterator(*map, KeyIt(INVALID));
673    }
674           
[844]675  public:
676    // STL  compatibility typedefs.
[987]677    typedef Value value_type;
[844]678    typedef Iterator iterator;
679    typedef ConstIterator const_iterator;
[987]680    typedef Reference reference;
681    typedef ConstReference const_reference;
682    typedef Pointer pointer;
683    typedef ConstPointer const_pointer;
[844]684    typedef int difference_type;
685
[830]686  };
687
688  /// @}
689
[822]690}
691
692#endif
Note: See TracBrowser for help on using the repository browser.