lemon/bits/array_map.h
author alpar
Sat, 27 Aug 2005 11:37:05 +0000
changeset 1660 93792a112fd5
parent 1587 8f1c317ebeb4
child 1669 66ae78d29f1e
permissions -rw-r--r--
- Doc images partly turned on again.
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/bits/array_map.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, 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@1307
    21
#include <lemon/bits/map_iterator.h>
deba@822
    22
alpar@1587
    23
///\ingroup graphmapfactory
deba@822
    24
///\file
deba@822
    25
///\brief Graph maps that construates and destruates
deba@822
    26
///their elements dynamically.
deba@822
    27
alpar@921
    28
namespace lemon {
deba@822
    29
deba@822
    30
alpar@1587
    31
  /// \addtogroup graphmapfactory
deba@822
    32
  /// @{
deba@822
    33
	
deba@1414
    34
  /// The ArrayMap template class is graph map structure what
deba@1414
    35
  /// automatically updates the map when a key is added to or erased from
deba@1414
    36
  /// the map. This map factory uses the allocators to implement 
deba@1414
    37
  /// the container functionality.
deba@1414
    38
  ///
deba@1414
    39
  /// The template parameter is the AlterationNotifier that the maps
deba@1414
    40
  /// will belong to and the Value.
deba@1414
    41
   
deba@822
    42
klao@946
    43
  template <typename _Graph, 
klao@946
    44
	    typename _Item,
klao@946
    45
	    typename _Value>
deba@1039
    46
  class ArrayMap : public AlterationNotifier<_Item>::ObserverBase {
deba@897
    47
deba@1267
    48
    typedef _Item Item;
deba@822
    49
  public:
deba@822
    50
		
deba@822
    51
    /// The graph type of the maps. 
klao@946
    52
    typedef _Graph Graph;
deba@822
    53
    /// The key type of the maps.
alpar@987
    54
    typedef _Item Key;
klao@946
    55
deba@1039
    56
    typedef AlterationNotifier<_Item> Registry;
klao@946
    57
deba@822
    58
    /// The MapBase of the Map which imlements the core regisitry function.
klao@946
    59
    typedef typename Registry::ObserverBase Parent;
deba@822
    60
		
deba@822
    61
    /// The value type of the map.
alpar@987
    62
    typedef _Value Value;
deba@822
    63
deba@822
    64
klao@946
    65
  private:
deba@822
    66
    typedef std::allocator<Value> Allocator;
deba@822
    67
klao@946
    68
klao@946
    69
  public:
klao@946
    70
deba@1414
    71
    /// Graph and Registry initialized map constructor.
deba@1414
    72
     
deba@980
    73
    ArrayMap(const Graph& _g) : graph(&_g) {
deba@1267
    74
      Item it;
deba@1267
    75
      attach(_g.getNotifier(Item()));
deba@822
    76
      allocate_memory();
deba@1267
    77
      for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980
    78
	int id = graph->id(it);;
deba@822
    79
	allocator.construct(&(values[id]), Value());
deba@822
    80
      }								
deba@822
    81
    }
deba@822
    82
klao@946
    83
    /// Constructor to use default value to initialize the map. 
klao@946
    84
klao@946
    85
    /// It constrates a map and initialize all of the the map. 
klao@946
    86
deba@980
    87
    ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) {
deba@1267
    88
      Item it;
deba@1039
    89
      attach(_g.getNotifier(_Item()));
deba@822
    90
      allocate_memory();
deba@1267
    91
      for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980
    92
	int id = graph->id(it);;
klao@946
    93
	allocator.construct(&(values[id]), _v);
deba@822
    94
      }								
deba@822
    95
    }
deba@822
    96
deba@1414
    97
    /// Constructor to copy a map of the same map type.
deba@1414
    98
     
alpar@1613
    99
    ArrayMap(const ArrayMap& copy) : Parent(), graph(copy.graph) {
klao@946
   100
      if (copy.attached()) {
klao@946
   101
	attach(*copy.getRegistry());
klao@946
   102
      }
deba@822
   103
      capacity = copy.capacity;
deba@822
   104
      if (capacity == 0) return;
deba@822
   105
      values = allocator.allocate(capacity);
deba@1267
   106
      Item it;
deba@1267
   107
      for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980
   108
	int id = graph->id(it);;
deba@891
   109
	allocator.construct(&(values[id]), copy.values[id]);
deba@822
   110
      }
deba@822
   111
    }
deba@822
   112
klao@978
   113
    using Parent::attach;
klao@978
   114
    using Parent::detach;
klao@978
   115
    using Parent::attached;
klao@978
   116
deba@1414
   117
    /// Assign operator to copy a map of the same map type.
deba@1414
   118
     
deba@822
   119
    ArrayMap& operator=(const ArrayMap& copy) {
deba@822
   120
      if (&copy == this) return *this;
deba@897
   121
      
klao@946
   122
      if (graph != copy.graph) {
klao@946
   123
	if (attached()) {
klao@946
   124
	  clear();
klao@946
   125
	  detach();
deba@897
   126
	}
klao@946
   127
	if (copy.attached()) {
klao@946
   128
	  attach(*copy.getRegistry());
klao@946
   129
	}
deba@897
   130
	capacity = copy.capacity;
deba@897
   131
	if (capacity == 0) return *this;
deba@897
   132
	values = allocator.allocate(capacity);      
deba@822
   133
      }
deba@891
   134
deba@1267
   135
      Item it;
deba@1267
   136
      for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980
   137
	int id = graph->id(it);;
deba@822
   138
	allocator.construct(&(values[id]), copy.values[id]);
deba@822
   139
      }
deba@891
   140
deba@822
   141
      return *this;
deba@822
   142
    }
deba@822
   143
deba@1414
   144
    /// The destructor of the map.
deba@1414
   145
     
klao@946
   146
    virtual ~ArrayMap() {      
klao@946
   147
      if (attached()) {
klao@946
   148
	clear();
klao@946
   149
	detach();
deba@822
   150
      }
deba@822
   151
    }
deba@822
   152
	
deba@822
   153
	
deba@1414
   154
    ///The subscript operator. The map can be subscripted by the
deba@1414
   155
    ///actual keys of the graph. 
deba@1414
   156
     
deba@1267
   157
    Value& operator[](const Key& key) {
deba@980
   158
      int id = graph->id(key);
deba@822
   159
      return values[id];
deba@822
   160
    } 
deba@822
   161
		
deba@1414
   162
deba@1414
   163
    ///The const subscript operator. The map can be subscripted by the
deba@1414
   164
    ///actual keys of the graph. 
deba@1414
   165
     
deba@1267
   166
    const Value& operator[](const Key& key) const {
deba@980
   167
      int id = graph->id(key);
deba@822
   168
      return values[id];
deba@822
   169
    }
deba@822
   170
	
deba@1414
   171
    /// Setter function of the map. Equivalent with map[key] = val.
deba@1414
   172
    /// This is a compatibility feature with the not dereferable maps.
deba@1414
   173
     
alpar@987
   174
    void set(const Key& key, const Value& val) {
klao@946
   175
      (*this)[key] = val;
deba@822
   176
    }
deba@822
   177
		
deba@1414
   178
    /// Add a new key to the map. It called by the map registry.
deba@1414
   179
     
alpar@987
   180
    void add(const Key& key) {
deba@980
   181
      int id = graph->id(key);
deba@822
   182
      if (id >= capacity) {
deba@822
   183
	int new_capacity = (capacity == 0 ? 1 : capacity);
deba@822
   184
	while (new_capacity <= id) {
deba@822
   185
	  new_capacity <<= 1;
deba@822
   186
	}
klao@946
   187
	Value* new_values = allocator.allocate(new_capacity);
deba@1267
   188
	Item it;
deba@1267
   189
	for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980
   190
	  int jd = graph->id(it);;
deba@822
   191
	  if (id != jd) {
deba@822
   192
	    allocator.construct(&(new_values[jd]), values[jd]);
deba@822
   193
	    allocator.destroy(&(values[jd]));
deba@822
   194
	  }
deba@822
   195
	}
deba@822
   196
	if (capacity != 0) allocator.deallocate(values, capacity);
deba@822
   197
	values = new_values;
deba@822
   198
	capacity = new_capacity;
deba@822
   199
      }
deba@822
   200
      allocator.construct(&(values[id]), Value());
deba@822
   201
    }
deba@1414
   202
deba@1414
   203
    void add(const std::vector<Key>& keys) {
deba@1414
   204
      int max_id = -1;
deba@1414
   205
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1414
   206
	int id = graph->id(keys[i]);
deba@1414
   207
	if (id > max_id) {
deba@1414
   208
	  max_id = id;
deba@1414
   209
	}
deba@1414
   210
      }
deba@1414
   211
      if (max_id >= capacity) {
deba@1414
   212
	int new_capacity = (capacity == 0 ? 1 : capacity);
deba@1414
   213
	while (new_capacity <= max_id) {
deba@1414
   214
	  new_capacity <<= 1;
deba@1414
   215
	}
deba@1414
   216
	Value* new_values = allocator.allocate(new_capacity);
deba@1414
   217
	Item it;
deba@1414
   218
	for (graph->first(it); it != INVALID; graph->next(it)) {
deba@1414
   219
	  int id = graph->id(it);
deba@1414
   220
	  bool found = false;
deba@1414
   221
	  for (int i = 0; i < (int)keys.size(); ++i) {
deba@1414
   222
	    int jd = graph->id(keys[i]);
deba@1414
   223
	    if (id == jd) {
deba@1414
   224
	      found = true;
deba@1414
   225
	      break;
deba@1414
   226
	    }
deba@1414
   227
	  }
deba@1414
   228
	  if (found) continue;
deba@1414
   229
	  allocator.construct(&(new_values[id]), values[id]);
deba@1414
   230
	  allocator.destroy(&(values[id]));
deba@1414
   231
	}
deba@1414
   232
	if (capacity != 0) allocator.deallocate(values, capacity);
deba@1414
   233
	values = new_values;
deba@1414
   234
	capacity = new_capacity;
deba@1414
   235
      }
deba@1414
   236
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1414
   237
	int id = graph->id(keys[i]);
deba@1414
   238
	allocator.construct(&(values[id]), Value());
deba@1414
   239
      }
deba@1414
   240
    }
deba@822
   241
		
deba@1414
   242
    /// Erase a key from the map. It called by the map registry.
deba@1414
   243
     
alpar@987
   244
    void erase(const Key& key) {
deba@980
   245
      int id = graph->id(key);
deba@822
   246
      allocator.destroy(&(values[id]));
deba@822
   247
    }
deba@822
   248
deba@1414
   249
    void erase(const std::vector<Key>& keys) {
deba@1414
   250
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1414
   251
	int id = graph->id(keys[i]);
deba@1414
   252
	allocator.destroy(&(values[id]));
deba@1414
   253
      }
deba@1414
   254
    }
deba@1414
   255
klao@946
   256
    void build() {
klao@946
   257
      allocate_memory();
deba@1267
   258
      Item it;
deba@1267
   259
      for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980
   260
	int id = graph->id(it);;
klao@946
   261
	allocator.construct(&(values[id]), Value());
klao@946
   262
      }								
klao@946
   263
    }
klao@946
   264
deba@822
   265
    void clear() {	
deba@822
   266
      if (capacity != 0) {
deba@1267
   267
	Item it;
deba@1267
   268
	for (graph->first(it); it != INVALID; graph->next(it)) {
deba@1414
   269
	  int id = graph->id(it);
klao@946
   270
	  allocator.destroy(&(values[id]));
klao@946
   271
	}								
deba@822
   272
	allocator.deallocate(values, capacity);
deba@822
   273
	capacity = 0;
deba@822
   274
      }
deba@822
   275
    }
deba@822
   276
deba@1271
   277
    const Graph* getGraph() {
deba@1271
   278
      return graph;
deba@1271
   279
    }
deba@1271
   280
deba@822
   281
  private:
deba@822
   282
      
deba@822
   283
    void allocate_memory() {
deba@980
   284
      int max_id = graph->maxId(_Item());
deba@822
   285
      if (max_id == -1) {
deba@822
   286
	capacity = 0;
deba@822
   287
	values = 0;
deba@822
   288
	return;
deba@822
   289
      }
deba@822
   290
      capacity = 1;
deba@822
   291
      while (capacity <= max_id) {
deba@822
   292
	capacity <<= 1;
deba@822
   293
      }
deba@822
   294
      values = allocator.allocate(capacity);	
deba@822
   295
    }      
deba@822
   296
klao@946
   297
    const Graph* graph;
deba@822
   298
    int capacity;
deba@822
   299
    Value* values;
deba@822
   300
    Allocator allocator;
deba@844
   301
deba@822
   302
  };		
deba@822
   303
klao@946
   304
  template <typename _Base> 
klao@946
   305
  class ArrayMappableGraphExtender : public _Base {
klao@946
   306
  public:
klao@946
   307
klao@946
   308
    typedef ArrayMappableGraphExtender<_Base> Graph;
klao@946
   309
    typedef _Base Parent;
klao@946
   310
klao@946
   311
    typedef typename Parent::Node Node;
klao@946
   312
    typedef typename Parent::NodeIt NodeIt;
deba@1039
   313
    typedef typename Parent::NodeNotifier NodeObserverRegistry;
klao@946
   314
klao@946
   315
    typedef typename Parent::Edge Edge;
klao@946
   316
    typedef typename Parent::EdgeIt EdgeIt;
deba@1039
   317
    typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
klao@946
   318
klao@946
   319
    
klao@946
   320
klao@946
   321
    template <typename _Value>
deba@1267
   322
    class NodeMap 
deba@1267
   323
      : public IterableMapExtender<ArrayMap<Graph, Node, _Value> > {
klao@946
   324
    public:
klao@946
   325
      typedef ArrayMappableGraphExtender<_Base> Graph;
klao@946
   326
klao@946
   327
      typedef typename Graph::Node Node;
klao@946
   328
      typedef typename Graph::NodeIt NodeIt;
klao@946
   329
deba@1267
   330
      typedef IterableMapExtender<ArrayMap<Graph, Node, _Value> > Parent;
klao@946
   331
klao@979
   332
      //typedef typename Parent::Graph Graph;
klao@946
   333
      typedef typename Parent::Value Value;
klao@946
   334
klao@946
   335
      NodeMap(const Graph& g) 
deba@980
   336
	: Parent(g) {}
klao@946
   337
      NodeMap(const Graph& g, const Value& v) 
deba@980
   338
	: Parent(g, v) {}
klao@946
   339
klao@946
   340
    };
klao@946
   341
klao@946
   342
    template <typename _Value>
deba@1267
   343
    class EdgeMap 
deba@1267
   344
      : public IterableMapExtender<ArrayMap<Graph, Edge, _Value> > {
klao@946
   345
    public:
klao@946
   346
      typedef ArrayMappableGraphExtender<_Base> Graph;
klao@946
   347
klao@946
   348
      typedef typename Graph::Edge Edge;
klao@946
   349
      typedef typename Graph::EdgeIt EdgeIt;
klao@946
   350
deba@1267
   351
      typedef IterableMapExtender<ArrayMap<Graph, Edge, _Value> > Parent;
klao@946
   352
klao@979
   353
      //typedef typename Parent::Graph Graph;
klao@946
   354
      typedef typename Parent::Value Value;
klao@946
   355
klao@946
   356
      EdgeMap(const Graph& g) 
deba@980
   357
	: Parent(g) {}
klao@946
   358
      EdgeMap(const Graph& g, const Value& v) 
deba@980
   359
	: Parent(g, v) {}
klao@946
   360
klao@946
   361
    };
klao@946
   362
    
klao@946
   363
  };
klao@946
   364
deba@822
   365
/// @}
deba@822
   366
deba@822
   367
}
deba@822
   368
alpar@921
   369
#endif //LEMON_ARRAY_MAP_H