lemon/iterable_maps.h
author deba
Fri, 03 Mar 2006 12:35:32 +0000
changeset 1996 5dc13b93f8b4
parent 1990 15fb7a4ea6be
child 2031 080d51024ac5
permissions -rw-r--r--
Some documentation arrangement modification
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@1990
    23
alpar@1677
    24
#include <vector>
deba@1931
    25
#include <map>
deba@1931
    26
deba@1931
    27
#include <iterator>
alpar@1677
    28
#include <limits>
alpar@1677
    29
alpar@1677
    30
///\ingroup maps
alpar@1677
    31
///\file
alpar@1677
    32
///\brief Maps that makes it possible to iterate through the keys having
alpar@1677
    33
///a certain value
alpar@1677
    34
///
alpar@1677
    35
///
alpar@1677
    36
alpar@1677
    37
alpar@1677
    38
namespace lemon {
deba@1913
    39
deba@1931
    40
  ///\ingroup graph_maps
deba@1913
    41
  ///
deba@1913
    42
  /// \brief Dynamic iterable bool map.
deba@1913
    43
  ///
deba@1913
    44
  /// This class provides a special graph map type which can store
deba@1913
    45
  /// for each graph item(node, edge, etc.) a bool value. For both 
deba@1913
    46
  /// the true and the false it is possible to iterate on the keys which
deba@1913
    47
  /// mapped to the given value.
deba@1913
    48
  /// 
deba@1913
    49
  /// \param _Graph The graph type.
deba@1913
    50
  /// \param _Item One of the graph's item type, the key of the map.
deba@1913
    51
  template <typename _Graph, typename _Item>
deba@1990
    52
  class IterableBoolMap : protected DefaultMap<_Graph, _Item, int> {
deba@1913
    53
  private:
deba@1913
    54
    typedef _Graph Graph;
deba@1913
    55
    
deba@1931
    56
    typedef typename ItemSetTraits<Graph, _Item>::ItemIt KeyIt;
deba@1990
    57
    typedef DefaultMap<_Graph, _Item, int> Parent;
deba@1913
    58
    
deba@1931
    59
    std::vector<_Item> array;
deba@1913
    60
    int sep;
deba@1913
    61
deba@1913
    62
    const Graph& graph;
alpar@1677
    63
alpar@1677
    64
  public:
alpar@1805
    65
deba@1913
    66
    /// Indicates that the map if reference map.
deba@1913
    67
    typedef True ReferenceMapTag;
alpar@1805
    68
deba@1913
    69
    /// The key type
deba@1931
    70
    typedef _Item Key;
deba@1913
    71
    /// The value type
deba@1913
    72
    typedef bool Value;
deba@1913
    73
    /// The const reference type.    
deba@1913
    74
    typedef const Value& ConstReference;
deba@1913
    75
deba@1931
    76
  private:
deba@1931
    77
deba@1931
    78
    int position(const Key& key) const {
deba@1931
    79
      return Parent::operator[](key);
deba@1931
    80
    }
deba@1931
    81
deba@1931
    82
  public:
deba@1913
    83
deba@1913
    84
    /// \brief Refernce to the value of the map.
deba@1913
    85
    ///
deba@1913
    86
    /// This class is near to similar to the bool type. It can
deba@1913
    87
    /// be converted to bool and it has the same operators.
deba@1913
    88
    class Reference {
deba@1913
    89
      friend class IterableBoolMap;
deba@1913
    90
    private:
deba@1913
    91
      Reference(IterableBoolMap& map, const Key& key) 
deba@1913
    92
	: _key(key), _map(map) {} 
alpar@1677
    93
    public:
deba@1913
    94
deba@1913
    95
      Reference& operator=(const Reference& value) {
deba@1913
    96
	_map.set(_key, (bool)value);
deba@1913
    97
 	return *this;
deba@1913
    98
      }
deba@1913
    99
deba@1913
   100
      operator bool() const { 
deba@1913
   101
	return static_cast<const IterableBoolMap&>(_map)[_key]; 
deba@1913
   102
      }
deba@1913
   103
deba@1913
   104
      Reference& operator=(bool value) { 
deba@1913
   105
	_map.set(_key, value); 
deba@1913
   106
	return *this; 
deba@1913
   107
      }
deba@1913
   108
      Reference& operator&=(bool value) {
deba@1913
   109
	_map.set(_key, _map[_key] & value); 
deba@1913
   110
	return *this; 	
deba@1913
   111
      }
deba@1913
   112
      Reference& operator|=(bool value) {
deba@1913
   113
	_map.set(_key, _map[_key] | value); 
deba@1913
   114
	return *this; 	
deba@1913
   115
      }
deba@1913
   116
      Reference& operator^=(bool value) {
deba@1913
   117
	_map.set(_key, _map[_key] ^ value); 
deba@1913
   118
	return *this; 	
deba@1913
   119
      }
deba@1913
   120
    private:
deba@1913
   121
      Key _key;
deba@1913
   122
      IterableBoolMap& _map; 
alpar@1677
   123
    };
deba@1913
   124
    
deba@1913
   125
    /// \brief Constructor of the Map with a default value.
deba@1913
   126
    ///
deba@1913
   127
    /// Constructor of the Map with a default value.
deba@1913
   128
    IterableBoolMap(const Graph& _graph, bool def = false) 
deba@1913
   129
      : Parent(_graph), graph(_graph) {
deba@1931
   130
      for (KeyIt it(graph); it != INVALID; ++it) {
deba@1913
   131
        Parent::set(it, array.size());
deba@1913
   132
        array.push_back(it);
deba@1913
   133
      }
deba@1913
   134
      sep = (def ? array.size() : 0);
deba@1913
   135
    }
deba@1913
   136
deba@1913
   137
    /// \brief Const subscript operator of the map.
deba@1913
   138
    ///
deba@1913
   139
    /// Const subscript operator of the map.
deba@1931
   140
    bool operator[](const Key& key) const {
deba@1931
   141
      return position(key) < sep;
deba@1913
   142
    }
deba@1913
   143
deba@1913
   144
    /// \brief Subscript operator of the map.
deba@1913
   145
    ///
deba@1913
   146
    /// Subscript operator of the map.
deba@1931
   147
    Reference operator[](const Key& key) {
deba@1931
   148
      return Reference(*this, key);
deba@1913
   149
    }
deba@1913
   150
deba@1913
   151
    /// \brief Set operation of the map.
deba@1913
   152
    ///
deba@1913
   153
    /// Set operation of the map.
deba@1931
   154
    void set(const Key& key, bool value) {
deba@1931
   155
      int pos = position(key);
deba@1913
   156
      if (value) {
deba@1913
   157
        if (pos < sep) return;
deba@1931
   158
        Key tmp = array[sep];
deba@1931
   159
        array[sep] = key;
deba@1931
   160
        Parent::set(key, sep);
deba@1913
   161
        array[pos] = tmp;
deba@1913
   162
        Parent::set(tmp, pos); 
deba@1913
   163
        ++sep;
deba@1913
   164
      } else {
deba@1913
   165
        if (pos >= sep) return;
deba@1913
   166
        --sep;
deba@1931
   167
        Key tmp = array[sep];
deba@1931
   168
        array[sep] = key;
deba@1931
   169
        Parent::set(key, sep);
deba@1913
   170
        array[pos] = tmp;
deba@1913
   171
        Parent::set(tmp, pos);
deba@1913
   172
      }
deba@1913
   173
    }
deba@1913
   174
deba@1931
   175
    /// \brief Returns the number of the keys mapped to true.
deba@1913
   176
    ///
deba@1931
   177
    /// Returns the number of the keys mapped to true.
deba@1913
   178
    int trueNum() const {
deba@1913
   179
      return sep;
deba@1913
   180
    } 
deba@1913
   181
    
deba@1931
   182
    /// \brief Returns the number of the keys mapped to false.
deba@1913
   183
    ///
deba@1931
   184
    /// Returns the number of the keys mapped to false.
deba@1913
   185
    int falseNum() const {
deba@1913
   186
      return array.size() - sep;
deba@1913
   187
    }
deba@1913
   188
deba@1913
   189
    /// \brief Iterator for the keys mapped to true.
deba@1913
   190
    ///
deba@1913
   191
    /// Iterator for the keys mapped to true. It works
deba@1913
   192
    /// like a graph item iterator in the map, it can be converted
deba@1931
   193
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   194
    /// if the iterator leave the last valid key it will be equal to 
deba@1913
   195
    /// \c INVALID.
deba@1931
   196
    class TrueIt : public Key {
alpar@1677
   197
    public:
deba@1931
   198
      typedef Key Parent;
deba@1913
   199
      
deba@1913
   200
      /// \brief Creates an iterator.
deba@1913
   201
      ///
deba@1913
   202
      /// Creates an iterator. It iterates on the 
deba@1913
   203
      /// keys which mapped to true.
alpar@1953
   204
      /// \param _map The IterableIntMap
deba@1913
   205
      TrueIt(const IterableBoolMap& _map) 
deba@1913
   206
        : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID), 
deba@1913
   207
          map(&_map) {}
deba@1913
   208
deba@1913
   209
      /// \brief Invalid constructor \& conversion.
deba@1913
   210
      ///
deba@1931
   211
      /// This constructor initializes the key to be invalid.
deba@1913
   212
      /// \sa Invalid for more details.
deba@1913
   213
      TrueIt(Invalid) : Parent(INVALID), map(0) {}
deba@1913
   214
deba@1913
   215
      /// \brief Increment operator.
deba@1913
   216
      ///
deba@1913
   217
      /// Increment Operator.
deba@1913
   218
      TrueIt& operator++() {
deba@1913
   219
        int pos = map->position(*this);
deba@1913
   220
        Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID);
deba@1913
   221
        return *this;
deba@1913
   222
      }
deba@1913
   223
deba@1913
   224
      
deba@1913
   225
    private:
deba@1913
   226
      const IterableBoolMap* map;
deba@1913
   227
    };
deba@1913
   228
deba@1913
   229
    /// \brief Iterator for the keys mapped to false.
deba@1913
   230
    ///
deba@1913
   231
    /// Iterator for the keys mapped to false. It works
deba@1913
   232
    /// like a graph item iterator in the map, it can be converted
deba@1931
   233
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   234
    /// if the iterator leave the last valid key it will be equal to 
deba@1913
   235
    /// \c INVALID.
deba@1931
   236
    class FalseIt : public Key {
deba@1913
   237
    public:
deba@1931
   238
      typedef Key Parent;
deba@1913
   239
      
deba@1913
   240
      /// \brief Creates an iterator.
deba@1913
   241
      ///
deba@1913
   242
      /// Creates an iterator. It iterates on the 
deba@1913
   243
      /// keys which mapped to false.
alpar@1953
   244
      /// \param _map The IterableIntMap
deba@1913
   245
      FalseIt(const IterableBoolMap& _map) 
deba@1931
   246
        : Parent(_map.sep < (int)_map.array.size() ? 
deba@1931
   247
                 _map.array.back() : INVALID), map(&_map) {}
deba@1913
   248
deba@1913
   249
      /// \brief Invalid constructor \& conversion.
deba@1913
   250
      ///
deba@1931
   251
      /// This constructor initializes the key to be invalid.
deba@1913
   252
      /// \sa Invalid for more details.
deba@1913
   253
      FalseIt(Invalid) : Parent(INVALID), map(0) {}
deba@1913
   254
deba@1913
   255
      /// \brief Increment operator.
deba@1913
   256
      ///
deba@1913
   257
      /// Increment Operator.
deba@1913
   258
      FalseIt& operator++() {
deba@1913
   259
        int pos = map->position(*this);
deba@1913
   260
        Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID);
deba@1913
   261
        return *this;
deba@1913
   262
      }
deba@1913
   263
deba@1913
   264
    private:
deba@1913
   265
      const IterableBoolMap* map;
deba@1913
   266
    };
deba@1913
   267
deba@1931
   268
    /// \brief Iterator for the keys mapped to a given value.
deba@1931
   269
    ///
deba@1931
   270
    /// Iterator for the keys mapped to a given value. It works
deba@1931
   271
    /// like a graph item iterator in the map, it can be converted
deba@1931
   272
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   273
    /// if the iterator leave the last valid key it will be equal to 
deba@1931
   274
    /// \c INVALID.
deba@1931
   275
    class ItemIt : public Key {
deba@1931
   276
    public:
deba@1931
   277
      typedef Key Parent;
deba@1931
   278
      
deba@1931
   279
      /// \brief Creates an iterator.
deba@1931
   280
      ///
deba@1931
   281
      /// Creates an iterator. It iterates on the 
deba@1931
   282
      /// keys which mapped to false.
alpar@1953
   283
      /// \param _map The IterableIntMap
deba@1931
   284
      /// \param value Which elements should be iterated.
deba@1931
   285
      ItemIt(const IterableBoolMap& _map, bool value) 
deba@1931
   286
        : Parent(value ? (_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID) :
deba@1931
   287
                 (_map.sep < (int)_map.array.size() ? 
deba@1931
   288
                  _map.array.back() : INVALID)), map(&_map) {}
deba@1931
   289
deba@1931
   290
      /// \brief Invalid constructor \& conversion.
deba@1931
   291
      ///
deba@1931
   292
      /// This constructor initializes the key to be invalid.
deba@1931
   293
      /// \sa Invalid for more details.
deba@1931
   294
      ItemIt(Invalid) : Parent(INVALID), map(0) {}
deba@1931
   295
deba@1931
   296
      /// \brief Increment operator.
deba@1931
   297
      ///
deba@1931
   298
      /// Increment Operator.
deba@1931
   299
      ItemIt& operator++() {
deba@1931
   300
        int pos = map->position(*this);
deba@1931
   301
        int sep = pos >= map->sep ? map->sep : 0;
deba@1931
   302
        Parent::operator=(pos > sep ? map->array[pos - 1] : INVALID);
deba@1931
   303
        return *this;
deba@1931
   304
      }
deba@1931
   305
deba@1931
   306
    private:
deba@1931
   307
      const IterableBoolMap* map;
deba@1931
   308
    };
deba@1931
   309
deba@1913
   310
  protected:
deba@1913
   311
    
deba@1931
   312
    virtual void add(const Key& key) {
deba@1931
   313
      Parent::add(key);
deba@1931
   314
      Parent::set(key, array.size());
deba@1931
   315
      array.push_back(key);
deba@1913
   316
    }
deba@1913
   317
deba@1931
   318
    virtual void add(const std::vector<Key>& keys) {
deba@1931
   319
      Parent::add(keys);
deba@1931
   320
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1931
   321
        Parent::set(keys[i], array.size());
deba@1931
   322
        array.push_back(keys[i]);
deba@1913
   323
      }
deba@1913
   324
    }
deba@1913
   325
deba@1931
   326
    virtual void erase(const Key& key) {
deba@1931
   327
      int pos = position(key); 
deba@1913
   328
      if (pos < sep) {
deba@1913
   329
        --sep;
deba@1913
   330
        Parent::set(array[sep], pos);
deba@1913
   331
        array[pos] = array[sep];
deba@1913
   332
        Parent::set(array.back(), sep);
deba@1913
   333
        array[sep] = array.back();
deba@1913
   334
        array.pop_back();
deba@1913
   335
      } else {
deba@1913
   336
        Parent::set(array.back(), pos);
deba@1913
   337
        array[pos] = array.back();
deba@1913
   338
        array.pop_back();
deba@1913
   339
      }
deba@1931
   340
      Parent::erase(key);
deba@1913
   341
    }
deba@1913
   342
deba@1931
   343
    virtual void erase(const std::vector<Key>& keys) {
deba@1931
   344
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1931
   345
        int pos = position(keys[i]); 
deba@1913
   346
        if (pos < sep) {
deba@1913
   347
          --sep;
deba@1913
   348
          Parent::set(array[sep], pos);
deba@1913
   349
          array[pos] = array[sep];
deba@1913
   350
          Parent::set(array.back(), sep);
deba@1913
   351
          array[sep] = array.back();
deba@1913
   352
          array.pop_back();
deba@1913
   353
        } else {
deba@1913
   354
          Parent::set(array.back(), pos);
deba@1913
   355
          array[pos] = array.back();
deba@1913
   356
          array.pop_back();
deba@1913
   357
        }
deba@1913
   358
      }
deba@1931
   359
      Parent::erase(keys);
deba@1913
   360
    }    
deba@1913
   361
deba@1913
   362
    virtual void build() {
deba@1913
   363
      Parent::build();
deba@1931
   364
      for (KeyIt it(graph); it != INVALID; ++it) {
deba@1913
   365
        Parent::set(it, array.size());
deba@1913
   366
        array.push_back(it);
deba@1913
   367
      }
deba@1913
   368
      sep = 0;      
deba@1913
   369
    }
deba@1913
   370
deba@1913
   371
    virtual void clear() {
deba@1913
   372
      array.clear();
deba@1913
   373
      sep = 0;
deba@1913
   374
      Parent::clear();
deba@1913
   375
    }
deba@1913
   376
    
alpar@1677
   377
  };
alpar@1873
   378
  
deba@1752
   379
deba@1752
   380
  namespace _iterable_maps_bits {
deba@1752
   381
    template <typename Item>
deba@1752
   382
    struct IterableIntMapNode {
deba@1913
   383
      IterableIntMapNode() : value(-1) {}
deba@1810
   384
      IterableIntMapNode(int _value) : value(_value) {}
deba@1752
   385
      Item prev, next;
deba@1752
   386
      int value;
deba@1752
   387
    };
deba@1752
   388
  }
deba@1752
   389
deba@1931
   390
  ///\ingroup graph_maps
deba@1752
   391
  ///
deba@1752
   392
  /// \brief Dynamic iterable integer map.
deba@1752
   393
  ///
deba@1810
   394
  /// This class provides a special graph map type which can store
deba@1810
   395
  /// for each graph item(node, edge, etc.) an integer value. For each
deba@1810
   396
  /// non negative value it is possible to iterate on the keys which
deba@1810
   397
  /// mapped to the given value.
deba@1810
   398
  /// 
deba@1810
   399
  /// \param _Graph The graph type.
deba@1810
   400
  /// \param _Item One of the graph's item type, the key of the map.
deba@1752
   401
  template <typename _Graph, typename _Item>
deba@1990
   402
  class IterableIntMap 
deba@1990
   403
    : protected DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@1990
   404
                           IterableIntMapNode<_Item> > {
deba@1752
   405
  public:
deba@1990
   406
    typedef DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@1990
   407
                       IterableIntMapNode<_Item> >
deba@1990
   408
    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@1990
   692
    : protected DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@1990
   693
                           IterableValueMapNode<_Item, _Value> > {
deba@1931
   694
  public:
deba@1990
   695
    typedef DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@1990
   696
                       IterableValueMapNode<_Item, _Value> > Parent;
deba@1931
   697
deba@1931
   698
    /// The key type
deba@1931
   699
    typedef _Item Key;
deba@1931
   700
    /// The value type
deba@1931
   701
    typedef _Value Value;
deba@1931
   702
    /// The graph type
deba@1931
   703
    typedef _Graph Graph;
deba@1931
   704
deba@1990
   705
  protected:
deba@1990
   706
deba@1990
   707
    typedef typename ItemSetTraits<_Graph, Key>::ItemIt KeyIt; 
deba@1990
   708
deba@1990
   709
  public:
deba@1990
   710
deba@1931
   711
    /// \brief Constructor of the Map with a given value.
deba@1931
   712
    ///
deba@1931
   713
    /// Constructor of the Map with a given value.
deba@1931
   714
    explicit IterableValueMap(const Graph& graph, 
deba@1931
   715
                              const Value& value = Value()) 
deba@1990
   716
      : Parent(graph, _iterable_maps_bits::
deba@1990
   717
               IterableValueMapNode<_Item, _Value>(value)) {
deba@1990
   718
      for (KeyIt it(*Parent::getGraph()); it != INVALID; ++it) {
deba@1931
   719
        lace(it);
deba@1931
   720
      }
deba@1931
   721
    }
deba@1931
   722
deba@1990
   723
  protected:
deba@1931
   724
    
deba@1931
   725
    void unlace(const Key& key) {
deba@1931
   726
      typename Parent::Value& node = Parent::operator[](key);
deba@1931
   727
      if (node.prev != INVALID) {
deba@1931
   728
	Parent::operator[](node.prev).next = node.next;	
deba@1931
   729
      } else {
deba@1931
   730
        if (node.next != INVALID) {
deba@1931
   731
          first[node.value] = node.next;
deba@1931
   732
        } else {
deba@1931
   733
          first.erase(node.value);
deba@1931
   734
        }
deba@1931
   735
      }
deba@1931
   736
      if (node.next != INVALID) {
deba@1931
   737
	Parent::operator[](node.next).prev = node.prev;
deba@1931
   738
      }
deba@1931
   739
    }
deba@1931
   740
deba@1931
   741
    void lace(const Key& key) {
deba@1931
   742
      typename Parent::Value& node = Parent::operator[](key);
deba@1931
   743
      typename std::map<Value, Key>::iterator it = first.find(node.value);
deba@1931
   744
      if (it == first.end()) {
deba@1931
   745
        node.prev = node.next = INVALID;
deba@1931
   746
        if (node.next != INVALID) {
deba@1931
   747
          Parent::operator[](node.next).prev = key;	
deba@1931
   748
        }
deba@1931
   749
        first.insert(make_pair(node.value, key));
deba@1931
   750
      } else {
deba@1931
   751
        node.prev = INVALID;
deba@1931
   752
        node.next = it->second;
deba@1931
   753
        if (node.next != INVALID) {
deba@1931
   754
          Parent::operator[](node.next).prev = key;	
deba@1931
   755
        }
deba@1931
   756
        it->second = key;
deba@1931
   757
      }
deba@1931
   758
    }
deba@1931
   759
deba@1931
   760
  public:
deba@1931
   761
deba@1931
   762
    /// \brief Forward iterator for values.
deba@1931
   763
    ///
deba@1931
   764
    /// This iterator is an stl compatible forward
deba@1931
   765
    /// iterator on the values of the map. The values can
deba@1931
   766
    /// be accessed in the [beginValue, endValue) range.
deba@1931
   767
    ///
deba@1931
   768
    class ValueIterator 
deba@1931
   769
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@1931
   770
      friend class IterableValueMap;
deba@1931
   771
    private:
deba@1931
   772
      ValueIterator(typename std::map<Value, Key>::const_iterator _it) 
deba@1931
   773
        : it(_it) {}
deba@1931
   774
    public:
deba@1931
   775
      
deba@1931
   776
      ValueIterator() {}
deba@1931
   777
deba@1931
   778
      ValueIterator& operator++() { ++it; return *this; }
deba@1931
   779
      ValueIterator operator++(int) { 
deba@1931
   780
        ValueIterator tmp(*this); 
deba@1931
   781
        operator++();
deba@1931
   782
        return tmp; 
deba@1931
   783
      }
deba@1931
   784
deba@1931
   785
      const Value& operator*() const { return it->first; }
deba@1931
   786
      const Value* operator->() const { return &(it->first); }
deba@1931
   787
deba@1931
   788
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@1931
   789
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@1931
   790
      
deba@1931
   791
    private:
deba@1931
   792
      typename std::map<Value, Key>::const_iterator it;
deba@1931
   793
    };
deba@1931
   794
deba@1931
   795
    /// \brief Returns an iterator to the first value.
deba@1931
   796
    ///
deba@1931
   797
    /// Returns an stl compatible iterator to the 
deba@1931
   798
    /// first value of the map. The values of the
deba@1931
   799
    /// map can be accessed in the [beginValue, endValue)
deba@1931
   800
    /// range.
deba@1931
   801
    ValueIterator beginValue() const {
deba@1931
   802
      return ValueIterator(first.begin());
deba@1931
   803
    }
deba@1931
   804
deba@1931
   805
    /// \brief Returns an iterator after the last value.
deba@1931
   806
    ///
deba@1931
   807
    /// Returns an stl compatible iterator after the 
deba@1931
   808
    /// last value of the map. The values of the
deba@1931
   809
    /// map can be accessed in the [beginValue, endValue)
deba@1931
   810
    /// range.
deba@1931
   811
    ValueIterator endValue() const {
deba@1931
   812
      return ValueIterator(first.end());
deba@1931
   813
    }
deba@1931
   814
deba@1931
   815
    /// \brief Set operation of the map.
deba@1931
   816
    ///
deba@1931
   817
    /// Set operation of the map.
deba@1931
   818
    void set(const Key& key, const Value& value) {
deba@1931
   819
      unlace(key);
deba@1931
   820
      Parent::operator[](key).value = value;
deba@1931
   821
      lace(key);
deba@1931
   822
    }
deba@1931
   823
deba@1931
   824
    /// \brief Const subscript operator of the map.
deba@1931
   825
    ///
deba@1931
   826
    /// Const subscript operator of the map.
deba@1931
   827
    const Value& operator[](const Key& key) const {
deba@1931
   828
      return Parent::operator[](key).value;
deba@1931
   829
    }
deba@1931
   830
deba@1931
   831
    /// \brief Iterator for the keys with the same value.
deba@1931
   832
    ///
deba@1931
   833
    /// Iterator for the keys with the same value. It works
deba@1931
   834
    /// like a graph item iterator in the map, it can be converted
deba@1931
   835
    /// the item type of the map, incremented with \c ++ operator, and
deba@1931
   836
    /// if the iterator leave the last valid item it will be equal to 
deba@1931
   837
    /// \c INVALID.
deba@1931
   838
    class ItemIt : public _Item {
deba@1931
   839
    public:
deba@1931
   840
      typedef _Item Parent;
deba@1931
   841
deba@1931
   842
      /// \brief Invalid constructor \& conversion.
deba@1931
   843
      ///
deba@1931
   844
      /// This constructor initializes the item to be invalid.
deba@1931
   845
      /// \sa Invalid for more details.
deba@1931
   846
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@1931
   847
deba@1931
   848
      /// \brief Creates an iterator with a value.
deba@1931
   849
      ///
deba@1931
   850
      /// Creates an iterator with a value. It iterates on the 
deba@1931
   851
      /// keys which have the given value.
deba@1931
   852
      /// \param map The IterableValueMap
deba@1931
   853
      /// \param value The value
deba@1931
   854
      ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
deba@1931
   855
        typename std::map<Value, Key>::const_iterator it = 
deba@1931
   856
          map.first.find(value); 
deba@1931
   857
	if (it == map.first.end()) {	  
deba@1931
   858
	  Parent::operator=(INVALID);
deba@1931
   859
	} else {
deba@1931
   860
	  Parent::operator=(it->second);
deba@1931
   861
	}
deba@1931
   862
      } 
deba@1931
   863
deba@1931
   864
      /// \brief Increment operator.
deba@1931
   865
      ///
deba@1931
   866
      /// Increment Operator.
deba@1931
   867
      ItemIt& operator++() {
deba@1931
   868
	Parent::operator=(_map->IterableValueMap::Parent::
deba@1931
   869
			  operator[](static_cast<Parent&>(*this)).next);
deba@1931
   870
	return *this;
deba@1931
   871
      }
deba@1931
   872
deba@1931
   873
deba@1931
   874
    private:
deba@1931
   875
      const IterableValueMap* _map;
deba@1931
   876
    };
deba@1931
   877
deba@1931
   878
  protected:
deba@1931
   879
    
deba@1990
   880
    virtual void add(const Key& key) {
deba@1990
   881
      Parent::add(key);
deba@1990
   882
      unlace(key);
deba@1990
   883
    }
deba@1990
   884
deba@1990
   885
    virtual void add(const std::vector<Key>& keys) {
deba@1990
   886
      Parent::add(keys);
deba@1990
   887
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1990
   888
        lace(keys[i]);
deba@1990
   889
      }
deba@1990
   890
    }
deba@1990
   891
deba@1931
   892
    virtual void erase(const Key& key) {
deba@1931
   893
      unlace(key);
deba@1931
   894
      Parent::erase(key);
deba@1931
   895
    }
deba@1931
   896
deba@1931
   897
    virtual void erase(const std::vector<Key>& keys) {
deba@1931
   898
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1931
   899
        unlace(keys[i]);
deba@1931
   900
      }
deba@1931
   901
      Parent::erase(keys);
deba@1931
   902
    }
deba@1931
   903
deba@1990
   904
    virtual void build() {
deba@1990
   905
      Parent::build();
deba@1990
   906
      for (KeyIt it(*Parent::getGraph()); it != INVALID; ++it) {
deba@1990
   907
        lace(it);
deba@1990
   908
      }
deba@1990
   909
    }
deba@1990
   910
deba@1931
   911
    virtual void clear() {
deba@1931
   912
      first.clear();
deba@1931
   913
      Parent::clear();
deba@1931
   914
    }
deba@1931
   915
deba@1931
   916
  private:
deba@1931
   917
    std::map<Value, Key> first;
deba@1931
   918
  };
deba@1931
   919
alpar@1677
   920
  /// @}
alpar@1677
   921
}