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