COIN-OR::LEMON - Graph Library

Changeset 844:9bf990cb066d in lemon-0.x for src/hugo/map_iterator.h


Ignore:
Timestamp:
09/13/04 22:05:13 (20 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1143
Message:

Bug fix in the symmetric maps.
Faster map initialization.
Iterators and Containers STL compatible.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/hugo/map_iterator.h

    r830 r844  
    22#ifndef MAP_ITERATOR_H
    33#define MAP_ITERATOR_H
     4
     5#include <iterator>
    46
    57#include <hugo/extended_pair.h>
     
    2325
    2426  public:
     27
    2528    /// The key type of the iterator.
    2629    typedef typename Map::KeyType KeyType;
     
    8083    friend class MapConstIterator<Map>;
    8184
    82   public:
     85
     86  public:
     87
     88    /// The iterator base class.
     89    typedef MapIteratorBase<Map> Base;
    8390
    8491    /// The key type of the iterator.
     
    102109   
    103110  public:
     111
     112    /// The value type of the iterator.
     113    typedef extended_pair<KeyType, const KeyType&,
     114      ValueType, const ValueType&> PairValueType;
    104115
    105116    /// The reference type of the iterator.
     
    112123    /// Constructor to initalize the iterators returned
    113124    /// by the begin() and end().
    114     MapIterator(Map& pmap, const KeyIt& pit)
    115       : MapIteratorBase<Map>(pit), map(&pmap) {}
     125    MapIterator(Map& pmap, const KeyIt& pit) : Base(pit), map(&pmap) {}
    116126
    117127    /// Dereference operator for the iterator.
    118128    PairReferenceType operator*() {
    119       return PairReferenceType(it, (*map)[it]);
     129      return PairReferenceType(Base::it, (*map)[Base::it]);
    120130    }
    121131
     
    133143    /// Arrow operator for the iterator.
    134144    PairPointerType operator->() {
    135       return PairPointerType(it, ((*map)[it]));
     145      return PairPointerType(Base::it, ((*map)[Base::it]));
    136146    }
    137147       
    138148    /// The pre increment operator of the iterator.
    139149    MapIterator& operator++() {
    140       increment();
     150      Base::increment();
    141151      return *this;
    142152    }
     
    144154    /// The post increment operator of the iterator.
    145155    MapIterator operator++(int) {
    146       MapIterator tmp(it);
    147       increment();
     156      MapIterator tmp(*this);
     157      Base::increment();
    148158      return tmp;
    149159    }
     
    151161  private:
    152162    Map* map;
     163
     164  public:
     165    // STL  compatibility typedefs.
     166    typedef std::forward_iterator_tag iterator_category;
     167    typedef int difference_type;
     168    typedef PairValueType value_type;
     169    typedef PairReferenceType reference;
     170    typedef PairPointerType pointer;
    153171  };
    154172
     
    160178   
    161179  public:
     180
     181    /// The iterator base class.
     182    typedef MapIteratorBase<Map> Base;
    162183
    163184    /// The key type of the iterator.
     
    188209    ///  by the begin() and end().
    189210    MapConstIterator(const Map& pmap, const KeyIt& pit)
    190       : MapIteratorBase<Map>(pit), map(&pmap) {}
     211      : Base(pit), map(&pmap) {}
    191212
    192213    /// Constructor to create const iterator from a non const.
    193214    MapConstIterator(const MapIterator<Map>& pit) {
    194       it = pit.it;
     215      Base::it = pit.Base::it;
    195216      map = pit.map;
    196217    }
     218
     219    /// The value type of the iterator.
     220    typedef extended_pair<KeyType, const KeyType&,
     221      ValueType, const ValueType&> PairValueType;
    197222
    198223    /// The reference type of map.
     
    202227    /// Dereference operator for the iterator.
    203228    PairReferenceType operator*() {
    204       return PairReferenceType(it, (*map)[it]);
     229      return PairReferenceType(Base::it, (*map)[Base::it]);
    205230    }
    206231
     
    218243    /// Arrow operator for the iterator.
    219244    PairPointerType operator->() {
    220       return PairPointerType(it, ((*map)[it]));
     245      return PairPointerType(Base::it, (*map)[Base::it]);
    221246    }
    222247
    223248    /// The pre increment operator of the iterator.
    224249    MapConstIterator& operator++() {
    225       increment();
     250      Base::increment();
    226251      return *this;
    227252    }
     
    229254    /// The post increment operator of the iterator.
    230255    MapConstIterator operator++(int) {
    231       MapConstIterator<Map> tmp(it);
    232       increment();
     256      MapConstIterator tmp(*this);
     257      Base::increment();
    233258      return tmp;
    234259    }
     
    236261  private:
    237262    const Map* map;
     263
     264  public:
     265    // STL  compatibility typedefs.
     266    typedef std::input_iterator_tag iterator_category;
     267    typedef int difference_type;
     268    typedef PairValueType value_type;
     269    typedef PairReferenceType reference;
     270    typedef PairPointerType pointer;
    238271  };
    239272
     
    246279  public:
    247280
     281    /// The iterator base class.
     282    typedef MapIteratorBase<Map> Base;
     283 
    248284    /// The key type of the iterator.
    249285    typedef typename Map::KeyType KeyType;
     
    257293
    258294    /// KeyIt initialized iterator.
    259     MapKeyIterator(const KeyIt& pit) : MapIteratorBase<Map>(pit) {}
     295    MapKeyIterator(const KeyIt& pit) : Base(pit) {}
    260296
    261297    /// The pre increment operator of the iterator.
    262298    MapKeyIterator& operator++() {
    263       increment();
     299      Base::increment();
    264300      return *this;
    265301    }
     
    268304    MapKeyIterator operator++(int) {
    269305      MapKeyIterator tmp(*this);
    270       increment();
     306      Base::increment();
    271307      return tmp;
    272308    }
     
    274310    /// The dereferencing operator of the iterator.
    275311    KeyType operator*() const {
    276       return static_cast<KeyType>(it);
    277     }
     312      return static_cast<KeyType>(Base::it);
     313    }
     314
     315  public:
     316    // STL  compatibility typedefs.
     317    typedef std::input_iterator_tag iterator_category;
     318    typedef int difference_type;
     319    typedef KeyType value_type;
     320    typedef const KeyType& reference;
     321    typedef const KeyType* pointer;
    278322  };
    279323
    280324  template <typename Map> class MapConstValueIterator;
    281325
     326  /** MapValueIterator creates an stl compatible iterator
     327   *  for the values.
     328   */
    282329  template <typename Map>
    283330  class MapValueIterator : public MapIteratorBase<Map> {
     
    286333
    287334  public:
     335
     336    /// The iterator base class.
     337    typedef MapIteratorBase<Map> Base;
    288338
    289339    /// The key type of the iterator.
     
    318368    /// Map and KeyIt initialized iterator.
    319369    MapValueIterator(Map& pmap, const KeyIt& pit)
    320       : MapIteratorBase<Map>(pit), map(&pmap) {}
     370      : Base(pit), map(&pmap) {}
    321371   
    322372
    323373    /// The pre increment operator of the iterator.
    324374    MapValueIterator& operator++() {
    325       increment();
     375      Base::increment();
    326376      return *this;
    327377    }
     
    330380    MapValueIterator operator++(int) {
    331381      MapValueIterator tmp(*this);
    332       increment();
     382      Base::increment();
    333383      return tmp;
    334384    }
     
    336386    /// The dereferencing operator of the iterator.
    337387    ReferenceType operator*() const {
    338       return (*map)[it];
     388      return (*map)[Base::it];
    339389    }
    340390
     
    344394    }
    345395
    346   };
    347 
     396  public:
     397    // STL  compatibility typedefs.
     398    typedef std::forward_iterator_tag iterator_category;
     399    typedef int difference_type;
     400    typedef ValueType value_type;
     401    typedef ReferenceType reference;
     402    typedef PointerType pointer;
     403  };
     404
     405  /** MapValueIterator creates an stl compatible iterator
     406   *  for the const values.
     407   */
    348408
    349409  template <typename Map>
     
    352412  public:
    353413
    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 
     414    /// The iterator base class.
     415    typedef MapIteratorBase<Map> Base;
     416
     417    /// The key type of the iterator.
     418    typedef typename Map::KeyType KeyType;
     419    /// The iterator to iterate on the keys.
     420    typedef typename Map::KeyIt KeyIt;
    359421
    360422    /// The value type of the iterator.
     
    383445    /// Constructor to create const iterator from a non const.
    384446    MapConstValueIterator(const MapValueIterator<Map>& pit) {
    385       it = pit.it;
     447      Base::it = pit.Base::it;
    386448      map = pit.map;
    387449    }
     
    389451    /// Map and KeyIt initialized iterator.
    390452    MapConstValueIterator(const Map& pmap, const KeyIt& pit)
    391       : MapIteratorBase<Map>(pit), map(&pmap) {}
     453      : Base(pit), map(&pmap) {}
    392454
    393455    /// The pre increment operator of the iterator.
    394456    MapConstValueIterator& operator++() {
    395       increment();
     457      Base::increment();
    396458      return *this;
    397459    }
     
    400462    MapConstValueIterator operator++(int) {
    401463      MapConstValueIterator tmp(*this);
    402       increment();
     464      Base::increment();
    403465      return tmp;
    404466    }
     
    406468    /// The dereferencing operator of the iterator.
    407469    ConstReferenceType operator*() const {
    408       return (*map)[it];
     470      return (*map)[Base::it];
    409471    }
    410472
     
    414476    }
    415477
     478  public:
     479    // STL  compatibility typedefs.
     480    typedef std::input_iterator_tag iterator_category;
     481    typedef int difference_type;
     482    typedef ValueType value_type;
     483    typedef ConstReferenceType reference;
     484    typedef ConstPointerType pointer;
    416485  };
    417486
     
    431500    /// The iterator to iterate on the keys.
    432501    typedef typename Map::KeyIt KeyIt;
     502
     503
     504    /// The value type of the iterator.
     505    typedef typename Map::ValueType ValueType;
     506    /// The reference type of the iterator.
     507    typedef typename Map::ReferenceType ReferenceType;
     508    /// The pointer type of the iterator.
     509    typedef typename Map::PointerType PointerType;
     510
     511    /// The const value type of the iterator.
     512    typedef typename Map::ConstValueType ConstValueType;
     513    /// The const reference type of the iterator.
     514    typedef typename Map::ConstReferenceType ConstReferenceType;
     515    /// The pointer type of the iterator.
     516    typedef typename Map::ConstPointerType ConstPointerType;
    433517
    434518    /// The map initialized const key set.
     
    447531      return ConstIterator(KeyIt(INVALID));
    448532    }
     533 
     534  public:
     535    // STL  compatibility typedefs.
     536    typedef ValueType value_type;
     537    typedef ConstIterator const_iterator;
     538    typedef ConstReferenceType const_reference;
     539    typedef ConstPointerType const_pointer;
     540    typedef int difference_type;
    449541  };
    450542
     
    465557    typedef typename Map::KeyIt KeyIt;
    466558
     559
     560    /// The value type of the iterator.
     561    typedef typename Map::ValueType ValueType;
     562    /// The reference type of the iterator.
     563    typedef typename Map::ReferenceType ReferenceType;
     564    /// The pointer type of the iterator.
     565    typedef typename Map::PointerType PointerType;
     566
     567    /// The const value type of the iterator.
     568    typedef typename Map::ConstValueType ConstValueType;
     569    /// The const reference type of the iterator.
     570    typedef typename Map::ConstReferenceType ConstReferenceType;
     571    /// The pointer type of the iterator.
     572    typedef typename Map::ConstPointerType ConstPointerType;
     573
    467574    /// The map initialized const value set.
    468575    MapConstValueSet(const Map& pmap) : map(&pmap) {}
     
    480587      return ConstIterator(*map, KeyIt(INVALID));
    481588    }
     589
     590  public:
     591    // STL  compatibility typedefs.
     592    typedef ValueType value_type;
     593    typedef ConstIterator const_iterator;
     594    typedef ConstReferenceType const_reference;
     595    typedef ConstPointerType const_pointer;
     596    typedef int difference_type;
    482597  };
    483598
     
    499614    typedef typename Map::KeyIt KeyIt;
    500615
     616
     617    /// The value type of the iterator.
     618    typedef typename Map::ValueType ValueType;
     619    /// The reference type of the iterator.
     620    typedef typename Map::ReferenceType ReferenceType;
     621    /// The pointer type of the iterator.
     622    typedef typename Map::PointerType PointerType;
     623
     624    /// The const value type of the iterator.
     625    typedef typename Map::ConstValueType ConstValueType;
     626    /// The const reference type of the iterator.
     627    typedef typename Map::ConstReferenceType ConstReferenceType;
     628    /// The pointer type of the iterator.
     629    typedef typename Map::ConstPointerType ConstPointerType;
     630
    501631    /// The map initialized value set.
    502632    MapValueSet(Map& pmap) : map(&pmap) {}
     
    528658    }
    529659           
     660  public:
     661    // STL  compatibility typedefs.
     662    typedef ValueType value_type;
     663    typedef Iterator iterator;
     664    typedef ConstIterator const_iterator;
     665    typedef ReferenceType reference;
     666    typedef ConstReferenceType const_reference;
     667    typedef PointerType pointer;
     668    typedef ConstPointerType const_pointer;
     669    typedef int difference_type;
     670
    530671  };
    531672
Note: See TracChangeset for help on using the changeset viewer.