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