src/lemon/array_map.h
author alpar
Sat, 13 Nov 2004 17:07:10 +0000
changeset 987 87f7c54892df
parent 980 0f1044b7a3af
child 988 aa19ca32d9b0
permissions -rw-r--r--
Naming changes:
- ValueType -> Value
- KeyType -> Key
- ReferenceType ->Reference
- PointerType -> Pointer
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/array_map.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_ARRAY_MAP_H
alpar@921
    18
#define LEMON_ARRAY_MAP_H
deba@822
    19
deba@822
    20
#include <memory>
deba@822
    21
alpar@921
    22
#include <lemon/map_iterator.h>
deba@822
    23
deba@822
    24
///\ingroup graphmaps
deba@822
    25
///\file
deba@822
    26
///\brief Graph maps that construates and destruates
deba@822
    27
///their elements dynamically.
deba@822
    28
alpar@921
    29
namespace lemon {
deba@822
    30
deba@822
    31
deba@822
    32
  /// \addtogroup graphmaps
deba@822
    33
  /// @{
deba@822
    34
	
deba@822
    35
  /** The ArrayMap template class is graph map structure what
deba@822
    36
   *  automatically updates the map when a key is added to or erased from
deba@822
    37
   *  the map. This map factory uses the allocators to implement 
deba@822
    38
   *  the container functionality.
deba@822
    39
   *
deba@822
    40
   *  The template parameter is the MapRegistry that the maps
alpar@987
    41
   *  will belong to and the Value.
deba@822
    42
   */
deba@822
    43
klao@946
    44
  template <typename _Graph, 
klao@946
    45
	    typename _Item,
klao@946
    46
	    typename _ItemIt,
klao@946
    47
	    typename _Value>
klao@946
    48
  class ArrayMap : public AlterationObserverRegistry<_Item>::ObserverBase {
deba@897
    49
deba@822
    50
  public:
deba@822
    51
		
deba@822
    52
    /// The graph type of the maps. 
klao@946
    53
    typedef _Graph Graph;
deba@822
    54
    /// The key type of the maps.
alpar@987
    55
    typedef _Item Key;
klao@946
    56
klao@946
    57
    typedef AlterationObserverRegistry<_Item> Registry;
klao@946
    58
klao@946
    59
  private:
deba@822
    60
    /// The iterator to iterate on the keys.
klao@946
    61
    typedef _ItemIt KeyIt;
klao@946
    62
deba@822
    63
    /// The MapBase of the Map which imlements the core regisitry function.
klao@946
    64
    typedef typename Registry::ObserverBase Parent;
deba@822
    65
		
deba@822
    66
    
deba@822
    67
  public:
deba@822
    68
deba@822
    69
    /// The value type of the map.
alpar@987
    70
    typedef _Value Value;
deba@822
    71
    /// The reference type of the map;
alpar@987
    72
    typedef Value& Reference;
deba@822
    73
    /// The pointer type of the map;
alpar@987
    74
    typedef Value* Pointer;
deba@822
    75
deba@822
    76
    /// The const value type of the map.
alpar@987
    77
    typedef const Value ConstValue;
deba@822
    78
    /// The const reference type of the map;
alpar@987
    79
    typedef const Value& ConstReference;
deba@822
    80
    /// The pointer type of the map;
alpar@987
    81
    typedef const Value* ConstPointer;
deba@822
    82
deba@822
    83
klao@946
    84
  private:
deba@822
    85
    typedef std::allocator<Value> Allocator;
deba@822
    86
klao@946
    87
klao@946
    88
  public:
klao@946
    89
deba@822
    90
    /** Graph and Registry initialized map constructor.
deba@822
    91
     */
deba@980
    92
    ArrayMap(const Graph& _g) : graph(&_g) {
deba@980
    93
      attach(_g.getObserverRegistry(_Item()));
deba@822
    94
      allocate_memory();
klao@946
    95
      for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
    96
	int id = graph->id(it);;
deba@822
    97
	allocator.construct(&(values[id]), Value());
deba@822
    98
      }								
deba@822
    99
    }
deba@822
   100
klao@946
   101
    /// Constructor to use default value to initialize the map. 
klao@946
   102
klao@946
   103
    /// It constrates a map and initialize all of the the map. 
klao@946
   104
deba@980
   105
    ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) {
deba@980
   106
      attach(_g.getObserverRegistry(_Item()));
deba@822
   107
      allocate_memory();
klao@946
   108
      for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   109
	int id = graph->id(it);;
klao@946
   110
	allocator.construct(&(values[id]), _v);
deba@822
   111
      }								
deba@822
   112
    }
deba@822
   113
deba@822
   114
    /** Constructor to copy a map of the same map type.
deba@822
   115
     */
klao@946
   116
    ArrayMap(const ArrayMap& copy) {
klao@946
   117
      if (copy.attached()) {
klao@946
   118
	attach(*copy.getRegistry());
klao@946
   119
      }
deba@822
   120
      capacity = copy.capacity;
deba@822
   121
      if (capacity == 0) return;
deba@822
   122
      values = allocator.allocate(capacity);
klao@946
   123
      for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   124
	int id = graph->id(it);;
deba@891
   125
	allocator.construct(&(values[id]), copy.values[id]);
deba@822
   126
      }
deba@822
   127
    }
deba@822
   128
klao@978
   129
    using Parent::attach;
klao@978
   130
    using Parent::detach;
klao@978
   131
    using Parent::attached;
klao@978
   132
deba@822
   133
    /** Assign operator to copy a map of the same map type.
deba@822
   134
     */
deba@822
   135
    ArrayMap& operator=(const ArrayMap& copy) {
deba@822
   136
      if (&copy == this) return *this;
deba@897
   137
      
klao@946
   138
      if (graph != copy.graph) {
klao@946
   139
	if (attached()) {
klao@946
   140
	  clear();
klao@946
   141
	  detach();
deba@897
   142
	}
klao@946
   143
	if (copy.attached()) {
klao@946
   144
	  attach(*copy.getRegistry());
klao@946
   145
	}
deba@897
   146
	capacity = copy.capacity;
deba@897
   147
	if (capacity == 0) return *this;
deba@897
   148
	values = allocator.allocate(capacity);      
deba@822
   149
      }
deba@891
   150
klao@946
   151
      for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   152
	int id = graph->id(it);;
deba@822
   153
	allocator.construct(&(values[id]), copy.values[id]);
deba@822
   154
      }
deba@891
   155
deba@822
   156
      return *this;
deba@822
   157
    }
deba@822
   158
deba@822
   159
    /** The destructor of the map.
deba@822
   160
     */
klao@946
   161
    virtual ~ArrayMap() {      
klao@946
   162
      if (attached()) {
klao@946
   163
	clear();
klao@946
   164
	detach();
deba@822
   165
      }
deba@822
   166
    }
deba@822
   167
	
deba@822
   168
	
deba@822
   169
    /**
deba@822
   170
     * The subscript operator. The map can be subscripted by the
deba@822
   171
     * actual keys of the graph. 
deba@822
   172
     */
alpar@987
   173
    Reference operator[](const Key& key) {
deba@980
   174
      int id = graph->id(key);
deba@822
   175
      return values[id];
deba@822
   176
    } 
deba@822
   177
		
deba@822
   178
    /**
deba@822
   179
     * The const subscript operator. The map can be subscripted by the
deba@822
   180
     * actual keys of the graph. 
deba@822
   181
     */
alpar@987
   182
    ConstReference operator[](const Key& key) const {
deba@980
   183
      int id = graph->id(key);
deba@822
   184
      return values[id];
deba@822
   185
    }
deba@822
   186
	
deba@822
   187
    /** Setter function of the map. Equivalent with map[key] = val.
deba@822
   188
     *  This is a compatibility feature with the not dereferable maps.
deba@822
   189
     */
alpar@987
   190
    void set(const Key& key, const Value& val) {
klao@946
   191
      (*this)[key] = val;
deba@822
   192
    }
deba@822
   193
		
deba@822
   194
    /** Add a new key to the map. It called by the map registry.
deba@822
   195
     */
alpar@987
   196
    void add(const Key& key) {
deba@980
   197
      int id = graph->id(key);
deba@822
   198
      if (id >= capacity) {
deba@822
   199
	int new_capacity = (capacity == 0 ? 1 : capacity);
deba@822
   200
	while (new_capacity <= id) {
deba@822
   201
	  new_capacity <<= 1;
deba@822
   202
	}
klao@946
   203
	Value* new_values = allocator.allocate(new_capacity);
klao@946
   204
	for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   205
	  int jd = graph->id(it);;
deba@822
   206
	  if (id != jd) {
deba@822
   207
	    allocator.construct(&(new_values[jd]), values[jd]);
deba@822
   208
	    allocator.destroy(&(values[jd]));
deba@822
   209
	  }
deba@822
   210
	}
deba@822
   211
	if (capacity != 0) allocator.deallocate(values, capacity);
deba@822
   212
	values = new_values;
deba@822
   213
	capacity = new_capacity;
deba@822
   214
      }
deba@822
   215
      allocator.construct(&(values[id]), Value());
deba@822
   216
    }
deba@822
   217
		
deba@822
   218
    /** Erase a key from the map. It called by the map registry.
deba@822
   219
     */
alpar@987
   220
    void erase(const Key& key) {
deba@980
   221
      int id = graph->id(key);
deba@822
   222
      allocator.destroy(&(values[id]));
deba@822
   223
    }
deba@822
   224
klao@946
   225
    void build() {
klao@946
   226
      allocate_memory();
klao@946
   227
      for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   228
	int id = graph->id(it);;
klao@946
   229
	allocator.construct(&(values[id]), Value());
klao@946
   230
      }								
klao@946
   231
    }
klao@946
   232
deba@822
   233
    void clear() {	
deba@822
   234
      if (capacity != 0) {
klao@946
   235
	for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   236
	  int id = graph->id(it);;
klao@946
   237
	  allocator.destroy(&(values[id]));
klao@946
   238
	}								
deba@822
   239
	allocator.deallocate(values, capacity);
deba@822
   240
	capacity = 0;
deba@822
   241
      }
deba@822
   242
    }
deba@822
   243
klao@946
   244
//     /// The stl compatible pair iterator of the map.
klao@946
   245
//     typedef MapIterator<ArrayMap> Iterator;
klao@946
   246
//     /// The stl compatible const pair iterator of the map.
klao@946
   247
//     typedef MapConstIterator<ArrayMap> ConstIterator;
deba@822
   248
klao@946
   249
//     /** Returns the begin iterator of the map.
klao@946
   250
//      */
klao@946
   251
//     Iterator begin() {
klao@946
   252
//       return Iterator(*this, KeyIt(*MapBase::getGraph()));
klao@946
   253
//     }
deba@822
   254
klao@946
   255
//     /** Returns the end iterator of the map.
klao@946
   256
//      */
klao@946
   257
//     Iterator end() {
klao@946
   258
//       return Iterator(*this, INVALID);
klao@946
   259
//     }
deba@822
   260
klao@946
   261
//     /** Returns the begin ConstIterator of the map.
klao@946
   262
//      */
klao@946
   263
//     ConstIterator begin() const {
klao@946
   264
//       return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
klao@946
   265
//     }
deba@822
   266
klao@946
   267
//     /** Returns the end const_iterator of the map.
klao@946
   268
//      */
klao@946
   269
//     ConstIterator end() const {
klao@946
   270
//       return ConstIterator(*this, INVALID);
klao@946
   271
//     }
deba@822
   272
klao@946
   273
//     /// The KeySet of the Map.
klao@946
   274
//     typedef MapConstKeySet<ArrayMap> ConstKeySet;
deba@830
   275
klao@946
   276
//     /// KeySet getter function.
klao@946
   277
//     ConstKeySet keySet() const {
klao@946
   278
//       return ConstKeySet(*this); 
klao@946
   279
//     }
deba@830
   280
klao@946
   281
//     /// The ConstValueSet of the Map.
klao@946
   282
//     typedef MapConstValueSet<ArrayMap> ConstValueSet;
deba@830
   283
klao@946
   284
//     /// ConstValueSet getter function.
klao@946
   285
//     ConstValueSet valueSet() const {
klao@946
   286
//       return ConstValueSet(*this);
klao@946
   287
//     }
deba@830
   288
klao@946
   289
//     /// The ValueSet of the Map.
klao@946
   290
//     typedef MapValueSet<ArrayMap> ValueSet;
deba@830
   291
klao@946
   292
//     /// ValueSet getter function.
klao@946
   293
//     ValueSet valueSet() {
klao@946
   294
//       return ValueSet(*this);
klao@946
   295
//     }
deba@830
   296
deba@822
   297
  private:
deba@822
   298
      
deba@822
   299
    void allocate_memory() {
deba@980
   300
      int max_id = graph->maxId(_Item());
deba@822
   301
      if (max_id == -1) {
deba@822
   302
	capacity = 0;
deba@822
   303
	values = 0;
deba@822
   304
	return;
deba@822
   305
      }
deba@822
   306
      capacity = 1;
deba@822
   307
      while (capacity <= max_id) {
deba@822
   308
	capacity <<= 1;
deba@822
   309
      }
deba@822
   310
      values = allocator.allocate(capacity);	
deba@822
   311
    }      
deba@822
   312
klao@946
   313
    const Graph* graph;
deba@822
   314
    int capacity;
deba@822
   315
    Value* values;
deba@822
   316
    Allocator allocator;
deba@844
   317
deba@844
   318
  public:
klao@946
   319
//     // STL  compatibility typedefs.
klao@946
   320
//     typedef Iterator iterator;
klao@946
   321
//     typedef ConstIterator const_iterator;
alpar@987
   322
//     typedef typename Iterator::PairValue value_type;
alpar@987
   323
//     typedef typename Iterator::Key key_type;
alpar@987
   324
//     typedef typename Iterator::Value data_type;
alpar@987
   325
//     typedef typename Iterator::PairReference reference;
alpar@987
   326
//     typedef typename Iterator::PairPointer pointer;
alpar@987
   327
//     typedef typename ConstIterator::PairReference const_reference;
alpar@987
   328
//     typedef typename ConstIterator::PairPointer const_pointer;
klao@946
   329
//     typedef int difference_type;		
deba@822
   330
  };		
deba@822
   331
klao@946
   332
  template <typename _Base> 
klao@946
   333
  class ArrayMappableGraphExtender : public _Base {
klao@946
   334
  public:
klao@946
   335
klao@946
   336
    typedef ArrayMappableGraphExtender<_Base> Graph;
klao@946
   337
    typedef _Base Parent;
klao@946
   338
klao@946
   339
    typedef typename Parent::Node Node;
klao@946
   340
    typedef typename Parent::NodeIt NodeIt;
klao@946
   341
    typedef typename Parent::NodeObserverRegistry NodeObserverRegistry;
klao@946
   342
klao@946
   343
    typedef typename Parent::Edge Edge;
klao@946
   344
    typedef typename Parent::EdgeIt EdgeIt;
klao@946
   345
    typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
klao@946
   346
klao@946
   347
    
klao@946
   348
klao@946
   349
    template <typename _Value>
deba@980
   350
    class NodeMap : public ArrayMap<Graph, Node, NodeIt, _Value> {
klao@946
   351
    public:
klao@946
   352
      typedef ArrayMappableGraphExtender<_Base> Graph;
klao@946
   353
klao@946
   354
      typedef typename Graph::Node Node;
klao@946
   355
      typedef typename Graph::NodeIt NodeIt;
klao@946
   356
deba@980
   357
      typedef ArrayMap<Graph, Node, NodeIt, _Value> Parent;
klao@946
   358
klao@979
   359
      //typedef typename Parent::Graph Graph;
klao@946
   360
      typedef typename Parent::Value Value;
klao@946
   361
klao@946
   362
      NodeMap(const Graph& g) 
deba@980
   363
	: Parent(g) {}
klao@946
   364
      NodeMap(const Graph& g, const Value& v) 
deba@980
   365
	: Parent(g, v) {}
klao@946
   366
klao@946
   367
    };
klao@946
   368
klao@946
   369
    template <typename _Value>
deba@980
   370
    class EdgeMap : public ArrayMap<Graph, Edge, EdgeIt, _Value> {
klao@946
   371
    public:
klao@946
   372
      typedef ArrayMappableGraphExtender<_Base> Graph;
klao@946
   373
klao@946
   374
      typedef typename Graph::Edge Edge;
klao@946
   375
      typedef typename Graph::EdgeIt EdgeIt;
klao@946
   376
deba@980
   377
      typedef ArrayMap<Graph, Edge, EdgeIt, _Value> Parent;
klao@946
   378
klao@979
   379
      //typedef typename Parent::Graph Graph;
klao@946
   380
      typedef typename Parent::Value Value;
klao@946
   381
klao@946
   382
      EdgeMap(const Graph& g) 
deba@980
   383
	: Parent(g) {}
klao@946
   384
      EdgeMap(const Graph& g, const Value& v) 
deba@980
   385
	: Parent(g, v) {}
klao@946
   386
klao@946
   387
    };
klao@946
   388
    
klao@946
   389
  };
klao@946
   390
deba@822
   391
/// @}
deba@822
   392
deba@822
   393
}
deba@822
   394
alpar@921
   395
#endif //LEMON_ARRAY_MAP_H