src/lemon/map_iterator.h
changeset 1269 4c63ff4e16fa
parent 1164 80bb73097736
child 1271 40e5d0d44a65
equal deleted inserted replaced
2:e0b5ff72e97e 3:f5725184e6f3
    18 #define LEMON_MAP_ITERATOR_H
    18 #define LEMON_MAP_ITERATOR_H
    19 
    19 
    20 #include <iterator>
    20 #include <iterator>
    21 
    21 
    22 #include <lemon/extended_pair.h>
    22 #include <lemon/extended_pair.h>
       
    23 #include <lemon/map_utils.h>
    23 
    24 
    24 ///\ingroup graphmaps
    25 ///\ingroup graphmaps
    25 ///\file
    26 ///\file
    26 ///\brief Iterators on the maps.
    27 ///\brief Iterators on the maps.
    27 
    28 
    33   /** The base class all of the map iterators.
    34   /** The base class all of the map iterators.
    34    *  The class defines the typedefs of the iterators,
    35    *  The class defines the typedefs of the iterators,
    35    *  simple step functions and equality operators.
    36    *  simple step functions and equality operators.
    36    */ 
    37    */ 
    37 
    38 
    38   template <typename Map>
    39   template <
       
    40     typename _Graph,
       
    41     typename _Item>
    39   class MapIteratorBase {
    42   class MapIteratorBase {
    40 
    43 
    41   public:
    44   protected:
    42 
    45 
    43     /// The key type of the iterator.
    46     /// The key type of the iterator.
    44     typedef typename Map::Key Key;
    47     typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt;
    45     /// The iterator to iterate on the keys.
    48 
    46     typedef typename Map::KeyIt KeyIt;
    49     ItemIt it;
    47 
       
    48     /// The value type of the iterator.
       
    49     typedef typename Map::Value Value;
       
    50     /// The reference type of the iterator.
       
    51     typedef typename Map::Reference Reference;
       
    52     /// The pointer type of the iterator.
       
    53     typedef typename Map::Pointer Pointer;
       
    54 
       
    55     /// The const value type of the iterator.
       
    56     typedef typename Map::ConstValue ConstValue;
       
    57     /// The const reference type of the iterator.
       
    58     typedef typename Map::ConstReference ConstReference;
       
    59     /// The pointer type of the iterator.
       
    60     typedef typename Map::ConstPointer ConstPointer;
       
    61 
       
    62   protected:
       
    63 
       
    64     KeyIt it;
       
    65 
    50 
    66     /// Default constructor.
    51     /// Default constructor.
    67     MapIteratorBase() {}
    52     MapIteratorBase() {}
    68 
    53 
    69     /// KeyIt initialized MapIteratorBase constructor.
    54     /// ItemIt initialized MapIteratorBase constructor.
    70     MapIteratorBase(const KeyIt pit) : it(pit) {}
    55     MapIteratorBase(const ItemIt _it) : it(_it) {}
    71 
    56 
    72   public:
    57   public:
    73 
    58 
    74     /// Stepping forward in the map.   
    59     /// Stepping forward in the map.   
    75     void increment() { 
    60     void increment() { 
    76       ++it; 
    61       ++it; 
    77     }
    62     }
    78 
    63 
    79     /// The equality operator of the map.
    64     /// The equality operator of the map.
    80     bool operator==(const MapIteratorBase& pit) const {
    65     bool operator==(const MapIteratorBase& _it) const {
    81       return pit.it == it;
    66       return _it.it == it;
    82     }
    67     }
    83 	
    68 	
    84     /// The not-equality operator of the map.
    69     /// The not-equality operator of the map.
    85     bool operator!=(const MapIteratorBase& pit) const {
    70     bool operator!=(const MapIteratorBase& _it) const {
    86       return !(*this == pit);
    71       return !(*this == _it);
    87     }
    72     }
    88   };
    73   };
    89 
    74 
    90   template <typename Map> class MapConstIterator;
    75 
       
    76   template <
       
    77     typename _Graph,
       
    78     typename _Item,
       
    79     typename _Map>
       
    80   class MapConstIterator;
    91 
    81 
    92   /** Compatible iterator with the stl maps' iterators.
    82   /** Compatible iterator with the stl maps' iterators.
    93    * It iterates on pairs of a key and a value.
    83    * It iterates on pairs of a key and a value.
    94    */
    84    */
    95   template <typename Map>  
    85   template <
    96   class MapIterator : public MapIteratorBase<Map> {
    86     typename _Graph,
    97 
    87     typename _Item,
    98     friend class MapConstIterator<Map>;
    88     typename _Map>
       
    89   class MapIterator : public MapIteratorBase<_Graph, _Item> {
       
    90 
       
    91     friend class MapConstIterator<_Graph, _Item, _Map>;
    99 
    92 
   100 
    93 
   101   public:
    94   public:
   102 
    95 
   103     /// The iterator base class.
    96     /// The iterator base class.
   104     typedef MapIteratorBase<Map> Base;
    97     typedef MapIteratorBase<_Graph, _Item> Parent;
   105 
    98 
   106     /// The key type of the iterator.
    99     typedef _Item Item;
   107     typedef typename Map::Key Key;
   100     typedef _Map Map;
   108     /// The iterator to iterate on the keys.
   101     typedef _Graph Graph;
   109     typedef typename Map::KeyIt KeyIt;
   102 
       
   103   protected:
       
   104 
       
   105     typedef typename Parent::ItemIt ItemIt;
       
   106 
       
   107     typedef typename ReferenceMapTraits<_Map>::Value MapValue;
       
   108     typedef typename ReferenceMapTraits<_Map>::Reference MapReference;
       
   109     
       
   110   public:
   110 
   111 
   111     /// The value type of the iterator.
   112     /// The value type of the iterator.
   112     typedef typename Map::Value Value;
   113     typedef extended_pair<Item, const Item&,
   113     /// The reference type of the iterator.
   114       MapValue, const MapValue&> Value;
   114     typedef typename Map::Reference Reference;
       
   115     /// The pointer type of the iterator.
       
   116     typedef typename Map::Pointer Pointer;
       
   117 
       
   118     /// The const value type of the iterator.
       
   119     typedef typename Map::ConstValue ConstValue;
       
   120     /// The const reference type of the iterator.
       
   121     typedef typename Map::ConstReference ConstReference;
       
   122     /// The pointer type of the iterator.
       
   123     typedef typename Map::ConstPointer ConstPointer;
       
   124     
       
   125   public:
       
   126 
       
   127     /// The value type of the iterator.
       
   128     typedef extended_pair<Key, const Key&,
       
   129       Value, const Value&> PairValue;
       
   130 
   115 
   131     /// The reference type of the iterator. 
   116     /// The reference type of the iterator. 
   132     typedef extended_pair<const Key&, const Key&, 
   117     typedef extended_pair<const Item&, const Item&, 
   133       Reference, Reference> PairReference;
   118       MapReference, MapReference> Reference;
   134 
   119 
   135     /// Default constructor. 
   120     /// Default constructor. 
   136     MapIterator() {}
   121     MapIterator() {}
   137 
   122 
   138     /// Constructor to initalize the iterators returned 
   123     /// Constructor to initalize the iterators returned 
   139     /// by the begin() and end().
   124     /// by the begin() and end().
   140     MapIterator(Map& pmap, const KeyIt& pit) : Base(pit), map(&pmap) {}
   125     MapIterator(Map& _map, const ItemIt& _it) 
       
   126       : Parent(_it), map(&_map) {}
   141 
   127 
   142     /// Dereference operator for the iterator.
   128     /// Dereference operator for the iterator.
   143     PairReference operator*() {
   129     Reference operator*() {
   144       return PairReference(Base::it, (*map)[Base::it]);
   130       return Reference(Parent::it, (*map)[Parent::it]);
   145     }
   131     }
   146 
   132 
   147     /// The pointer type of the iterator.
   133     /// The pointer type of the iterator.
   148     class PairPointer {
   134     class Pointer {
   149       friend class MapIterator;
   135       friend class MapIterator;
   150     private:
   136     protected:
   151       PairReference data;
   137       Reference data;
   152       PairPointer(const Key& key, Reference val) 
   138       Pointer(const Item& item, MapReference val) 
   153 	: data(key, val) {}
   139 	: data(item, val) {}
   154     public:
   140     public:
   155       PairReference* operator->() {return &data;}
   141       Reference* operator->() {return &data;}
   156     };
   142     };
   157 
   143 
   158     /// Arrow operator for the iterator.
   144     /// Arrow operator for the iterator.
   159     PairPointer operator->() {
   145     Pointer operator->() {
   160       return PairPointer(Base::it, ((*map)[Base::it])); 
   146       return Pointer(Parent::it, (*map)[Parent::it]); 
   161     }
   147     }
   162 	
   148 	
   163     /// The pre increment operator of the iterator.
   149     /// The pre increment operator of the iterator.
   164     MapIterator& operator++() { 
   150     MapIterator& operator++() { 
   165       Base::increment(); 
   151       Parent::increment(); 
   166       return *this; 
   152       return *this; 
   167     }
   153     }
   168 
   154 
   169     /// The post increment operator of the iterator.
   155     /// The post increment operator of the iterator.
   170     MapIterator operator++(int) { 
   156     MapIterator operator++(int) { 
   171       MapIterator tmp(*this); 
   157       MapIterator tmp(*this); 
   172       Base::increment(); 
   158       Parent::increment(); 
   173       return tmp; 
   159       return tmp; 
   174     }
   160     }
   175 
   161 
   176   private:
   162   protected:
       
   163 
   177     Map* map;
   164     Map* map;
   178 
   165 
   179   public:
   166   public:
   180     // STL  compatibility typedefs.
   167     // STL  compatibility typedefs.
   181     typedef std::forward_iterator_tag iterator_category;
   168     typedef std::forward_iterator_tag iterator_category;
   182     typedef int difference_type;
   169     typedef int difference_type;
   183     typedef PairValue value_type;
   170     typedef Value value_type;
   184     typedef PairReference reference;
   171     typedef Reference reference;
   185     typedef PairPointer pointer;
   172     typedef Pointer pointer;
   186   };
   173   };
   187 
   174 
   188   /** Compatible iterator with the stl maps' iterators.
   175   /** Compatible iterator with the stl maps' iterators.
   189    *  It iterates on pairs of a key and a value.
   176    *  It iterates on pairs of a key and a value.
   190    */
   177    */
   191   template <typename Map>
   178   template <
   192   class MapConstIterator : public MapIteratorBase<Map> {
   179     typename _Graph,
   193     
   180     typename _Item,
       
   181     typename _Map>
       
   182   class MapConstIterator : public MapIteratorBase<_Graph, _Item> {
       
   183 
   194   public:
   184   public:
   195 
   185 
   196     /// The iterator base class.
   186     /// The iterator base class.
   197     typedef MapIteratorBase<Map> Base;
   187     typedef MapIteratorBase<_Graph, _Item> Parent;
   198 
   188 
   199     /// The key type of the iterator.
   189     typedef _Graph Graph;
   200     typedef typename Map::Key Key;
   190     typedef _Item Item;
   201     /// The iterator to iterate on the keys.
   191     typedef _Map Map;
   202     typedef typename Map::KeyIt KeyIt;
   192 
       
   193   protected:
       
   194 
       
   195     typedef typename Parent::ItemIt ItemIt;
       
   196 
       
   197     typedef typename ReferenceMapTraits<_Map>::Value MapValue;
       
   198     typedef typename ReferenceMapTraits<_Map>::ConstReference
       
   199     MapReference;
       
   200     
       
   201   public:
   203 
   202 
   204     /// The value type of the iterator.
   203     /// The value type of the iterator.
   205     typedef typename Map::Value Value;
   204     typedef extended_pair<Item, const Item&,
   206     /// The reference type of the iterator.
   205       MapValue, const MapValue&> Value;
   207     typedef typename Map::Reference Reference;
   206 
   208     /// The pointer type of the iterator.
   207     /// The reference type of the iterator. 
   209     typedef typename Map::Pointer Pointer;
   208     typedef extended_pair<const Item&, const Item&, 
   210 
   209       MapReference, MapReference> Reference;
   211     /// The const value type of the iterator.
       
   212     typedef typename Map::ConstValue ConstValue;
       
   213     /// The const reference type of the iterator.
       
   214     typedef typename Map::ConstReference ConstReference;
       
   215     /// The pointer type of the iterator.
       
   216     typedef typename Map::ConstPointer ConstPointer;
       
   217 
       
   218   public:    
       
   219 
   210 
   220     /// Default constructor. 
   211     /// Default constructor. 
   221     MapConstIterator() {}
   212     MapConstIterator() {}
   222 
   213 
   223     /// Constructor to initalize the the iterators returned
   214     /// Constructor to initalize the iterators returned 
   224     ///  by the begin() and end().
   215     /// by the begin() and end().
   225     MapConstIterator(const Map& pmap, const KeyIt& pit) 
   216     MapConstIterator(const Map& _map, const ItemIt& _it) 
   226       : Base(pit), map(&pmap) {}
   217       : Parent(_it), map(&_map) {}
   227 
       
   228     /// Constructor to create const iterator from a non const.
       
   229     MapConstIterator(const MapIterator<Map>& pit) {
       
   230       Base::it = pit.Base::it;
       
   231       map = pit.map;
       
   232     }
       
   233 
       
   234     /// The value type of the iterator.
       
   235     typedef extended_pair<Key, const Key&,
       
   236       Value, const Value&> PairValue;
       
   237 
       
   238     /// The reference type of map.
       
   239     typedef extended_pair<const Key&, const Key&, 
       
   240       ConstReference, ConstReference> PairReference;
       
   241 
   218 
   242     /// Dereference operator for the iterator.
   219     /// Dereference operator for the iterator.
   243     PairReference operator*() {
   220     Reference operator*() {
   244       return PairReference(Base::it, (*map)[Base::it]);
   221       return Reference(Parent::it, (*map)[Parent::it]);
   245     }
   222     }
   246 
   223 
   247     /// The pointer type of the iterator.
   224     /// The pointer type of the iterator.
   248     class PairPointer {
   225     class Pointer {
   249       friend class MapConstIterator;
   226       friend class MapConstIterator;
   250     private:
   227     protected:
   251       PairReference data;
   228       Reference data;
   252       PairPointer(const Key& key, ConstReference val) 
   229       Pointer(const Item& item, MapReference val) 
   253 	: data(key, val) {}
   230 	: data(item, val) {}
   254     public:
   231     public:
   255       PairReference* operator->() {return &data;}
   232       Reference* operator->() {return &data;}
   256     };
   233     };
   257 
   234 
   258     /// Arrow operator for the iterator.
   235     /// Arrow operator for the iterator.
   259     PairPointer operator->() {
   236     Pointer operator->() {
   260       return PairPointer(Base::it, (*map)[Base::it]); 
   237       return Pointer(Parent::it, ((*map)[Parent::it])); 
   261     }
   238     }
   262 
   239 	
   263     /// The pre increment operator of the iterator.
   240     /// The pre increment operator of the iterator.
   264     MapConstIterator& operator++() { 
   241     MapConstIterator& operator++() { 
   265       Base::increment(); 
   242       Parent::increment(); 
   266       return *this; 
   243       return *this; 
   267     }
   244     }
   268 
   245 
   269     /// The post increment operator of the iterator.
   246     /// The post increment operator of the iterator.
   270     MapConstIterator operator++(int) { 
   247     MapConstIterator operator++(int) { 
   271       MapConstIterator tmp(*this); 
   248       MapConstIterator tmp(*this); 
   272       Base::increment(); 
   249       Parent::increment(); 
   273       return tmp; 
   250       return tmp; 
   274     }
   251     }
   275 
   252 
   276   private:
   253   protected:
   277     const Map* map;
   254     const Map* map;
   278 
   255 
   279   public:
   256   public:
   280     // STL  compatibility typedefs.
   257     // STL  compatibility typedefs.
   281     typedef std::input_iterator_tag iterator_category;
   258     typedef std::forward_iterator_tag iterator_category;
   282     typedef int difference_type;
   259     typedef int difference_type;
   283     typedef PairValue value_type;
   260     typedef Value value_type;
   284     typedef PairReference reference;
   261     typedef Reference reference;
   285     typedef PairPointer pointer;
   262     typedef Pointer pointer;
   286   };
   263   };
   287 
   264  
   288   /** The class makes the KeyIt to an stl compatible iterator
   265   /** The class makes the ItemIt to an stl compatible iterator
   289    *  with dereferencing operator.
   266    *  with dereferencing operator.
   290    */
   267    */
   291   template <typename Map>
   268   template <
   292   class MapKeyIterator : public MapIteratorBase<Map> {
   269     typename _Graph,
       
   270     typename _Item>
       
   271   class MapConstKeyIterator : public MapIteratorBase<_Graph, _Item> {
   293 
   272 
   294   public:
   273   public:
   295 
   274 
   296     /// The iterator base class.
   275     /// The iterator base class.
   297     typedef MapIteratorBase<Map> Base;
   276     typedef MapIteratorBase<_Graph, _Item> Parent;
   298  
   277  
   299     /// The key type of the iterator.
   278     typedef _Graph Graph;
   300     typedef typename Map::Key Key;
   279     typedef _Item Item;
       
   280 
       
   281   protected:
   301     /// The iterator to iterate on the keys.
   282     /// The iterator to iterate on the keys.
   302     typedef typename Map::KeyIt KeyIt;
   283     typedef typename Parent::ItemIt ItemIt;
   303 
   284 
   304   public:
   285   public:
       
   286 
       
   287     typedef Item Value;
       
   288     typedef const Item& Reference;
       
   289     typedef const Item* Pointer;
   305 
   290 
   306     /// Default constructor.
   291     /// Default constructor.
   307     MapKeyIterator() {}
   292     MapConstKeyIterator() {}
   308 
   293 
   309     /// KeyIt initialized iterator.
   294     /// ItemIt initialized iterator.
   310     MapKeyIterator(const KeyIt& pit) : Base(pit) {}
   295     MapConstKeyIterator(const ItemIt& pit) : Parent(pit) {}
   311 
   296 
   312     /// The pre increment operator of the iterator.
   297     /// The pre increment operator of the iterator.
   313     MapKeyIterator& operator++() {
   298     MapConstKeyIterator& operator++() {
   314       Base::increment(); 
   299       Parent::increment(); 
   315       return *this; 
   300       return *this; 
   316     }
   301     }
   317 
   302 
   318     /// The post increment operator of the iterator.
   303     /// The post increment operator of the iterator.
   319     MapKeyIterator operator++(int) {
   304     MapConstKeyIterator operator++(int) {
   320       MapKeyIterator tmp(*this);
   305       MapConstKeyIterator tmp(*this);
   321       Base::increment();
   306       Parent::increment();
   322       return tmp;
   307       return tmp;
   323     }
   308     }
   324 
   309 
   325     /// The dereferencing operator of the iterator.
   310     /// The dereferencing operator of the iterator.
   326     Key operator*() const {
   311     Item operator*() const {
   327       return static_cast<Key>(Base::it);
   312       return static_cast<Item>(Parent::it);
   328     }
   313     }
   329 
   314 
   330   public:
   315   public:
   331     // STL  compatibility typedefs.
   316     // STL  compatibility typedefs.
   332     typedef std::input_iterator_tag iterator_category;
   317     typedef std::input_iterator_tag iterator_category;
   333     typedef int difference_type;
   318     typedef int difference_type;
   334     typedef Key value_type;
   319     typedef Value value_type;
   335     typedef const Key& reference;
   320     typedef Reference reference;
   336     typedef const Key* pointer;
   321     typedef Pointer pointer;
   337   };
   322   };
   338 
   323 
   339   template <typename Map> class MapConstValueIterator;
   324   template <
       
   325     typename _Graph, 
       
   326     typename _Item,
       
   327     typename _Map>
       
   328   class MapConstValueIterator;
   340 
   329 
   341   /** MapValueIterator creates an stl compatible iterator
   330   /** MapValueIterator creates an stl compatible iterator
   342    *  for the values.
   331    *  for the values.
   343    */
   332    */
   344   template <typename Map>
   333   template <
   345   class MapValueIterator : public MapIteratorBase<Map> {
   334     typename _Graph,
   346 
   335     typename _Item,
   347     friend class MapConstValueIterator<Map>;
   336     typename _Map>
       
   337   class MapValueIterator : public MapIteratorBase<_Graph, _Item> {
       
   338 
       
   339     friend class MapConstValueIterator<_Graph, _Item, _Map>;
   348 
   340 
   349   public:
   341   public:
   350 
   342 
   351     /// The iterator base class.
   343     /// The iterator base class.
   352     typedef MapIteratorBase<Map> Base;
   344     typedef MapIteratorBase<_Graph, _Item> Parent;
   353 
   345 
   354     /// The key type of the iterator.
   346     typedef _Graph Graph;
   355     typedef typename Map::Key Key;
   347     typedef _Item Item;
       
   348     typedef _Map Map;
       
   349 
       
   350   protected:
       
   351 
   356     /// The iterator to iterate on the keys.
   352     /// The iterator to iterate on the keys.
   357     typedef typename Map::KeyIt KeyIt;
   353     typedef typename Parent::ItemIt ItemIt;
   358 
       
   359 
   354 
   360     /// The value type of the iterator.
   355     /// The value type of the iterator.
   361     typedef typename Map::Value Value;
   356     typedef typename ReferenceMapTraits<Map>::Value MapValue;
   362     /// The reference type of the iterator.
   357     /// The reference type of the iterator.
   363     typedef typename Map::Reference Reference;
   358     typedef typename ReferenceMapTraits<Map>::Reference MapReference;
   364     /// The pointer type of the iterator.
   359     /// The pointer type of the iterator.
   365     typedef typename Map::Pointer Pointer;
   360     typedef typename ReferenceMapTraits<Map>::Pointer MapPointer;
   366 
   361 
   367     /// The const value type of the iterator.
   362   public:
   368     typedef typename Map::ConstValue ConstValue;
   363 
   369     /// The const reference type of the iterator.
   364     typedef MapValue Value;
   370     typedef typename Map::ConstReference ConstReference;
   365     typedef MapReference Reference;
   371     /// The pointer type of the iterator.
   366     typedef MapPointer Pointer;
   372     typedef typename Map::ConstPointer ConstPointer;
       
   373 
       
   374   private:
       
   375 
       
   376     Map* map;
       
   377 
       
   378   public:
       
   379 
   367 
   380     /// Default constructor.
   368     /// Default constructor.
   381     MapValueIterator() {}
   369     MapValueIterator() {}
   382 
   370 
   383     /// Map and KeyIt initialized iterator.
   371     /// Map and ItemIt initialized iterator.
   384     MapValueIterator(Map& pmap, const KeyIt& pit) 
   372     MapValueIterator(Map& _map, const ItemIt& _it) 
   385       : Base(pit), map(&pmap) {}
   373       : Parent(_it), map(&_map) {}
   386     
   374     
   387 
   375 
   388     /// The pre increment operator of the iterator.
   376     /// The pre increment operator of the iterator.
   389     MapValueIterator& operator++() {
   377     MapValueIterator& operator++() {
   390       Base::increment(); 
   378       Parent::increment(); 
   391       return *this; 
   379       return *this; 
   392     }
   380     }
   393 
   381 
   394     /// The post increment operator of the iterator.
   382     /// The post increment operator of the iterator.
   395     MapValueIterator operator++(int) {
   383     MapValueIterator operator++(int) {
   396       MapValueIterator tmp(*this);
   384       MapValueIterator tmp(*this);
   397       Base::increment();
   385       Parent::increment();
   398       return tmp;
   386       return tmp;
   399     }
   387     }
   400 
   388 
   401     /// The dereferencing operator of the iterator.
   389     /// The dereferencing operator of the iterator.
   402     Reference operator*() const {
   390     Reference operator*() const {
   403       return (*map)[Base::it];
   391       return (*map)[Parent::it];
   404     }
   392     }
   405 
   393 
   406     /// The arrow operator of the iterator.
   394     /// The arrow operator of the iterator.
   407     Pointer operator->() const {
   395     Pointer operator->() const {
   408       return &(operator*());
   396       return &(operator*());
   409     }
   397     }
   410 
   398 
       
   399   protected:
       
   400 
       
   401     Map* map;
       
   402 
   411   public:
   403   public:
   412     // STL  compatibility typedefs.
   404     // STL  compatibility typedefs.
   413     typedef std::forward_iterator_tag iterator_category;
   405     typedef std::forward_iterator_tag iterator_category;
   414     typedef int difference_type;
   406     typedef int difference_type;
   415     typedef Value value_type;
   407     typedef Value value_type;
   416     typedef Reference reference;
   408     typedef Reference reference;
   417     typedef Pointer pointer;
   409     typedef Pointer pointer;
   418   };
   410   };
   419 
   411 
   420   /** MapValueIterator creates an stl compatible iterator
   412   /** MapValueIterator creates an stl compatible iterator
   421    *  for the const values.
   413    *  for the values.
   422    */
   414    */
   423 
   415   template <
   424   template <typename Map>
   416     typename _Graph,
   425   class MapConstValueIterator : public MapIteratorBase<Map> {
   417     typename _Item,
       
   418     typename _Map>
       
   419   class MapConstValueIterator : public MapIteratorBase<_Graph, _Item> {
   426 
   420 
   427   public:
   421   public:
   428 
   422 
   429     /// The iterator base class.
   423     /// The iterator base class.
   430     typedef MapIteratorBase<Map> Base;
   424     typedef MapIteratorBase<_Graph, _Item> Parent;
   431 
   425 
   432     /// The key type of the iterator.
   426     typedef _Graph Graph;
   433     typedef typename Map::Key Key;
   427     typedef _Item Item;
       
   428     typedef _Map Map;
       
   429 
       
   430   protected:
       
   431 
   434     /// The iterator to iterate on the keys.
   432     /// The iterator to iterate on the keys.
   435     typedef typename Map::KeyIt KeyIt;
   433     typedef typename Parent::ItemIt ItemIt;
   436 
   434 
   437     /// The value type of the iterator.
   435     /// The value type of the iterator.
   438     typedef typename Map::Value Value;
   436     typedef typename ReferenceMapTraits<Map>::Value MapValue;
   439     /// The reference type of the iterator.
   437     /// The reference type of the iterator.
   440     typedef typename Map::Reference Reference;
   438     typedef typename ReferenceMapTraits<Map>::ConstReference MapReference;
   441     /// The pointer type of the iterator.
   439     /// The pointer type of the iterator.
   442     typedef typename Map::Pointer Pointer;
   440     typedef typename ReferenceMapTraits<Map>::ConstPointer MapPointer;
   443 
   441 
   444     /// The const value type of the iterator.
   442   public:
   445     typedef typename Map::ConstValue ConstValue;
   443 
   446     /// The const reference type of the iterator.
   444     typedef MapValue Value;
   447     typedef typename Map::ConstReference ConstReference;
   445     typedef MapReference Reference;
   448     /// The pointer type of the iterator.
   446     typedef MapPointer Pointer;
   449     typedef typename Map::ConstPointer ConstPointer;
       
   450 
       
   451   private:
       
   452 
       
   453     const Map* map;
       
   454 
       
   455   public:
       
   456 
   447 
   457     /// Default constructor.
   448     /// Default constructor.
   458     MapConstValueIterator() {}
   449     MapConstValueIterator() {}
   459 
   450 
   460     /// Constructor to create const iterator from a non const.
   451     /// Map and ItemIt initialized iterator.
   461     MapConstValueIterator(const MapValueIterator<Map>& pit) {
   452     MapConstValueIterator(const Map& _map, const ItemIt& _it) 
   462       Base::it = pit.Base::it;
   453       : Parent(_it), map(&_map) {}
   463       map = pit.map;
   454     
   464     }
       
   465 
       
   466     /// Map and KeyIt initialized iterator.
       
   467     MapConstValueIterator(const Map& pmap, const KeyIt& pit) 
       
   468       : Base(pit), map(&pmap) {}
       
   469 
   455 
   470     /// The pre increment operator of the iterator.
   456     /// The pre increment operator of the iterator.
   471     MapConstValueIterator& operator++() {
   457     MapConstValueIterator& operator++() {
   472       Base::increment(); 
   458       Parent::increment(); 
   473       return *this; 
   459       return *this; 
   474     }
   460     }
   475 
   461 
   476     /// The post increment operator of the iterator.
   462     /// The post increment operator of the iterator.
   477     MapConstValueIterator operator++(int) {
   463     MapConstValueIterator operator++(int) {
   478       MapConstValueIterator tmp(*this);
   464       MapConstValueIterator tmp(*this);
   479       Base::increment();
   465       Parent::increment();
   480       return tmp;
   466       return tmp;
   481     }
   467     }
   482 
   468 
   483     /// The dereferencing operator of the iterator.
   469     /// The dereferencing operator of the iterator.
   484     ConstReference operator*() const {
   470     Reference operator*() const {
   485       return (*map)[Base::it];
   471       return (*map)[Parent::it];
   486     }
   472     }
   487 
   473 
   488     /// The arrow operator of the iterator.
   474     /// The arrow operator of the iterator.
   489     ConstPointer operator->() const {
   475     Pointer operator->() const {
   490       return &(operator*());
   476       return &(operator*());
   491     }
   477     }
   492 
   478 
   493   public:
   479   protected:
   494     // STL  compatibility typedefs.
   480 
   495     typedef std::input_iterator_tag iterator_category;
   481     const Map* map;
   496     typedef int difference_type;
   482 
   497     typedef Value value_type;
   483   public:
   498     typedef ConstReference reference;
   484     // STL  compatibility typedefs.
   499     typedef ConstPointer pointer;
   485     typedef std::forward_iterator_tag iterator_category;
       
   486     typedef int difference_type;
       
   487     typedef Value value_type;
       
   488     typedef Reference reference;
       
   489     typedef Pointer pointer;
   500   };
   490   };
   501 
   491 
   502 
   492 
   503   /** This class makes from a map an iteratable set
   493   /** This class makes from a map an iteratable set
   504    *  which contains all the keys of the map.
   494    *  which contains all the keys of the map.
   505    */
   495    */
   506   template <typename Map>
   496   template <typename _Graph, typename _Item>
   507   class MapConstKeySet {
   497   class MapConstKeySet {
   508 
   498 
   509     const Map* map;
   499   public:
   510 
   500 
   511   public:
   501     typedef _Graph Graph;
   512 
       
   513     /// The key type of the iterator.
   502     /// The key type of the iterator.
   514     typedef typename Map::Key Key;
   503     typedef _Item Item;
   515     /// The iterator to iterate on the keys.
   504     /// The iterator to iterate on the keys.
   516     typedef typename Map::KeyIt KeyIt;
   505 
   517 
   506   protected:
   518 
   507 
   519     /// The value type of the iterator.
   508     typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt;
   520     typedef typename Map::Value Value;
   509 
       
   510   public:
       
   511 
       
   512     /// The map initialized const key set.
       
   513     MapConstKeySet(const Graph& _graph) : graph(&_graph) {}
       
   514 
       
   515     /// The const iterator of the set.
       
   516     typedef MapConstKeyIterator<_Graph, _Item> ConstIterator;
       
   517 
       
   518     typedef typename ConstIterator::Value Value;
   521     /// The reference type of the iterator.
   519     /// The reference type of the iterator.
   522     typedef typename Map::Reference Reference;
   520     typedef typename ConstIterator::Reference ConstReference;
   523     /// The pointer type of the iterator.
   521     /// The pointer type of the iterator.
   524     typedef typename Map::Pointer Pointer;
   522     typedef typename ConstIterator::Pointer ConstPointer;
   525 
       
   526     /// The const value type of the iterator.
       
   527     typedef typename Map::ConstValue ConstValue;
       
   528     /// The const reference type of the iterator.
       
   529     typedef typename Map::ConstReference ConstReference;
       
   530     /// The pointer type of the iterator.
       
   531     typedef typename Map::ConstPointer ConstPointer;
       
   532 
       
   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 
   523 
   539     /// It gives back the const iterator pointed to the first element.
   524     /// It gives back the const iterator pointed to the first element.
   540     ConstIterator begin() const {
   525     ConstIterator begin() const {
   541       return ConstIterator(KeyIt(*map->getGraph()));
   526       return ConstIterator(ItemIt(*graph));
   542     }
   527     }
   543             
   528             
   544     /// It gives back the const iterator pointed to the first ivalid element.
   529     /// It gives back the const iterator pointed to the first ivalid element.
   545     ConstIterator end() const {
   530     ConstIterator end() const {
   546       return ConstIterator(KeyIt(INVALID));
   531       return ConstIterator(ItemIt(INVALID));
   547     }
   532     }
       
   533 
       
   534   protected:
       
   535 
       
   536     const Graph* graph;
   548  
   537  
   549   public:
   538   public:
   550     // STL  compatibility typedefs.
   539     // STL  compatibility typedefs.
   551     typedef Value value_type;
   540     typedef Value value_type;
   552     typedef ConstIterator const_iterator;
   541     typedef ConstIterator const_iterator;
   557 
   546 
   558   /** This class makes from a map an iteratable set
   547   /** This class makes from a map an iteratable set
   559    *  which contains all the values of the map.
   548    *  which contains all the values of the map.
   560    *  The values cannot be modified.
   549    *  The values cannot be modified.
   561    */
   550    */
   562   template <typename Map>
   551   template <typename _Graph, typename _Item, typename _Map>
   563   class MapConstValueSet {
   552   class MapConstValueSet {
   564 
   553 
   565     const Map* map;
   554   public:
   566 
   555     
   567   public:
   556     typedef _Graph Graph;
   568 
   557     typedef _Item Item;
   569     /// The key type of the iterator.
   558     typedef _Map Map;
   570     typedef typename Map::Key Key;
   559 
       
   560   protected:
       
   561 
   571     /// The iterator to iterate on the keys.
   562     /// The iterator to iterate on the keys.
   572     typedef typename Map::KeyIt KeyIt;
   563     typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
   573 
   564 
   574 
   565   public:
   575     /// The value type of the iterator.
       
   576     typedef typename Map::Value Value;
       
   577     /// The reference type of the iterator.
       
   578     typedef typename Map::Reference Reference;
       
   579     /// The pointer type of the iterator.
       
   580     typedef typename Map::Pointer Pointer;
       
   581 
       
   582     /// The const value type of the iterator.
       
   583     typedef typename Map::ConstValue ConstValue;
       
   584     /// The const reference type of the iterator.
       
   585     typedef typename Map::ConstReference ConstReference;
       
   586     /// The pointer type of the iterator.
       
   587     typedef typename Map::ConstPointer ConstPointer;
       
   588 
   566 
   589     /// The map initialized const value set.
   567     /// The map initialized const value set.
   590     MapConstValueSet(const Map& pmap) : map(&pmap) {}
   568     MapConstValueSet(const Graph& _graph, const Map& _map) 
       
   569       : graph(&_graph), map(&_map) {}
   591 
   570 
   592     /// The const iterator of the set.
   571     /// The const iterator of the set.
   593     typedef MapConstValueIterator<Map> ConstIterator;
   572     typedef MapConstValueIterator<_Graph, _Item, _Map> ConstIterator;
       
   573 
       
   574     typedef typename ConstIterator::Value Value;
       
   575     typedef typename ConstIterator::Reference ConstReference;
       
   576     typedef typename ConstIterator::Pointer ConstPointer;
   594 
   577 
   595     /// It gives back the const iterator pointed to the first element.
   578     /// It gives back the const iterator pointed to the first element.
   596     ConstIterator begin() const {
   579     ConstIterator begin() const {
   597       return ConstIterator(*map, KeyIt(*map->getGraph()));
   580       return ConstIterator(*map, ItemIt(*graph));
   598     }
   581     }
   599 
   582 
   600     /// It gives back the const iterator pointed to the first invalid element.
   583     /// It gives back the const iterator pointed to the first invalid element.
   601     ConstIterator end() const {
   584     ConstIterator end() const {
   602       return ConstIterator(*map, KeyIt(INVALID));
   585       return ConstIterator(*map, ItemIt(INVALID));
   603     }
   586     }
       
   587 
       
   588   protected:
       
   589     
       
   590     const Map* map;
       
   591     const Graph * graph;
   604 
   592 
   605   public:
   593   public:
   606     // STL  compatibility typedefs.
   594     // STL  compatibility typedefs.
   607     typedef Value value_type;
   595     typedef Value value_type;
   608     typedef ConstIterator const_iterator;
   596     typedef ConstIterator const_iterator;
   614 
   602 
   615   /** This class makes from a map an iteratable set
   603   /** This class makes from a map an iteratable set
   616    *  which contains all the values of the map.
   604    *  which contains all the values of the map.
   617    *  The values can be modified.
   605    *  The values can be modified.
   618    */
   606    */
   619   template <typename Map>
   607   template <typename _Graph, typename _Item, typename _Map>
   620   class MapValueSet {
   608   class MapValueSet {
   621 
   609 
   622     Map* map;
   610   public:
   623 
   611     
   624   public:
   612     typedef _Graph Graph;
   625 
   613     typedef _Item Item;
   626     /// The key type of the iterator.
   614     typedef _Map Map;
   627     typedef typename Map::Key Key;
   615 
       
   616   protected:
       
   617 
   628     /// The iterator to iterate on the keys.
   618     /// The iterator to iterate on the keys.
   629     typedef typename Map::KeyIt KeyIt;
   619     typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
   630 
   620 
   631 
   621   public:
   632     /// The value type of the iterator.
   622 
   633     typedef typename Map::Value Value;
   623     /// The map initialized const value set.
   634     /// The reference type of the iterator.
   624     MapValueSet(const Graph& _graph, Map& _map) 
   635     typedef typename Map::Reference Reference;
   625       : graph(&_graph), map(&_map) {}
   636     /// The pointer type of the iterator.
       
   637     typedef typename Map::Pointer Pointer;
       
   638 
       
   639     /// The const value type of the iterator.
       
   640     typedef typename Map::ConstValue ConstValue;
       
   641     /// The const reference type of the iterator.
       
   642     typedef typename Map::ConstReference ConstReference;
       
   643     /// The pointer type of the iterator.
       
   644     typedef typename Map::ConstPointer ConstPointer;
       
   645 
       
   646     /// The map initialized value set.
       
   647     MapValueSet(Map& pmap) : map(&pmap) {}
       
   648 
   626 
   649     /// The const iterator of the set.
   627     /// The const iterator of the set.
   650     typedef MapConstValueIterator<Map> ConstIterator;
   628     typedef MapValueIterator<_Graph, _Item, _Map> Iterator;
       
   629     /// The const iterator of the set.
       
   630     typedef MapConstValueIterator<_Graph, _Item, _Map> ConstIterator;
       
   631 
       
   632     typedef typename ConstIterator::Value Value;
       
   633     typedef typename Iterator::Reference Reference;
       
   634     typedef typename Iterator::Pointer Pointer;
       
   635     typedef typename ConstIterator::Reference ConstReference;
       
   636     typedef typename ConstIterator::Pointer ConstPointer;
   651 
   637 
   652     /// It gives back the const iterator pointed to the first element.
   638     /// It gives back the const iterator pointed to the first element.
   653     ConstIterator begin() const {
   639     ConstIterator begin() const {
   654       return ConstIterator(*map, KeyIt(*map->getGraph()));
   640       return ConstIterator(*map, ItemIt(*graph));
   655     }
   641     }
   656 
   642 
   657     /// It gives back the const iterator pointed to the first invalid element.
   643     /// It gives back the const iterator pointed to the first invalid element.
   658     ConstIterator end() const {
   644     ConstIterator end() const {
   659       return ConstIterator(*map, KeyIt(INVALID));
   645       return ConstIterator(*map, ItemIt(INVALID));
   660     }
   646     }
   661 
       
   662     /// The iterator of the set.
       
   663     typedef MapValueIterator<Map> Iterator;
       
   664 
   647 
   665     /// It gives back the iterator pointed to the first element.
   648     /// It gives back the iterator pointed to the first element.
   666     Iterator begin() {
   649     Iterator begin() {
   667       return Iterator(*map, KeyIt(*map->getGraph()));
   650       return Iterator(*map, ItemIt(*graph));
   668     }
   651     }
   669 
   652 
   670     /// It gives back the iterator pointed to the first invalid element.
   653     /// It gives back the iterator pointed to the first invalid element.
   671     Iterator end() {
   654     Iterator end() {
   672       return Iterator(*map, KeyIt(INVALID));
   655       return Iterator(*map, ItemIt(INVALID));
   673     }
   656     }
   674             
   657 
       
   658   protected:
       
   659     
       
   660     Map* map;
       
   661     const Graph * graph;
       
   662 
   675   public:
   663   public:
   676     // STL  compatibility typedefs.
   664     // STL  compatibility typedefs.
   677     typedef Value value_type;
   665     typedef Value value_type;
   678     typedef Iterator iterator;
   666     typedef Iterator iterator;
   679     typedef ConstIterator const_iterator;
   667     typedef ConstIterator const_iterator;
   683     typedef ConstPointer const_pointer;
   671     typedef ConstPointer const_pointer;
   684     typedef int difference_type;
   672     typedef int difference_type;
   685 
   673 
   686   };
   674   };
   687 
   675 
       
   676   /** This class makes from a map an iteratable set
       
   677    *  which contains all the values of the map.
       
   678    *  The values can be modified.
       
   679    */
       
   680   template <
       
   681     typename _Graph, 
       
   682     typename _Item,
       
   683     typename _Map
       
   684     >
       
   685   class MapSet {
       
   686   public:    
       
   687 
       
   688     typedef _Graph Graph;
       
   689     typedef _Item Item;
       
   690     typedef _Map Map;
       
   691 
       
   692   protected:
       
   693 
       
   694     typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt;
       
   695 
       
   696   public:
       
   697 
       
   698     /// The map initialized value set.
       
   699     MapSet(const Graph& _graph, Map& _map) : graph(&_graph), map(&_map) {}
       
   700 
       
   701     /// The const iterator of the set.
       
   702     typedef MapIterator<_Graph, _Item, _Map> Iterator;
       
   703     typedef MapConstIterator<_Graph, _Item, _Map> ConstIterator;
       
   704 
       
   705     typedef typename ConstIterator::Value Value;
       
   706     typedef typename Iterator::Reference Reference;
       
   707     typedef typename Iterator::Pointer Pointer;
       
   708     typedef typename ConstIterator::Reference ConstReference;
       
   709     typedef typename ConstIterator::Pointer ConstPointer;
       
   710 
       
   711 
       
   712     /// It gives back the const iterator pointed to the first element.
       
   713     ConstIterator begin() const {
       
   714       return ConstIterator(*map, ItemIt(*graph));
       
   715     }
       
   716 
       
   717     /// It gives back the const iterator pointed to the first invalid element.
       
   718     ConstIterator end() const {
       
   719       return ConstIterator(*map, ItemIt(INVALID));
       
   720     }
       
   721 
       
   722     /// The iterator of the set.
       
   723 
       
   724     /// It gives back the iterator pointed to the first element.
       
   725     Iterator begin() {
       
   726       return Iterator(*map, ItemIt(*graph));
       
   727     }
       
   728 
       
   729     /// It gives back the iterator pointed to the first invalid element.
       
   730     Iterator end() {
       
   731       return Iterator(*map, ItemIt(INVALID));
       
   732     }
       
   733 
       
   734   protected:
       
   735     
       
   736     const Graph* graph;
       
   737     Map* map;
       
   738             
       
   739   public:
       
   740     // STL  compatibility typedefs.
       
   741     typedef Value value_type;
       
   742     typedef Iterator iterator;
       
   743     typedef ConstIterator const_iterator;
       
   744     typedef Reference reference;
       
   745     typedef ConstReference const_reference;
       
   746     typedef Pointer pointer;
       
   747     typedef ConstPointer const_pointer;
       
   748     typedef int difference_type;
       
   749 
       
   750   };
       
   751 
       
   752   template <
       
   753     typename _Graph, 
       
   754     typename _Item,
       
   755     typename _Map
       
   756     >
       
   757   class ConstMapSet {
       
   758     
       
   759     typedef _Graph Graph;
       
   760     typedef _Map Map;
       
   761 
       
   762     const Graph* graph;
       
   763     const Map* map;
       
   764 
       
   765   public:
       
   766 
       
   767     typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt;
       
   768 
       
   769 
       
   770     /// The map initialized value set.
       
   771     ConstMapSet(const Graph& _graph, const Map& _map) 
       
   772       : graph(&_graph), map(&_map) {}
       
   773 
       
   774     /// The const iterator of the set.
       
   775     typedef MapConstIterator<_Graph, _Item, _Map> ConstIterator;
       
   776 
       
   777     typedef typename ConstIterator::Value Value;
       
   778     typedef typename ConstIterator::Reference ConstReference;
       
   779     typedef typename ConstIterator::Pointer ConstPointer;
       
   780 
       
   781 
       
   782     /// It gives back the const iterator pointed to the first element.
       
   783     ConstIterator begin() const {
       
   784       return ConstIterator(*map, ItemIt(*graph));
       
   785     }
       
   786 
       
   787     /// It gives back the const iterator pointed to the first invalid element.
       
   788     ConstIterator end() const {
       
   789       return ConstIterator(*map, ItemIt(INVALID));
       
   790     }
       
   791             
       
   792   public:
       
   793     // STL  compatibility typedefs.
       
   794     typedef Value value_type;
       
   795     typedef ConstIterator const_iterator;
       
   796     typedef ConstReference const_reference;
       
   797     typedef ConstPointer const_pointer;
       
   798     typedef int difference_type;
       
   799 
       
   800   };
       
   801 
       
   802   template <typename _Map>
       
   803   class IterableMapExtender : public _Map {
       
   804   public:
       
   805 
       
   806     typedef _Map Parent;
       
   807     typedef Parent Map;
       
   808     typedef typename Map::Graph Graph;
       
   809     typedef typename Map::Key Item;
       
   810     typedef typename Map::Value Value;
       
   811 
       
   812     typedef MapSet<Graph, Item, Map> MapSet;
       
   813 
       
   814     IterableMapExtender() : Parent() {}
       
   815 
       
   816     IterableMapExtender(const Graph& graph) : Parent(graph) {}
       
   817 
       
   818     IterableMapExtender(const Graph& graph, const Value& value) 
       
   819       : Parent(graph, value) {}
       
   820 
       
   821     MapSet mapSet() { 
       
   822       return MapSet(*Parent::getGraph(), *this); 
       
   823     }
       
   824 
       
   825     typedef ConstMapSet<Graph, Item, Map> ConstMapSet;
       
   826 
       
   827     ConstMapSet mapSet() const { 
       
   828       return ConstMapSet(*Parent::getGraph(), *this); 
       
   829     }
       
   830 
       
   831     typedef MapConstKeySet<Graph, Item> ConstKeySet;
       
   832  
       
   833     ConstKeySet keySet() const {
       
   834       return ConstKeySet(*Parent::getGraph());
       
   835     }
       
   836 
       
   837     typedef MapValueSet<Graph, Item, Map> ValueSet;
       
   838  
       
   839     ValueSet valueSet() {
       
   840       return ValueSet(*Parent::getGraph(), *this);
       
   841     }
       
   842 
       
   843     typedef MapConstValueSet<Graph, Item, Map> ConstValueSet;
       
   844  
       
   845     ConstValueSet valueSet() const {
       
   846       return ConstValueSet(*Parent::getGraph(), *this);
       
   847     }
       
   848 
       
   849   };
       
   850 
   688   /// @}
   851   /// @}
   689 
   852 
   690 }
   853 }
   691 
   854 
   692 #endif
   855 #endif