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