lemon/iterable_maps.h
author ladanyi
Sun, 29 Jan 2006 22:04:48 +0000
changeset 1917 87d3518d73d8
parent 1875 98698b69a902
child 1931 6abf67b02ff5
permissions -rw-r--r--
include the gui in the deb
alpar@1677
     1
/* -*- C++ -*-
alpar@1677
     2
 * lemon/iterable_maps.h - Part of LEMON, a generic C++ optimization library
alpar@1677
     3
 *
alpar@1875
     4
 * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1677
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1677
     6
 *
alpar@1677
     7
 * Permission to use, modify and distribute this software is granted
alpar@1677
     8
 * provided that this copyright notice appears in all copies. For
alpar@1677
     9
 * precise terms see the accompanying LICENSE file.
alpar@1677
    10
 *
alpar@1677
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1677
    12
 * express or implied, and with no claim as to its suitability for any
alpar@1677
    13
 * purpose.
alpar@1677
    14
 *
alpar@1677
    15
 */
alpar@1677
    16
deba@1752
    17
#include <lemon/traits.h>
deba@1752
    18
#include <lemon/invalid.h>
alpar@1677
    19
#include <vector>
alpar@1677
    20
#include <limits>
alpar@1677
    21
alpar@1677
    22
///\ingroup maps
alpar@1677
    23
///\file
alpar@1677
    24
///\brief Maps that makes it possible to iterate through the keys having
alpar@1677
    25
///a certain value
alpar@1677
    26
///
alpar@1677
    27
///
alpar@1677
    28
alpar@1677
    29
alpar@1677
    30
namespace lemon {
deba@1913
    31
deba@1913
    32
  ///\ingroup maps
deba@1913
    33
  ///
deba@1913
    34
  /// \brief Dynamic iterable bool map.
deba@1913
    35
  ///
deba@1913
    36
  /// This class provides a special graph map type which can store
deba@1913
    37
  /// for each graph item(node, edge, etc.) a bool value. For both 
deba@1913
    38
  /// the true and the false it is possible to iterate on the keys which
deba@1913
    39
  /// mapped to the given value.
deba@1913
    40
  /// 
deba@1913
    41
  /// \param _Graph The graph type.
deba@1913
    42
  /// \param _Item One of the graph's item type, the key of the map.
deba@1913
    43
  template <typename _Graph, typename _Item>
deba@1913
    44
  class IterableBoolMap 
deba@1913
    45
    : protected ItemSetTraits<_Graph, _Item>::template Map<int>::Parent {
deba@1913
    46
  private:
deba@1913
    47
    typedef _Graph Graph;
deba@1913
    48
    typedef _Item Item;
deba@1913
    49
    
deba@1913
    50
    typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
deba@1913
    51
    typedef typename ItemSetTraits<Graph, Item>
deba@1913
    52
    ::template Map<int>::Parent Parent;
deba@1913
    53
    
deba@1913
    54
    std::vector<Item> array;
deba@1913
    55
    int sep;
deba@1913
    56
deba@1913
    57
    const Graph& graph;
alpar@1677
    58
alpar@1677
    59
  private:
alpar@1677
    60
deba@1913
    61
    int position(const Item& item) const {
deba@1913
    62
      return Parent::operator[](item);
alpar@1677
    63
    }
alpar@1677
    64
alpar@1677
    65
  public:
alpar@1805
    66
deba@1913
    67
    /// Indicates that the map if reference map.
deba@1913
    68
    typedef True ReferenceMapTag;
alpar@1805
    69
deba@1913
    70
    /// The key type
deba@1913
    71
    typedef Item Key;
deba@1913
    72
    /// The value type
deba@1913
    73
    typedef bool Value;
deba@1913
    74
    /// The const reference type.    
deba@1913
    75
    typedef const Value& ConstReference;
deba@1913
    76
deba@1913
    77
deba@1913
    78
    /// \brief Refernce to the value of the map.
deba@1913
    79
    ///
deba@1913
    80
    /// This class is near to similar to the bool type. It can
deba@1913
    81
    /// be converted to bool and it has the same operators.
deba@1913
    82
    class Reference {
deba@1913
    83
      friend class IterableBoolMap;
deba@1913
    84
    private:
deba@1913
    85
      Reference(IterableBoolMap& map, const Key& key) 
deba@1913
    86
	: _key(key), _map(map) {} 
alpar@1677
    87
    public:
deba@1913
    88
deba@1913
    89
      Reference& operator=(const Reference& value) {
deba@1913
    90
	_map.set(_key, (bool)value);
deba@1913
    91
 	return *this;
deba@1913
    92
      }
deba@1913
    93
deba@1913
    94
      operator bool() const { 
deba@1913
    95
	return static_cast<const IterableBoolMap&>(_map)[_key]; 
deba@1913
    96
      }
deba@1913
    97
deba@1913
    98
      Reference& operator=(bool value) { 
deba@1913
    99
	_map.set(_key, value); 
deba@1913
   100
	return *this; 
deba@1913
   101
      }
deba@1913
   102
      Reference& operator&=(bool value) {
deba@1913
   103
	_map.set(_key, _map[_key] & value); 
deba@1913
   104
	return *this; 	
deba@1913
   105
      }
deba@1913
   106
      Reference& operator|=(bool value) {
deba@1913
   107
	_map.set(_key, _map[_key] | value); 
deba@1913
   108
	return *this; 	
deba@1913
   109
      }
deba@1913
   110
      Reference& operator^=(bool value) {
deba@1913
   111
	_map.set(_key, _map[_key] ^ value); 
deba@1913
   112
	return *this; 	
deba@1913
   113
      }
deba@1913
   114
    private:
deba@1913
   115
      Key _key;
deba@1913
   116
      IterableBoolMap& _map; 
alpar@1677
   117
    };
deba@1913
   118
    
deba@1913
   119
    /// \brief Constructor of the Map with a default value.
deba@1913
   120
    ///
deba@1913
   121
    /// Constructor of the Map with a default value.
deba@1913
   122
    IterableBoolMap(const Graph& _graph, bool def = false) 
deba@1913
   123
      : Parent(_graph), graph(_graph) {
deba@1913
   124
      for (ItemIt it(graph); it != INVALID; ++it) {
deba@1913
   125
        Parent::set(it, array.size());
deba@1913
   126
        array.push_back(it);
deba@1913
   127
      }
deba@1913
   128
      sep = (def ? array.size() : 0);
deba@1913
   129
    }
deba@1913
   130
deba@1913
   131
    /// \brief Const subscript operator of the map.
deba@1913
   132
    ///
deba@1913
   133
    /// Const subscript operator of the map.
deba@1913
   134
    bool operator[](const Item& item) const {
deba@1913
   135
      return position(item) < sep;
deba@1913
   136
    }
deba@1913
   137
deba@1913
   138
    /// \brief Subscript operator of the map.
deba@1913
   139
    ///
deba@1913
   140
    /// Subscript operator of the map.
deba@1913
   141
    Reference operator[](const Item& item) {
deba@1913
   142
      return Reference(*this, item);
deba@1913
   143
    }
deba@1913
   144
deba@1913
   145
    /// \brief Set operation of the map.
deba@1913
   146
    ///
deba@1913
   147
    /// Set operation of the map.
deba@1913
   148
    void set(const Item& item, bool value) {
deba@1913
   149
      int pos = position(item);
deba@1913
   150
      if (value) {
deba@1913
   151
        if (pos < sep) return;
deba@1913
   152
        Item tmp = array[sep];
deba@1913
   153
        array[sep] = item;
deba@1913
   154
        Parent::set(item, sep);
deba@1913
   155
        array[pos] = tmp;
deba@1913
   156
        Parent::set(tmp, pos); 
deba@1913
   157
        ++sep;
deba@1913
   158
      } else {
deba@1913
   159
        if (pos >= sep) return;
deba@1913
   160
        --sep;
deba@1913
   161
        Item tmp = array[sep];
deba@1913
   162
        array[sep] = item;
deba@1913
   163
        Parent::set(item, sep);
deba@1913
   164
        array[pos] = tmp;
deba@1913
   165
        Parent::set(tmp, pos);
deba@1913
   166
      }
deba@1913
   167
    }
deba@1913
   168
deba@1913
   169
    /// \brief Returns the number of the items mapped to true.
deba@1913
   170
    ///
deba@1913
   171
    /// Returns the number of the items mapped to true.
deba@1913
   172
    int trueNum() const {
deba@1913
   173
      return sep;
deba@1913
   174
    } 
deba@1913
   175
    
deba@1913
   176
    /// \brief Returns the number of the items mapped to false.
deba@1913
   177
    ///
deba@1913
   178
    /// Returns the number of the items mapped to false.
deba@1913
   179
    int falseNum() const {
deba@1913
   180
      return array.size() - sep;
deba@1913
   181
    }
deba@1913
   182
deba@1913
   183
    /// \brief Iterator for the keys mapped to true.
deba@1913
   184
    ///
deba@1913
   185
    /// Iterator for the keys mapped to true. It works
deba@1913
   186
    /// like a graph item iterator in the map, it can be converted
deba@1913
   187
    /// the item type of the map, incremented with \c ++ operator, and
deba@1913
   188
    /// if the iterator leave the last valid item it will be equal to 
deba@1913
   189
    /// \c INVALID.
deba@1913
   190
    class TrueIt : public Item {
alpar@1677
   191
    public:
deba@1913
   192
      typedef Item Parent;
deba@1913
   193
      
deba@1913
   194
      /// \brief Creates an iterator.
deba@1913
   195
      ///
deba@1913
   196
      /// Creates an iterator. It iterates on the 
deba@1913
   197
      /// keys which mapped to true.
deba@1913
   198
      /// \param map The IterableIntMap
deba@1913
   199
      TrueIt(const IterableBoolMap& _map) 
deba@1913
   200
        : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID), 
deba@1913
   201
          map(&_map) {}
deba@1913
   202
deba@1913
   203
      /// \brief Invalid constructor \& conversion.
deba@1913
   204
      ///
deba@1913
   205
      /// This constructor initializes the item to be invalid.
deba@1913
   206
      /// \sa Invalid for more details.
deba@1913
   207
      TrueIt(Invalid) : Parent(INVALID), map(0) {}
deba@1913
   208
deba@1913
   209
      /// \brief Increment operator.
deba@1913
   210
      ///
deba@1913
   211
      /// Increment Operator.
deba@1913
   212
      TrueIt& operator++() {
deba@1913
   213
        int pos = map->position(*this);
deba@1913
   214
        Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID);
deba@1913
   215
        return *this;
deba@1913
   216
      }
deba@1913
   217
deba@1913
   218
      
deba@1913
   219
    private:
deba@1913
   220
      const IterableBoolMap* map;
deba@1913
   221
    };
deba@1913
   222
deba@1913
   223
    /// \brief Iterator for the keys mapped to false.
deba@1913
   224
    ///
deba@1913
   225
    /// Iterator for the keys mapped to false. It works
deba@1913
   226
    /// like a graph item iterator in the map, it can be converted
deba@1913
   227
    /// the item type of the map, incremented with \c ++ operator, and
deba@1913
   228
    /// if the iterator leave the last valid item it will be equal to 
deba@1913
   229
    /// \c INVALID.
deba@1913
   230
    class FalseIt : public Item {
deba@1913
   231
    public:
deba@1913
   232
      typedef Item Parent;
deba@1913
   233
      
deba@1913
   234
      /// \brief Creates an iterator.
deba@1913
   235
      ///
deba@1913
   236
      /// Creates an iterator. It iterates on the 
deba@1913
   237
      /// keys which mapped to false.
deba@1913
   238
      /// \param map The IterableIntMap
deba@1913
   239
      FalseIt(const IterableBoolMap& _map) 
deba@1913
   240
        : Parent(_map.sep < _map.array.size() ? _map.array.back() : INVALID), 
deba@1913
   241
          map(&_map) {}
deba@1913
   242
deba@1913
   243
      /// \brief Invalid constructor \& conversion.
deba@1913
   244
      ///
deba@1913
   245
      /// This constructor initializes the item to be invalid.
deba@1913
   246
      /// \sa Invalid for more details.
deba@1913
   247
      FalseIt(Invalid) : Parent(INVALID), map(0) {}
deba@1913
   248
deba@1913
   249
      /// \brief Increment operator.
deba@1913
   250
      ///
deba@1913
   251
      /// Increment Operator.
deba@1913
   252
      FalseIt& operator++() {
deba@1913
   253
        int pos = map->position(*this);
deba@1913
   254
        Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID);
deba@1913
   255
        return *this;
deba@1913
   256
      }
deba@1913
   257
deba@1913
   258
    private:
deba@1913
   259
      const IterableBoolMap* map;
deba@1913
   260
    };
deba@1913
   261
deba@1913
   262
  protected:
deba@1913
   263
    
deba@1913
   264
    virtual void add(const Item& item) {
deba@1913
   265
      Parent::add(item);
deba@1913
   266
      Parent::set(item, array.size());
deba@1913
   267
      array.push_back(item);
deba@1913
   268
    }
deba@1913
   269
deba@1913
   270
    virtual void add(const std::vector<Item>& items) {
deba@1913
   271
      Parent::add(items);
deba@1913
   272
      for (int i = 0; i < (int)items.size(); ++i) {
deba@1913
   273
        Parent::set(items[i], array.size());
deba@1913
   274
        array.push_back(items[i]);
deba@1913
   275
      }
deba@1913
   276
    }
deba@1913
   277
deba@1913
   278
    virtual void erase(const Item& item) {
deba@1913
   279
      int pos = position(item); 
deba@1913
   280
      if (pos < sep) {
deba@1913
   281
        --sep;
deba@1913
   282
        Parent::set(array[sep], pos);
deba@1913
   283
        array[pos] = array[sep];
deba@1913
   284
        Parent::set(array.back(), sep);
deba@1913
   285
        array[sep] = array.back();
deba@1913
   286
        array.pop_back();
deba@1913
   287
      } else {
deba@1913
   288
        Parent::set(array.back(), pos);
deba@1913
   289
        array[pos] = array.back();
deba@1913
   290
        array.pop_back();
deba@1913
   291
      }
deba@1913
   292
      Parent::erase(item);
deba@1913
   293
    }
deba@1913
   294
deba@1913
   295
    virtual void erase(const std::vector<Item>& items) {
deba@1913
   296
      for (int i = 0; i < (int)items.size(); ++i) {
deba@1913
   297
        int pos = position(items[i]); 
deba@1913
   298
        if (pos < sep) {
deba@1913
   299
          --sep;
deba@1913
   300
          Parent::set(array[sep], pos);
deba@1913
   301
          array[pos] = array[sep];
deba@1913
   302
          Parent::set(array.back(), sep);
deba@1913
   303
          array[sep] = array.back();
deba@1913
   304
          array.pop_back();
deba@1913
   305
        } else {
deba@1913
   306
          Parent::set(array.back(), pos);
deba@1913
   307
          array[pos] = array.back();
deba@1913
   308
          array.pop_back();
deba@1913
   309
        }
deba@1913
   310
      }
deba@1913
   311
      Parent::erase(items);
deba@1913
   312
    }    
deba@1913
   313
deba@1913
   314
    virtual void build() {
deba@1913
   315
      Parent::build();
deba@1913
   316
      for (ItemIt it(graph); it != INVALID; ++it) {
deba@1913
   317
        Parent::set(it, array.size());
deba@1913
   318
        array.push_back(it);
deba@1913
   319
      }
deba@1913
   320
      sep = 0;      
deba@1913
   321
    }
deba@1913
   322
deba@1913
   323
    virtual void clear() {
deba@1913
   324
      array.clear();
deba@1913
   325
      sep = 0;
deba@1913
   326
      Parent::clear();
deba@1913
   327
    }
deba@1913
   328
    
alpar@1677
   329
  };
alpar@1873
   330
  
deba@1752
   331
deba@1752
   332
  namespace _iterable_maps_bits {
deba@1752
   333
    template <typename Item>
deba@1752
   334
    struct IterableIntMapNode {
deba@1913
   335
      IterableIntMapNode() : value(-1) {}
deba@1810
   336
      IterableIntMapNode(int _value) : value(_value) {}
deba@1752
   337
      Item prev, next;
deba@1752
   338
      int value;
deba@1752
   339
    };
deba@1752
   340
  }
deba@1752
   341
deba@1752
   342
  ///\ingroup maps
deba@1752
   343
  ///
deba@1752
   344
  /// \brief Dynamic iterable integer map.
deba@1752
   345
  ///
deba@1810
   346
  /// This class provides a special graph map type which can store
deba@1810
   347
  /// for each graph item(node, edge, etc.) an integer value. For each
deba@1810
   348
  /// non negative value it is possible to iterate on the keys which
deba@1810
   349
  /// mapped to the given value.
deba@1810
   350
  /// 
deba@1810
   351
  /// \param _Graph The graph type.
deba@1810
   352
  /// \param _Item One of the graph's item type, the key of the map.
deba@1752
   353
  template <typename _Graph, typename _Item>
deba@1752
   354
  class IterableIntMap : protected ItemSetTraits<_Graph, _Item> 
deba@1752
   355
  ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent {
deba@1752
   356
  public:
deba@1752
   357
    typedef typename ItemSetTraits<_Graph, _Item> 
deba@1752
   358
    ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >
deba@1752
   359
    ::Parent Parent;
deba@1752
   360
deba@1810
   361
    /// The key type
deba@1752
   362
    typedef _Item Key;
deba@1810
   363
    /// The value type
deba@1752
   364
    typedef int Value;
deba@1810
   365
    /// The graph type
deba@1752
   366
    typedef _Graph Graph;
deba@1752
   367
deba@1810
   368
    /// \brief Constructor of the Map.
deba@1810
   369
    ///
deba@1810
   370
    /// Constructor of the Map. It set all values -1.
deba@1810
   371
    explicit IterableIntMap(const Graph& graph) 
deba@1913
   372
      : Parent(graph) {}
deba@1810
   373
deba@1810
   374
    /// \brief Constructor of the Map with a given value.
deba@1810
   375
    ///
deba@1810
   376
    /// Constructor of the Map with a given value.
deba@1810
   377
    explicit IterableIntMap(const Graph& graph, int value) 
deba@1810
   378
      : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
deba@1810
   379
      if (value >= 0) {
deba@1810
   380
	for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@1810
   381
	  lace(it);
deba@1810
   382
	}
deba@1810
   383
      }
deba@1810
   384
    }
deba@1752
   385
deba@1752
   386
  private:
deba@1752
   387
    
deba@1752
   388
    void unlace(const Key& key) {
deba@1752
   389
      typename Parent::Value& node = Parent::operator[](key);
deba@1752
   390
      if (node.value < 0) return;
deba@1752
   391
      if (node.prev != INVALID) {
deba@1752
   392
	Parent::operator[](node.prev).next = node.next;	
deba@1752
   393
      } else {
deba@1752
   394
	first[node.value] = node.next;
deba@1752
   395
      }
deba@1752
   396
      if (node.next != INVALID) {
deba@1752
   397
	Parent::operator[](node.next).prev = node.prev;
deba@1752
   398
      }
deba@1752
   399
      while (!first.empty() && first.back() == INVALID) {
deba@1752
   400
	first.pop_back();
deba@1752
   401
      }
deba@1752
   402
    }
deba@1752
   403
deba@1752
   404
    void lace(const Key& key) {
deba@1752
   405
      typename Parent::Value& node = Parent::operator[](key);
deba@1752
   406
      if (node.value < 0) return;
deba@1752
   407
      if (node.value >= (int)first.size()) {
deba@1752
   408
	first.resize(node.value + 1, INVALID);
deba@1752
   409
      } 
deba@1752
   410
      node.prev = INVALID;
deba@1752
   411
      node.next = first[node.value];
deba@1752
   412
      if (node.next != INVALID) {
deba@1752
   413
	Parent::operator[](node.next).prev = key;	
deba@1752
   414
      }
deba@1752
   415
      first[node.value] = key;
deba@1752
   416
    }
deba@1752
   417
deba@1752
   418
  public:
deba@1752
   419
deba@1810
   420
    /// Indicates that the map if reference map.
deba@1752
   421
    typedef True ReferenceMapTag;
deba@1752
   422
deba@1810
   423
    /// \brief Refernce to the value of the map.
deba@1810
   424
    ///
deba@1810
   425
    /// This class is near to similar to the int type. It can
deba@1810
   426
    /// be converted to int and it has the same operators.
deba@1752
   427
    class Reference {
deba@1752
   428
      friend class IterableIntMap;
deba@1752
   429
    private:
deba@1752
   430
      Reference(IterableIntMap& map, const Key& key) 
deba@1752
   431
	: _key(key), _map(map) {} 
deba@1752
   432
    public:
deba@1752
   433
deba@1752
   434
      Reference& operator=(const Reference& value) {
deba@1752
   435
	_map.set(_key, (const int&)value);
deba@1752
   436
 	return *this;
deba@1752
   437
      }
deba@1752
   438
deba@1752
   439
      operator const int&() const { 
deba@1752
   440
	return static_cast<const IterableIntMap&>(_map)[_key]; 
deba@1752
   441
      }
deba@1752
   442
deba@1752
   443
      Reference& operator=(int value) { 
deba@1752
   444
	_map.set(_key, value); 
deba@1752
   445
	return *this; 
deba@1752
   446
      }
deba@1759
   447
      Reference& operator++() {
deba@1759
   448
	_map.set(_key, _map[_key] + 1); 
deba@1759
   449
	return *this; 	
deba@1759
   450
      }
deba@1759
   451
      int operator++(int) {
deba@1759
   452
	int value = _map[_key];
deba@1759
   453
	_map.set(_key, value + 1); 
deba@1759
   454
	return value; 	
deba@1759
   455
      }
deba@1759
   456
      Reference& operator--() {
deba@1759
   457
	_map.set(_key, _map[_key] - 1); 
deba@1759
   458
	return *this; 	
deba@1759
   459
      }
deba@1759
   460
      int operator--(int) {
deba@1759
   461
	int value = _map[_key];
deba@1759
   462
	_map.set(_key, value - 1); 
deba@1759
   463
	return value; 	
deba@1759
   464
      }
deba@1752
   465
      Reference& operator+=(int value) { 
deba@1752
   466
	_map.set(_key, _map[_key] + value); 
deba@1752
   467
	return *this;
deba@1752
   468
      }
deba@1752
   469
      Reference& operator-=(int value) { 
deba@1752
   470
	_map.set(_key, _map[_key] - value); 
deba@1752
   471
	return *this;
deba@1752
   472
      }
deba@1752
   473
      Reference& operator*=(int value) { 
deba@1752
   474
	_map.set(_key, _map[_key] * value); 
deba@1752
   475
	return *this;
deba@1752
   476
      }
deba@1752
   477
      Reference& operator/=(int value) { 
deba@1752
   478
	_map.set(_key, _map[_key] / value); 
deba@1752
   479
	return *this;
deba@1752
   480
      }
deba@1752
   481
      Reference& operator%=(int value) { 
deba@1752
   482
	_map.set(_key, _map[_key] % value); 
deba@1752
   483
	return *this;
deba@1752
   484
      }
deba@1752
   485
      Reference& operator&=(int value) { 
deba@1752
   486
	_map.set(_key, _map[_key] & value); 
deba@1752
   487
	return *this;
deba@1752
   488
      }
deba@1752
   489
      Reference& operator|=(int value) { 
deba@1752
   490
	_map.set(_key, _map[_key] | value); 
deba@1752
   491
	return *this;
deba@1752
   492
      }
deba@1752
   493
      Reference& operator^=(int value) { 
deba@1752
   494
	_map.set(_key, _map[_key] ^ value); 
deba@1752
   495
	return *this;
deba@1752
   496
      }
deba@1752
   497
      Reference& operator<<=(int value) { 
deba@1752
   498
	_map.set(_key, _map[_key] << value); 
deba@1752
   499
	return *this;
deba@1752
   500
      }
deba@1752
   501
      Reference& operator>>=(int value) { 
deba@1752
   502
	_map.set(_key, _map[_key] >> value); 
deba@1752
   503
	return *this;
deba@1752
   504
      }
deba@1752
   505
deba@1752
   506
    private:
deba@1752
   507
      Key _key;
deba@1752
   508
      IterableIntMap& _map; 
deba@1752
   509
    };
deba@1810
   510
deba@1810
   511
    /// The const reference type.    
deba@1752
   512
    typedef const Value& ConstReference;
deba@1752
   513
deba@1810
   514
    /// \brief Gives back the maximal value plus one.
deba@1810
   515
    ///
deba@1810
   516
    /// Gives back the maximal value plus one.
deba@1752
   517
    int size() const {
deba@1752
   518
      return (int)first.size();
deba@1752
   519
    }
deba@1752
   520
    
deba@1810
   521
    /// \brief Set operation of the map.
deba@1810
   522
    ///
deba@1810
   523
    /// Set operation of the map.
deba@1752
   524
    void set(const Key& key, const Value& value) {
deba@1752
   525
      unlace(key);
deba@1752
   526
      Parent::operator[](key).value = value;
deba@1752
   527
      lace(key);
deba@1752
   528
    }
deba@1752
   529
deba@1810
   530
    /// \brief Const subscript operator of the map.
deba@1810
   531
    ///
deba@1810
   532
    /// Const subscript operator of the map.
deba@1752
   533
    const Value& operator[](const Key& key) const {
deba@1752
   534
      return Parent::operator[](key).value;
deba@1752
   535
    }
deba@1752
   536
deba@1810
   537
    /// \brief Subscript operator of the map.
deba@1810
   538
    ///
deba@1810
   539
    /// Subscript operator of the map.
deba@1752
   540
    Reference operator[](const Key& key) {
deba@1752
   541
      return Reference(*this, key);
deba@1752
   542
    }
deba@1752
   543
deba@1810
   544
    /// \brief Iterator for the keys with the same value.
deba@1810
   545
    ///
deba@1810
   546
    /// Iterator for the keys with the same value. It works
deba@1810
   547
    /// like a graph item iterator in the map, it can be converted
deba@1810
   548
    /// the item type of the map, incremented with \c ++ operator, and
deba@1810
   549
    /// if the iterator leave the last valid item it will be equal to 
deba@1810
   550
    /// \c INVALID.
deba@1752
   551
    class ItemIt : public _Item {
deba@1752
   552
    public:
deba@1752
   553
      typedef _Item Parent;
deba@1752
   554
deba@1810
   555
      /// \brief Invalid constructor \& conversion.
deba@1810
   556
      ///
deba@1810
   557
      /// This constructor initializes the item to be invalid.
deba@1810
   558
      /// \sa Invalid for more details.
deba@1752
   559
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@1752
   560
deba@1810
   561
      /// \brief Creates an iterator with a value.
deba@1810
   562
      ///
deba@1810
   563
      /// Creates an iterator with a value. It iterates on the 
deba@1810
   564
      /// keys which have the given value.
deba@1810
   565
      /// \param map The IterableIntMap
deba@1810
   566
      /// \param value The value
deba@1752
   567
      ItemIt(const IterableIntMap& map, int value) : _map(&map) {
deba@1752
   568
	if (value < 0 || value >= (int)_map->first.size()) {	  
deba@1752
   569
	  Parent::operator=(INVALID);
deba@1752
   570
	} else {
deba@1752
   571
	  Parent::operator=(_map->first[value]);
deba@1752
   572
	}
deba@1752
   573
      } 
deba@1752
   574
deba@1810
   575
      /// \brief Increment operator.
deba@1810
   576
      ///
deba@1810
   577
      /// Increment Operator.
deba@1752
   578
      ItemIt& operator++() {
deba@1752
   579
	Parent::operator=(_map->IterableIntMap::Parent::
deba@1752
   580
			  operator[](static_cast<Parent&>(*this)).next);
deba@1752
   581
	return *this;
deba@1752
   582
      }
deba@1752
   583
deba@1752
   584
deba@1752
   585
    private:
deba@1752
   586
      const IterableIntMap* _map;
deba@1752
   587
    };
deba@1752
   588
deba@1752
   589
  protected:
deba@1752
   590
    
deba@1752
   591
    virtual void erase(const Key& key) {
deba@1752
   592
      unlace(key);
deba@1752
   593
      Parent::erase(key);
deba@1752
   594
    }
deba@1752
   595
deba@1913
   596
    virtual void erase(const std::vector<Key>& keys) {
deba@1913
   597
      for (int i = 0; i < keys.size(); ++i) {
deba@1913
   598
        unlace(keys[i]);
deba@1913
   599
      }
deba@1913
   600
      Parent::erase(keys);
deba@1913
   601
    }
deba@1913
   602
deba@1752
   603
    virtual void clear() {
deba@1752
   604
      first.clear();
deba@1752
   605
      Parent::clear();
deba@1752
   606
    }
deba@1752
   607
deba@1752
   608
  private:
deba@1752
   609
    std::vector<_Item> first;
deba@1752
   610
  };
deba@1752
   611
alpar@1677
   612
  /// @}
alpar@1677
   613
}