lemon/iterable_maps.h
author deba
Tue, 04 Dec 2007 14:08:27 +0000
changeset 2531 426a4e35e167
parent 2391 14a343be7a5a
child 2553 bfced05fa852
permissions -rw-r--r--
rename graphs script
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@2391
     5
 * Copyright (C) 2003-2007
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@2386
   100
	_map.set(_key, static_cast<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@2496
   179
    /// \brief Set all items.
deba@2496
   180
    ///
deba@2496
   181
    /// Set all items in the map.
deba@2496
   182
    /// \note Constant time operation.
deba@2496
   183
    void setAll(bool value) {
deba@2496
   184
      sep = (value ? array.size() : 0);      
deba@2496
   185
    }
deba@2496
   186
deba@1931
   187
    /// \brief Returns the number of the keys mapped to true.
deba@1913
   188
    ///
deba@1931
   189
    /// Returns the number of the keys mapped to true.
deba@1913
   190
    int trueNum() const {
deba@1913
   191
      return sep;
deba@1913
   192
    } 
deba@1913
   193
    
deba@1931
   194
    /// \brief Returns the number of the keys mapped to false.
deba@1913
   195
    ///
deba@1931
   196
    /// Returns the number of the keys mapped to false.
deba@1913
   197
    int falseNum() const {
deba@1913
   198
      return array.size() - sep;
deba@1913
   199
    }
deba@1913
   200
deba@1913
   201
    /// \brief Iterator for the keys mapped to true.
deba@1913
   202
    ///
deba@1913
   203
    /// Iterator for the keys mapped to true. It works
deba@1913
   204
    /// like a graph item iterator in the map, it can be converted
deba@1931
   205
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   206
    /// if the iterator leave the last valid key it will be equal to 
deba@1913
   207
    /// \c INVALID.
deba@1931
   208
    class TrueIt : public Key {
alpar@1677
   209
    public:
deba@1931
   210
      typedef Key Parent;
deba@1913
   211
      
deba@1913
   212
      /// \brief Creates an iterator.
deba@1913
   213
      ///
deba@1913
   214
      /// Creates an iterator. It iterates on the 
deba@1913
   215
      /// keys which mapped to true.
alpar@1953
   216
      /// \param _map The IterableIntMap
deba@2112
   217
      explicit TrueIt(const IterableBoolMap& _map) 
deba@1913
   218
        : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID), 
deba@1913
   219
          map(&_map) {}
deba@1913
   220
deba@1913
   221
      /// \brief Invalid constructor \& conversion.
deba@1913
   222
      ///
deba@1931
   223
      /// This constructor initializes the key to be invalid.
deba@1913
   224
      /// \sa Invalid for more details.
deba@1913
   225
      TrueIt(Invalid) : Parent(INVALID), map(0) {}
deba@1913
   226
deba@1913
   227
      /// \brief Increment operator.
deba@1913
   228
      ///
deba@1913
   229
      /// Increment Operator.
deba@1913
   230
      TrueIt& operator++() {
deba@1913
   231
        int pos = map->position(*this);
deba@1913
   232
        Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID);
deba@1913
   233
        return *this;
deba@1913
   234
      }
deba@1913
   235
deba@1913
   236
      
deba@1913
   237
    private:
deba@1913
   238
      const IterableBoolMap* map;
deba@1913
   239
    };
deba@1913
   240
deba@1913
   241
    /// \brief Iterator for the keys mapped to false.
deba@1913
   242
    ///
deba@1913
   243
    /// Iterator for the keys mapped to false. It works
deba@1913
   244
    /// like a graph item iterator in the map, it can be converted
deba@1931
   245
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   246
    /// if the iterator leave the last valid key it will be equal to 
deba@1913
   247
    /// \c INVALID.
deba@1931
   248
    class FalseIt : public Key {
deba@1913
   249
    public:
deba@1931
   250
      typedef Key Parent;
deba@1913
   251
      
deba@1913
   252
      /// \brief Creates an iterator.
deba@1913
   253
      ///
deba@1913
   254
      /// Creates an iterator. It iterates on the 
deba@1913
   255
      /// keys which mapped to false.
alpar@1953
   256
      /// \param _map The IterableIntMap
deba@2112
   257
      explicit FalseIt(const IterableBoolMap& _map) 
deba@2386
   258
        : Parent(_map.sep < int(_map.array.size()) ? 
deba@1931
   259
                 _map.array.back() : INVALID), map(&_map) {}
deba@1913
   260
deba@1913
   261
      /// \brief Invalid constructor \& conversion.
deba@1913
   262
      ///
deba@1931
   263
      /// This constructor initializes the key to be invalid.
deba@1913
   264
      /// \sa Invalid for more details.
deba@1913
   265
      FalseIt(Invalid) : Parent(INVALID), map(0) {}
deba@1913
   266
deba@1913
   267
      /// \brief Increment operator.
deba@1913
   268
      ///
deba@1913
   269
      /// Increment Operator.
deba@1913
   270
      FalseIt& operator++() {
deba@1913
   271
        int pos = map->position(*this);
deba@1913
   272
        Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID);
deba@1913
   273
        return *this;
deba@1913
   274
      }
deba@1913
   275
deba@1913
   276
    private:
deba@1913
   277
      const IterableBoolMap* map;
deba@1913
   278
    };
deba@1913
   279
deba@1931
   280
    /// \brief Iterator for the keys mapped to a given value.
deba@1931
   281
    ///
deba@1931
   282
    /// Iterator for the keys mapped to a given value. It works
deba@1931
   283
    /// like a graph item iterator in the map, it can be converted
deba@1931
   284
    /// the key type of the map, incremented with \c ++ operator, and
deba@1931
   285
    /// if the iterator leave the last valid key it will be equal to 
deba@1931
   286
    /// \c INVALID.
deba@1931
   287
    class ItemIt : public Key {
deba@1931
   288
    public:
deba@1931
   289
      typedef Key Parent;
deba@1931
   290
      
deba@1931
   291
      /// \brief Creates an iterator.
deba@1931
   292
      ///
deba@1931
   293
      /// Creates an iterator. It iterates on the 
deba@1931
   294
      /// keys which mapped to false.
alpar@1953
   295
      /// \param _map The IterableIntMap
deba@1931
   296
      /// \param value Which elements should be iterated.
deba@1931
   297
      ItemIt(const IterableBoolMap& _map, bool value) 
deba@1931
   298
        : Parent(value ? (_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID) :
deba@2386
   299
                 (_map.sep < int(_map.array.size()) ? 
deba@1931
   300
                  _map.array.back() : INVALID)), map(&_map) {}
deba@1931
   301
deba@1931
   302
      /// \brief Invalid constructor \& conversion.
deba@1931
   303
      ///
deba@1931
   304
      /// This constructor initializes the key to be invalid.
deba@1931
   305
      /// \sa Invalid for more details.
deba@1931
   306
      ItemIt(Invalid) : Parent(INVALID), map(0) {}
deba@1931
   307
deba@1931
   308
      /// \brief Increment operator.
deba@1931
   309
      ///
deba@1931
   310
      /// Increment Operator.
deba@1931
   311
      ItemIt& operator++() {
deba@1931
   312
        int pos = map->position(*this);
deba@1931
   313
        int sep = pos >= map->sep ? map->sep : 0;
deba@1931
   314
        Parent::operator=(pos > sep ? map->array[pos - 1] : INVALID);
deba@1931
   315
        return *this;
deba@1931
   316
      }
deba@1931
   317
deba@1931
   318
    private:
deba@1931
   319
      const IterableBoolMap* map;
deba@1931
   320
    };
deba@1931
   321
deba@1913
   322
  protected:
deba@1913
   323
    
deba@1931
   324
    virtual void add(const Key& key) {
deba@1931
   325
      Parent::add(key);
deba@1931
   326
      Parent::set(key, array.size());
deba@1931
   327
      array.push_back(key);
deba@1913
   328
    }
deba@1913
   329
deba@1931
   330
    virtual void add(const std::vector<Key>& keys) {
deba@1931
   331
      Parent::add(keys);
deba@2386
   332
      for (int i = 0; i < int(keys.size()); ++i) {
deba@1931
   333
        Parent::set(keys[i], array.size());
deba@1931
   334
        array.push_back(keys[i]);
deba@1913
   335
      }
deba@1913
   336
    }
deba@1913
   337
deba@1931
   338
    virtual void erase(const Key& key) {
deba@1931
   339
      int pos = position(key); 
deba@1913
   340
      if (pos < sep) {
deba@1913
   341
        --sep;
deba@1913
   342
        Parent::set(array[sep], pos);
deba@1913
   343
        array[pos] = array[sep];
deba@1913
   344
        Parent::set(array.back(), sep);
deba@1913
   345
        array[sep] = array.back();
deba@1913
   346
        array.pop_back();
deba@1913
   347
      } else {
deba@1913
   348
        Parent::set(array.back(), pos);
deba@1913
   349
        array[pos] = array.back();
deba@1913
   350
        array.pop_back();
deba@1913
   351
      }
deba@1931
   352
      Parent::erase(key);
deba@1913
   353
    }
deba@1913
   354
deba@1931
   355
    virtual void erase(const std::vector<Key>& keys) {
deba@2386
   356
      for (int i = 0; i < int(keys.size()); ++i) {
deba@1931
   357
        int pos = position(keys[i]); 
deba@1913
   358
        if (pos < sep) {
deba@1913
   359
          --sep;
deba@1913
   360
          Parent::set(array[sep], pos);
deba@1913
   361
          array[pos] = array[sep];
deba@1913
   362
          Parent::set(array.back(), sep);
deba@1913
   363
          array[sep] = array.back();
deba@1913
   364
          array.pop_back();
deba@1913
   365
        } else {
deba@1913
   366
          Parent::set(array.back(), pos);
deba@1913
   367
          array[pos] = array.back();
deba@1913
   368
          array.pop_back();
deba@1913
   369
        }
deba@1913
   370
      }
deba@1931
   371
      Parent::erase(keys);
deba@1913
   372
    }    
deba@1913
   373
deba@1913
   374
    virtual void build() {
deba@1913
   375
      Parent::build();
deba@1931
   376
      for (KeyIt it(graph); it != INVALID; ++it) {
deba@1913
   377
        Parent::set(it, array.size());
deba@1913
   378
        array.push_back(it);
deba@1913
   379
      }
deba@1913
   380
      sep = 0;      
deba@1913
   381
    }
deba@1913
   382
deba@1913
   383
    virtual void clear() {
deba@1913
   384
      array.clear();
deba@1913
   385
      sep = 0;
deba@1913
   386
      Parent::clear();
deba@1913
   387
    }
deba@1913
   388
    
alpar@1677
   389
  };
alpar@1873
   390
  
deba@1752
   391
deba@1752
   392
  namespace _iterable_maps_bits {
deba@1752
   393
    template <typename Item>
deba@1752
   394
    struct IterableIntMapNode {
deba@1913
   395
      IterableIntMapNode() : value(-1) {}
deba@1810
   396
      IterableIntMapNode(int _value) : value(_value) {}
deba@1752
   397
      Item prev, next;
deba@1752
   398
      int value;
deba@1752
   399
    };
deba@1752
   400
  }
deba@1752
   401
deba@1931
   402
  ///\ingroup graph_maps
deba@1752
   403
  ///
deba@1752
   404
  /// \brief Dynamic iterable integer map.
deba@1752
   405
  ///
deba@1810
   406
  /// This class provides a special graph map type which can store
deba@1810
   407
  /// for each graph item(node, edge, etc.) an integer value. For each
deba@1810
   408
  /// non negative value it is possible to iterate on the keys which
deba@1810
   409
  /// mapped to the given value.
deba@1810
   410
  /// 
deba@1810
   411
  /// \param _Graph The graph type.
deba@1810
   412
  /// \param _Item One of the graph's item type, the key of the map.
deba@1752
   413
  template <typename _Graph, typename _Item>
deba@1990
   414
  class IterableIntMap 
deba@2031
   415
    : protected MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   416
                                       IterableIntMapNode<_Item> > >{
deba@1752
   417
  public:
deba@2031
   418
    typedef MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   419
                                   IterableIntMapNode<_Item> > > Parent;
deba@1752
   420
deba@1810
   421
    /// The key type
deba@1752
   422
    typedef _Item Key;
deba@1810
   423
    /// The value type
deba@1752
   424
    typedef int Value;
deba@1810
   425
    /// The graph type
deba@1752
   426
    typedef _Graph Graph;
deba@1752
   427
deba@1810
   428
    /// \brief Constructor of the Map.
deba@1810
   429
    ///
deba@1810
   430
    /// Constructor of the Map. It set all values -1.
deba@1810
   431
    explicit IterableIntMap(const Graph& graph) 
deba@1913
   432
      : Parent(graph) {}
deba@1810
   433
deba@1810
   434
    /// \brief Constructor of the Map with a given value.
deba@1810
   435
    ///
deba@1810
   436
    /// Constructor of the Map with a given value.
deba@1810
   437
    explicit IterableIntMap(const Graph& graph, int value) 
deba@1810
   438
      : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
deba@1810
   439
      if (value >= 0) {
deba@1810
   440
	for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@1810
   441
	  lace(it);
deba@1810
   442
	}
deba@1810
   443
      }
deba@1810
   444
    }
deba@1752
   445
deba@1752
   446
  private:
deba@1752
   447
    
deba@1752
   448
    void unlace(const Key& key) {
deba@1752
   449
      typename Parent::Value& node = Parent::operator[](key);
deba@1752
   450
      if (node.value < 0) return;
deba@1752
   451
      if (node.prev != INVALID) {
deba@1752
   452
	Parent::operator[](node.prev).next = node.next;	
deba@1752
   453
      } else {
deba@1752
   454
	first[node.value] = node.next;
deba@1752
   455
      }
deba@1752
   456
      if (node.next != INVALID) {
deba@1752
   457
	Parent::operator[](node.next).prev = node.prev;
deba@1752
   458
      }
deba@1752
   459
      while (!first.empty() && first.back() == INVALID) {
deba@1752
   460
	first.pop_back();
deba@1752
   461
      }
deba@1752
   462
    }
deba@1752
   463
deba@1752
   464
    void lace(const Key& key) {
deba@1752
   465
      typename Parent::Value& node = Parent::operator[](key);
deba@1752
   466
      if (node.value < 0) return;
deba@2386
   467
      if (node.value >= int(first.size())) {
deba@1752
   468
	first.resize(node.value + 1, INVALID);
deba@1752
   469
      } 
deba@1752
   470
      node.prev = INVALID;
deba@1752
   471
      node.next = first[node.value];
deba@1752
   472
      if (node.next != INVALID) {
deba@1752
   473
	Parent::operator[](node.next).prev = key;	
deba@1752
   474
      }
deba@1752
   475
      first[node.value] = key;
deba@1752
   476
    }
deba@1752
   477
deba@1752
   478
  public:
deba@1752
   479
deba@1810
   480
    /// Indicates that the map if reference map.
deba@1752
   481
    typedef True ReferenceMapTag;
deba@1752
   482
deba@1810
   483
    /// \brief Refernce to the value of the map.
deba@1810
   484
    ///
deba@1810
   485
    /// This class is near to similar to the int type. It can
deba@1810
   486
    /// be converted to int and it has the same operators.
deba@1752
   487
    class Reference {
deba@1752
   488
      friend class IterableIntMap;
deba@1752
   489
    private:
deba@1752
   490
      Reference(IterableIntMap& map, const Key& key) 
deba@1752
   491
	: _key(key), _map(map) {} 
deba@1752
   492
    public:
deba@1752
   493
deba@1752
   494
      Reference& operator=(const Reference& value) {
deba@2386
   495
	_map.set(_key, static_cast<const int&>(value));
deba@1752
   496
 	return *this;
deba@1752
   497
      }
deba@1752
   498
deba@1752
   499
      operator const int&() const { 
deba@1752
   500
	return static_cast<const IterableIntMap&>(_map)[_key]; 
deba@1752
   501
      }
deba@1752
   502
deba@1752
   503
      Reference& operator=(int value) { 
deba@1752
   504
	_map.set(_key, value); 
deba@1752
   505
	return *this; 
deba@1752
   506
      }
deba@1759
   507
      Reference& operator++() {
deba@1759
   508
	_map.set(_key, _map[_key] + 1); 
deba@1759
   509
	return *this; 	
deba@1759
   510
      }
deba@1759
   511
      int operator++(int) {
deba@1759
   512
	int value = _map[_key];
deba@1759
   513
	_map.set(_key, value + 1); 
deba@1759
   514
	return value; 	
deba@1759
   515
      }
deba@1759
   516
      Reference& operator--() {
deba@1759
   517
	_map.set(_key, _map[_key] - 1); 
deba@1759
   518
	return *this; 	
deba@1759
   519
      }
deba@1759
   520
      int operator--(int) {
deba@1759
   521
	int value = _map[_key];
deba@1759
   522
	_map.set(_key, value - 1); 
deba@1759
   523
	return value; 	
deba@1759
   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
      Reference& operator<<=(int value) { 
deba@1752
   558
	_map.set(_key, _map[_key] << value); 
deba@1752
   559
	return *this;
deba@1752
   560
      }
deba@1752
   561
      Reference& operator>>=(int value) { 
deba@1752
   562
	_map.set(_key, _map[_key] >> value); 
deba@1752
   563
	return *this;
deba@1752
   564
      }
deba@1752
   565
deba@1752
   566
    private:
deba@1752
   567
      Key _key;
deba@1752
   568
      IterableIntMap& _map; 
deba@1752
   569
    };
deba@1810
   570
deba@1810
   571
    /// The const reference type.    
deba@1752
   572
    typedef const Value& ConstReference;
deba@1752
   573
deba@1810
   574
    /// \brief Gives back the maximal value plus one.
deba@1810
   575
    ///
deba@1810
   576
    /// Gives back the maximal value plus one.
deba@1931
   577
    unsigned int size() const {
deba@1931
   578
      return first.size();
deba@1752
   579
    }
deba@1752
   580
    
deba@1810
   581
    /// \brief Set operation of the map.
deba@1810
   582
    ///
deba@1810
   583
    /// Set operation of the map.
deba@1752
   584
    void set(const Key& key, const Value& value) {
deba@1752
   585
      unlace(key);
deba@1752
   586
      Parent::operator[](key).value = value;
deba@1752
   587
      lace(key);
deba@1752
   588
    }
deba@1752
   589
deba@1810
   590
    /// \brief Const subscript operator of the map.
deba@1810
   591
    ///
deba@1810
   592
    /// Const subscript operator of the map.
deba@1752
   593
    const Value& operator[](const Key& key) const {
deba@1752
   594
      return Parent::operator[](key).value;
deba@1752
   595
    }
deba@1752
   596
deba@1810
   597
    /// \brief Subscript operator of the map.
deba@1810
   598
    ///
deba@1810
   599
    /// Subscript operator of the map.
deba@1752
   600
    Reference operator[](const Key& key) {
deba@1752
   601
      return Reference(*this, key);
deba@1752
   602
    }
deba@1752
   603
deba@1810
   604
    /// \brief Iterator for the keys with the same value.
deba@1810
   605
    ///
deba@1810
   606
    /// Iterator for the keys with the same value. It works
deba@1810
   607
    /// like a graph item iterator in the map, it can be converted
deba@1810
   608
    /// the item type of the map, incremented with \c ++ operator, and
deba@1810
   609
    /// if the iterator leave the last valid item it will be equal to 
deba@1810
   610
    /// \c INVALID.
deba@1752
   611
    class ItemIt : public _Item {
deba@1752
   612
    public:
deba@1752
   613
      typedef _Item Parent;
deba@1752
   614
deba@1810
   615
      /// \brief Invalid constructor \& conversion.
deba@1810
   616
      ///
deba@1810
   617
      /// This constructor initializes the item to be invalid.
deba@1810
   618
      /// \sa Invalid for more details.
deba@1752
   619
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@1752
   620
deba@1810
   621
      /// \brief Creates an iterator with a value.
deba@1810
   622
      ///
deba@1810
   623
      /// Creates an iterator with a value. It iterates on the 
deba@1810
   624
      /// keys which have the given value.
deba@1810
   625
      /// \param map The IterableIntMap
deba@1810
   626
      /// \param value The value
deba@1752
   627
      ItemIt(const IterableIntMap& map, int value) : _map(&map) {
deba@2386
   628
	if (value < 0 || value >= int(_map->first.size())) {	  
deba@1752
   629
	  Parent::operator=(INVALID);
deba@1752
   630
	} else {
deba@1752
   631
	  Parent::operator=(_map->first[value]);
deba@1752
   632
	}
deba@1752
   633
      } 
deba@1752
   634
deba@1810
   635
      /// \brief Increment operator.
deba@1810
   636
      ///
deba@1810
   637
      /// Increment Operator.
deba@1752
   638
      ItemIt& operator++() {
deba@1752
   639
	Parent::operator=(_map->IterableIntMap::Parent::
deba@1752
   640
			  operator[](static_cast<Parent&>(*this)).next);
deba@1752
   641
	return *this;
deba@1752
   642
      }
deba@1752
   643
deba@1752
   644
deba@1752
   645
    private:
deba@1752
   646
      const IterableIntMap* _map;
deba@1752
   647
    };
deba@1752
   648
deba@1752
   649
  protected:
deba@1752
   650
    
deba@1752
   651
    virtual void erase(const Key& key) {
deba@1752
   652
      unlace(key);
deba@1752
   653
      Parent::erase(key);
deba@1752
   654
    }
deba@1752
   655
deba@1913
   656
    virtual void erase(const std::vector<Key>& keys) {
deba@2386
   657
      for (int i = 0; i < int(keys.size()); ++i) {
deba@1913
   658
        unlace(keys[i]);
deba@1913
   659
      }
deba@1913
   660
      Parent::erase(keys);
deba@1913
   661
    }
deba@1913
   662
deba@1752
   663
    virtual void clear() {
deba@1752
   664
      first.clear();
deba@1752
   665
      Parent::clear();
deba@1752
   666
    }
deba@1752
   667
deba@1752
   668
  private:
deba@1752
   669
    std::vector<_Item> first;
deba@1752
   670
  };
deba@1752
   671
deba@1931
   672
  namespace _iterable_maps_bits {
deba@1931
   673
    template <typename Item, typename Value>
deba@1931
   674
    struct IterableValueMapNode {
deba@1931
   675
      IterableValueMapNode(Value _value = Value()) : value(_value) {}
deba@1931
   676
      Item prev, next;
deba@1931
   677
      Value value;
deba@1931
   678
    };
deba@1931
   679
  }
deba@1931
   680
deba@1931
   681
  ///\ingroup graph_maps
deba@1931
   682
  ///
deba@1931
   683
  /// \brief Dynamic iterable map for comparable values.
deba@1931
   684
  ///
deba@1931
   685
  /// This class provides a special graph map type which can store
deba@1931
   686
  /// for each graph item(node, edge, etc.) a value. For each
deba@1931
   687
  /// value it is possible to iterate on the keys which mapped to the 
deba@1931
   688
  /// given value. The type stores for each value a linked list with
deba@1931
   689
  /// the items which mapped to the value, and the values are stored
deba@1931
   690
  /// in balanced binary tree. The values of the map can be accessed
deba@1931
   691
  /// with stl compatible forward iterator.
deba@1931
   692
  ///
deba@1931
   693
  /// This type is not reference map so it cannot be modified with
deba@1931
   694
  /// the subscription operator.
deba@1931
   695
  ///
deba@1931
   696
  /// \see InvertableMap
deba@1931
   697
  /// 
deba@1931
   698
  /// \param _Graph The graph type.
deba@1931
   699
  /// \param _Item One of the graph's item type, the key of the map.
deba@1931
   700
  /// \param _Value Any comparable value type.
deba@1931
   701
  template <typename _Graph, typename _Item, typename _Value>
deba@1990
   702
  class IterableValueMap 
deba@2031
   703
    : protected MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   704
                                       IterableValueMapNode<_Item, _Value> > >{
deba@1931
   705
  public:
deba@2031
   706
    typedef MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
deba@2031
   707
                                   IterableValueMapNode<_Item, _Value> > >
deba@2031
   708
    Parent;
deba@1931
   709
deba@1931
   710
    /// The key type
deba@1931
   711
    typedef _Item Key;
deba@1931
   712
    /// The value type
deba@1931
   713
    typedef _Value Value;
deba@1931
   714
    /// The graph type
deba@1931
   715
    typedef _Graph Graph;
deba@1931
   716
deba@1990
   717
  public:
deba@1990
   718
deba@1931
   719
    /// \brief Constructor of the Map with a given value.
deba@1931
   720
    ///
deba@1931
   721
    /// Constructor of the Map with a given value.
deba@1931
   722
    explicit IterableValueMap(const Graph& graph, 
deba@1931
   723
                              const Value& value = Value()) 
deba@1990
   724
      : Parent(graph, _iterable_maps_bits::
deba@1990
   725
               IterableValueMapNode<_Item, _Value>(value)) {
deba@2031
   726
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@1931
   727
        lace(it);
deba@1931
   728
      }
deba@1931
   729
    }
deba@1931
   730
deba@1990
   731
  protected:
deba@1931
   732
    
deba@1931
   733
    void unlace(const Key& key) {
deba@1931
   734
      typename Parent::Value& node = Parent::operator[](key);
deba@1931
   735
      if (node.prev != INVALID) {
deba@1931
   736
	Parent::operator[](node.prev).next = node.next;	
deba@1931
   737
      } else {
deba@1931
   738
        if (node.next != INVALID) {
deba@1931
   739
          first[node.value] = node.next;
deba@1931
   740
        } else {
deba@1931
   741
          first.erase(node.value);
deba@1931
   742
        }
deba@1931
   743
      }
deba@1931
   744
      if (node.next != INVALID) {
deba@1931
   745
	Parent::operator[](node.next).prev = node.prev;
deba@1931
   746
      }
deba@1931
   747
    }
deba@1931
   748
deba@1931
   749
    void lace(const Key& key) {
deba@1931
   750
      typename Parent::Value& node = Parent::operator[](key);
deba@1931
   751
      typename std::map<Value, Key>::iterator it = first.find(node.value);
deba@1931
   752
      if (it == first.end()) {
deba@1931
   753
        node.prev = node.next = INVALID;
deba@1931
   754
        if (node.next != INVALID) {
deba@1931
   755
          Parent::operator[](node.next).prev = key;	
deba@1931
   756
        }
deba@1931
   757
        first.insert(make_pair(node.value, key));
deba@1931
   758
      } else {
deba@1931
   759
        node.prev = INVALID;
deba@1931
   760
        node.next = it->second;
deba@1931
   761
        if (node.next != INVALID) {
deba@1931
   762
          Parent::operator[](node.next).prev = key;	
deba@1931
   763
        }
deba@1931
   764
        it->second = key;
deba@1931
   765
      }
deba@1931
   766
    }
deba@1931
   767
deba@1931
   768
  public:
deba@1931
   769
deba@1931
   770
    /// \brief Forward iterator for values.
deba@1931
   771
    ///
deba@1931
   772
    /// This iterator is an stl compatible forward
deba@1931
   773
    /// iterator on the values of the map. The values can
deba@1931
   774
    /// be accessed in the [beginValue, endValue) range.
deba@1931
   775
    ///
deba@1931
   776
    class ValueIterator 
deba@1931
   777
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@1931
   778
      friend class IterableValueMap;
deba@1931
   779
    private:
deba@1931
   780
      ValueIterator(typename std::map<Value, Key>::const_iterator _it) 
deba@1931
   781
        : it(_it) {}
deba@1931
   782
    public:
deba@1931
   783
      
deba@1931
   784
      ValueIterator() {}
deba@1931
   785
deba@1931
   786
      ValueIterator& operator++() { ++it; return *this; }
deba@1931
   787
      ValueIterator operator++(int) { 
deba@1931
   788
        ValueIterator tmp(*this); 
deba@1931
   789
        operator++();
deba@1931
   790
        return tmp; 
deba@1931
   791
      }
deba@1931
   792
deba@1931
   793
      const Value& operator*() const { return it->first; }
deba@1931
   794
      const Value* operator->() const { return &(it->first); }
deba@1931
   795
deba@1931
   796
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@1931
   797
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@1931
   798
      
deba@1931
   799
    private:
deba@1931
   800
      typename std::map<Value, Key>::const_iterator it;
deba@1931
   801
    };
deba@1931
   802
deba@1931
   803
    /// \brief Returns an iterator to the first value.
deba@1931
   804
    ///
deba@1931
   805
    /// Returns an stl compatible iterator to the 
deba@1931
   806
    /// first value of the map. The values of the
deba@1931
   807
    /// map can be accessed in the [beginValue, endValue)
deba@1931
   808
    /// range.
deba@1931
   809
    ValueIterator beginValue() const {
deba@1931
   810
      return ValueIterator(first.begin());
deba@1931
   811
    }
deba@1931
   812
deba@1931
   813
    /// \brief Returns an iterator after the last value.
deba@1931
   814
    ///
deba@1931
   815
    /// Returns an stl compatible iterator after the 
deba@1931
   816
    /// last value of the map. The values of the
deba@1931
   817
    /// map can be accessed in the [beginValue, endValue)
deba@1931
   818
    /// range.
deba@1931
   819
    ValueIterator endValue() const {
deba@1931
   820
      return ValueIterator(first.end());
deba@1931
   821
    }
deba@1931
   822
deba@1931
   823
    /// \brief Set operation of the map.
deba@1931
   824
    ///
deba@1931
   825
    /// Set operation of the map.
deba@1931
   826
    void set(const Key& key, const Value& value) {
deba@1931
   827
      unlace(key);
deba@1931
   828
      Parent::operator[](key).value = value;
deba@1931
   829
      lace(key);
deba@1931
   830
    }
deba@1931
   831
deba@1931
   832
    /// \brief Const subscript operator of the map.
deba@1931
   833
    ///
deba@1931
   834
    /// Const subscript operator of the map.
deba@1931
   835
    const Value& operator[](const Key& key) const {
deba@1931
   836
      return Parent::operator[](key).value;
deba@1931
   837
    }
deba@1931
   838
deba@1931
   839
    /// \brief Iterator for the keys with the same value.
deba@1931
   840
    ///
deba@1931
   841
    /// Iterator for the keys with the same value. It works
deba@1931
   842
    /// like a graph item iterator in the map, it can be converted
deba@1931
   843
    /// the item type of the map, incremented with \c ++ operator, and
deba@1931
   844
    /// if the iterator leave the last valid item it will be equal to 
deba@1931
   845
    /// \c INVALID.
deba@1931
   846
    class ItemIt : public _Item {
deba@1931
   847
    public:
deba@1931
   848
      typedef _Item Parent;
deba@1931
   849
deba@1931
   850
      /// \brief Invalid constructor \& conversion.
deba@1931
   851
      ///
deba@1931
   852
      /// This constructor initializes the item to be invalid.
deba@1931
   853
      /// \sa Invalid for more details.
deba@1931
   854
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@1931
   855
deba@1931
   856
      /// \brief Creates an iterator with a value.
deba@1931
   857
      ///
deba@1931
   858
      /// Creates an iterator with a value. It iterates on the 
deba@1931
   859
      /// keys which have the given value.
deba@1931
   860
      /// \param map The IterableValueMap
deba@1931
   861
      /// \param value The value
deba@1931
   862
      ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
deba@1931
   863
        typename std::map<Value, Key>::const_iterator it = 
deba@1931
   864
          map.first.find(value); 
deba@1931
   865
	if (it == map.first.end()) {	  
deba@1931
   866
	  Parent::operator=(INVALID);
deba@1931
   867
	} else {
deba@1931
   868
	  Parent::operator=(it->second);
deba@1931
   869
	}
deba@1931
   870
      } 
deba@1931
   871
deba@1931
   872
      /// \brief Increment operator.
deba@1931
   873
      ///
deba@1931
   874
      /// Increment Operator.
deba@1931
   875
      ItemIt& operator++() {
deba@1931
   876
	Parent::operator=(_map->IterableValueMap::Parent::
deba@1931
   877
			  operator[](static_cast<Parent&>(*this)).next);
deba@1931
   878
	return *this;
deba@1931
   879
      }
deba@1931
   880
deba@1931
   881
deba@1931
   882
    private:
deba@1931
   883
      const IterableValueMap* _map;
deba@1931
   884
    };
deba@1931
   885
deba@1931
   886
  protected:
deba@1931
   887
    
deba@1990
   888
    virtual void add(const Key& key) {
deba@1990
   889
      Parent::add(key);
deba@1990
   890
      unlace(key);
deba@1990
   891
    }
deba@1990
   892
deba@1990
   893
    virtual void add(const std::vector<Key>& keys) {
deba@1990
   894
      Parent::add(keys);
deba@2386
   895
      for (int i = 0; i < int(keys.size()); ++i) {
deba@1990
   896
        lace(keys[i]);
deba@1990
   897
      }
deba@1990
   898
    }
deba@1990
   899
deba@1931
   900
    virtual void erase(const Key& key) {
deba@1931
   901
      unlace(key);
deba@1931
   902
      Parent::erase(key);
deba@1931
   903
    }
deba@1931
   904
deba@1931
   905
    virtual void erase(const std::vector<Key>& keys) {
deba@2386
   906
      for (int i = 0; i < int(keys.size()); ++i) {
deba@1931
   907
        unlace(keys[i]);
deba@1931
   908
      }
deba@1931
   909
      Parent::erase(keys);
deba@1931
   910
    }
deba@1931
   911
deba@1990
   912
    virtual void build() {
deba@1990
   913
      Parent::build();
deba@2031
   914
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@1990
   915
        lace(it);
deba@1990
   916
      }
deba@1990
   917
    }
deba@1990
   918
deba@1931
   919
    virtual void clear() {
deba@1931
   920
      first.clear();
deba@1931
   921
      Parent::clear();
deba@1931
   922
    }
deba@1931
   923
deba@1931
   924
  private:
deba@1931
   925
    std::map<Value, Key> first;
deba@1931
   926
  };
deba@1931
   927
alpar@1677
   928
  /// @}
alpar@1677
   929
}
deba@2210
   930
deba@2210
   931
#endif