src/lemon/array_map.h
author deba
Thu, 11 Nov 2004 09:31:55 +0000
changeset 980 0f1044b7a3af
parent 979 b5fb023cdb7b
child 987 87f7c54892df
permissions -rw-r--r--
maxNodeId() and maxEdgeId() changed to maxId(Node) and maxId(Edge)
getNodeObserverRegistry() and getEdgeObserverRegistry() changed to
getObserverRegistry(Node) and getObserverRegistry(Edge)

IdMappableGraphExtender erased
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
deba@822
    41
   *  will belong to and the ValueType.
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.
klao@946
    55
    typedef _Item KeyType;
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
klao@946
    63
    typedef _Value Value;
deba@822
    64
deba@822
    65
    /// The MapBase of the Map which imlements the core regisitry function.
klao@946
    66
    typedef typename Registry::ObserverBase Parent;
deba@822
    67
		
deba@822
    68
    
deba@822
    69
  public:
deba@822
    70
deba@822
    71
    /// The value type of the map.
deba@822
    72
    typedef Value ValueType;
deba@822
    73
    /// The reference type of the map;
deba@822
    74
    typedef Value& ReferenceType;
deba@822
    75
    /// The pointer type of the map;
deba@822
    76
    typedef Value* PointerType;
deba@822
    77
deba@822
    78
    /// The const value type of the map.
deba@822
    79
    typedef const Value ConstValueType;
deba@822
    80
    /// The const reference type of the map;
deba@822
    81
    typedef const Value& ConstReferenceType;
deba@822
    82
    /// The pointer type of the map;
deba@822
    83
    typedef const Value* ConstPointerType;
deba@822
    84
deba@822
    85
klao@946
    86
  private:
deba@822
    87
    typedef std::allocator<Value> Allocator;
deba@822
    88
klao@946
    89
klao@946
    90
  public:
klao@946
    91
deba@822
    92
    /** Graph and Registry initialized map constructor.
deba@822
    93
     */
deba@980
    94
    ArrayMap(const Graph& _g) : graph(&_g) {
deba@980
    95
      attach(_g.getObserverRegistry(_Item()));
deba@822
    96
      allocate_memory();
klao@946
    97
      for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
    98
	int id = graph->id(it);;
deba@822
    99
	allocator.construct(&(values[id]), Value());
deba@822
   100
      }								
deba@822
   101
    }
deba@822
   102
klao@946
   103
    /// Constructor to use default value to initialize the map. 
klao@946
   104
klao@946
   105
    /// It constrates a map and initialize all of the the map. 
klao@946
   106
deba@980
   107
    ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) {
deba@980
   108
      attach(_g.getObserverRegistry(_Item()));
deba@822
   109
      allocate_memory();
klao@946
   110
      for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   111
	int id = graph->id(it);;
klao@946
   112
	allocator.construct(&(values[id]), _v);
deba@822
   113
      }								
deba@822
   114
    }
deba@822
   115
deba@822
   116
    /** Constructor to copy a map of the same map type.
deba@822
   117
     */
klao@946
   118
    ArrayMap(const ArrayMap& copy) {
klao@946
   119
      if (copy.attached()) {
klao@946
   120
	attach(*copy.getRegistry());
klao@946
   121
      }
deba@822
   122
      capacity = copy.capacity;
deba@822
   123
      if (capacity == 0) return;
deba@822
   124
      values = allocator.allocate(capacity);
klao@946
   125
      for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   126
	int id = graph->id(it);;
deba@891
   127
	allocator.construct(&(values[id]), copy.values[id]);
deba@822
   128
      }
deba@822
   129
    }
deba@822
   130
klao@978
   131
    using Parent::attach;
klao@978
   132
    using Parent::detach;
klao@978
   133
    using Parent::attached;
klao@978
   134
deba@822
   135
    /** Assign operator to copy a map of the same map type.
deba@822
   136
     */
deba@822
   137
    ArrayMap& operator=(const ArrayMap& copy) {
deba@822
   138
      if (&copy == this) return *this;
deba@897
   139
      
klao@946
   140
      if (graph != copy.graph) {
klao@946
   141
	if (attached()) {
klao@946
   142
	  clear();
klao@946
   143
	  detach();
deba@897
   144
	}
klao@946
   145
	if (copy.attached()) {
klao@946
   146
	  attach(*copy.getRegistry());
klao@946
   147
	}
deba@897
   148
	capacity = copy.capacity;
deba@897
   149
	if (capacity == 0) return *this;
deba@897
   150
	values = allocator.allocate(capacity);      
deba@822
   151
      }
deba@891
   152
klao@946
   153
      for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   154
	int id = graph->id(it);;
deba@822
   155
	allocator.construct(&(values[id]), copy.values[id]);
deba@822
   156
      }
deba@891
   157
deba@822
   158
      return *this;
deba@822
   159
    }
deba@822
   160
deba@822
   161
    /** The destructor of the map.
deba@822
   162
     */
klao@946
   163
    virtual ~ArrayMap() {      
klao@946
   164
      if (attached()) {
klao@946
   165
	clear();
klao@946
   166
	detach();
deba@822
   167
      }
deba@822
   168
    }
deba@822
   169
	
deba@822
   170
	
deba@822
   171
    /**
deba@822
   172
     * The subscript operator. The map can be subscripted by the
deba@822
   173
     * actual keys of the graph. 
deba@822
   174
     */
deba@822
   175
    ReferenceType operator[](const KeyType& key) {
deba@980
   176
      int id = graph->id(key);
deba@822
   177
      return values[id];
deba@822
   178
    } 
deba@822
   179
		
deba@822
   180
    /**
deba@822
   181
     * The const subscript operator. The map can be subscripted by the
deba@822
   182
     * actual keys of the graph. 
deba@822
   183
     */
deba@822
   184
    ConstReferenceType operator[](const KeyType& key) const {
deba@980
   185
      int id = graph->id(key);
deba@822
   186
      return values[id];
deba@822
   187
    }
deba@822
   188
	
deba@822
   189
    /** Setter function of the map. Equivalent with map[key] = val.
deba@822
   190
     *  This is a compatibility feature with the not dereferable maps.
deba@822
   191
     */
deba@822
   192
    void set(const KeyType& key, const ValueType& val) {
klao@946
   193
      (*this)[key] = val;
deba@822
   194
    }
deba@822
   195
		
deba@822
   196
    /** Add a new key to the map. It called by the map registry.
deba@822
   197
     */
deba@822
   198
    void add(const KeyType& key) {
deba@980
   199
      int id = graph->id(key);
deba@822
   200
      if (id >= capacity) {
deba@822
   201
	int new_capacity = (capacity == 0 ? 1 : capacity);
deba@822
   202
	while (new_capacity <= id) {
deba@822
   203
	  new_capacity <<= 1;
deba@822
   204
	}
klao@946
   205
	Value* new_values = allocator.allocate(new_capacity);
klao@946
   206
	for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   207
	  int jd = graph->id(it);;
deba@822
   208
	  if (id != jd) {
deba@822
   209
	    allocator.construct(&(new_values[jd]), values[jd]);
deba@822
   210
	    allocator.destroy(&(values[jd]));
deba@822
   211
	  }
deba@822
   212
	}
deba@822
   213
	if (capacity != 0) allocator.deallocate(values, capacity);
deba@822
   214
	values = new_values;
deba@822
   215
	capacity = new_capacity;
deba@822
   216
      }
deba@822
   217
      allocator.construct(&(values[id]), Value());
deba@822
   218
    }
deba@822
   219
		
deba@822
   220
    /** Erase a key from the map. It called by the map registry.
deba@822
   221
     */
deba@822
   222
    void erase(const KeyType& key) {
deba@980
   223
      int id = graph->id(key);
deba@822
   224
      allocator.destroy(&(values[id]));
deba@822
   225
    }
deba@822
   226
klao@946
   227
    void build() {
klao@946
   228
      allocate_memory();
klao@946
   229
      for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   230
	int id = graph->id(it);;
klao@946
   231
	allocator.construct(&(values[id]), Value());
klao@946
   232
      }								
klao@946
   233
    }
klao@946
   234
deba@822
   235
    void clear() {	
deba@822
   236
      if (capacity != 0) {
klao@946
   237
	for (KeyIt it(*graph); it != INVALID; ++it) {
deba@980
   238
	  int id = graph->id(it);;
klao@946
   239
	  allocator.destroy(&(values[id]));
klao@946
   240
	}								
deba@822
   241
	allocator.deallocate(values, capacity);
deba@822
   242
	capacity = 0;
deba@822
   243
      }
deba@822
   244
    }
deba@822
   245
klao@946
   246
//     /// The stl compatible pair iterator of the map.
klao@946
   247
//     typedef MapIterator<ArrayMap> Iterator;
klao@946
   248
//     /// The stl compatible const pair iterator of the map.
klao@946
   249
//     typedef MapConstIterator<ArrayMap> ConstIterator;
deba@822
   250
klao@946
   251
//     /** Returns the begin iterator of the map.
klao@946
   252
//      */
klao@946
   253
//     Iterator begin() {
klao@946
   254
//       return Iterator(*this, KeyIt(*MapBase::getGraph()));
klao@946
   255
//     }
deba@822
   256
klao@946
   257
//     /** Returns the end iterator of the map.
klao@946
   258
//      */
klao@946
   259
//     Iterator end() {
klao@946
   260
//       return Iterator(*this, INVALID);
klao@946
   261
//     }
deba@822
   262
klao@946
   263
//     /** Returns the begin ConstIterator of the map.
klao@946
   264
//      */
klao@946
   265
//     ConstIterator begin() const {
klao@946
   266
//       return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
klao@946
   267
//     }
deba@822
   268
klao@946
   269
//     /** Returns the end const_iterator of the map.
klao@946
   270
//      */
klao@946
   271
//     ConstIterator end() const {
klao@946
   272
//       return ConstIterator(*this, INVALID);
klao@946
   273
//     }
deba@822
   274
klao@946
   275
//     /// The KeySet of the Map.
klao@946
   276
//     typedef MapConstKeySet<ArrayMap> ConstKeySet;
deba@830
   277
klao@946
   278
//     /// KeySet getter function.
klao@946
   279
//     ConstKeySet keySet() const {
klao@946
   280
//       return ConstKeySet(*this); 
klao@946
   281
//     }
deba@830
   282
klao@946
   283
//     /// The ConstValueSet of the Map.
klao@946
   284
//     typedef MapConstValueSet<ArrayMap> ConstValueSet;
deba@830
   285
klao@946
   286
//     /// ConstValueSet getter function.
klao@946
   287
//     ConstValueSet valueSet() const {
klao@946
   288
//       return ConstValueSet(*this);
klao@946
   289
//     }
deba@830
   290
klao@946
   291
//     /// The ValueSet of the Map.
klao@946
   292
//     typedef MapValueSet<ArrayMap> ValueSet;
deba@830
   293
klao@946
   294
//     /// ValueSet getter function.
klao@946
   295
//     ValueSet valueSet() {
klao@946
   296
//       return ValueSet(*this);
klao@946
   297
//     }
deba@830
   298
deba@822
   299
  private:
deba@822
   300
      
deba@822
   301
    void allocate_memory() {
deba@980
   302
      int max_id = graph->maxId(_Item());
deba@822
   303
      if (max_id == -1) {
deba@822
   304
	capacity = 0;
deba@822
   305
	values = 0;
deba@822
   306
	return;
deba@822
   307
      }
deba@822
   308
      capacity = 1;
deba@822
   309
      while (capacity <= max_id) {
deba@822
   310
	capacity <<= 1;
deba@822
   311
      }
deba@822
   312
      values = allocator.allocate(capacity);	
deba@822
   313
    }      
deba@822
   314
klao@946
   315
    const Graph* graph;
deba@822
   316
    int capacity;
deba@822
   317
    Value* values;
deba@822
   318
    Allocator allocator;
deba@844
   319
deba@844
   320
  public:
klao@946
   321
//     // STL  compatibility typedefs.
klao@946
   322
//     typedef Iterator iterator;
klao@946
   323
//     typedef ConstIterator const_iterator;
klao@946
   324
//     typedef typename Iterator::PairValueType value_type;
klao@946
   325
//     typedef typename Iterator::KeyType key_type;
klao@946
   326
//     typedef typename Iterator::ValueType data_type;
klao@946
   327
//     typedef typename Iterator::PairReferenceType reference;
klao@946
   328
//     typedef typename Iterator::PairPointerType pointer;
klao@946
   329
//     typedef typename ConstIterator::PairReferenceType const_reference;
klao@946
   330
//     typedef typename ConstIterator::PairPointerType const_pointer;
klao@946
   331
//     typedef int difference_type;		
deba@822
   332
  };		
deba@822
   333
klao@946
   334
  template <typename _Base> 
klao@946
   335
  class ArrayMappableGraphExtender : public _Base {
klao@946
   336
  public:
klao@946
   337
klao@946
   338
    typedef ArrayMappableGraphExtender<_Base> Graph;
klao@946
   339
    typedef _Base Parent;
klao@946
   340
klao@946
   341
    typedef typename Parent::Node Node;
klao@946
   342
    typedef typename Parent::NodeIt NodeIt;
klao@946
   343
    typedef typename Parent::NodeObserverRegistry NodeObserverRegistry;
klao@946
   344
klao@946
   345
    typedef typename Parent::Edge Edge;
klao@946
   346
    typedef typename Parent::EdgeIt EdgeIt;
klao@946
   347
    typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
klao@946
   348
klao@946
   349
    
klao@946
   350
klao@946
   351
    template <typename _Value>
deba@980
   352
    class NodeMap : public ArrayMap<Graph, Node, NodeIt, _Value> {
klao@946
   353
    public:
klao@946
   354
      typedef ArrayMappableGraphExtender<_Base> Graph;
klao@946
   355
klao@946
   356
      typedef typename Graph::Node Node;
klao@946
   357
      typedef typename Graph::NodeIt NodeIt;
klao@946
   358
deba@980
   359
      typedef ArrayMap<Graph, Node, NodeIt, _Value> Parent;
klao@946
   360
klao@979
   361
      //typedef typename Parent::Graph Graph;
klao@946
   362
      typedef typename Parent::Value Value;
klao@946
   363
klao@946
   364
      NodeMap(const Graph& g) 
deba@980
   365
	: Parent(g) {}
klao@946
   366
      NodeMap(const Graph& g, const Value& v) 
deba@980
   367
	: Parent(g, v) {}
klao@946
   368
klao@946
   369
    };
klao@946
   370
klao@946
   371
    template <typename _Value>
deba@980
   372
    class EdgeMap : public ArrayMap<Graph, Edge, EdgeIt, _Value> {
klao@946
   373
    public:
klao@946
   374
      typedef ArrayMappableGraphExtender<_Base> Graph;
klao@946
   375
klao@946
   376
      typedef typename Graph::Edge Edge;
klao@946
   377
      typedef typename Graph::EdgeIt EdgeIt;
klao@946
   378
deba@980
   379
      typedef ArrayMap<Graph, Edge, EdgeIt, _Value> Parent;
klao@946
   380
klao@979
   381
      //typedef typename Parent::Graph Graph;
klao@946
   382
      typedef typename Parent::Value Value;
klao@946
   383
klao@946
   384
      EdgeMap(const Graph& g) 
deba@980
   385
	: Parent(g) {}
klao@946
   386
      EdgeMap(const Graph& g, const Value& v) 
deba@980
   387
	: Parent(g, v) {}
klao@946
   388
klao@946
   389
    };
klao@946
   390
    
klao@946
   391
  };
klao@946
   392
deba@822
   393
/// @}
deba@822
   394
deba@822
   395
}
deba@822
   396
alpar@921
   397
#endif //LEMON_ARRAY_MAP_H