src/hugo/map_iterator.h
changeset 830 89dfa3bece81
parent 822 88226d9fe821
child 844 9bf990cb066d
     1.1 --- a/src/hugo/map_iterator.h	Thu Sep 09 09:40:45 2004 +0000
     1.2 +++ b/src/hugo/map_iterator.h	Sun Sep 12 19:32:21 2004 +0000
     1.3 @@ -4,21 +4,79 @@
     1.4  
     1.5  #include <hugo/extended_pair.h>
     1.6  
     1.7 +///\ingroup graphmaps
     1.8 +///\file
     1.9 +///\brief Iterators on the maps.
    1.10 +
    1.11  namespace hugo {
    1.12  
    1.13 +  /// \addtogroup graphmaps
    1.14 +  /// @{
    1.15 +
    1.16 +  /** The base class all of the map iterators.
    1.17 +   *  The class defines the typedefs of the iterators,
    1.18 +   *  simple step functions and equality operators.
    1.19 +   */ 
    1.20  
    1.21    template <typename Map>
    1.22 -  class MapIterator;
    1.23 +  class MapIteratorBase {
    1.24  
    1.25 -  template <typename Map>
    1.26 -  class MapConstIterator;
    1.27 +  public:
    1.28 +    /// The key type of the iterator.
    1.29 +    typedef typename Map::KeyType KeyType;
    1.30 +    /// The iterator to iterate on the keys.
    1.31 +    typedef typename Map::KeyIt KeyIt;
    1.32 +
    1.33 +    /// The value type of the iterator.
    1.34 +    typedef typename Map::ValueType ValueType;
    1.35 +    /// The reference type of the iterator.
    1.36 +    typedef typename Map::ReferenceType ReferenceType;
    1.37 +    /// The pointer type of the iterator.
    1.38 +    typedef typename Map::PointerType PointerType;
    1.39 +
    1.40 +    /// The const value type of the iterator.
    1.41 +    typedef typename Map::ConstValueType ConstValueType;
    1.42 +    /// The const reference type of the iterator.
    1.43 +    typedef typename Map::ConstReferenceType ConstReferenceType;
    1.44 +    /// The pointer type of the iterator.
    1.45 +    typedef typename Map::ConstPointerType ConstPointerType;
    1.46 +
    1.47 +  protected:
    1.48 +
    1.49 +    KeyIt it;
    1.50 +
    1.51 +    /// Default constructor.
    1.52 +    MapIteratorBase() {}
    1.53 +
    1.54 +    /// KeyIt initialized MapIteratorBase constructor.
    1.55 +    MapIteratorBase(const KeyIt pit) : it(pit) {}
    1.56 +
    1.57 +  public:
    1.58 +
    1.59 +    /// Stepping forward in the map.   
    1.60 +    void increment() { 
    1.61 +      ++it; 
    1.62 +    }
    1.63 +
    1.64 +    /// The equality operator of the map.
    1.65 +    bool operator==(const MapIteratorBase& pit) const {
    1.66 +      return pit.it == it;
    1.67 +    }
    1.68 +	
    1.69 +    /// The not-equality operator of the map.
    1.70 +    bool operator!=(const MapIteratorBase& pit) const {
    1.71 +      return !(*this == pit);
    1.72 +    }
    1.73 +  };
    1.74 +
    1.75 +  template <typename Map> class MapConstIterator;
    1.76  
    1.77    /** Compatible iterator with the stl maps' iterators.
    1.78 -   *  It iterates on pairs of a key and a value.
    1.79 +   * It iterates on pairs of a key and a value.
    1.80     */
    1.81    template <typename Map>  
    1.82 -  class MapIterator {
    1.83 -    //    friend class Map;
    1.84 +  class MapIterator : public MapIteratorBase<Map> {
    1.85 +
    1.86      friend class MapConstIterator<Map>;
    1.87  
    1.88    public:
    1.89 @@ -44,27 +102,24 @@
    1.90      
    1.91    public:
    1.92  
    1.93 -    /** Constructor to initalize the the iterators returned
    1.94 -     *  by the begin() and end().
    1.95 -     */
    1.96 -    MapIterator (Map& pmap, const KeyIt& pit) 
    1.97 -      : map(&pmap), it(pit) {}
    1.98 -
    1.99 -  public:
   1.100 -
   1.101 -    /** Default constructor. 
   1.102 -     */
   1.103 -    MapIterator() {}
   1.104 -
   1.105 +    /// The reference type of the iterator. 
   1.106      typedef extended_pair<const KeyType&, const KeyType&, 
   1.107        ReferenceType, ReferenceType> PairReferenceType;
   1.108  
   1.109 -    /** Dereference operator for map.
   1.110 -     */	 
   1.111 +    /// Default constructor. 
   1.112 +    MapIterator() {}
   1.113 +
   1.114 +    /// Constructor to initalize the iterators returned 
   1.115 +    /// by the begin() and end().
   1.116 +    MapIterator(Map& pmap, const KeyIt& pit) 
   1.117 +      : MapIteratorBase<Map>(pit), map(&pmap) {}
   1.118 +
   1.119 +    /// Dereference operator for the iterator.
   1.120      PairReferenceType operator*() {
   1.121        return PairReferenceType(it, (*map)[it]);
   1.122      }
   1.123  
   1.124 +    /// The pointer type of the iterator.
   1.125      class PairPointerType {
   1.126        friend class MapIterator;
   1.127      private:
   1.128 @@ -75,64 +130,34 @@
   1.129        PairReferenceType* operator->() {return &data;}
   1.130      };
   1.131  
   1.132 -    /** Arrow operator for map.
   1.133 -     */	 
   1.134 +    /// Arrow operator for the iterator.
   1.135      PairPointerType operator->() {
   1.136        return PairPointerType(it, ((*map)[it])); 
   1.137      }
   1.138 -
   1.139 -    /** The pre increment operator of the map.
   1.140 -     */
   1.141 +	
   1.142 +    /// The pre increment operator of the iterator.
   1.143      MapIterator& operator++() { 
   1.144 -      ++it; 
   1.145 +      increment(); 
   1.146        return *this; 
   1.147      }
   1.148  
   1.149 -    /** The post increment operator of the map.
   1.150 -     */
   1.151 +    /// The post increment operator of the iterator.
   1.152      MapIterator operator++(int) { 
   1.153        MapIterator tmp(it); 
   1.154 -      ++it; 
   1.155 +      increment(); 
   1.156        return tmp; 
   1.157      }
   1.158  
   1.159 -    /** The equality operator of the map.
   1.160 -     */
   1.161 -    bool operator==(const MapIterator& p_it) const {
   1.162 -      return p_it.it == it;
   1.163 -    }
   1.164 -	
   1.165 -    /** The not-equality operator of the map.
   1.166 -     */
   1.167 -    bool operator!=(const MapIterator& p_it) const {
   1.168 -      return !(*this == p_it);
   1.169 -    }
   1.170 -
   1.171 -    /** The equality operator of the map.
   1.172 -     */
   1.173 -    bool operator==(const MapConstIterator<Map>& p_it) const {
   1.174 -      return p_it.it == it;
   1.175 -    }
   1.176 -	
   1.177 -    /** The not-equality operator of the map.
   1.178 -     */
   1.179 -    bool operator!=(const MapConstIterator<Map>& p_it) const {
   1.180 -      return !(*this == p_it);
   1.181 -    }
   1.182 -	
   1.183    private:
   1.184      Map* map;
   1.185 -    KeyIt it;
   1.186    };
   1.187  
   1.188    /** Compatible iterator with the stl maps' iterators.
   1.189     *  It iterates on pairs of a key and a value.
   1.190     */
   1.191    template <typename Map>
   1.192 -  class MapConstIterator {
   1.193 -    // friend class Map;
   1.194 -    friend class MapIterator<Map>;
   1.195 -
   1.196 +  class MapConstIterator : public MapIteratorBase<Map> {
   1.197 +    
   1.198    public:
   1.199  
   1.200      /// The key type of the iterator.
   1.201 @@ -156,28 +181,30 @@
   1.202  
   1.203    public:    
   1.204  
   1.205 -    /** Constructor to initalize the the iterators returned
   1.206 -     *  by the begin() and end().
   1.207 -     */
   1.208 -
   1.209 -    MapConstIterator (const Map& pmap, const KeyIt& pit) 
   1.210 -      : map(&pmap), it(pit) {}
   1.211 -
   1.212 -  public:
   1.213 -
   1.214 -    /** Default constructor. 
   1.215 -     */
   1.216 +    /// Default constructor. 
   1.217      MapConstIterator() {}
   1.218  
   1.219 +    /// Constructor to initalize the the iterators returned
   1.220 +    ///  by the begin() and end().
   1.221 +    MapConstIterator(const Map& pmap, const KeyIt& pit) 
   1.222 +      : MapIteratorBase<Map>(pit), map(&pmap) {}
   1.223 +
   1.224 +    /// Constructor to create const iterator from a non const.
   1.225 +    MapConstIterator(const MapIterator<Map>& pit) {
   1.226 +      it = pit.it;
   1.227 +      map = pit.map;
   1.228 +    }
   1.229 +
   1.230 +    /// The reference type of map.
   1.231      typedef extended_pair<const KeyType&, const KeyType&, 
   1.232        ConstReferenceType, ConstReferenceType> PairReferenceType;
   1.233  
   1.234 -    /** Dereference operator for map.
   1.235 -     */	 
   1.236 +    /// Dereference operator for the iterator.
   1.237      PairReferenceType operator*() {
   1.238        return PairReferenceType(it, (*map)[it]);
   1.239      }
   1.240  
   1.241 +    /// The pointer type of the iterator.
   1.242      class PairPointerType {
   1.243        friend class MapConstIterator;
   1.244      private:
   1.245 @@ -188,56 +215,322 @@
   1.246        PairReferenceType* operator->() {return &data;}
   1.247      };
   1.248  
   1.249 -    /** Arrow operator for map.
   1.250 -     */	 
   1.251 +    /// Arrow operator for the iterator.
   1.252      PairPointerType operator->() {
   1.253        return PairPointerType(it, ((*map)[it])); 
   1.254      }
   1.255  
   1.256 -    /** The pre increment operator of the map.
   1.257 -     */
   1.258 +    /// The pre increment operator of the iterator.
   1.259      MapConstIterator& operator++() { 
   1.260 -      ++it; 
   1.261 +      increment(); 
   1.262        return *this; 
   1.263      }
   1.264  
   1.265 -    /** The post increment operator of the map.
   1.266 -     */
   1.267 +    /// The post increment operator of the iterator.
   1.268      MapConstIterator operator++(int) { 
   1.269        MapConstIterator<Map> tmp(it); 
   1.270 -      ++it; 
   1.271 +      increment(); 
   1.272        return tmp; 
   1.273      }
   1.274  
   1.275 -    /** The equality operator of the map.
   1.276 -     */
   1.277 -    bool operator==(const MapIterator<Map>& p_it) const {
   1.278 -      return p_it.it == it;
   1.279 -    }
   1.280 -	
   1.281 -    /** The not-equality operator of the map.
   1.282 -     */
   1.283 -    bool operator!=(const MapIterator<Map>& p_it) const {
   1.284 -      return !(*this == p_it);
   1.285 +  private:
   1.286 +    const Map* map;
   1.287 +  };
   1.288 +
   1.289 +  /** The class makes the KeyIt to an stl compatible iterator
   1.290 +   *  with dereferencing operator.
   1.291 +   */
   1.292 +  template <typename Map>
   1.293 +  class MapKeyIterator : public MapIteratorBase<Map> {
   1.294 +
   1.295 +  public:
   1.296 +
   1.297 +    /// The key type of the iterator.
   1.298 +    typedef typename Map::KeyType KeyType;
   1.299 +    /// The iterator to iterate on the keys.
   1.300 +    typedef typename Map::KeyIt KeyIt;
   1.301 +
   1.302 +  public:
   1.303 +
   1.304 +    /// Default constructor.
   1.305 +    MapKeyIterator() {}
   1.306 +
   1.307 +    /// KeyIt initialized iterator.
   1.308 +    MapKeyIterator(const KeyIt& pit) : MapIteratorBase<Map>(pit) {}
   1.309 +
   1.310 +    /// The pre increment operator of the iterator.
   1.311 +    MapKeyIterator& operator++() {
   1.312 +      increment(); 
   1.313 +      return *this; 
   1.314      }
   1.315  
   1.316 -    /** The equality operator of the map.
   1.317 -     */
   1.318 -    bool operator==(const MapConstIterator& p_it) const {
   1.319 -      return p_it.it == it;
   1.320 +    /// The post increment operator of the iterator.
   1.321 +    MapKeyIterator operator++(int) {
   1.322 +      MapKeyIterator tmp(*this);
   1.323 +      increment();
   1.324 +      return tmp;
   1.325      }
   1.326 -	
   1.327 -    /** The not-equality operator of the map.
   1.328 -     */
   1.329 -    bool operator!=(const MapConstIterator& p_it) const {
   1.330 -      return !(*this == p_it);
   1.331 +
   1.332 +    /// The dereferencing operator of the iterator.
   1.333 +    KeyType operator*() const {
   1.334 +      return static_cast<KeyType>(it);
   1.335      }
   1.336 -	
   1.337 +  };
   1.338 +
   1.339 +  template <typename Map> class MapConstValueIterator;
   1.340 +
   1.341 +  template <typename Map>
   1.342 +  class MapValueIterator : public MapIteratorBase<Map> {
   1.343 +
   1.344 +    friend class MapConstValueIterator<Map>;
   1.345 +
   1.346 +  public:
   1.347 +
   1.348 +    /// The key type of the iterator.
   1.349 +    typedef typename Map::KeyType KeyType;
   1.350 +    /// The iterator to iterate on the keys.
   1.351 +    typedef typename Map::KeyIt KeyIt;
   1.352 +
   1.353 +
   1.354 +    /// The value type of the iterator.
   1.355 +    typedef typename Map::ValueType ValueType;
   1.356 +    /// The reference type of the iterator.
   1.357 +    typedef typename Map::ReferenceType ReferenceType;
   1.358 +    /// The pointer type of the iterator.
   1.359 +    typedef typename Map::PointerType PointerType;
   1.360 +
   1.361 +    /// The const value type of the iterator.
   1.362 +    typedef typename Map::ConstValueType ConstValueType;
   1.363 +    /// The const reference type of the iterator.
   1.364 +    typedef typename Map::ConstReferenceType ConstReferenceType;
   1.365 +    /// The pointer type of the iterator.
   1.366 +    typedef typename Map::ConstPointerType ConstPointerType;
   1.367 +
   1.368    private:
   1.369 +
   1.370 +    Map* map;
   1.371 +
   1.372 +  public:
   1.373 +
   1.374 +    /// Default constructor.
   1.375 +    MapValueIterator() {}
   1.376 +
   1.377 +    /// Map and KeyIt initialized iterator.
   1.378 +    MapValueIterator(Map& pmap, const KeyIt& pit) 
   1.379 +      : MapIteratorBase<Map>(pit), map(&pmap) {}
   1.380 +    
   1.381 +
   1.382 +    /// The pre increment operator of the iterator.
   1.383 +    MapValueIterator& operator++() {
   1.384 +      increment(); 
   1.385 +      return *this; 
   1.386 +    }
   1.387 +
   1.388 +    /// The post increment operator of the iterator.
   1.389 +    MapValueIterator operator++(int) {
   1.390 +      MapValueIterator tmp(*this);
   1.391 +      increment();
   1.392 +      return tmp;
   1.393 +    }
   1.394 +
   1.395 +    /// The dereferencing operator of the iterator.
   1.396 +    ReferenceType operator*() const {
   1.397 +      return (*map)[it];
   1.398 +    }
   1.399 +
   1.400 +    /// The arrow operator of the iterator.
   1.401 +    PointerType operator->() const {
   1.402 +      return &(operator*());
   1.403 +    }
   1.404 +
   1.405 +  };
   1.406 +
   1.407 +
   1.408 +  template <typename Map>
   1.409 +  class MapConstValueIterator : public MapIteratorBase<Map> {
   1.410 +
   1.411 +  public:
   1.412 +
   1.413 +    /// The key type of the iterator.
   1.414 +    typedef typename Map::KeyType KeyType;
   1.415 +    /// The iterator to iterate on the keys.
   1.416 +    typedef typename Map::KeyIt KeyIt;
   1.417 +
   1.418 +
   1.419 +    /// The value type of the iterator.
   1.420 +    typedef typename Map::ValueType ValueType;
   1.421 +    /// The reference type of the iterator.
   1.422 +    typedef typename Map::ReferenceType ReferenceType;
   1.423 +    /// The pointer type of the iterator.
   1.424 +    typedef typename Map::PointerType PointerType;
   1.425 +
   1.426 +    /// The const value type of the iterator.
   1.427 +    typedef typename Map::ConstValueType ConstValueType;
   1.428 +    /// The const reference type of the iterator.
   1.429 +    typedef typename Map::ConstReferenceType ConstReferenceType;
   1.430 +    /// The pointer type of the iterator.
   1.431 +    typedef typename Map::ConstPointerType ConstPointerType;
   1.432 +
   1.433 +  private:
   1.434 +
   1.435      const Map* map;
   1.436 -    KeyIt it;
   1.437 +
   1.438 +  public:
   1.439 +
   1.440 +    /// Default constructor.
   1.441 +    MapConstValueIterator() {}
   1.442 +
   1.443 +    /// Constructor to create const iterator from a non const.
   1.444 +    MapConstValueIterator(const MapValueIterator<Map>& pit) {
   1.445 +      it = pit.it;
   1.446 +      map = pit.map;
   1.447 +    }
   1.448 +
   1.449 +    /// Map and KeyIt initialized iterator.
   1.450 +    MapConstValueIterator(const Map& pmap, const KeyIt& pit) 
   1.451 +      : MapIteratorBase<Map>(pit), map(&pmap) {}
   1.452 +
   1.453 +    /// The pre increment operator of the iterator.
   1.454 +    MapConstValueIterator& operator++() {
   1.455 +      increment(); 
   1.456 +      return *this; 
   1.457 +    }
   1.458 +
   1.459 +    /// The post increment operator of the iterator.
   1.460 +    MapConstValueIterator operator++(int) {
   1.461 +      MapConstValueIterator tmp(*this);
   1.462 +      increment();
   1.463 +      return tmp;
   1.464 +    }
   1.465 +
   1.466 +    /// The dereferencing operator of the iterator.
   1.467 +    ConstReferenceType operator*() const {
   1.468 +      return (*map)[it];
   1.469 +    }
   1.470 +
   1.471 +    /// The arrow operator of the iterator.
   1.472 +    ConstPointerType operator->() const {
   1.473 +      return &(operator*());
   1.474 +    }
   1.475 +
   1.476    };
   1.477  
   1.478 +
   1.479 +  /** This class makes from a map an iteratable set
   1.480 +   *  which contains all the keys of the map.
   1.481 +   */
   1.482 +  template <typename Map>
   1.483 +  class MapConstKeySet {
   1.484 +
   1.485 +    const Map* map;
   1.486 +
   1.487 +  public:
   1.488 +
   1.489 +    /// The key type of the iterator.
   1.490 +    typedef typename Map::KeyType KeyType;
   1.491 +    /// The iterator to iterate on the keys.
   1.492 +    typedef typename Map::KeyIt KeyIt;
   1.493 +
   1.494 +    /// The map initialized const key set.
   1.495 +    MapConstKeySet(const Map& pmap) : map(&pmap) {}
   1.496 +
   1.497 +    /// The const iterator of the set.
   1.498 +    typedef MapKeyIterator<Map> ConstIterator;
   1.499 +
   1.500 +    /// It gives back the const iterator pointed to the first element.
   1.501 +    ConstIterator begin() const {
   1.502 +      return ConstIterator(KeyIt(*map->getGraph()));
   1.503 +    }
   1.504 +            
   1.505 +    /// It gives back the const iterator pointed to the first ivalid element.
   1.506 +    ConstIterator end() const {
   1.507 +      return ConstIterator(KeyIt(INVALID));
   1.508 +    }
   1.509 +  };
   1.510 +
   1.511 +  /** This class makes from a map an iteratable set
   1.512 +   *  which contains all the values of the map.
   1.513 +   *  The values cannot be modified.
   1.514 +   */
   1.515 +  template <typename Map>
   1.516 +  class MapConstValueSet {
   1.517 +
   1.518 +    const Map* map;
   1.519 +
   1.520 +  public:
   1.521 +
   1.522 +    /// The key type of the iterator.
   1.523 +    typedef typename Map::KeyType KeyType;
   1.524 +    /// The iterator to iterate on the keys.
   1.525 +    typedef typename Map::KeyIt KeyIt;
   1.526 +
   1.527 +    /// The map initialized const value set.
   1.528 +    MapConstValueSet(const Map& pmap) : map(&pmap) {}
   1.529 +
   1.530 +    /// The const iterator of the set.
   1.531 +    typedef MapConstValueIterator<Map> ConstIterator;
   1.532 +
   1.533 +    /// It gives back the const iterator pointed to the first element.
   1.534 +    ConstIterator begin() const {
   1.535 +      return ConstIterator(*map, KeyIt(*map->getGraph()));
   1.536 +    }
   1.537 +
   1.538 +    /// It gives back the const iterator pointed to the first invalid element.
   1.539 +    ConstIterator end() const {
   1.540 +      return ConstIterator(*map, KeyIt(INVALID));
   1.541 +    }
   1.542 +  };
   1.543 +
   1.544 +
   1.545 +  /** This class makes from a map an iteratable set
   1.546 +   *  which contains all the values of the map.
   1.547 +   *  The values can be modified.
   1.548 +   */
   1.549 +  template <typename Map>
   1.550 +  class MapValueSet {
   1.551 +
   1.552 +    Map* map;
   1.553 +
   1.554 +  public:
   1.555 +
   1.556 +    /// The key type of the iterator.
   1.557 +    typedef typename Map::KeyType KeyType;
   1.558 +    /// The iterator to iterate on the keys.
   1.559 +    typedef typename Map::KeyIt KeyIt;
   1.560 +
   1.561 +    /// The map initialized value set.
   1.562 +    MapValueSet(Map& pmap) : map(&pmap) {}
   1.563 +
   1.564 +    /// The const iterator of the set.
   1.565 +    typedef MapConstValueIterator<Map> ConstIterator;
   1.566 +
   1.567 +    /// It gives back the const iterator pointed to the first element.
   1.568 +    ConstIterator begin() const {
   1.569 +      return ConstIterator(*map, KeyIt(*map->getGraph()));
   1.570 +    }
   1.571 +
   1.572 +    /// It gives back the const iterator pointed to the first invalid element.
   1.573 +    ConstIterator end() const {
   1.574 +      return ConstIterator(*map, KeyIt(INVALID));
   1.575 +    }
   1.576 +
   1.577 +    /// The iterator of the set.
   1.578 +    typedef MapValueIterator<Map> Iterator;
   1.579 +
   1.580 +    /// It gives back the iterator pointed to the first element.
   1.581 +    Iterator begin() {
   1.582 +      return Iterator(*map, KeyIt(*map->getGraph()));
   1.583 +    }
   1.584 +
   1.585 +    /// It gives back the iterator pointed to the first invalid element.
   1.586 +    Iterator end() {
   1.587 +      return Iterator(*map, KeyIt(INVALID));
   1.588 +    }
   1.589 +            
   1.590 +  };
   1.591 +
   1.592 +  /// @}
   1.593 +
   1.594  }
   1.595  
   1.596  #endif