lemon/bits/array_map.h
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 01 Apr 2008 16:25:51 +0100
changeset 115 7b44eea654d0
parent 57 c1acf0018c0a
child 209 765619b7cbb2
permissions -rw-r--r--
Merge
deba@57
     1
/* -*- C++ -*-
deba@57
     2
 *
deba@57
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@57
     4
 *
alpar@107
     5
 * Copyright (C) 2003-2008
deba@57
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57
     8
 *
deba@57
     9
 * Permission to use, modify and distribute this software is granted
deba@57
    10
 * provided that this copyright notice appears in all copies. For
deba@57
    11
 * precise terms see the accompanying LICENSE file.
deba@57
    12
 *
deba@57
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@57
    14
 * express or implied, and with no claim as to its suitability for any
deba@57
    15
 * purpose.
deba@57
    16
 *
deba@57
    17
 */
deba@57
    18
deba@57
    19
#ifndef LEMON_BITS_ARRAY_MAP_H
deba@57
    20
#define LEMON_BITS_ARRAY_MAP_H
deba@57
    21
deba@57
    22
#include <memory>
deba@57
    23
deba@57
    24
#include <lemon/bits/traits.h>
deba@57
    25
#include <lemon/bits/alteration_notifier.h>
deba@57
    26
#include <lemon/concept_check.h>
deba@57
    27
#include <lemon/concepts/maps.h>
deba@57
    28
deba@57
    29
/// \ingroup graphbits
deba@57
    30
/// \file
deba@57
    31
/// \brief Graph map based on the array storage.
deba@57
    32
deba@57
    33
namespace lemon {
deba@57
    34
deba@57
    35
  /// \ingroup graphbits
deba@57
    36
  ///
deba@57
    37
  /// \brief Graph map based on the array storage.
deba@57
    38
  ///
deba@57
    39
  /// The ArrayMap template class is graph map structure what
deba@57
    40
  /// automatically updates the map when a key is added to or erased from
deba@57
    41
  /// the map. This map uses the allocators to implement 
deba@57
    42
  /// the container functionality.
deba@57
    43
  ///
deba@57
    44
  /// The template parameters are the Graph the current Item type and
deba@57
    45
  /// the Value type of the map.
deba@57
    46
  template <typename _Graph, typename _Item, typename _Value>
deba@57
    47
  class ArrayMap 
deba@57
    48
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
deba@57
    49
  public:
deba@57
    50
    /// The graph type of the maps. 
deba@57
    51
    typedef _Graph Graph;
deba@57
    52
    /// The item type of the map.
deba@57
    53
    typedef _Item Item;
deba@57
    54
    /// The reference map tag.
deba@57
    55
    typedef True ReferenceMapTag;
deba@57
    56
deba@57
    57
    /// The key type of the maps.
deba@57
    58
    typedef _Item Key;
deba@57
    59
    /// The value type of the map.
deba@57
    60
    typedef _Value Value;
deba@57
    61
deba@57
    62
    /// The const reference type of the map.
deba@57
    63
    typedef const _Value& ConstReference;
deba@57
    64
    /// The reference type of the map.
deba@57
    65
    typedef _Value& Reference;
deba@57
    66
deba@57
    67
    /// The notifier type.
deba@57
    68
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
deba@57
    69
deba@57
    70
    /// The MapBase of the Map which imlements the core regisitry function.
deba@57
    71
    typedef typename Notifier::ObserverBase Parent;
deba@57
    72
		
deba@57
    73
  private:
deba@57
    74
    typedef std::allocator<Value> Allocator;
deba@57
    75
deba@57
    76
  public:
deba@57
    77
deba@57
    78
    /// \brief Graph initialized map constructor.
deba@57
    79
    ///
deba@57
    80
    /// Graph initialized map constructor.
deba@57
    81
    explicit ArrayMap(const Graph& graph) {
deba@57
    82
      Parent::attach(graph.notifier(Item()));
deba@57
    83
      allocate_memory();
deba@57
    84
      Notifier* nf = Parent::notifier();
deba@57
    85
      Item it;
deba@57
    86
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@57
    87
	int id = nf->id(it);;
deba@57
    88
	allocator.construct(&(values[id]), Value());
deba@57
    89
      }								
deba@57
    90
    }
deba@57
    91
deba@57
    92
    /// \brief Constructor to use default value to initialize the map. 
deba@57
    93
    ///
deba@57
    94
    /// It constructs a map and initialize all of the the map. 
deba@57
    95
    ArrayMap(const Graph& graph, const Value& value) {
deba@57
    96
      Parent::attach(graph.notifier(Item()));
deba@57
    97
      allocate_memory();
deba@57
    98
      Notifier* nf = Parent::notifier();
deba@57
    99
      Item it;
deba@57
   100
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@57
   101
	int id = nf->id(it);;
deba@57
   102
	allocator.construct(&(values[id]), value);
deba@57
   103
      }								
deba@57
   104
    }
deba@57
   105
deba@57
   106
    /// \brief Constructor to copy a map of the same map type.
deba@57
   107
    ///
deba@57
   108
    /// Constructor to copy a map of the same map type.     
deba@57
   109
    ArrayMap(const ArrayMap& copy) : Parent() {
deba@57
   110
      if (copy.attached()) {
deba@57
   111
	attach(*copy.notifier());
deba@57
   112
      }
deba@57
   113
      capacity = copy.capacity;
deba@57
   114
      if (capacity == 0) return;
deba@57
   115
      values = allocator.allocate(capacity);
deba@57
   116
      Notifier* nf = Parent::notifier();
deba@57
   117
      Item it;
deba@57
   118
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@57
   119
	int id = nf->id(it);;
deba@57
   120
	allocator.construct(&(values[id]), copy.values[id]);
deba@57
   121
      }
deba@57
   122
    }
deba@57
   123
deba@57
   124
    /// \brief Assign operator.
deba@57
   125
    ///
deba@57
   126
    /// This operator assigns for each item in the map the
deba@57
   127
    /// value mapped to the same item in the copied map.  
deba@57
   128
    /// The parameter map should be indiced with the same
deba@57
   129
    /// itemset because this assign operator does not change
deba@57
   130
    /// the container of the map. 
deba@57
   131
    ArrayMap& operator=(const ArrayMap& cmap) {
deba@57
   132
      return operator=<ArrayMap>(cmap);
deba@57
   133
    }
deba@57
   134
deba@57
   135
deba@57
   136
    /// \brief Template assign operator.
deba@57
   137
    ///
deba@57
   138
    /// The given parameter should be conform to the ReadMap
deba@57
   139
    /// concecpt and could be indiced by the current item set of
deba@57
   140
    /// the NodeMap. In this case the value for each item
deba@57
   141
    /// is assigned by the value of the given ReadMap. 
deba@57
   142
    template <typename CMap>
deba@57
   143
    ArrayMap& operator=(const CMap& cmap) {
deba@57
   144
      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
deba@57
   145
      const typename Parent::Notifier* nf = Parent::notifier();
deba@57
   146
      Item it;
deba@57
   147
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@57
   148
        set(it, cmap[it]);
deba@57
   149
      }
deba@57
   150
      return *this;
deba@57
   151
    }
deba@57
   152
deba@57
   153
    /// \brief The destructor of the map.
deba@57
   154
    ///     
deba@57
   155
    /// The destructor of the map.
deba@57
   156
    virtual ~ArrayMap() {      
deba@57
   157
      if (attached()) {
deba@57
   158
	clear();
deba@57
   159
	detach();
deba@57
   160
      }
deba@57
   161
    }
deba@57
   162
		
deba@57
   163
  protected:
deba@57
   164
deba@57
   165
    using Parent::attach;
deba@57
   166
    using Parent::detach;
deba@57
   167
    using Parent::attached;
deba@57
   168
deba@57
   169
  public:
deba@57
   170
deba@57
   171
    /// \brief The subscript operator. 
deba@57
   172
    ///
deba@57
   173
    /// The subscript operator. The map can be subscripted by the
deba@57
   174
    /// actual keys of the graph. 
deba@57
   175
    Value& operator[](const Key& key) {
deba@57
   176
      int id = Parent::notifier()->id(key);
deba@57
   177
      return values[id];
deba@57
   178
    } 
deba@57
   179
		
deba@57
   180
    /// \brief The const subscript operator.
deba@57
   181
    ///
deba@57
   182
    /// The const subscript operator. The map can be subscripted by the
deba@57
   183
    /// actual keys of the graph. 
deba@57
   184
    const Value& operator[](const Key& key) const {
deba@57
   185
      int id = Parent::notifier()->id(key);
deba@57
   186
      return values[id];
deba@57
   187
    }
deba@57
   188
deba@57
   189
    /// \brief Setter function of the map.
deba@57
   190
    ///	
deba@57
   191
    /// Setter function of the map. Equivalent with map[key] = val.
deba@57
   192
    /// This is a compatibility feature with the not dereferable maps.
deba@57
   193
    void set(const Key& key, const Value& val) {
deba@57
   194
      (*this)[key] = val;
deba@57
   195
    }
deba@57
   196
deba@57
   197
  protected:
deba@57
   198
deba@57
   199
    /// \brief Adds a new key to the map.
deba@57
   200
    ///		
deba@57
   201
    /// It adds a new key to the map. It called by the observer notifier
deba@57
   202
    /// and it overrides the add() member function of the observer base.     
deba@57
   203
    virtual void add(const Key& key) {
deba@57
   204
      Notifier* nf = Parent::notifier();
deba@57
   205
      int id = nf->id(key);
deba@57
   206
      if (id >= capacity) {
deba@57
   207
	int new_capacity = (capacity == 0 ? 1 : capacity);
deba@57
   208
	while (new_capacity <= id) {
deba@57
   209
	  new_capacity <<= 1;
deba@57
   210
	}
deba@57
   211
	Value* new_values = allocator.allocate(new_capacity);
deba@57
   212
	Item it;
deba@57
   213
	for (nf->first(it); it != INVALID; nf->next(it)) {
deba@57
   214
	  int jd = nf->id(it);;
deba@57
   215
	  if (id != jd) {
deba@57
   216
	    allocator.construct(&(new_values[jd]), values[jd]);
deba@57
   217
	    allocator.destroy(&(values[jd]));
deba@57
   218
	  }
deba@57
   219
	}
deba@57
   220
	if (capacity != 0) allocator.deallocate(values, capacity);
deba@57
   221
	values = new_values;
deba@57
   222
	capacity = new_capacity;
deba@57
   223
      }
deba@57
   224
      allocator.construct(&(values[id]), Value());
deba@57
   225
    }
deba@57
   226
deba@57
   227
    /// \brief Adds more new keys to the map.
deba@57
   228
    ///		
deba@57
   229
    /// It adds more new keys to the map. It called by the observer notifier
deba@57
   230
    /// and it overrides the add() member function of the observer base.     
deba@57
   231
    virtual void add(const std::vector<Key>& keys) {
deba@57
   232
      Notifier* nf = Parent::notifier();
deba@57
   233
      int max_id = -1;
deba@57
   234
      for (int i = 0; i < int(keys.size()); ++i) {
deba@57
   235
	int id = nf->id(keys[i]);
deba@57
   236
	if (id > max_id) {
deba@57
   237
	  max_id = id;
deba@57
   238
	}
deba@57
   239
      }
deba@57
   240
      if (max_id >= capacity) {
deba@57
   241
	int new_capacity = (capacity == 0 ? 1 : capacity);
deba@57
   242
	while (new_capacity <= max_id) {
deba@57
   243
	  new_capacity <<= 1;
deba@57
   244
	}
deba@57
   245
	Value* new_values = allocator.allocate(new_capacity);
deba@57
   246
	Item it;
deba@57
   247
	for (nf->first(it); it != INVALID; nf->next(it)) {
deba@57
   248
	  int id = nf->id(it);
deba@57
   249
	  bool found = false;
deba@57
   250
	  for (int i = 0; i < int(keys.size()); ++i) {
deba@57
   251
	    int jd = nf->id(keys[i]);
deba@57
   252
	    if (id == jd) {
deba@57
   253
	      found = true;
deba@57
   254
	      break;
deba@57
   255
	    }
deba@57
   256
	  }
deba@57
   257
	  if (found) continue;
deba@57
   258
	  allocator.construct(&(new_values[id]), values[id]);
deba@57
   259
	  allocator.destroy(&(values[id]));
deba@57
   260
	}
deba@57
   261
	if (capacity != 0) allocator.deallocate(values, capacity);
deba@57
   262
	values = new_values;
deba@57
   263
	capacity = new_capacity;
deba@57
   264
      }
deba@57
   265
      for (int i = 0; i < int(keys.size()); ++i) {
deba@57
   266
	int id = nf->id(keys[i]);
deba@57
   267
	allocator.construct(&(values[id]), Value());
deba@57
   268
      }
deba@57
   269
    }
deba@57
   270
		
deba@57
   271
    /// \brief Erase a key from the map.
deba@57
   272
    ///
deba@57
   273
    /// Erase a key from the map. It called by the observer notifier
deba@57
   274
    /// and it overrides the erase() member function of the observer base.     
deba@57
   275
    virtual void erase(const Key& key) {
deba@57
   276
      int id = Parent::notifier()->id(key);
deba@57
   277
      allocator.destroy(&(values[id]));
deba@57
   278
    }
deba@57
   279
deba@57
   280
    /// \brief Erase more keys from the map.
deba@57
   281
    ///
deba@57
   282
    /// Erase more keys from the map. It called by the observer notifier
deba@57
   283
    /// and it overrides the erase() member function of the observer base.     
deba@57
   284
    virtual void erase(const std::vector<Key>& keys) {
deba@57
   285
      for (int i = 0; i < int(keys.size()); ++i) {
deba@57
   286
	int id = Parent::notifier()->id(keys[i]);
deba@57
   287
	allocator.destroy(&(values[id]));
deba@57
   288
      }
deba@57
   289
    }
deba@57
   290
deba@57
   291
    /// \brief Buildes the map.
deba@57
   292
    ///	
deba@57
   293
    /// It buildes the map. It called by the observer notifier
deba@57
   294
    /// and it overrides the build() member function of the observer base. 
deba@57
   295
    virtual void build() {
deba@57
   296
      Notifier* nf = Parent::notifier();
deba@57
   297
      allocate_memory();
deba@57
   298
      Item it;
deba@57
   299
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@57
   300
	int id = nf->id(it);;
deba@57
   301
	allocator.construct(&(values[id]), Value());
deba@57
   302
      }								
deba@57
   303
    }
deba@57
   304
deba@57
   305
    /// \brief Clear the map.
deba@57
   306
    ///
deba@57
   307
    /// It erase all items from the map. It called by the observer notifier
deba@57
   308
    /// and it overrides the clear() member function of the observer base.     
deba@57
   309
    virtual void clear() {	
deba@57
   310
      Notifier* nf = Parent::notifier();
deba@57
   311
      if (capacity != 0) {
deba@57
   312
	Item it;
deba@57
   313
	for (nf->first(it); it != INVALID; nf->next(it)) {
deba@57
   314
	  int id = nf->id(it);
deba@57
   315
	  allocator.destroy(&(values[id]));
deba@57
   316
	}								
deba@57
   317
	allocator.deallocate(values, capacity);
deba@57
   318
	capacity = 0;
deba@57
   319
      }
deba@57
   320
    }
deba@57
   321
deba@57
   322
  private:
deba@57
   323
      
deba@57
   324
    void allocate_memory() {
deba@57
   325
      int max_id = Parent::notifier()->maxId();
deba@57
   326
      if (max_id == -1) {
deba@57
   327
	capacity = 0;
deba@57
   328
	values = 0;
deba@57
   329
	return;
deba@57
   330
      }
deba@57
   331
      capacity = 1;
deba@57
   332
      while (capacity <= max_id) {
deba@57
   333
	capacity <<= 1;
deba@57
   334
      }
deba@57
   335
      values = allocator.allocate(capacity);	
deba@57
   336
    }      
deba@57
   337
deba@57
   338
    int capacity;
deba@57
   339
    Value* values;
deba@57
   340
    Allocator allocator;
deba@57
   341
deba@57
   342
  };		
deba@57
   343
deba@57
   344
}
deba@57
   345
deba@57
   346
#endif