src/hugo/map_iterator.h
author alpar
Mon, 13 Sep 2004 18:00:26 +0000
changeset 843 d56fad02dc55
parent 822 88226d9fe821
child 844 9bf990cb066d
permissions -rw-r--r--
Minor corrections. "make distclean" still doesn't work.
     1 // -*- c++ -*-
     2 #ifndef MAP_ITERATOR_H
     3 #define MAP_ITERATOR_H
     4 
     5 #include <hugo/extended_pair.h>
     6 
     7 ///\ingroup graphmaps
     8 ///\file
     9 ///\brief Iterators on the maps.
    10 
    11 namespace hugo {
    12 
    13   /// \addtogroup graphmaps
    14   /// @{
    15 
    16   /** The base class all of the map iterators.
    17    *  The class defines the typedefs of the iterators,
    18    *  simple step functions and equality operators.
    19    */ 
    20 
    21   template <typename Map>
    22   class MapIteratorBase {
    23 
    24   public:
    25     /// The key type of the iterator.
    26     typedef typename Map::KeyType KeyType;
    27     /// The iterator to iterate on the keys.
    28     typedef typename Map::KeyIt KeyIt;
    29 
    30     /// The value type of the iterator.
    31     typedef typename Map::ValueType ValueType;
    32     /// The reference type of the iterator.
    33     typedef typename Map::ReferenceType ReferenceType;
    34     /// The pointer type of the iterator.
    35     typedef typename Map::PointerType PointerType;
    36 
    37     /// The const value type of the iterator.
    38     typedef typename Map::ConstValueType ConstValueType;
    39     /// The const reference type of the iterator.
    40     typedef typename Map::ConstReferenceType ConstReferenceType;
    41     /// The pointer type of the iterator.
    42     typedef typename Map::ConstPointerType ConstPointerType;
    43 
    44   protected:
    45 
    46     KeyIt it;
    47 
    48     /// Default constructor.
    49     MapIteratorBase() {}
    50 
    51     /// KeyIt initialized MapIteratorBase constructor.
    52     MapIteratorBase(const KeyIt pit) : it(pit) {}
    53 
    54   public:
    55 
    56     /// Stepping forward in the map.   
    57     void increment() { 
    58       ++it; 
    59     }
    60 
    61     /// The equality operator of the map.
    62     bool operator==(const MapIteratorBase& pit) const {
    63       return pit.it == it;
    64     }
    65 	
    66     /// The not-equality operator of the map.
    67     bool operator!=(const MapIteratorBase& pit) const {
    68       return !(*this == pit);
    69     }
    70   };
    71 
    72   template <typename Map> class MapConstIterator;
    73 
    74   /** Compatible iterator with the stl maps' iterators.
    75    * It iterates on pairs of a key and a value.
    76    */
    77   template <typename Map>  
    78   class MapIterator : public MapIteratorBase<Map> {
    79 
    80     friend class MapConstIterator<Map>;
    81 
    82   public:
    83 
    84     /// The key type of the iterator.
    85     typedef typename Map::KeyType KeyType;
    86     /// The iterator to iterate on the keys.
    87     typedef typename Map::KeyIt KeyIt;
    88 
    89     /// The value type of the iterator.
    90     typedef typename Map::ValueType ValueType;
    91     /// The reference type of the iterator.
    92     typedef typename Map::ReferenceType ReferenceType;
    93     /// The pointer type of the iterator.
    94     typedef typename Map::PointerType PointerType;
    95 
    96     /// The const value type of the iterator.
    97     typedef typename Map::ConstValueType ConstValueType;
    98     /// The const reference type of the iterator.
    99     typedef typename Map::ConstReferenceType ConstReferenceType;
   100     /// The pointer type of the iterator.
   101     typedef typename Map::ConstPointerType ConstPointerType;
   102     
   103   public:
   104 
   105     /// The reference type of the iterator. 
   106     typedef extended_pair<const KeyType&, const KeyType&, 
   107       ReferenceType, ReferenceType> PairReferenceType;
   108 
   109     /// Default constructor. 
   110     MapIterator() {}
   111 
   112     /// Constructor to initalize the iterators returned 
   113     /// by the begin() and end().
   114     MapIterator(Map& pmap, const KeyIt& pit) 
   115       : MapIteratorBase<Map>(pit), map(&pmap) {}
   116 
   117     /// Dereference operator for the iterator.
   118     PairReferenceType operator*() {
   119       return PairReferenceType(it, (*map)[it]);
   120     }
   121 
   122     /// The pointer type of the iterator.
   123     class PairPointerType {
   124       friend class MapIterator;
   125     private:
   126       PairReferenceType data;
   127       PairPointerType(const KeyType& key, ReferenceType val) 
   128 	: data(key, val) {}
   129     public:
   130       PairReferenceType* operator->() {return &data;}
   131     };
   132 
   133     /// Arrow operator for the iterator.
   134     PairPointerType operator->() {
   135       return PairPointerType(it, ((*map)[it])); 
   136     }
   137 	
   138     /// The pre increment operator of the iterator.
   139     MapIterator& operator++() { 
   140       increment(); 
   141       return *this; 
   142     }
   143 
   144     /// The post increment operator of the iterator.
   145     MapIterator operator++(int) { 
   146       MapIterator tmp(it); 
   147       increment(); 
   148       return tmp; 
   149     }
   150 
   151   private:
   152     Map* map;
   153   };
   154 
   155   /** Compatible iterator with the stl maps' iterators.
   156    *  It iterates on pairs of a key and a value.
   157    */
   158   template <typename Map>
   159   class MapConstIterator : public MapIteratorBase<Map> {
   160     
   161   public:
   162 
   163     /// The key type of the iterator.
   164     typedef typename Map::KeyType KeyType;
   165     /// The iterator to iterate on the keys.
   166     typedef typename Map::KeyIt KeyIt;
   167 
   168     /// The value type of the iterator.
   169     typedef typename Map::ValueType ValueType;
   170     /// The reference type of the iterator.
   171     typedef typename Map::ReferenceType ReferenceType;
   172     /// The pointer type of the iterator.
   173     typedef typename Map::PointerType PointerType;
   174 
   175     /// The const value type of the iterator.
   176     typedef typename Map::ConstValueType ConstValueType;
   177     /// The const reference type of the iterator.
   178     typedef typename Map::ConstReferenceType ConstReferenceType;
   179     /// The pointer type of the iterator.
   180     typedef typename Map::ConstPointerType ConstPointerType;
   181 
   182   public:    
   183 
   184     /// Default constructor. 
   185     MapConstIterator() {}
   186 
   187     /// Constructor to initalize the the iterators returned
   188     ///  by the begin() and end().
   189     MapConstIterator(const Map& pmap, const KeyIt& pit) 
   190       : MapIteratorBase<Map>(pit), map(&pmap) {}
   191 
   192     /// Constructor to create const iterator from a non const.
   193     MapConstIterator(const MapIterator<Map>& pit) {
   194       it = pit.it;
   195       map = pit.map;
   196     }
   197 
   198     /// The reference type of map.
   199     typedef extended_pair<const KeyType&, const KeyType&, 
   200       ConstReferenceType, ConstReferenceType> PairReferenceType;
   201 
   202     /// Dereference operator for the iterator.
   203     PairReferenceType operator*() {
   204       return PairReferenceType(it, (*map)[it]);
   205     }
   206 
   207     /// The pointer type of the iterator.
   208     class PairPointerType {
   209       friend class MapConstIterator;
   210     private:
   211       PairReferenceType data;
   212       PairPointerType(const KeyType& key, ConstReferenceType val) 
   213 	: data(key, val) {}
   214     public:
   215       PairReferenceType* operator->() {return &data;}
   216     };
   217 
   218     /// Arrow operator for the iterator.
   219     PairPointerType operator->() {
   220       return PairPointerType(it, ((*map)[it])); 
   221     }
   222 
   223     /// The pre increment operator of the iterator.
   224     MapConstIterator& operator++() { 
   225       increment(); 
   226       return *this; 
   227     }
   228 
   229     /// The post increment operator of the iterator.
   230     MapConstIterator operator++(int) { 
   231       MapConstIterator<Map> tmp(it); 
   232       increment(); 
   233       return tmp; 
   234     }
   235 
   236   private:
   237     const Map* map;
   238   };
   239 
   240   /** The class makes the KeyIt to an stl compatible iterator
   241    *  with dereferencing operator.
   242    */
   243   template <typename Map>
   244   class MapKeyIterator : public MapIteratorBase<Map> {
   245 
   246   public:
   247 
   248     /// The key type of the iterator.
   249     typedef typename Map::KeyType KeyType;
   250     /// The iterator to iterate on the keys.
   251     typedef typename Map::KeyIt KeyIt;
   252 
   253   public:
   254 
   255     /// Default constructor.
   256     MapKeyIterator() {}
   257 
   258     /// KeyIt initialized iterator.
   259     MapKeyIterator(const KeyIt& pit) : MapIteratorBase<Map>(pit) {}
   260 
   261     /// The pre increment operator of the iterator.
   262     MapKeyIterator& operator++() {
   263       increment(); 
   264       return *this; 
   265     }
   266 
   267     /// The post increment operator of the iterator.
   268     MapKeyIterator operator++(int) {
   269       MapKeyIterator tmp(*this);
   270       increment();
   271       return tmp;
   272     }
   273 
   274     /// The dereferencing operator of the iterator.
   275     KeyType operator*() const {
   276       return static_cast<KeyType>(it);
   277     }
   278   };
   279 
   280   template <typename Map> class MapConstValueIterator;
   281 
   282   template <typename Map>
   283   class MapValueIterator : public MapIteratorBase<Map> {
   284 
   285     friend class MapConstValueIterator<Map>;
   286 
   287   public:
   288 
   289     /// The key type of the iterator.
   290     typedef typename Map::KeyType KeyType;
   291     /// The iterator to iterate on the keys.
   292     typedef typename Map::KeyIt KeyIt;
   293 
   294 
   295     /// The value type of the iterator.
   296     typedef typename Map::ValueType ValueType;
   297     /// The reference type of the iterator.
   298     typedef typename Map::ReferenceType ReferenceType;
   299     /// The pointer type of the iterator.
   300     typedef typename Map::PointerType PointerType;
   301 
   302     /// The const value type of the iterator.
   303     typedef typename Map::ConstValueType ConstValueType;
   304     /// The const reference type of the iterator.
   305     typedef typename Map::ConstReferenceType ConstReferenceType;
   306     /// The pointer type of the iterator.
   307     typedef typename Map::ConstPointerType ConstPointerType;
   308 
   309   private:
   310 
   311     Map* map;
   312 
   313   public:
   314 
   315     /// Default constructor.
   316     MapValueIterator() {}
   317 
   318     /// Map and KeyIt initialized iterator.
   319     MapValueIterator(Map& pmap, const KeyIt& pit) 
   320       : MapIteratorBase<Map>(pit), map(&pmap) {}
   321     
   322 
   323     /// The pre increment operator of the iterator.
   324     MapValueIterator& operator++() {
   325       increment(); 
   326       return *this; 
   327     }
   328 
   329     /// The post increment operator of the iterator.
   330     MapValueIterator operator++(int) {
   331       MapValueIterator tmp(*this);
   332       increment();
   333       return tmp;
   334     }
   335 
   336     /// The dereferencing operator of the iterator.
   337     ReferenceType operator*() const {
   338       return (*map)[it];
   339     }
   340 
   341     /// The arrow operator of the iterator.
   342     PointerType operator->() const {
   343       return &(operator*());
   344     }
   345 
   346   };
   347 
   348 
   349   template <typename Map>
   350   class MapConstValueIterator : public MapIteratorBase<Map> {
   351 
   352   public:
   353 
   354     /// The key type of the iterator.
   355     typedef typename Map::KeyType KeyType;
   356     /// The iterator to iterate on the keys.
   357     typedef typename Map::KeyIt KeyIt;
   358 
   359 
   360     /// The value type of the iterator.
   361     typedef typename Map::ValueType ValueType;
   362     /// The reference type of the iterator.
   363     typedef typename Map::ReferenceType ReferenceType;
   364     /// The pointer type of the iterator.
   365     typedef typename Map::PointerType PointerType;
   366 
   367     /// The const value type of the iterator.
   368     typedef typename Map::ConstValueType ConstValueType;
   369     /// The const reference type of the iterator.
   370     typedef typename Map::ConstReferenceType ConstReferenceType;
   371     /// The pointer type of the iterator.
   372     typedef typename Map::ConstPointerType ConstPointerType;
   373 
   374   private:
   375 
   376     const Map* map;
   377 
   378   public:
   379 
   380     /// Default constructor.
   381     MapConstValueIterator() {}
   382 
   383     /// Constructor to create const iterator from a non const.
   384     MapConstValueIterator(const MapValueIterator<Map>& pit) {
   385       it = pit.it;
   386       map = pit.map;
   387     }
   388 
   389     /// Map and KeyIt initialized iterator.
   390     MapConstValueIterator(const Map& pmap, const KeyIt& pit) 
   391       : MapIteratorBase<Map>(pit), map(&pmap) {}
   392 
   393     /// The pre increment operator of the iterator.
   394     MapConstValueIterator& operator++() {
   395       increment(); 
   396       return *this; 
   397     }
   398 
   399     /// The post increment operator of the iterator.
   400     MapConstValueIterator operator++(int) {
   401       MapConstValueIterator tmp(*this);
   402       increment();
   403       return tmp;
   404     }
   405 
   406     /// The dereferencing operator of the iterator.
   407     ConstReferenceType operator*() const {
   408       return (*map)[it];
   409     }
   410 
   411     /// The arrow operator of the iterator.
   412     ConstPointerType operator->() const {
   413       return &(operator*());
   414     }
   415 
   416   };
   417 
   418 
   419   /** This class makes from a map an iteratable set
   420    *  which contains all the keys of the map.
   421    */
   422   template <typename Map>
   423   class MapConstKeySet {
   424 
   425     const Map* map;
   426 
   427   public:
   428 
   429     /// The key type of the iterator.
   430     typedef typename Map::KeyType KeyType;
   431     /// The iterator to iterate on the keys.
   432     typedef typename Map::KeyIt KeyIt;
   433 
   434     /// The map initialized const key set.
   435     MapConstKeySet(const Map& pmap) : map(&pmap) {}
   436 
   437     /// The const iterator of the set.
   438     typedef MapKeyIterator<Map> ConstIterator;
   439 
   440     /// It gives back the const iterator pointed to the first element.
   441     ConstIterator begin() const {
   442       return ConstIterator(KeyIt(*map->getGraph()));
   443     }
   444             
   445     /// It gives back the const iterator pointed to the first ivalid element.
   446     ConstIterator end() const {
   447       return ConstIterator(KeyIt(INVALID));
   448     }
   449   };
   450 
   451   /** This class makes from a map an iteratable set
   452    *  which contains all the values of the map.
   453    *  The values cannot be modified.
   454    */
   455   template <typename Map>
   456   class MapConstValueSet {
   457 
   458     const Map* map;
   459 
   460   public:
   461 
   462     /// The key type of the iterator.
   463     typedef typename Map::KeyType KeyType;
   464     /// The iterator to iterate on the keys.
   465     typedef typename Map::KeyIt KeyIt;
   466 
   467     /// The map initialized const value set.
   468     MapConstValueSet(const Map& pmap) : map(&pmap) {}
   469 
   470     /// The const iterator of the set.
   471     typedef MapConstValueIterator<Map> ConstIterator;
   472 
   473     /// It gives back the const iterator pointed to the first element.
   474     ConstIterator begin() const {
   475       return ConstIterator(*map, KeyIt(*map->getGraph()));
   476     }
   477 
   478     /// It gives back the const iterator pointed to the first invalid element.
   479     ConstIterator end() const {
   480       return ConstIterator(*map, KeyIt(INVALID));
   481     }
   482   };
   483 
   484 
   485   /** This class makes from a map an iteratable set
   486    *  which contains all the values of the map.
   487    *  The values can be modified.
   488    */
   489   template <typename Map>
   490   class MapValueSet {
   491 
   492     Map* map;
   493 
   494   public:
   495 
   496     /// The key type of the iterator.
   497     typedef typename Map::KeyType KeyType;
   498     /// The iterator to iterate on the keys.
   499     typedef typename Map::KeyIt KeyIt;
   500 
   501     /// The map initialized value set.
   502     MapValueSet(Map& pmap) : map(&pmap) {}
   503 
   504     /// The const iterator of the set.
   505     typedef MapConstValueIterator<Map> ConstIterator;
   506 
   507     /// It gives back the const iterator pointed to the first element.
   508     ConstIterator begin() const {
   509       return ConstIterator(*map, KeyIt(*map->getGraph()));
   510     }
   511 
   512     /// It gives back the const iterator pointed to the first invalid element.
   513     ConstIterator end() const {
   514       return ConstIterator(*map, KeyIt(INVALID));
   515     }
   516 
   517     /// The iterator of the set.
   518     typedef MapValueIterator<Map> Iterator;
   519 
   520     /// It gives back the iterator pointed to the first element.
   521     Iterator begin() {
   522       return Iterator(*map, KeyIt(*map->getGraph()));
   523     }
   524 
   525     /// It gives back the iterator pointed to the first invalid element.
   526     Iterator end() {
   527       return Iterator(*map, KeyIt(INVALID));
   528     }
   529             
   530   };
   531 
   532   /// @}
   533 
   534 }
   535 
   536 #endif