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