lemon/iterable_maps.h
author deba
Thu, 23 Feb 2006 08:55:54 +0000
changeset 1980 a954b780e3ab
parent 1953 d4f411003580
child 1990 15fb7a4ea6be
permissions -rw-r--r--
Renaming to be convient to the naming of the adaptors
Concept checking of the ugraph adaptors
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@1752
    19
#include <lemon/traits.h>
deba@1752
    20
#include <lemon/invalid.h>
deba@1931
    21
alpar@1677
    22
#include <vector>
deba@1931
    23
#include <map>
deba@1931
    24
deba@1931
    25
#include <iterator>
alpar@1677
    26
#include <limits>
alpar@1677
    27
alpar@1677
    28
///\ingroup maps
alpar@1677
    29
///\file
alpar@1677
    30
///\brief Maps that makes it possible to iterate through the keys having
alpar@1677
    31
///a certain value
alpar@1677
    32
///
alpar@1677
    33
///
alpar@1677
    34
alpar@1677
    35
alpar@1677
    36
namespace lemon {
deba@1913
    37
deba@1931
    38
  ///\ingroup graph_maps
deba@1913
    39
  ///
deba@1913
    40
  /// \brief Dynamic iterable bool map.
deba@1913
    41
  ///
deba@1913
    42
  /// This class provides a special graph map type which can store
deba@1913
    43
  /// for each graph item(node, edge, etc.) a bool value. For both 
deba@1913
    44
  /// the true and the false it is possible to iterate on the keys which
deba@1913
    45
  /// mapped to the given value.
deba@1913
    46
  /// 
deba@1913
    47
  /// \param _Graph The graph type.
deba@1913
    48
  /// \param _Item One of the graph's item type, the key of the map.
deba@1913
    49
  template <typename _Graph, typename _Item>
deba@1913
    50
  class IterableBoolMap 
deba@1913
    51
    : protected ItemSetTraits<_Graph, _Item>::template Map<int>::Parent {
deba@1913
    52
  private:
deba@1913
    53
    typedef _Graph Graph;
deba@1913
    54
    
deba@1931
    55
    typedef typename ItemSetTraits<Graph, _Item>::ItemIt KeyIt;
deba@1931
    56
    typedef typename ItemSetTraits<Graph, _Item>
deba@1913
    57
    ::template Map<int>::Parent 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@1752
   402
  class IterableIntMap : protected ItemSetTraits<_Graph, _Item> 
deba@1752
   403
  ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent {
deba@1752
   404
  public:
deba@1752
   405
    typedef typename ItemSetTraits<_Graph, _Item> 
deba@1752
   406
    ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >
deba@1752
   407
    ::Parent Parent;
deba@1752
   408
deba@1810
   409
    /// The key type
deba@1752
   410
    typedef _Item Key;
deba@1810
   411
    /// The value type
deba@1752
   412
    typedef int Value;
deba@1810
   413
    /// The graph type
deba@1752
   414
    typedef _Graph Graph;
deba@1752
   415
deba@1810
   416
    /// \brief Constructor of the Map.
deba@1810
   417
    ///
deba@1810
   418
    /// Constructor of the Map. It set all values -1.
deba@1810
   419
    explicit IterableIntMap(const Graph& graph) 
deba@1913
   420
      : Parent(graph) {}
deba@1810
   421
deba@1810
   422
    /// \brief Constructor of the Map with a given value.
deba@1810
   423
    ///
deba@1810
   424
    /// Constructor of the Map with a given value.
deba@1810
   425
    explicit IterableIntMap(const Graph& graph, int value) 
deba@1810
   426
      : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
deba@1810
   427
      if (value >= 0) {
deba@1810
   428
	for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@1810
   429
	  lace(it);
deba@1810
   430
	}
deba@1810
   431
      }
deba@1810
   432
    }
deba@1752
   433
deba@1752
   434
  private:
deba@1752
   435
    
deba@1752
   436
    void unlace(const Key& key) {
deba@1752
   437
      typename Parent::Value& node = Parent::operator[](key);
deba@1752
   438
      if (node.value < 0) return;
deba@1752
   439
      if (node.prev != INVALID) {
deba@1752
   440
	Parent::operator[](node.prev).next = node.next;	
deba@1752
   441
      } else {
deba@1752
   442
	first[node.value] = node.next;
deba@1752
   443
      }
deba@1752
   444
      if (node.next != INVALID) {
deba@1752
   445
	Parent::operator[](node.next).prev = node.prev;
deba@1752
   446
      }
deba@1752
   447
      while (!first.empty() && first.back() == INVALID) {
deba@1752
   448
	first.pop_back();
deba@1752
   449
      }
deba@1752
   450
    }
deba@1752
   451
deba@1752
   452
    void lace(const Key& key) {
deba@1752
   453
      typename Parent::Value& node = Parent::operator[](key);
deba@1752
   454
      if (node.value < 0) return;
deba@1752
   455
      if (node.value >= (int)first.size()) {
deba@1752
   456
	first.resize(node.value + 1, INVALID);
deba@1752
   457
      } 
deba@1752
   458
      node.prev = INVALID;
deba@1752
   459
      node.next = first[node.value];
deba@1752
   460
      if (node.next != INVALID) {
deba@1752
   461
	Parent::operator[](node.next).prev = key;	
deba@1752
   462
      }
deba@1752
   463
      first[node.value] = key;
deba@1752
   464
    }
deba@1752
   465
deba@1752
   466
  public:
deba@1752
   467
deba@1810
   468
    /// Indicates that the map if reference map.
deba@1752
   469
    typedef True ReferenceMapTag;
deba@1752
   470
deba@1810
   471
    /// \brief Refernce to the value of the map.
deba@1810
   472
    ///
deba@1810
   473
    /// This class is near to similar to the int type. It can
deba@1810
   474
    /// be converted to int and it has the same operators.
deba@1752
   475
    class Reference {
deba@1752
   476
      friend class IterableIntMap;
deba@1752
   477
    private:
deba@1752
   478
      Reference(IterableIntMap& map, const Key& key) 
deba@1752
   479
	: _key(key), _map(map) {} 
deba@1752
   480
    public:
deba@1752
   481
deba@1752
   482
      Reference& operator=(const Reference& value) {
deba@1752
   483
	_map.set(_key, (const int&)value);
deba@1752
   484
 	return *this;
deba@1752
   485
      }
deba@1752
   486
deba@1752
   487
      operator const int&() const { 
deba@1752
   488
	return static_cast<const IterableIntMap&>(_map)[_key]; 
deba@1752
   489
      }
deba@1752
   490
deba@1752
   491
      Reference& operator=(int value) { 
deba@1752
   492
	_map.set(_key, value); 
deba@1752
   493
	return *this; 
deba@1752
   494
      }
deba@1759
   495
      Reference& operator++() {
deba@1759
   496
	_map.set(_key, _map[_key] + 1); 
deba@1759
   497
	return *this; 	
deba@1759
   498
      }
deba@1759
   499
      int operator++(int) {
deba@1759
   500
	int value = _map[_key];
deba@1759
   501
	_map.set(_key, value + 1); 
deba@1759
   502
	return value; 	
deba@1759
   503
      }
deba@1759
   504
      Reference& operator--() {
deba@1759
   505
	_map.set(_key, _map[_key] - 1); 
deba@1759
   506
	return *this; 	
deba@1759
   507
      }
deba@1759
   508
      int operator--(int) {
deba@1759
   509
	int value = _map[_key];
deba@1759
   510
	_map.set(_key, value - 1); 
deba@1759
   511
	return value; 	
deba@1759
   512
      }
deba@1752
   513
      Reference& operator+=(int value) { 
deba@1752
   514
	_map.set(_key, _map[_key] + value); 
deba@1752
   515
	return *this;
deba@1752
   516
      }
deba@1752
   517
      Reference& operator-=(int value) { 
deba@1752
   518
	_map.set(_key, _map[_key] - value); 
deba@1752
   519
	return *this;
deba@1752
   520
      }
deba@1752
   521
      Reference& operator*=(int value) { 
deba@1752
   522
	_map.set(_key, _map[_key] * value); 
deba@1752
   523
	return *this;
deba@1752
   524
      }
deba@1752
   525
      Reference& operator/=(int value) { 
deba@1752
   526
	_map.set(_key, _map[_key] / value); 
deba@1752
   527
	return *this;
deba@1752
   528
      }
deba@1752
   529
      Reference& operator%=(int value) { 
deba@1752
   530
	_map.set(_key, _map[_key] % value); 
deba@1752
   531
	return *this;
deba@1752
   532
      }
deba@1752
   533
      Reference& operator&=(int value) { 
deba@1752
   534
	_map.set(_key, _map[_key] & value); 
deba@1752
   535
	return *this;
deba@1752
   536
      }
deba@1752
   537
      Reference& operator|=(int value) { 
deba@1752
   538
	_map.set(_key, _map[_key] | value); 
deba@1752
   539
	return *this;
deba@1752
   540
      }
deba@1752
   541
      Reference& operator^=(int value) { 
deba@1752
   542
	_map.set(_key, _map[_key] ^ value); 
deba@1752
   543
	return *this;
deba@1752
   544
      }
deba@1752
   545
      Reference& operator<<=(int value) { 
deba@1752
   546
	_map.set(_key, _map[_key] << value); 
deba@1752
   547
	return *this;
deba@1752
   548
      }
deba@1752
   549
      Reference& operator>>=(int value) { 
deba@1752
   550
	_map.set(_key, _map[_key] >> value); 
deba@1752
   551
	return *this;
deba@1752
   552
      }
deba@1752
   553
deba@1752
   554
    private:
deba@1752
   555
      Key _key;
deba@1752
   556
      IterableIntMap& _map; 
deba@1752
   557
    };
deba@1810
   558
deba@1810
   559
    /// The const reference type.    
deba@1752
   560
    typedef const Value& ConstReference;
deba@1752
   561
deba@1810
   562
    /// \brief Gives back the maximal value plus one.
deba@1810
   563
    ///
deba@1810
   564
    /// Gives back the maximal value plus one.
deba@1931
   565
    unsigned int size() const {
deba@1931
   566
      return first.size();
deba@1752
   567
    }
deba@1752
   568
    
deba@1810
   569
    /// \brief Set operation of the map.
deba@1810
   570
    ///
deba@1810
   571
    /// Set operation of the map.
deba@1752
   572
    void set(const Key& key, const Value& value) {
deba@1752
   573
      unlace(key);
deba@1752
   574
      Parent::operator[](key).value = value;
deba@1752
   575
      lace(key);
deba@1752
   576
    }
deba@1752
   577
deba@1810
   578
    /// \brief Const subscript operator of the map.
deba@1810
   579
    ///
deba@1810
   580
    /// Const subscript operator of the map.
deba@1752
   581
    const Value& operator[](const Key& key) const {
deba@1752
   582
      return Parent::operator[](key).value;
deba@1752
   583
    }
deba@1752
   584
deba@1810
   585
    /// \brief Subscript operator of the map.
deba@1810
   586
    ///
deba@1810
   587
    /// Subscript operator of the map.
deba@1752
   588
    Reference operator[](const Key& key) {
deba@1752
   589
      return Reference(*this, key);
deba@1752
   590
    }
deba@1752
   591
deba@1810
   592
    /// \brief Iterator for the keys with the same value.
deba@1810
   593
    ///
deba@1810
   594
    /// Iterator for the keys with the same value. It works
deba@1810
   595
    /// like a graph item iterator in the map, it can be converted
deba@1810
   596
    /// the item type of the map, incremented with \c ++ operator, and
deba@1810
   597
    /// if the iterator leave the last valid item it will be equal to 
deba@1810
   598
    /// \c INVALID.
deba@1752
   599
    class ItemIt : public _Item {
deba@1752
   600
    public:
deba@1752
   601
      typedef _Item Parent;
deba@1752
   602
deba@1810
   603
      /// \brief Invalid constructor \& conversion.
deba@1810
   604
      ///
deba@1810
   605
      /// This constructor initializes the item to be invalid.
deba@1810
   606
      /// \sa Invalid for more details.
deba@1752
   607
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@1752
   608
deba@1810
   609
      /// \brief Creates an iterator with a value.
deba@1810
   610
      ///
deba@1810
   611
      /// Creates an iterator with a value. It iterates on the 
deba@1810
   612
      /// keys which have the given value.
deba@1810
   613
      /// \param map The IterableIntMap
deba@1810
   614
      /// \param value The value
deba@1752
   615
      ItemIt(const IterableIntMap& map, int value) : _map(&map) {
deba@1752
   616
	if (value < 0 || value >= (int)_map->first.size()) {	  
deba@1752
   617
	  Parent::operator=(INVALID);
deba@1752
   618
	} else {
deba@1752
   619
	  Parent::operator=(_map->first[value]);
deba@1752
   620
	}
deba@1752
   621
      } 
deba@1752
   622
deba@1810
   623
      /// \brief Increment operator.
deba@1810
   624
      ///
deba@1810
   625
      /// Increment Operator.
deba@1752
   626
      ItemIt& operator++() {
deba@1752
   627
	Parent::operator=(_map->IterableIntMap::Parent::
deba@1752
   628
			  operator[](static_cast<Parent&>(*this)).next);
deba@1752
   629
	return *this;
deba@1752
   630
      }
deba@1752
   631
deba@1752
   632
deba@1752
   633
    private:
deba@1752
   634
      const IterableIntMap* _map;
deba@1752
   635
    };
deba@1752
   636
deba@1752
   637
  protected:
deba@1752
   638
    
deba@1752
   639
    virtual void erase(const Key& key) {
deba@1752
   640
      unlace(key);
deba@1752
   641
      Parent::erase(key);
deba@1752
   642
    }
deba@1752
   643
deba@1913
   644
    virtual void erase(const std::vector<Key>& keys) {
deba@1931
   645
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1913
   646
        unlace(keys[i]);
deba@1913
   647
      }
deba@1913
   648
      Parent::erase(keys);
deba@1913
   649
    }
deba@1913
   650
deba@1752
   651
    virtual void clear() {
deba@1752
   652
      first.clear();
deba@1752
   653
      Parent::clear();
deba@1752
   654
    }
deba@1752
   655
deba@1752
   656
  private:
deba@1752
   657
    std::vector<_Item> first;
deba@1752
   658
  };
deba@1752
   659
deba@1931
   660
  namespace _iterable_maps_bits {
deba@1931
   661
    template <typename Item, typename Value>
deba@1931
   662
    struct IterableValueMapNode {
deba@1931
   663
      IterableValueMapNode(Value _value = Value()) : value(_value) {}
deba@1931
   664
      Item prev, next;
deba@1931
   665
      Value value;
deba@1931
   666
    };
deba@1931
   667
  }
deba@1931
   668
deba@1931
   669
  ///\ingroup graph_maps
deba@1931
   670
  ///
deba@1931
   671
  /// \brief Dynamic iterable map for comparable values.
deba@1931
   672
  ///
deba@1931
   673
  /// This class provides a special graph map type which can store
deba@1931
   674
  /// for each graph item(node, edge, etc.) a value. For each
deba@1931
   675
  /// value it is possible to iterate on the keys which mapped to the 
deba@1931
   676
  /// given value. The type stores for each value a linked list with
deba@1931
   677
  /// the items which mapped to the value, and the values are stored
deba@1931
   678
  /// in balanced binary tree. The values of the map can be accessed
deba@1931
   679
  /// with stl compatible forward iterator.
deba@1931
   680
  ///
deba@1931
   681
  /// This type is not reference map so it cannot be modified with
deba@1931
   682
  /// the subscription operator.
deba@1931
   683
  ///
deba@1931
   684
  /// \see InvertableMap
deba@1931
   685
  /// 
deba@1931
   686
  /// \param _Graph The graph type.
deba@1931
   687
  /// \param _Item One of the graph's item type, the key of the map.
deba@1931
   688
  /// \param _Value Any comparable value type.
deba@1931
   689
  template <typename _Graph, typename _Item, typename _Value>
deba@1931
   690
  class IterableValueMap : protected ItemSetTraits<_Graph, _Item> 
deba@1931
   691
  ::template Map<_iterable_maps_bits::IterableValueMapNode<_Item, _Value> >
deba@1931
   692
  ::Parent {
deba@1931
   693
  public:
deba@1931
   694
    typedef typename ItemSetTraits<_Graph, _Item> 
deba@1931
   695
    ::template Map<_iterable_maps_bits::IterableValueMapNode<_Item, _Value> >
deba@1931
   696
    ::Parent 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@1931
   705
    /// \brief Constructor of the Map with a given value.
deba@1931
   706
    ///
deba@1931
   707
    /// Constructor of the Map with a given value.
deba@1931
   708
    explicit IterableValueMap(const Graph& graph, 
deba@1931
   709
                              const Value& value = Value()) 
deba@1931
   710
      : Parent(graph, _iterable_maps_bits::IterableValueMapNode<_Item, 
deba@1931
   711
               _Value>(value)) {
deba@1931
   712
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@1931
   713
        lace(it);
deba@1931
   714
      }
deba@1931
   715
    }
deba@1931
   716
deba@1931
   717
  private:
deba@1931
   718
    
deba@1931
   719
    void unlace(const Key& key) {
deba@1931
   720
      typename Parent::Value& node = Parent::operator[](key);
deba@1931
   721
      if (node.prev != INVALID) {
deba@1931
   722
	Parent::operator[](node.prev).next = node.next;	
deba@1931
   723
      } else {
deba@1931
   724
        if (node.next != INVALID) {
deba@1931
   725
          first[node.value] = node.next;
deba@1931
   726
        } else {
deba@1931
   727
          first.erase(node.value);
deba@1931
   728
        }
deba@1931
   729
      }
deba@1931
   730
      if (node.next != INVALID) {
deba@1931
   731
	Parent::operator[](node.next).prev = node.prev;
deba@1931
   732
      }
deba@1931
   733
    }
deba@1931
   734
deba@1931
   735
    void lace(const Key& key) {
deba@1931
   736
      typename Parent::Value& node = Parent::operator[](key);
deba@1931
   737
      typename std::map<Value, Key>::iterator it = first.find(node.value);
deba@1931
   738
      if (it == first.end()) {
deba@1931
   739
        node.prev = node.next = INVALID;
deba@1931
   740
        if (node.next != INVALID) {
deba@1931
   741
          Parent::operator[](node.next).prev = key;	
deba@1931
   742
        }
deba@1931
   743
        first.insert(make_pair(node.value, key));
deba@1931
   744
      } else {
deba@1931
   745
        node.prev = INVALID;
deba@1931
   746
        node.next = it->second;
deba@1931
   747
        if (node.next != INVALID) {
deba@1931
   748
          Parent::operator[](node.next).prev = key;	
deba@1931
   749
        }
deba@1931
   750
        it->second = key;
deba@1931
   751
      }
deba@1931
   752
    }
deba@1931
   753
deba@1931
   754
  public:
deba@1931
   755
deba@1931
   756
    /// \brief Forward iterator for values.
deba@1931
   757
    ///
deba@1931
   758
    /// This iterator is an stl compatible forward
deba@1931
   759
    /// iterator on the values of the map. The values can
deba@1931
   760
    /// be accessed in the [beginValue, endValue) range.
deba@1931
   761
    ///
deba@1931
   762
    class ValueIterator 
deba@1931
   763
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@1931
   764
      friend class IterableValueMap;
deba@1931
   765
    private:
deba@1931
   766
      ValueIterator(typename std::map<Value, Key>::const_iterator _it) 
deba@1931
   767
        : it(_it) {}
deba@1931
   768
    public:
deba@1931
   769
      
deba@1931
   770
      ValueIterator() {}
deba@1931
   771
deba@1931
   772
      ValueIterator& operator++() { ++it; return *this; }
deba@1931
   773
      ValueIterator operator++(int) { 
deba@1931
   774
        ValueIterator tmp(*this); 
deba@1931
   775
        operator++();
deba@1931
   776
        return tmp; 
deba@1931
   777
      }
deba@1931
   778
deba@1931
   779
      const Value& operator*() const { return it->first; }
deba@1931
   780
      const Value* operator->() const { return &(it->first); }
deba@1931
   781
deba@1931
   782
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@1931
   783
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@1931
   784
      
deba@1931
   785
    private:
deba@1931
   786
      typename std::map<Value, Key>::const_iterator it;
deba@1931
   787
    };
deba@1931
   788
deba@1931
   789
    /// \brief Returns an iterator to the first value.
deba@1931
   790
    ///
deba@1931
   791
    /// Returns an stl compatible iterator to the 
deba@1931
   792
    /// first value of the map. The values of the
deba@1931
   793
    /// map can be accessed in the [beginValue, endValue)
deba@1931
   794
    /// range.
deba@1931
   795
    ValueIterator beginValue() const {
deba@1931
   796
      return ValueIterator(first.begin());
deba@1931
   797
    }
deba@1931
   798
deba@1931
   799
    /// \brief Returns an iterator after the last value.
deba@1931
   800
    ///
deba@1931
   801
    /// Returns an stl compatible iterator after the 
deba@1931
   802
    /// last value of the map. The values of the
deba@1931
   803
    /// map can be accessed in the [beginValue, endValue)
deba@1931
   804
    /// range.
deba@1931
   805
    ValueIterator endValue() const {
deba@1931
   806
      return ValueIterator(first.end());
deba@1931
   807
    }
deba@1931
   808
deba@1931
   809
    /// \brief Set operation of the map.
deba@1931
   810
    ///
deba@1931
   811
    /// Set operation of the map.
deba@1931
   812
    void set(const Key& key, const Value& value) {
deba@1931
   813
      unlace(key);
deba@1931
   814
      Parent::operator[](key).value = value;
deba@1931
   815
      lace(key);
deba@1931
   816
    }
deba@1931
   817
deba@1931
   818
    /// \brief Const subscript operator of the map.
deba@1931
   819
    ///
deba@1931
   820
    /// Const subscript operator of the map.
deba@1931
   821
    const Value& operator[](const Key& key) const {
deba@1931
   822
      return Parent::operator[](key).value;
deba@1931
   823
    }
deba@1931
   824
deba@1931
   825
    /// \brief Iterator for the keys with the same value.
deba@1931
   826
    ///
deba@1931
   827
    /// Iterator for the keys with the same value. It works
deba@1931
   828
    /// like a graph item iterator in the map, it can be converted
deba@1931
   829
    /// the item type of the map, incremented with \c ++ operator, and
deba@1931
   830
    /// if the iterator leave the last valid item it will be equal to 
deba@1931
   831
    /// \c INVALID.
deba@1931
   832
    class ItemIt : public _Item {
deba@1931
   833
    public:
deba@1931
   834
      typedef _Item Parent;
deba@1931
   835
deba@1931
   836
      /// \brief Invalid constructor \& conversion.
deba@1931
   837
      ///
deba@1931
   838
      /// This constructor initializes the item to be invalid.
deba@1931
   839
      /// \sa Invalid for more details.
deba@1931
   840
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@1931
   841
deba@1931
   842
      /// \brief Creates an iterator with a value.
deba@1931
   843
      ///
deba@1931
   844
      /// Creates an iterator with a value. It iterates on the 
deba@1931
   845
      /// keys which have the given value.
deba@1931
   846
      /// \param map The IterableValueMap
deba@1931
   847
      /// \param value The value
deba@1931
   848
      ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
deba@1931
   849
        typename std::map<Value, Key>::const_iterator it = 
deba@1931
   850
          map.first.find(value); 
deba@1931
   851
	if (it == map.first.end()) {	  
deba@1931
   852
	  Parent::operator=(INVALID);
deba@1931
   853
	} else {
deba@1931
   854
	  Parent::operator=(it->second);
deba@1931
   855
	}
deba@1931
   856
      } 
deba@1931
   857
deba@1931
   858
      /// \brief Increment operator.
deba@1931
   859
      ///
deba@1931
   860
      /// Increment Operator.
deba@1931
   861
      ItemIt& operator++() {
deba@1931
   862
	Parent::operator=(_map->IterableValueMap::Parent::
deba@1931
   863
			  operator[](static_cast<Parent&>(*this)).next);
deba@1931
   864
	return *this;
deba@1931
   865
      }
deba@1931
   866
deba@1931
   867
deba@1931
   868
    private:
deba@1931
   869
      const IterableValueMap* _map;
deba@1931
   870
    };
deba@1931
   871
deba@1931
   872
  protected:
deba@1931
   873
    
deba@1931
   874
    virtual void erase(const Key& key) {
deba@1931
   875
      unlace(key);
deba@1931
   876
      Parent::erase(key);
deba@1931
   877
    }
deba@1931
   878
deba@1931
   879
    virtual void erase(const std::vector<Key>& keys) {
deba@1931
   880
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1931
   881
        unlace(keys[i]);
deba@1931
   882
      }
deba@1931
   883
      Parent::erase(keys);
deba@1931
   884
    }
deba@1931
   885
deba@1931
   886
    virtual void clear() {
deba@1931
   887
      first.clear();
deba@1931
   888
      Parent::clear();
deba@1931
   889
    }
deba@1931
   890
deba@1931
   891
  private:
deba@1931
   892
    std::map<Value, Key> first;
deba@1931
   893
  };
deba@1931
   894
alpar@1677
   895
  /// @}
alpar@1677
   896
}