lemon/iterable_maps.h
author athos
Thu, 21 Sep 2006 14:46:28 +0000
changeset 2218 50f1a780a5ff
parent 2112 f27dfd29c5c0
child 2386 81b47fc5c444
permissions -rw-r--r--
Interface to the cplex MIP solver: it is little, a bit sour but it is ours.
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@2210
    19
#ifndef LEMON_ITERABLE_MAPS_H
deba@2210
    20
#define LEMON_ITERABLE_MAPS_H
deba@2210
    21
deba@1993
    22
#include <lemon/bits/traits.h>
deba@1993
    23
#include <lemon/bits/invalid.h>
deba@1931
    24
deba@1990
    25
#include <lemon/bits/default_map.h>
deba@2031
    26
#include <lemon/bits/map_extender.h>
deba@1990
    27
alpar@1677
    28
#include <vector>
deba@1931
    29
#include <map>
deba@1931
    30
deba@1931
    31
#include <iterator>
alpar@1677
    32
#include <limits>
alpar@1677
    33
alpar@1677
    34
///\ingroup maps
alpar@1677
    35
///\file
alpar@1677
    36
///\brief Maps that makes it possible to iterate through the keys having
alpar@1677
    37
///a certain value
alpar@1677
    38
///
alpar@1677
    39
///
alpar@1677
    40
alpar@1677
    41
alpar@1677
    42
namespace lemon {
deba@1913
    43
deba@1931
    44
  ///\ingroup graph_maps
deba@1913
    45
  ///
deba@1913
    46
  /// \brief Dynamic iterable bool map.
deba@1913
    47
  ///
deba@1913
    48
  /// This class provides a special graph map type which can store
deba@1913
    49
  /// for each graph item(node, edge, etc.) a bool value. For both 
deba@1913
    50
  /// the true and the false it is possible to iterate on the keys which
deba@1913
    51
  /// mapped to the given value.
deba@1913
    52
  /// 
deba@1913
    53
  /// \param _Graph The graph type.
deba@1913
    54
  /// \param _Item One of the graph's item type, the key of the map.
deba@1913
    55
  template <typename _Graph, typename _Item>
deba@1990
    56
  class IterableBoolMap : protected DefaultMap<_Graph, _Item, int> {
deba@1913
    57
  private:
deba@1913
    58
    typedef _Graph Graph;
deba@1913
    59
    
deba@1931
    60
    typedef typename ItemSetTraits<Graph, _Item>::ItemIt KeyIt;
deba@1990
    61
    typedef DefaultMap<_Graph, _Item, int> Parent;
deba@1913
    62
    
deba@1931
    63
    std::vector<_Item> array;
deba@1913
    64
    int sep;
deba@1913
    65
deba@1913
    66
    const Graph& graph;
alpar@1677
    67
alpar@1677
    68
  public:
alpar@1805
    69
deba@1913
    70
    /// Indicates that the map if reference map.
deba@1913
    71
    typedef True ReferenceMapTag;
alpar@1805
    72
deba@1913
    73
    /// The key type
deba@1931
    74
    typedef _Item Key;
deba@1913
    75
    /// The value type
deba@1913
    76
    typedef bool Value;
deba@1913
    77
    /// The const reference type.    
deba@1913
    78
    typedef const Value& ConstReference;
deba@1913
    79
deba@1931
    80
  private:
deba@1931
    81
deba@1931
    82
    int position(const Key& key) const {
deba@1931
    83
      return Parent::operator[](key);
deba@1931
    84
    }
deba@1931
    85
deba@1931
    86
  public:
deba@1913
    87
deba@1913
    88
    /// \brief Refernce to the value of the map.
deba@1913
    89
    ///
deba@1913
    90
    /// This class is near to similar to the bool type. It can
deba@1913
    91
    /// be converted to bool and it has the same operators.
deba@1913
    92
    class Reference {
deba@1913
    93
      friend class IterableBoolMap;
deba@1913
    94
    private:
deba@1913
    95
      Reference(IterableBoolMap& map, const Key& key) 
deba@1913
    96
	: _key(key), _map(map) {} 
alpar@1677
    97
    public:
deba@1913
    98
deba@1913
    99
      Reference& operator=(const Reference& value) {
deba@1913
   100
	_map.set(_key, (bool)value);
deba@1913
   101
 	return *this;
deba@1913
   102
      }
deba@1913
   103
deba@1913
   104
      operator bool() const { 
deba@1913
   105
	return static_cast<const IterableBoolMap&>(_map)[_key]; 
deba@1913
   106
      }
deba@1913
   107
deba@1913
   108
      Reference& operator=(bool value) { 
deba@1913
   109
	_map.set(_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
      Reference& operator^=(bool value) {
deba@1913
   121
	_map.set(_key, _map[_key] ^ value); 
deba@1913
   122
	return *this; 	
deba@1913
   123
      }
deba@1913
   124
    private:
deba@1913
   125
      Key _key;
deba@1913
   126
      IterableBoolMap& _map; 
alpar@1677
   127
    };
deba@1913
   128
    
deba@1913
   129
    /// \brief Constructor of the Map with a default value.
deba@1913
   130
    ///
deba@1913
   131
    /// Constructor of the Map with a default value.
deba@2112
   132
    explicit IterableBoolMap(const Graph& _graph, bool def = false) 
deba@1913
   133
      : Parent(_graph), graph(_graph) {
deba@1931
   134
      for (KeyIt it(graph); it != INVALID; ++it) {
deba@1913
   135
        Parent::set(it, array.size());
deba@1913
   136
        array.push_back(it);
deba@1913
   137
      }
deba@1913
   138
      sep = (def ? array.size() : 0);
deba@1913
   139
    }
deba@1913
   140
deba@1913
   141
    /// \brief Const subscript operator of the map.
deba@1913
   142
    ///
deba@1913
   143
    /// Const subscript operator of the map.
deba@1931
   144
    bool operator[](const Key& key) const {
deba@1931
   145
      return position(key) < sep;
deba@1913
   146
    }
deba@1913
   147
deba@1913
   148
    /// \brief Subscript operator of the map.
deba@1913
   149
    ///
deba@1913
   150
    /// Subscript operator of the map.
deba@1931
   151
    Reference operator[](const Key& key) {
deba@1931
   152
      return Reference(*this, key);
deba@1913
   153
    }
deba@1913
   154
deba@1913
   155
    /// \brief Set operation of the map.
deba@1913
   156
    ///
deba@1913
   157
    /// Set operation of the map.
deba@1931
   158
    void set(const Key& key, bool value) {
deba@1931
   159
      int pos = position(key);
deba@1913
   160
      if (value) {
deba@1913
   161
        if (pos < sep) return;
deba@1931
   162
        Key tmp = array[sep];
deba@1931
   163
        array[sep] = key;
deba@1931
   164
        Parent::set(key, sep);
deba@1913
   165
        array[pos] = tmp;
deba@1913
   166
        Parent::set(tmp, pos); 
deba@1913
   167
        ++sep;
deba@1913
   168
      } else {
deba@1913
   169
        if (pos >= sep) return;
deba@1913
   170
        --sep;
deba@1931
   171
        Key tmp = array[sep];
deba@1931
   172
        array[sep] = key;
deba@1931
   173
        Parent::set(key, sep);
deba@1913
   174
        array[pos] = tmp;
deba@1913
   175
        Parent::set(tmp, pos);
deba@1913
   176
      }
deba@1913
   177
    }
deba@1913
   178
deba@1931
   179
    /// \brief Returns the number of the keys mapped to true.
deba@1913
   180
    ///
deba@1931
   181
    /// Returns the number of the keys mapped to true.
deba@1913
   182
    int trueNum() const {
deba@1913
   183
      return sep;
deba@1913
   184
    } 
deba@1913
   185
    
deba@1931
   186
    /// \brief Returns the number of the keys mapped to false.
deba@1913
   187
    ///
deba@1931
   188
    /// Returns the number of the keys mapped to false.
deba@1913
   189
    int falseNum() const {
deba@1913
   190
      return array.size() - sep;
deba@1913
   191
    }
deba@1913
   192
deba@1913
   193
    /// \brief Iterator for the keys mapped to true.
deba@1913
   194
    ///
deba@1913
   195
    /// Iterator for the keys mapped to true. It works
deba@1913
   196
    /// like a graph item iterator in the map, it can be converted
deba@1931
   197
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   198
    /// if the iterator leave the last valid key it will be equal to 
deba@1913
   199
    /// \c INVALID.
deba@1931
   200
    class TrueIt : public Key {
alpar@1677
   201
    public:
deba@1931
   202
      typedef Key Parent;
deba@1913
   203
      
deba@1913
   204
      /// \brief Creates an iterator.
deba@1913
   205
      ///
deba@1913
   206
      /// Creates an iterator. It iterates on the 
deba@1913
   207
      /// keys which mapped to true.
alpar@1953
   208
      /// \param _map The IterableIntMap
deba@2112
   209
      explicit TrueIt(const IterableBoolMap& _map) 
deba@1913
   210
        : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID), 
deba@1913
   211
          map(&_map) {}
deba@1913
   212
deba@1913
   213
      /// \brief Invalid constructor \& conversion.
deba@1913
   214
      ///
deba@1931
   215
      /// This constructor initializes the key to be invalid.
deba@1913
   216
      /// \sa Invalid for more details.
deba@1913
   217
      TrueIt(Invalid) : Parent(INVALID), map(0) {}
deba@1913
   218
deba@1913
   219
      /// \brief Increment operator.
deba@1913
   220
      ///
deba@1913
   221
      /// Increment Operator.
deba@1913
   222
      TrueIt& operator++() {
deba@1913
   223
        int pos = map->position(*this);
deba@1913
   224
        Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID);
deba@1913
   225
        return *this;
deba@1913
   226
      }
deba@1913
   227
deba@1913
   228
      
deba@1913
   229
    private:
deba@1913
   230
      const IterableBoolMap* map;
deba@1913
   231
    };
deba@1913
   232
deba@1913
   233
    /// \brief Iterator for the keys mapped to false.
deba@1913
   234
    ///
deba@1913
   235
    /// Iterator for the keys mapped to false. It works
deba@1913
   236
    /// like a graph item iterator in the map, it can be converted
deba@1931
   237
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   238
    /// if the iterator leave the last valid key it will be equal to 
deba@1913
   239
    /// \c INVALID.
deba@1931
   240
    class FalseIt : public Key {
deba@1913
   241
    public:
deba@1931
   242
      typedef Key Parent;
deba@1913
   243
      
deba@1913
   244
      /// \brief Creates an iterator.
deba@1913
   245
      ///
deba@1913
   246
      /// Creates an iterator. It iterates on the 
deba@1913
   247
      /// keys which mapped to false.
alpar@1953
   248
      /// \param _map The IterableIntMap
deba@2112
   249
      explicit FalseIt(const IterableBoolMap& _map) 
deba@1931
   250
        : Parent(_map.sep < (int)_map.array.size() ? 
deba@1931
   251
                 _map.array.back() : INVALID), map(&_map) {}
deba@1913
   252
deba@1913
   253
      /// \brief Invalid constructor \& conversion.
deba@1913
   254
      ///
deba@1931
   255
      /// This constructor initializes the key to be invalid.
deba@1913
   256
      /// \sa Invalid for more details.
deba@1913
   257
      FalseIt(Invalid) : Parent(INVALID), map(0) {}
deba@1913
   258
deba@1913
   259
      /// \brief Increment operator.
deba@1913
   260
      ///
deba@1913
   261
      /// Increment Operator.
deba@1913
   262
      FalseIt& operator++() {
deba@1913
   263
        int pos = map->position(*this);
deba@1913
   264
        Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID);
deba@1913
   265
        return *this;
deba@1913
   266
      }
deba@1913
   267
deba@1913
   268
    private:
deba@1913
   269
      const IterableBoolMap* map;
deba@1913
   270
    };
deba@1913
   271
deba@1931
   272
    /// \brief Iterator for the keys mapped to a given value.
deba@1931
   273
    ///
deba@1931
   274
    /// Iterator for the keys mapped to a given value. It works
deba@1931
   275
    /// like a graph item iterator in the map, it can be converted
deba@1931
   276
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   277
    /// if the iterator leave the last valid key it will be equal to 
deba@1931
   278
    /// \c INVALID.
deba@1931
   279
    class ItemIt : public Key {
deba@1931
   280
    public:
deba@1931
   281
      typedef Key Parent;
deba@1931
   282
      
deba@1931
   283
      /// \brief Creates an iterator.
deba@1931
   284
      ///
deba@1931
   285
      /// Creates an iterator. It iterates on the 
deba@1931
   286
      /// keys which mapped to false.
alpar@1953
   287
      /// \param _map The IterableIntMap
deba@1931
   288
      /// \param value Which elements should be iterated.
deba@1931
   289
      ItemIt(const IterableBoolMap& _map, bool value) 
deba@1931
   290
        : Parent(value ? (_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID) :
deba@1931
   291
                 (_map.sep < (int)_map.array.size() ? 
deba@1931
   292
                  _map.array.back() : INVALID)), map(&_map) {}
deba@1931
   293
deba@1931
   294
      /// \brief Invalid constructor \& conversion.
deba@1931
   295
      ///
deba@1931
   296
      /// This constructor initializes the key to be invalid.
deba@1931
   297
      /// \sa Invalid for more details.
deba@1931
   298
      ItemIt(Invalid) : Parent(INVALID), map(0) {}
deba@1931
   299
deba@1931
   300
      /// \brief Increment operator.
deba@1931
   301
      ///
deba@1931
   302
      /// Increment Operator.
deba@1931
   303
      ItemIt& operator++() {
deba@1931
   304
        int pos = map->position(*this);
deba@1931
   305
        int sep = pos >= map->sep ? map->sep : 0;
deba@1931
   306
        Parent::operator=(pos > sep ? map->array[pos - 1] : INVALID);
deba@1931
   307
        return *this;
deba@1931
   308
      }
deba@1931
   309
deba@1931
   310
    private:
deba@1931
   311
      const IterableBoolMap* map;
deba@1931
   312
    };
deba@1931
   313
deba@1913
   314
  protected:
deba@1913
   315
    
deba@1931
   316
    virtual void add(const Key& key) {
deba@1931
   317
      Parent::add(key);
deba@1931
   318
      Parent::set(key, array.size());
deba@1931
   319
      array.push_back(key);
deba@1913
   320
    }
deba@1913
   321
deba@1931
   322
    virtual void add(const std::vector<Key>& keys) {
deba@1931
   323
      Parent::add(keys);
deba@1931
   324
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1931
   325
        Parent::set(keys[i], array.size());
deba@1931
   326
        array.push_back(keys[i]);
deba@1913
   327
      }
deba@1913
   328
    }
deba@1913
   329
deba@1931
   330
    virtual void erase(const Key& key) {
deba@1931
   331
      int pos = position(key); 
deba@1913
   332
      if (pos < sep) {
deba@1913
   333
        --sep;
deba@1913
   334
        Parent::set(array[sep], pos);
deba@1913
   335
        array[pos] = array[sep];
deba@1913
   336
        Parent::set(array.back(), sep);
deba@1913
   337
        array[sep] = array.back();
deba@1913
   338
        array.pop_back();
deba@1913
   339
      } else {
deba@1913
   340
        Parent::set(array.back(), pos);
deba@1913
   341
        array[pos] = array.back();
deba@1913
   342
        array.pop_back();
deba@1913
   343
      }
deba@1931
   344
      Parent::erase(key);
deba@1913
   345
    }
deba@1913
   346
deba@1931
   347
    virtual void erase(const std::vector<Key>& keys) {
deba@1931
   348
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1931
   349
        int pos = position(keys[i]); 
deba@1913
   350
        if (pos < sep) {
deba@1913
   351
          --sep;
deba@1913
   352
          Parent::set(array[sep], pos);
deba@1913
   353
          array[pos] = array[sep];
deba@1913
   354
          Parent::set(array.back(), sep);
deba@1913
   355
          array[sep] = array.back();
deba@1913
   356
          array.pop_back();
deba@1913
   357
        } else {
deba@1913
   358
          Parent::set(array.back(), pos);
deba@1913
   359
          array[pos] = array.back();
deba@1913
   360
          array.pop_back();
deba@1913
   361
        }
deba@1913
   362
      }
deba@1931
   363
      Parent::erase(keys);
deba@1913
   364
    }    
deba@1913
   365
deba@1913
   366
    virtual void build() {
deba@1913
   367
      Parent::build();
deba@1931
   368
      for (KeyIt it(graph); it != INVALID; ++it) {
deba@1913
   369
        Parent::set(it, array.size());
deba@1913
   370
        array.push_back(it);
deba@1913
   371
      }
deba@1913
   372
      sep = 0;      
deba@1913
   373
    }
deba@1913
   374
deba@1913
   375
    virtual void clear() {
deba@1913
   376
      array.clear();
deba@1913
   377
      sep = 0;
deba@1913
   378
      Parent::clear();
deba@1913
   379
    }
deba@1913
   380
    
alpar@1677
   381
  };
alpar@1873
   382
  
deba@1752
   383
deba@1752
   384
  namespace _iterable_maps_bits {
deba@1752
   385
    template <typename Item>
deba@1752
   386
    struct IterableIntMapNode {
deba@1913
   387
      IterableIntMapNode() : value(-1) {}
deba@1810
   388
      IterableIntMapNode(int _value) : value(_value) {}
deba@1752
   389
      Item prev, next;
deba@1752
   390
      int value;
deba@1752
   391
    };
deba@1752
   392
  }
deba@1752
   393
deba@1931
   394
  ///\ingroup graph_maps
deba@1752
   395
  ///
deba@1752
   396
  /// \brief Dynamic iterable integer map.
deba@1752
   397
  ///
deba@1810
   398
  /// This class provides a special graph map type which can store
deba@1810
   399
  /// for each graph item(node, edge, etc.) an integer value. For each
deba@1810
   400
  /// non negative value it is possible to iterate on the keys which
deba@1810
   401
  /// mapped to the given value.
deba@1810
   402
  /// 
deba@1810
   403
  /// \param _Graph The graph type.
deba@1810
   404
  /// \param _Item One of the graph's item type, the key of the map.
deba@1752
   405
  template <typename _Graph, typename _Item>
deba@1990
   406
  class IterableIntMap 
deba@2031
   407
    : protected MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   408
                                       IterableIntMapNode<_Item> > >{
deba@1752
   409
  public:
deba@2031
   410
    typedef MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   411
                                   IterableIntMapNode<_Item> > > Parent;
deba@1752
   412
deba@1810
   413
    /// The key type
deba@1752
   414
    typedef _Item Key;
deba@1810
   415
    /// The value type
deba@1752
   416
    typedef int Value;
deba@1810
   417
    /// The graph type
deba@1752
   418
    typedef _Graph Graph;
deba@1752
   419
deba@1810
   420
    /// \brief Constructor of the Map.
deba@1810
   421
    ///
deba@1810
   422
    /// Constructor of the Map. It set all values -1.
deba@1810
   423
    explicit IterableIntMap(const Graph& graph) 
deba@1913
   424
      : Parent(graph) {}
deba@1810
   425
deba@1810
   426
    /// \brief Constructor of the Map with a given value.
deba@1810
   427
    ///
deba@1810
   428
    /// Constructor of the Map with a given value.
deba@1810
   429
    explicit IterableIntMap(const Graph& graph, int value) 
deba@1810
   430
      : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
deba@1810
   431
      if (value >= 0) {
deba@1810
   432
	for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@1810
   433
	  lace(it);
deba@1810
   434
	}
deba@1810
   435
      }
deba@1810
   436
    }
deba@1752
   437
deba@1752
   438
  private:
deba@1752
   439
    
deba@1752
   440
    void unlace(const Key& key) {
deba@1752
   441
      typename Parent::Value& node = Parent::operator[](key);
deba@1752
   442
      if (node.value < 0) return;
deba@1752
   443
      if (node.prev != INVALID) {
deba@1752
   444
	Parent::operator[](node.prev).next = node.next;	
deba@1752
   445
      } else {
deba@1752
   446
	first[node.value] = node.next;
deba@1752
   447
      }
deba@1752
   448
      if (node.next != INVALID) {
deba@1752
   449
	Parent::operator[](node.next).prev = node.prev;
deba@1752
   450
      }
deba@1752
   451
      while (!first.empty() && first.back() == INVALID) {
deba@1752
   452
	first.pop_back();
deba@1752
   453
      }
deba@1752
   454
    }
deba@1752
   455
deba@1752
   456
    void lace(const Key& key) {
deba@1752
   457
      typename Parent::Value& node = Parent::operator[](key);
deba@1752
   458
      if (node.value < 0) return;
deba@1752
   459
      if (node.value >= (int)first.size()) {
deba@1752
   460
	first.resize(node.value + 1, INVALID);
deba@1752
   461
      } 
deba@1752
   462
      node.prev = INVALID;
deba@1752
   463
      node.next = first[node.value];
deba@1752
   464
      if (node.next != INVALID) {
deba@1752
   465
	Parent::operator[](node.next).prev = key;	
deba@1752
   466
      }
deba@1752
   467
      first[node.value] = key;
deba@1752
   468
    }
deba@1752
   469
deba@1752
   470
  public:
deba@1752
   471
deba@1810
   472
    /// Indicates that the map if reference map.
deba@1752
   473
    typedef True ReferenceMapTag;
deba@1752
   474
deba@1810
   475
    /// \brief Refernce to the value of the map.
deba@1810
   476
    ///
deba@1810
   477
    /// This class is near to similar to the int type. It can
deba@1810
   478
    /// be converted to int and it has the same operators.
deba@1752
   479
    class Reference {
deba@1752
   480
      friend class IterableIntMap;
deba@1752
   481
    private:
deba@1752
   482
      Reference(IterableIntMap& map, const Key& key) 
deba@1752
   483
	: _key(key), _map(map) {} 
deba@1752
   484
    public:
deba@1752
   485
deba@1752
   486
      Reference& operator=(const Reference& value) {
deba@1752
   487
	_map.set(_key, (const int&)value);
deba@1752
   488
 	return *this;
deba@1752
   489
      }
deba@1752
   490
deba@1752
   491
      operator const int&() const { 
deba@1752
   492
	return static_cast<const IterableIntMap&>(_map)[_key]; 
deba@1752
   493
      }
deba@1752
   494
deba@1752
   495
      Reference& operator=(int value) { 
deba@1752
   496
	_map.set(_key, value); 
deba@1752
   497
	return *this; 
deba@1752
   498
      }
deba@1759
   499
      Reference& operator++() {
deba@1759
   500
	_map.set(_key, _map[_key] + 1); 
deba@1759
   501
	return *this; 	
deba@1759
   502
      }
deba@1759
   503
      int operator++(int) {
deba@1759
   504
	int value = _map[_key];
deba@1759
   505
	_map.set(_key, value + 1); 
deba@1759
   506
	return value; 	
deba@1759
   507
      }
deba@1759
   508
      Reference& operator--() {
deba@1759
   509
	_map.set(_key, _map[_key] - 1); 
deba@1759
   510
	return *this; 	
deba@1759
   511
      }
deba@1759
   512
      int operator--(int) {
deba@1759
   513
	int value = _map[_key];
deba@1759
   514
	_map.set(_key, value - 1); 
deba@1759
   515
	return value; 	
deba@1759
   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
      Reference& operator>>=(int value) { 
deba@1752
   554
	_map.set(_key, _map[_key] >> value); 
deba@1752
   555
	return *this;
deba@1752
   556
      }
deba@1752
   557
deba@1752
   558
    private:
deba@1752
   559
      Key _key;
deba@1752
   560
      IterableIntMap& _map; 
deba@1752
   561
    };
deba@1810
   562
deba@1810
   563
    /// The const reference type.    
deba@1752
   564
    typedef const Value& ConstReference;
deba@1752
   565
deba@1810
   566
    /// \brief Gives back the maximal value plus one.
deba@1810
   567
    ///
deba@1810
   568
    /// Gives back the maximal value plus one.
deba@1931
   569
    unsigned int size() const {
deba@1931
   570
      return first.size();
deba@1752
   571
    }
deba@1752
   572
    
deba@1810
   573
    /// \brief Set operation of the map.
deba@1810
   574
    ///
deba@1810
   575
    /// Set operation of the map.
deba@1752
   576
    void set(const Key& key, const Value& value) {
deba@1752
   577
      unlace(key);
deba@1752
   578
      Parent::operator[](key).value = value;
deba@1752
   579
      lace(key);
deba@1752
   580
    }
deba@1752
   581
deba@1810
   582
    /// \brief Const subscript operator of the map.
deba@1810
   583
    ///
deba@1810
   584
    /// Const subscript operator of the map.
deba@1752
   585
    const Value& operator[](const Key& key) const {
deba@1752
   586
      return Parent::operator[](key).value;
deba@1752
   587
    }
deba@1752
   588
deba@1810
   589
    /// \brief Subscript operator of the map.
deba@1810
   590
    ///
deba@1810
   591
    /// Subscript operator of the map.
deba@1752
   592
    Reference operator[](const Key& key) {
deba@1752
   593
      return Reference(*this, key);
deba@1752
   594
    }
deba@1752
   595
deba@1810
   596
    /// \brief Iterator for the keys with the same value.
deba@1810
   597
    ///
deba@1810
   598
    /// Iterator for the keys with the same value. It works
deba@1810
   599
    /// like a graph item iterator in the map, it can be converted
deba@1810
   600
    /// the item type of the map, incremented with \c ++ operator, and
deba@1810
   601
    /// if the iterator leave the last valid item it will be equal to 
deba@1810
   602
    /// \c INVALID.
deba@1752
   603
    class ItemIt : public _Item {
deba@1752
   604
    public:
deba@1752
   605
      typedef _Item Parent;
deba@1752
   606
deba@1810
   607
      /// \brief Invalid constructor \& conversion.
deba@1810
   608
      ///
deba@1810
   609
      /// This constructor initializes the item to be invalid.
deba@1810
   610
      /// \sa Invalid for more details.
deba@1752
   611
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@1752
   612
deba@1810
   613
      /// \brief Creates an iterator with a value.
deba@1810
   614
      ///
deba@1810
   615
      /// Creates an iterator with a value. It iterates on the 
deba@1810
   616
      /// keys which have the given value.
deba@1810
   617
      /// \param map The IterableIntMap
deba@1810
   618
      /// \param value The value
deba@1752
   619
      ItemIt(const IterableIntMap& map, int value) : _map(&map) {
deba@1752
   620
	if (value < 0 || value >= (int)_map->first.size()) {	  
deba@1752
   621
	  Parent::operator=(INVALID);
deba@1752
   622
	} else {
deba@1752
   623
	  Parent::operator=(_map->first[value]);
deba@1752
   624
	}
deba@1752
   625
      } 
deba@1752
   626
deba@1810
   627
      /// \brief Increment operator.
deba@1810
   628
      ///
deba@1810
   629
      /// Increment Operator.
deba@1752
   630
      ItemIt& operator++() {
deba@1752
   631
	Parent::operator=(_map->IterableIntMap::Parent::
deba@1752
   632
			  operator[](static_cast<Parent&>(*this)).next);
deba@1752
   633
	return *this;
deba@1752
   634
      }
deba@1752
   635
deba@1752
   636
deba@1752
   637
    private:
deba@1752
   638
      const IterableIntMap* _map;
deba@1752
   639
    };
deba@1752
   640
deba@1752
   641
  protected:
deba@1752
   642
    
deba@1752
   643
    virtual void erase(const Key& key) {
deba@1752
   644
      unlace(key);
deba@1752
   645
      Parent::erase(key);
deba@1752
   646
    }
deba@1752
   647
deba@1913
   648
    virtual void erase(const std::vector<Key>& keys) {
deba@1931
   649
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1913
   650
        unlace(keys[i]);
deba@1913
   651
      }
deba@1913
   652
      Parent::erase(keys);
deba@1913
   653
    }
deba@1913
   654
deba@1752
   655
    virtual void clear() {
deba@1752
   656
      first.clear();
deba@1752
   657
      Parent::clear();
deba@1752
   658
    }
deba@1752
   659
deba@1752
   660
  private:
deba@1752
   661
    std::vector<_Item> first;
deba@1752
   662
  };
deba@1752
   663
deba@1931
   664
  namespace _iterable_maps_bits {
deba@1931
   665
    template <typename Item, typename Value>
deba@1931
   666
    struct IterableValueMapNode {
deba@1931
   667
      IterableValueMapNode(Value _value = Value()) : value(_value) {}
deba@1931
   668
      Item prev, next;
deba@1931
   669
      Value value;
deba@1931
   670
    };
deba@1931
   671
  }
deba@1931
   672
deba@1931
   673
  ///\ingroup graph_maps
deba@1931
   674
  ///
deba@1931
   675
  /// \brief Dynamic iterable map for comparable values.
deba@1931
   676
  ///
deba@1931
   677
  /// This class provides a special graph map type which can store
deba@1931
   678
  /// for each graph item(node, edge, etc.) a value. For each
deba@1931
   679
  /// value it is possible to iterate on the keys which mapped to the 
deba@1931
   680
  /// given value. The type stores for each value a linked list with
deba@1931
   681
  /// the items which mapped to the value, and the values are stored
deba@1931
   682
  /// in balanced binary tree. The values of the map can be accessed
deba@1931
   683
  /// with stl compatible forward iterator.
deba@1931
   684
  ///
deba@1931
   685
  /// This type is not reference map so it cannot be modified with
deba@1931
   686
  /// the subscription operator.
deba@1931
   687
  ///
deba@1931
   688
  /// \see InvertableMap
deba@1931
   689
  /// 
deba@1931
   690
  /// \param _Graph The graph type.
deba@1931
   691
  /// \param _Item One of the graph's item type, the key of the map.
deba@1931
   692
  /// \param _Value Any comparable value type.
deba@1931
   693
  template <typename _Graph, typename _Item, typename _Value>
deba@1990
   694
  class IterableValueMap 
deba@2031
   695
    : protected MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   696
                                       IterableValueMapNode<_Item, _Value> > >{
deba@1931
   697
  public:
deba@2031
   698
    typedef MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   699
                                   IterableValueMapNode<_Item, _Value> > >
deba@2031
   700
    Parent;
deba@1931
   701
deba@1931
   702
    /// The key type
deba@1931
   703
    typedef _Item Key;
deba@1931
   704
    /// The value type
deba@1931
   705
    typedef _Value Value;
deba@1931
   706
    /// The graph type
deba@1931
   707
    typedef _Graph Graph;
deba@1931
   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@2031
   718
      for (typename Parent::ItemIt it(*this); 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@2031
   906
      for (typename Parent::ItemIt it(*this); 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
}
deba@2210
   922
deba@2210
   923
#endif