lemon/iterable_maps.h
author klao
Thu, 13 Apr 2006 17:57:03 +0000
changeset 2046 66d160810c0a
parent 1993 2115143eceea
child 2112 f27dfd29c5c0
permissions -rw-r--r--
more explicit :)
alpar@1677
     1
/* -*- C++ -*-
alpar@1677
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1677
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1677
     8
 *
alpar@1677
     9
 * Permission to use, modify and distribute this software is granted
alpar@1677
    10
 * provided that this copyright notice appears in all copies. For
alpar@1677
    11
 * precise terms see the accompanying LICENSE file.
alpar@1677
    12
 *
alpar@1677
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1677
    14
 * express or implied, and with no claim as to its suitability for any
alpar@1677
    15
 * purpose.
alpar@1677
    16
 *
alpar@1677
    17
 */
alpar@1677
    18
deba@1993
    19
#include <lemon/bits/traits.h>
deba@1993
    20
#include <lemon/bits/invalid.h>
deba@1931
    21
deba@1990
    22
#include <lemon/bits/default_map.h>
deba@2031
    23
#include <lemon/bits/map_extender.h>
deba@1990
    24
alpar@1677
    25
#include <vector>
deba@1931
    26
#include <map>
deba@1931
    27
deba@1931
    28
#include <iterator>
alpar@1677
    29
#include <limits>
alpar@1677
    30
alpar@1677
    31
///\ingroup maps
alpar@1677
    32
///\file
alpar@1677
    33
///\brief Maps that makes it possible to iterate through the keys having
alpar@1677
    34
///a certain value
alpar@1677
    35
///
alpar@1677
    36
///
alpar@1677
    37
alpar@1677
    38
alpar@1677
    39
namespace lemon {
deba@1913
    40
deba@1931
    41
  ///\ingroup graph_maps
deba@1913
    42
  ///
deba@1913
    43
  /// \brief Dynamic iterable bool map.
deba@1913
    44
  ///
deba@1913
    45
  /// This class provides a special graph map type which can store
deba@1913
    46
  /// for each graph item(node, edge, etc.) a bool value. For both 
deba@1913
    47
  /// the true and the false it is possible to iterate on the keys which
deba@1913
    48
  /// mapped to the given value.
deba@1913
    49
  /// 
deba@1913
    50
  /// \param _Graph The graph type.
deba@1913
    51
  /// \param _Item One of the graph's item type, the key of the map.
deba@1913
    52
  template <typename _Graph, typename _Item>
deba@1990
    53
  class IterableBoolMap : protected DefaultMap<_Graph, _Item, int> {
deba@1913
    54
  private:
deba@1913
    55
    typedef _Graph Graph;
deba@1913
    56
    
deba@1931
    57
    typedef typename ItemSetTraits<Graph, _Item>::ItemIt KeyIt;
deba@1990
    58
    typedef DefaultMap<_Graph, _Item, int> Parent;
deba@1913
    59
    
deba@1931
    60
    std::vector<_Item> array;
deba@1913
    61
    int sep;
deba@1913
    62
deba@1913
    63
    const Graph& graph;
alpar@1677
    64
alpar@1677
    65
  public:
alpar@1805
    66
deba@1913
    67
    /// Indicates that the map if reference map.
deba@1913
    68
    typedef True ReferenceMapTag;
alpar@1805
    69
deba@1913
    70
    /// The key type
deba@1931
    71
    typedef _Item Key;
deba@1913
    72
    /// The value type
deba@1913
    73
    typedef bool Value;
deba@1913
    74
    /// The const reference type.    
deba@1913
    75
    typedef const Value& ConstReference;
deba@1913
    76
deba@1931
    77
  private:
deba@1931
    78
deba@1931
    79
    int position(const Key& key) const {
deba@1931
    80
      return Parent::operator[](key);
deba@1931
    81
    }
deba@1931
    82
deba@1931
    83
  public:
deba@1913
    84
deba@1913
    85
    /// \brief Refernce to the value of the map.
deba@1913
    86
    ///
deba@1913
    87
    /// This class is near to similar to the bool type. It can
deba@1913
    88
    /// be converted to bool and it has the same operators.
deba@1913
    89
    class Reference {
deba@1913
    90
      friend class IterableBoolMap;
deba@1913
    91
    private:
deba@1913
    92
      Reference(IterableBoolMap& map, const Key& key) 
deba@1913
    93
	: _key(key), _map(map) {} 
alpar@1677
    94
    public:
deba@1913
    95
deba@1913
    96
      Reference& operator=(const Reference& value) {
deba@1913
    97
	_map.set(_key, (bool)value);
deba@1913
    98
 	return *this;
deba@1913
    99
      }
deba@1913
   100
deba@1913
   101
      operator bool() const { 
deba@1913
   102
	return static_cast<const IterableBoolMap&>(_map)[_key]; 
deba@1913
   103
      }
deba@1913
   104
deba@1913
   105
      Reference& operator=(bool value) { 
deba@1913
   106
	_map.set(_key, value); 
deba@1913
   107
	return *this; 
deba@1913
   108
      }
deba@1913
   109
      Reference& operator&=(bool value) {
deba@1913
   110
	_map.set(_key, _map[_key] & value); 
deba@1913
   111
	return *this; 	
deba@1913
   112
      }
deba@1913
   113
      Reference& operator|=(bool value) {
deba@1913
   114
	_map.set(_key, _map[_key] | value); 
deba@1913
   115
	return *this; 	
deba@1913
   116
      }
deba@1913
   117
      Reference& operator^=(bool value) {
deba@1913
   118
	_map.set(_key, _map[_key] ^ value); 
deba@1913
   119
	return *this; 	
deba@1913
   120
      }
deba@1913
   121
    private:
deba@1913
   122
      Key _key;
deba@1913
   123
      IterableBoolMap& _map; 
alpar@1677
   124
    };
deba@1913
   125
    
deba@1913
   126
    /// \brief Constructor of the Map with a default value.
deba@1913
   127
    ///
deba@1913
   128
    /// Constructor of the Map with a default value.
deba@1913
   129
    IterableBoolMap(const Graph& _graph, bool def = false) 
deba@1913
   130
      : Parent(_graph), graph(_graph) {
deba@1931
   131
      for (KeyIt it(graph); it != INVALID; ++it) {
deba@1913
   132
        Parent::set(it, array.size());
deba@1913
   133
        array.push_back(it);
deba@1913
   134
      }
deba@1913
   135
      sep = (def ? array.size() : 0);
deba@1913
   136
    }
deba@1913
   137
deba@1913
   138
    /// \brief Const subscript operator of the map.
deba@1913
   139
    ///
deba@1913
   140
    /// Const subscript operator of the map.
deba@1931
   141
    bool operator[](const Key& key) const {
deba@1931
   142
      return position(key) < sep;
deba@1913
   143
    }
deba@1913
   144
deba@1913
   145
    /// \brief Subscript operator of the map.
deba@1913
   146
    ///
deba@1913
   147
    /// Subscript operator of the map.
deba@1931
   148
    Reference operator[](const Key& key) {
deba@1931
   149
      return Reference(*this, key);
deba@1913
   150
    }
deba@1913
   151
deba@1913
   152
    /// \brief Set operation of the map.
deba@1913
   153
    ///
deba@1913
   154
    /// Set operation of the map.
deba@1931
   155
    void set(const Key& key, bool value) {
deba@1931
   156
      int pos = position(key);
deba@1913
   157
      if (value) {
deba@1913
   158
        if (pos < sep) return;
deba@1931
   159
        Key tmp = array[sep];
deba@1931
   160
        array[sep] = key;
deba@1931
   161
        Parent::set(key, sep);
deba@1913
   162
        array[pos] = tmp;
deba@1913
   163
        Parent::set(tmp, pos); 
deba@1913
   164
        ++sep;
deba@1913
   165
      } else {
deba@1913
   166
        if (pos >= sep) return;
deba@1913
   167
        --sep;
deba@1931
   168
        Key tmp = array[sep];
deba@1931
   169
        array[sep] = key;
deba@1931
   170
        Parent::set(key, sep);
deba@1913
   171
        array[pos] = tmp;
deba@1913
   172
        Parent::set(tmp, pos);
deba@1913
   173
      }
deba@1913
   174
    }
deba@1913
   175
deba@1931
   176
    /// \brief Returns the number of the keys mapped to true.
deba@1913
   177
    ///
deba@1931
   178
    /// Returns the number of the keys mapped to true.
deba@1913
   179
    int trueNum() const {
deba@1913
   180
      return sep;
deba@1913
   181
    } 
deba@1913
   182
    
deba@1931
   183
    /// \brief Returns the number of the keys mapped to false.
deba@1913
   184
    ///
deba@1931
   185
    /// Returns the number of the keys mapped to false.
deba@1913
   186
    int falseNum() const {
deba@1913
   187
      return array.size() - sep;
deba@1913
   188
    }
deba@1913
   189
deba@1913
   190
    /// \brief Iterator for the keys mapped to true.
deba@1913
   191
    ///
deba@1913
   192
    /// Iterator for the keys mapped to true. It works
deba@1913
   193
    /// like a graph item iterator in the map, it can be converted
deba@1931
   194
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   195
    /// if the iterator leave the last valid key it will be equal to 
deba@1913
   196
    /// \c INVALID.
deba@1931
   197
    class TrueIt : public Key {
alpar@1677
   198
    public:
deba@1931
   199
      typedef Key Parent;
deba@1913
   200
      
deba@1913
   201
      /// \brief Creates an iterator.
deba@1913
   202
      ///
deba@1913
   203
      /// Creates an iterator. It iterates on the 
deba@1913
   204
      /// keys which mapped to true.
alpar@1953
   205
      /// \param _map The IterableIntMap
deba@1913
   206
      TrueIt(const IterableBoolMap& _map) 
deba@1913
   207
        : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID), 
deba@1913
   208
          map(&_map) {}
deba@1913
   209
deba@1913
   210
      /// \brief Invalid constructor \& conversion.
deba@1913
   211
      ///
deba@1931
   212
      /// This constructor initializes the key to be invalid.
deba@1913
   213
      /// \sa Invalid for more details.
deba@1913
   214
      TrueIt(Invalid) : Parent(INVALID), map(0) {}
deba@1913
   215
deba@1913
   216
      /// \brief Increment operator.
deba@1913
   217
      ///
deba@1913
   218
      /// Increment Operator.
deba@1913
   219
      TrueIt& operator++() {
deba@1913
   220
        int pos = map->position(*this);
deba@1913
   221
        Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID);
deba@1913
   222
        return *this;
deba@1913
   223
      }
deba@1913
   224
deba@1913
   225
      
deba@1913
   226
    private:
deba@1913
   227
      const IterableBoolMap* map;
deba@1913
   228
    };
deba@1913
   229
deba@1913
   230
    /// \brief Iterator for the keys mapped to false.
deba@1913
   231
    ///
deba@1913
   232
    /// Iterator for the keys mapped to false. It works
deba@1913
   233
    /// like a graph item iterator in the map, it can be converted
deba@1931
   234
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   235
    /// if the iterator leave the last valid key it will be equal to 
deba@1913
   236
    /// \c INVALID.
deba@1931
   237
    class FalseIt : public Key {
deba@1913
   238
    public:
deba@1931
   239
      typedef Key Parent;
deba@1913
   240
      
deba@1913
   241
      /// \brief Creates an iterator.
deba@1913
   242
      ///
deba@1913
   243
      /// Creates an iterator. It iterates on the 
deba@1913
   244
      /// keys which mapped to false.
alpar@1953
   245
      /// \param _map The IterableIntMap
deba@1913
   246
      FalseIt(const IterableBoolMap& _map) 
deba@1931
   247
        : Parent(_map.sep < (int)_map.array.size() ? 
deba@1931
   248
                 _map.array.back() : INVALID), map(&_map) {}
deba@1913
   249
deba@1913
   250
      /// \brief Invalid constructor \& conversion.
deba@1913
   251
      ///
deba@1931
   252
      /// This constructor initializes the key to be invalid.
deba@1913
   253
      /// \sa Invalid for more details.
deba@1913
   254
      FalseIt(Invalid) : Parent(INVALID), map(0) {}
deba@1913
   255
deba@1913
   256
      /// \brief Increment operator.
deba@1913
   257
      ///
deba@1913
   258
      /// Increment Operator.
deba@1913
   259
      FalseIt& operator++() {
deba@1913
   260
        int pos = map->position(*this);
deba@1913
   261
        Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID);
deba@1913
   262
        return *this;
deba@1913
   263
      }
deba@1913
   264
deba@1913
   265
    private:
deba@1913
   266
      const IterableBoolMap* map;
deba@1913
   267
    };
deba@1913
   268
deba@1931
   269
    /// \brief Iterator for the keys mapped to a given value.
deba@1931
   270
    ///
deba@1931
   271
    /// Iterator for the keys mapped to a given value. It works
deba@1931
   272
    /// like a graph item iterator in the map, it can be converted
deba@1931
   273
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   274
    /// if the iterator leave the last valid key it will be equal to 
deba@1931
   275
    /// \c INVALID.
deba@1931
   276
    class ItemIt : public Key {
deba@1931
   277
    public:
deba@1931
   278
      typedef Key Parent;
deba@1931
   279
      
deba@1931
   280
      /// \brief Creates an iterator.
deba@1931
   281
      ///
deba@1931
   282
      /// Creates an iterator. It iterates on the 
deba@1931
   283
      /// keys which mapped to false.
alpar@1953
   284
      /// \param _map The IterableIntMap
deba@1931
   285
      /// \param value Which elements should be iterated.
deba@1931
   286
      ItemIt(const IterableBoolMap& _map, bool value) 
deba@1931
   287
        : Parent(value ? (_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID) :
deba@1931
   288
                 (_map.sep < (int)_map.array.size() ? 
deba@1931
   289
                  _map.array.back() : INVALID)), map(&_map) {}
deba@1931
   290
deba@1931
   291
      /// \brief Invalid constructor \& conversion.
deba@1931
   292
      ///
deba@1931
   293
      /// This constructor initializes the key to be invalid.
deba@1931
   294
      /// \sa Invalid for more details.
deba@1931
   295
      ItemIt(Invalid) : Parent(INVALID), map(0) {}
deba@1931
   296
deba@1931
   297
      /// \brief Increment operator.
deba@1931
   298
      ///
deba@1931
   299
      /// Increment Operator.
deba@1931
   300
      ItemIt& operator++() {
deba@1931
   301
        int pos = map->position(*this);
deba@1931
   302
        int sep = pos >= map->sep ? map->sep : 0;
deba@1931
   303
        Parent::operator=(pos > sep ? map->array[pos - 1] : INVALID);
deba@1931
   304
        return *this;
deba@1931
   305
      }
deba@1931
   306
deba@1931
   307
    private:
deba@1931
   308
      const IterableBoolMap* map;
deba@1931
   309
    };
deba@1931
   310
deba@1913
   311
  protected:
deba@1913
   312
    
deba@1931
   313
    virtual void add(const Key& key) {
deba@1931
   314
      Parent::add(key);
deba@1931
   315
      Parent::set(key, array.size());
deba@1931
   316
      array.push_back(key);
deba@1913
   317
    }
deba@1913
   318
deba@1931
   319
    virtual void add(const std::vector<Key>& keys) {
deba@1931
   320
      Parent::add(keys);
deba@1931
   321
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1931
   322
        Parent::set(keys[i], array.size());
deba@1931
   323
        array.push_back(keys[i]);
deba@1913
   324
      }
deba@1913
   325
    }
deba@1913
   326
deba@1931
   327
    virtual void erase(const Key& key) {
deba@1931
   328
      int pos = position(key); 
deba@1913
   329
      if (pos < sep) {
deba@1913
   330
        --sep;
deba@1913
   331
        Parent::set(array[sep], pos);
deba@1913
   332
        array[pos] = array[sep];
deba@1913
   333
        Parent::set(array.back(), sep);
deba@1913
   334
        array[sep] = array.back();
deba@1913
   335
        array.pop_back();
deba@1913
   336
      } else {
deba@1913
   337
        Parent::set(array.back(), pos);
deba@1913
   338
        array[pos] = array.back();
deba@1913
   339
        array.pop_back();
deba@1913
   340
      }
deba@1931
   341
      Parent::erase(key);
deba@1913
   342
    }
deba@1913
   343
deba@1931
   344
    virtual void erase(const std::vector<Key>& keys) {
deba@1931
   345
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1931
   346
        int pos = position(keys[i]); 
deba@1913
   347
        if (pos < sep) {
deba@1913
   348
          --sep;
deba@1913
   349
          Parent::set(array[sep], pos);
deba@1913
   350
          array[pos] = array[sep];
deba@1913
   351
          Parent::set(array.back(), sep);
deba@1913
   352
          array[sep] = array.back();
deba@1913
   353
          array.pop_back();
deba@1913
   354
        } else {
deba@1913
   355
          Parent::set(array.back(), pos);
deba@1913
   356
          array[pos] = array.back();
deba@1913
   357
          array.pop_back();
deba@1913
   358
        }
deba@1913
   359
      }
deba@1931
   360
      Parent::erase(keys);
deba@1913
   361
    }    
deba@1913
   362
deba@1913
   363
    virtual void build() {
deba@1913
   364
      Parent::build();
deba@1931
   365
      for (KeyIt it(graph); it != INVALID; ++it) {
deba@1913
   366
        Parent::set(it, array.size());
deba@1913
   367
        array.push_back(it);
deba@1913
   368
      }
deba@1913
   369
      sep = 0;      
deba@1913
   370
    }
deba@1913
   371
deba@1913
   372
    virtual void clear() {
deba@1913
   373
      array.clear();
deba@1913
   374
      sep = 0;
deba@1913
   375
      Parent::clear();
deba@1913
   376
    }
deba@1913
   377
    
alpar@1677
   378
  };
alpar@1873
   379
  
deba@1752
   380
deba@1752
   381
  namespace _iterable_maps_bits {
deba@1752
   382
    template <typename Item>
deba@1752
   383
    struct IterableIntMapNode {
deba@1913
   384
      IterableIntMapNode() : value(-1) {}
deba@1810
   385
      IterableIntMapNode(int _value) : value(_value) {}
deba@1752
   386
      Item prev, next;
deba@1752
   387
      int value;
deba@1752
   388
    };
deba@1752
   389
  }
deba@1752
   390
deba@1931
   391
  ///\ingroup graph_maps
deba@1752
   392
  ///
deba@1752
   393
  /// \brief Dynamic iterable integer map.
deba@1752
   394
  ///
deba@1810
   395
  /// This class provides a special graph map type which can store
deba@1810
   396
  /// for each graph item(node, edge, etc.) an integer value. For each
deba@1810
   397
  /// non negative value it is possible to iterate on the keys which
deba@1810
   398
  /// mapped to the given value.
deba@1810
   399
  /// 
deba@1810
   400
  /// \param _Graph The graph type.
deba@1810
   401
  /// \param _Item One of the graph's item type, the key of the map.
deba@1752
   402
  template <typename _Graph, typename _Item>
deba@1990
   403
  class IterableIntMap 
deba@2031
   404
    : protected MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   405
                                       IterableIntMapNode<_Item> > >{
deba@1752
   406
  public:
deba@2031
   407
    typedef MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   408
                                   IterableIntMapNode<_Item> > > Parent;
deba@1752
   409
deba@1810
   410
    /// The key type
deba@1752
   411
    typedef _Item Key;
deba@1810
   412
    /// The value type
deba@1752
   413
    typedef int Value;
deba@1810
   414
    /// The graph type
deba@1752
   415
    typedef _Graph Graph;
deba@1752
   416
deba@1810
   417
    /// \brief Constructor of the Map.
deba@1810
   418
    ///
deba@1810
   419
    /// Constructor of the Map. It set all values -1.
deba@1810
   420
    explicit IterableIntMap(const Graph& graph) 
deba@1913
   421
      : Parent(graph) {}
deba@1810
   422
deba@1810
   423
    /// \brief Constructor of the Map with a given value.
deba@1810
   424
    ///
deba@1810
   425
    /// Constructor of the Map with a given value.
deba@1810
   426
    explicit IterableIntMap(const Graph& graph, int value) 
deba@1810
   427
      : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
deba@1810
   428
      if (value >= 0) {
deba@1810
   429
	for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@1810
   430
	  lace(it);
deba@1810
   431
	}
deba@1810
   432
      }
deba@1810
   433
    }
deba@1752
   434
deba@1752
   435
  private:
deba@1752
   436
    
deba@1752
   437
    void unlace(const Key& key) {
deba@1752
   438
      typename Parent::Value& node = Parent::operator[](key);
deba@1752
   439
      if (node.value < 0) return;
deba@1752
   440
      if (node.prev != INVALID) {
deba@1752
   441
	Parent::operator[](node.prev).next = node.next;	
deba@1752
   442
      } else {
deba@1752
   443
	first[node.value] = node.next;
deba@1752
   444
      }
deba@1752
   445
      if (node.next != INVALID) {
deba@1752
   446
	Parent::operator[](node.next).prev = node.prev;
deba@1752
   447
      }
deba@1752
   448
      while (!first.empty() && first.back() == INVALID) {
deba@1752
   449
	first.pop_back();
deba@1752
   450
      }
deba@1752
   451
    }
deba@1752
   452
deba@1752
   453
    void lace(const Key& key) {
deba@1752
   454
      typename Parent::Value& node = Parent::operator[](key);
deba@1752
   455
      if (node.value < 0) return;
deba@1752
   456
      if (node.value >= (int)first.size()) {
deba@1752
   457
	first.resize(node.value + 1, INVALID);
deba@1752
   458
      } 
deba@1752
   459
      node.prev = INVALID;
deba@1752
   460
      node.next = first[node.value];
deba@1752
   461
      if (node.next != INVALID) {
deba@1752
   462
	Parent::operator[](node.next).prev = key;	
deba@1752
   463
      }
deba@1752
   464
      first[node.value] = key;
deba@1752
   465
    }
deba@1752
   466
deba@1752
   467
  public:
deba@1752
   468
deba@1810
   469
    /// Indicates that the map if reference map.
deba@1752
   470
    typedef True ReferenceMapTag;
deba@1752
   471
deba@1810
   472
    /// \brief Refernce to the value of the map.
deba@1810
   473
    ///
deba@1810
   474
    /// This class is near to similar to the int type. It can
deba@1810
   475
    /// be converted to int and it has the same operators.
deba@1752
   476
    class Reference {
deba@1752
   477
      friend class IterableIntMap;
deba@1752
   478
    private:
deba@1752
   479
      Reference(IterableIntMap& map, const Key& key) 
deba@1752
   480
	: _key(key), _map(map) {} 
deba@1752
   481
    public:
deba@1752
   482
deba@1752
   483
      Reference& operator=(const Reference& value) {
deba@1752
   484
	_map.set(_key, (const int&)value);
deba@1752
   485
 	return *this;
deba@1752
   486
      }
deba@1752
   487
deba@1752
   488
      operator const int&() const { 
deba@1752
   489
	return static_cast<const IterableIntMap&>(_map)[_key]; 
deba@1752
   490
      }
deba@1752
   491
deba@1752
   492
      Reference& operator=(int value) { 
deba@1752
   493
	_map.set(_key, value); 
deba@1752
   494
	return *this; 
deba@1752
   495
      }
deba@1759
   496
      Reference& operator++() {
deba@1759
   497
	_map.set(_key, _map[_key] + 1); 
deba@1759
   498
	return *this; 	
deba@1759
   499
      }
deba@1759
   500
      int operator++(int) {
deba@1759
   501
	int value = _map[_key];
deba@1759
   502
	_map.set(_key, value + 1); 
deba@1759
   503
	return value; 	
deba@1759
   504
      }
deba@1759
   505
      Reference& operator--() {
deba@1759
   506
	_map.set(_key, _map[_key] - 1); 
deba@1759
   507
	return *this; 	
deba@1759
   508
      }
deba@1759
   509
      int operator--(int) {
deba@1759
   510
	int value = _map[_key];
deba@1759
   511
	_map.set(_key, value - 1); 
deba@1759
   512
	return value; 	
deba@1759
   513
      }
deba@1752
   514
      Reference& operator+=(int value) { 
deba@1752
   515
	_map.set(_key, _map[_key] + value); 
deba@1752
   516
	return *this;
deba@1752
   517
      }
deba@1752
   518
      Reference& operator-=(int value) { 
deba@1752
   519
	_map.set(_key, _map[_key] - value); 
deba@1752
   520
	return *this;
deba@1752
   521
      }
deba@1752
   522
      Reference& operator*=(int value) { 
deba@1752
   523
	_map.set(_key, _map[_key] * value); 
deba@1752
   524
	return *this;
deba@1752
   525
      }
deba@1752
   526
      Reference& operator/=(int value) { 
deba@1752
   527
	_map.set(_key, _map[_key] / value); 
deba@1752
   528
	return *this;
deba@1752
   529
      }
deba@1752
   530
      Reference& operator%=(int value) { 
deba@1752
   531
	_map.set(_key, _map[_key] % value); 
deba@1752
   532
	return *this;
deba@1752
   533
      }
deba@1752
   534
      Reference& operator&=(int value) { 
deba@1752
   535
	_map.set(_key, _map[_key] & value); 
deba@1752
   536
	return *this;
deba@1752
   537
      }
deba@1752
   538
      Reference& operator|=(int value) { 
deba@1752
   539
	_map.set(_key, _map[_key] | value); 
deba@1752
   540
	return *this;
deba@1752
   541
      }
deba@1752
   542
      Reference& operator^=(int value) { 
deba@1752
   543
	_map.set(_key, _map[_key] ^ value); 
deba@1752
   544
	return *this;
deba@1752
   545
      }
deba@1752
   546
      Reference& operator<<=(int value) { 
deba@1752
   547
	_map.set(_key, _map[_key] << value); 
deba@1752
   548
	return *this;
deba@1752
   549
      }
deba@1752
   550
      Reference& operator>>=(int value) { 
deba@1752
   551
	_map.set(_key, _map[_key] >> value); 
deba@1752
   552
	return *this;
deba@1752
   553
      }
deba@1752
   554
deba@1752
   555
    private:
deba@1752
   556
      Key _key;
deba@1752
   557
      IterableIntMap& _map; 
deba@1752
   558
    };
deba@1810
   559
deba@1810
   560
    /// The const reference type.    
deba@1752
   561
    typedef const Value& ConstReference;
deba@1752
   562
deba@1810
   563
    /// \brief Gives back the maximal value plus one.
deba@1810
   564
    ///
deba@1810
   565
    /// Gives back the maximal value plus one.
deba@1931
   566
    unsigned int size() const {
deba@1931
   567
      return first.size();
deba@1752
   568
    }
deba@1752
   569
    
deba@1810
   570
    /// \brief Set operation of the map.
deba@1810
   571
    ///
deba@1810
   572
    /// Set operation of the map.
deba@1752
   573
    void set(const Key& key, const Value& value) {
deba@1752
   574
      unlace(key);
deba@1752
   575
      Parent::operator[](key).value = value;
deba@1752
   576
      lace(key);
deba@1752
   577
    }
deba@1752
   578
deba@1810
   579
    /// \brief Const subscript operator of the map.
deba@1810
   580
    ///
deba@1810
   581
    /// Const subscript operator of the map.
deba@1752
   582
    const Value& operator[](const Key& key) const {
deba@1752
   583
      return Parent::operator[](key).value;
deba@1752
   584
    }
deba@1752
   585
deba@1810
   586
    /// \brief Subscript operator of the map.
deba@1810
   587
    ///
deba@1810
   588
    /// Subscript operator of the map.
deba@1752
   589
    Reference operator[](const Key& key) {
deba@1752
   590
      return Reference(*this, key);
deba@1752
   591
    }
deba@1752
   592
deba@1810
   593
    /// \brief Iterator for the keys with the same value.
deba@1810
   594
    ///
deba@1810
   595
    /// Iterator for the keys with the same value. It works
deba@1810
   596
    /// like a graph item iterator in the map, it can be converted
deba@1810
   597
    /// the item type of the map, incremented with \c ++ operator, and
deba@1810
   598
    /// if the iterator leave the last valid item it will be equal to 
deba@1810
   599
    /// \c INVALID.
deba@1752
   600
    class ItemIt : public _Item {
deba@1752
   601
    public:
deba@1752
   602
      typedef _Item Parent;
deba@1752
   603
deba@1810
   604
      /// \brief Invalid constructor \& conversion.
deba@1810
   605
      ///
deba@1810
   606
      /// This constructor initializes the item to be invalid.
deba@1810
   607
      /// \sa Invalid for more details.
deba@1752
   608
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@1752
   609
deba@1810
   610
      /// \brief Creates an iterator with a value.
deba@1810
   611
      ///
deba@1810
   612
      /// Creates an iterator with a value. It iterates on the 
deba@1810
   613
      /// keys which have the given value.
deba@1810
   614
      /// \param map The IterableIntMap
deba@1810
   615
      /// \param value The value
deba@1752
   616
      ItemIt(const IterableIntMap& map, int value) : _map(&map) {
deba@1752
   617
	if (value < 0 || value >= (int)_map->first.size()) {	  
deba@1752
   618
	  Parent::operator=(INVALID);
deba@1752
   619
	} else {
deba@1752
   620
	  Parent::operator=(_map->first[value]);
deba@1752
   621
	}
deba@1752
   622
      } 
deba@1752
   623
deba@1810
   624
      /// \brief Increment operator.
deba@1810
   625
      ///
deba@1810
   626
      /// Increment Operator.
deba@1752
   627
      ItemIt& operator++() {
deba@1752
   628
	Parent::operator=(_map->IterableIntMap::Parent::
deba@1752
   629
			  operator[](static_cast<Parent&>(*this)).next);
deba@1752
   630
	return *this;
deba@1752
   631
      }
deba@1752
   632
deba@1752
   633
deba@1752
   634
    private:
deba@1752
   635
      const IterableIntMap* _map;
deba@1752
   636
    };
deba@1752
   637
deba@1752
   638
  protected:
deba@1752
   639
    
deba@1752
   640
    virtual void erase(const Key& key) {
deba@1752
   641
      unlace(key);
deba@1752
   642
      Parent::erase(key);
deba@1752
   643
    }
deba@1752
   644
deba@1913
   645
    virtual void erase(const std::vector<Key>& keys) {
deba@1931
   646
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1913
   647
        unlace(keys[i]);
deba@1913
   648
      }
deba@1913
   649
      Parent::erase(keys);
deba@1913
   650
    }
deba@1913
   651
deba@1752
   652
    virtual void clear() {
deba@1752
   653
      first.clear();
deba@1752
   654
      Parent::clear();
deba@1752
   655
    }
deba@1752
   656
deba@1752
   657
  private:
deba@1752
   658
    std::vector<_Item> first;
deba@1752
   659
  };
deba@1752
   660
deba@1931
   661
  namespace _iterable_maps_bits {
deba@1931
   662
    template <typename Item, typename Value>
deba@1931
   663
    struct IterableValueMapNode {
deba@1931
   664
      IterableValueMapNode(Value _value = Value()) : value(_value) {}
deba@1931
   665
      Item prev, next;
deba@1931
   666
      Value value;
deba@1931
   667
    };
deba@1931
   668
  }
deba@1931
   669
deba@1931
   670
  ///\ingroup graph_maps
deba@1931
   671
  ///
deba@1931
   672
  /// \brief Dynamic iterable map for comparable values.
deba@1931
   673
  ///
deba@1931
   674
  /// This class provides a special graph map type which can store
deba@1931
   675
  /// for each graph item(node, edge, etc.) a value. For each
deba@1931
   676
  /// value it is possible to iterate on the keys which mapped to the 
deba@1931
   677
  /// given value. The type stores for each value a linked list with
deba@1931
   678
  /// the items which mapped to the value, and the values are stored
deba@1931
   679
  /// in balanced binary tree. The values of the map can be accessed
deba@1931
   680
  /// with stl compatible forward iterator.
deba@1931
   681
  ///
deba@1931
   682
  /// This type is not reference map so it cannot be modified with
deba@1931
   683
  /// the subscription operator.
deba@1931
   684
  ///
deba@1931
   685
  /// \see InvertableMap
deba@1931
   686
  /// 
deba@1931
   687
  /// \param _Graph The graph type.
deba@1931
   688
  /// \param _Item One of the graph's item type, the key of the map.
deba@1931
   689
  /// \param _Value Any comparable value type.
deba@1931
   690
  template <typename _Graph, typename _Item, typename _Value>
deba@1990
   691
  class IterableValueMap 
deba@2031
   692
    : protected MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   693
                                       IterableValueMapNode<_Item, _Value> > >{
deba@1931
   694
  public:
deba@2031
   695
    typedef MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   696
                                   IterableValueMapNode<_Item, _Value> > >
deba@2031
   697
    Parent;
deba@1931
   698
deba@1931
   699
    /// The key type
deba@1931
   700
    typedef _Item Key;
deba@1931
   701
    /// The value type
deba@1931
   702
    typedef _Value Value;
deba@1931
   703
    /// The graph type
deba@1931
   704
    typedef _Graph Graph;
deba@1931
   705
deba@1990
   706
  public:
deba@1990
   707
deba@1931
   708
    /// \brief Constructor of the Map with a given value.
deba@1931
   709
    ///
deba@1931
   710
    /// Constructor of the Map with a given value.
deba@1931
   711
    explicit IterableValueMap(const Graph& graph, 
deba@1931
   712
                              const Value& value = Value()) 
deba@1990
   713
      : Parent(graph, _iterable_maps_bits::
deba@1990
   714
               IterableValueMapNode<_Item, _Value>(value)) {
deba@2031
   715
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@1931
   716
        lace(it);
deba@1931
   717
      }
deba@1931
   718
    }
deba@1931
   719
deba@1990
   720
  protected:
deba@1931
   721
    
deba@1931
   722
    void unlace(const Key& key) {
deba@1931
   723
      typename Parent::Value& node = Parent::operator[](key);
deba@1931
   724
      if (node.prev != INVALID) {
deba@1931
   725
	Parent::operator[](node.prev).next = node.next;	
deba@1931
   726
      } else {
deba@1931
   727
        if (node.next != INVALID) {
deba@1931
   728
          first[node.value] = node.next;
deba@1931
   729
        } else {
deba@1931
   730
          first.erase(node.value);
deba@1931
   731
        }
deba@1931
   732
      }
deba@1931
   733
      if (node.next != INVALID) {
deba@1931
   734
	Parent::operator[](node.next).prev = node.prev;
deba@1931
   735
      }
deba@1931
   736
    }
deba@1931
   737
deba@1931
   738
    void lace(const Key& key) {
deba@1931
   739
      typename Parent::Value& node = Parent::operator[](key);
deba@1931
   740
      typename std::map<Value, Key>::iterator it = first.find(node.value);
deba@1931
   741
      if (it == first.end()) {
deba@1931
   742
        node.prev = node.next = INVALID;
deba@1931
   743
        if (node.next != INVALID) {
deba@1931
   744
          Parent::operator[](node.next).prev = key;	
deba@1931
   745
        }
deba@1931
   746
        first.insert(make_pair(node.value, key));
deba@1931
   747
      } else {
deba@1931
   748
        node.prev = INVALID;
deba@1931
   749
        node.next = it->second;
deba@1931
   750
        if (node.next != INVALID) {
deba@1931
   751
          Parent::operator[](node.next).prev = key;	
deba@1931
   752
        }
deba@1931
   753
        it->second = key;
deba@1931
   754
      }
deba@1931
   755
    }
deba@1931
   756
deba@1931
   757
  public:
deba@1931
   758
deba@1931
   759
    /// \brief Forward iterator for values.
deba@1931
   760
    ///
deba@1931
   761
    /// This iterator is an stl compatible forward
deba@1931
   762
    /// iterator on the values of the map. The values can
deba@1931
   763
    /// be accessed in the [beginValue, endValue) range.
deba@1931
   764
    ///
deba@1931
   765
    class ValueIterator 
deba@1931
   766
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@1931
   767
      friend class IterableValueMap;
deba@1931
   768
    private:
deba@1931
   769
      ValueIterator(typename std::map<Value, Key>::const_iterator _it) 
deba@1931
   770
        : it(_it) {}
deba@1931
   771
    public:
deba@1931
   772
      
deba@1931
   773
      ValueIterator() {}
deba@1931
   774
deba@1931
   775
      ValueIterator& operator++() { ++it; return *this; }
deba@1931
   776
      ValueIterator operator++(int) { 
deba@1931
   777
        ValueIterator tmp(*this); 
deba@1931
   778
        operator++();
deba@1931
   779
        return tmp; 
deba@1931
   780
      }
deba@1931
   781
deba@1931
   782
      const Value& operator*() const { return it->first; }
deba@1931
   783
      const Value* operator->() const { return &(it->first); }
deba@1931
   784
deba@1931
   785
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@1931
   786
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@1931
   787
      
deba@1931
   788
    private:
deba@1931
   789
      typename std::map<Value, Key>::const_iterator it;
deba@1931
   790
    };
deba@1931
   791
deba@1931
   792
    /// \brief Returns an iterator to the first value.
deba@1931
   793
    ///
deba@1931
   794
    /// Returns an stl compatible iterator to the 
deba@1931
   795
    /// first value of the map. The values of the
deba@1931
   796
    /// map can be accessed in the [beginValue, endValue)
deba@1931
   797
    /// range.
deba@1931
   798
    ValueIterator beginValue() const {
deba@1931
   799
      return ValueIterator(first.begin());
deba@1931
   800
    }
deba@1931
   801
deba@1931
   802
    /// \brief Returns an iterator after the last value.
deba@1931
   803
    ///
deba@1931
   804
    /// Returns an stl compatible iterator after the 
deba@1931
   805
    /// last value of the map. The values of the
deba@1931
   806
    /// map can be accessed in the [beginValue, endValue)
deba@1931
   807
    /// range.
deba@1931
   808
    ValueIterator endValue() const {
deba@1931
   809
      return ValueIterator(first.end());
deba@1931
   810
    }
deba@1931
   811
deba@1931
   812
    /// \brief Set operation of the map.
deba@1931
   813
    ///
deba@1931
   814
    /// Set operation of the map.
deba@1931
   815
    void set(const Key& key, const Value& value) {
deba@1931
   816
      unlace(key);
deba@1931
   817
      Parent::operator[](key).value = value;
deba@1931
   818
      lace(key);
deba@1931
   819
    }
deba@1931
   820
deba@1931
   821
    /// \brief Const subscript operator of the map.
deba@1931
   822
    ///
deba@1931
   823
    /// Const subscript operator of the map.
deba@1931
   824
    const Value& operator[](const Key& key) const {
deba@1931
   825
      return Parent::operator[](key).value;
deba@1931
   826
    }
deba@1931
   827
deba@1931
   828
    /// \brief Iterator for the keys with the same value.
deba@1931
   829
    ///
deba@1931
   830
    /// Iterator for the keys with the same value. It works
deba@1931
   831
    /// like a graph item iterator in the map, it can be converted
deba@1931
   832
    /// the item type of the map, incremented with \c ++ operator, and
deba@1931
   833
    /// if the iterator leave the last valid item it will be equal to 
deba@1931
   834
    /// \c INVALID.
deba@1931
   835
    class ItemIt : public _Item {
deba@1931
   836
    public:
deba@1931
   837
      typedef _Item Parent;
deba@1931
   838
deba@1931
   839
      /// \brief Invalid constructor \& conversion.
deba@1931
   840
      ///
deba@1931
   841
      /// This constructor initializes the item to be invalid.
deba@1931
   842
      /// \sa Invalid for more details.
deba@1931
   843
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@1931
   844
deba@1931
   845
      /// \brief Creates an iterator with a value.
deba@1931
   846
      ///
deba@1931
   847
      /// Creates an iterator with a value. It iterates on the 
deba@1931
   848
      /// keys which have the given value.
deba@1931
   849
      /// \param map The IterableValueMap
deba@1931
   850
      /// \param value The value
deba@1931
   851
      ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
deba@1931
   852
        typename std::map<Value, Key>::const_iterator it = 
deba@1931
   853
          map.first.find(value); 
deba@1931
   854
	if (it == map.first.end()) {	  
deba@1931
   855
	  Parent::operator=(INVALID);
deba@1931
   856
	} else {
deba@1931
   857
	  Parent::operator=(it->second);
deba@1931
   858
	}
deba@1931
   859
      } 
deba@1931
   860
deba@1931
   861
      /// \brief Increment operator.
deba@1931
   862
      ///
deba@1931
   863
      /// Increment Operator.
deba@1931
   864
      ItemIt& operator++() {
deba@1931
   865
	Parent::operator=(_map->IterableValueMap::Parent::
deba@1931
   866
			  operator[](static_cast<Parent&>(*this)).next);
deba@1931
   867
	return *this;
deba@1931
   868
      }
deba@1931
   869
deba@1931
   870
deba@1931
   871
    private:
deba@1931
   872
      const IterableValueMap* _map;
deba@1931
   873
    };
deba@1931
   874
deba@1931
   875
  protected:
deba@1931
   876
    
deba@1990
   877
    virtual void add(const Key& key) {
deba@1990
   878
      Parent::add(key);
deba@1990
   879
      unlace(key);
deba@1990
   880
    }
deba@1990
   881
deba@1990
   882
    virtual void add(const std::vector<Key>& keys) {
deba@1990
   883
      Parent::add(keys);
deba@1990
   884
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1990
   885
        lace(keys[i]);
deba@1990
   886
      }
deba@1990
   887
    }
deba@1990
   888
deba@1931
   889
    virtual void erase(const Key& key) {
deba@1931
   890
      unlace(key);
deba@1931
   891
      Parent::erase(key);
deba@1931
   892
    }
deba@1931
   893
deba@1931
   894
    virtual void erase(const std::vector<Key>& keys) {
deba@1931
   895
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1931
   896
        unlace(keys[i]);
deba@1931
   897
      }
deba@1931
   898
      Parent::erase(keys);
deba@1931
   899
    }
deba@1931
   900
deba@1990
   901
    virtual void build() {
deba@1990
   902
      Parent::build();
deba@2031
   903
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@1990
   904
        lace(it);
deba@1990
   905
      }
deba@1990
   906
    }
deba@1990
   907
deba@1931
   908
    virtual void clear() {
deba@1931
   909
      first.clear();
deba@1931
   910
      Parent::clear();
deba@1931
   911
    }
deba@1931
   912
deba@1931
   913
  private:
deba@1931
   914
    std::map<Value, Key> first;
deba@1931
   915
  };
deba@1931
   916
alpar@1677
   917
  /// @}
alpar@1677
   918
}