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