lemon/bits/array_map.h
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 21 Sep 2008 07:49:57 +0100
changeset 268 986d30f5c1c0
parent 209 765619b7cbb2
child 314 2cc60866a0c9
permissions -rw-r--r--
Improvements in named-param.dox
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
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
alpar@209
    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>
alpar@209
    47
  class ArrayMap
deba@57
    48
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
deba@57
    49
  public:
alpar@209
    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;
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
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)) {
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
alpar@209
    92
    /// \brief Constructor to use default value to initialize the map.
deba@57
    93
    ///
alpar@209
    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:
deba@57
   107
    /// \brief Constructor to copy a map of the same map type.
deba@57
   108
    ///
alpar@209
   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
deba@57
   125
    /// \brief Assign operator.
deba@57
   126
    ///
deba@57
   127
    /// This operator assigns for each item in the map the
alpar@209
   128
    /// value mapped to the same item in the copied map.
deba@57
   129
    /// The parameter map should be indiced with the same
deba@57
   130
    /// itemset because this assign operator does not change
alpar@209
   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
deba@57
   137
    /// \brief Template assign operator.
deba@57
   138
    ///
deba@57
   139
    /// The given parameter should be conform to the ReadMap
deba@57
   140
    /// concecpt and could be indiced by the current item set of
deba@57
   141
    /// the NodeMap. In this case the value for each item
alpar@209
   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:
deba@57
   155
    /// \brief The destructor of the map.
alpar@209
   156
    ///
deba@57
   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
alpar@209
   173
    /// \brief The subscript operator.
deba@57
   174
    ///
deba@57
   175
    /// The subscript operator. The map can be subscripted by the
alpar@209
   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
deba@57
   182
    /// \brief The const subscript operator.
deba@57
   183
    ///
deba@57
   184
    /// The const subscript operator. The map can be subscripted by the
alpar@209
   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
deba@57
   191
    /// \brief Setter function of the map.
alpar@209
   192
    ///
deba@57
   193
    /// Setter function of the map. Equivalent with map[key] = val.
deba@57
   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
deba@57
   201
    /// \brief Adds a new key to the map.
alpar@209
   202
    ///
deba@57
   203
    /// It adds a new key to the map. It called by the observer notifier
alpar@209
   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
deba@57
   229
    /// \brief Adds more new keys to the map.
alpar@209
   230
    ///
deba@57
   231
    /// It adds more new keys to the map. It called by the observer notifier
alpar@209
   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
deba@57
   273
    /// \brief Erase a key from the map.
deba@57
   274
    ///
deba@57
   275
    /// Erase a key from the map. It called by the observer notifier
alpar@209
   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
deba@57
   282
    /// \brief Erase more keys from the map.
deba@57
   283
    ///
deba@57
   284
    /// Erase more keys from the map. It called by the observer notifier
alpar@209
   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
deba@57
   293
    /// \brief Buildes the map.
alpar@209
   294
    ///
deba@57
   295
    /// It buildes the map. It called by the observer notifier
alpar@209
   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
deba@57
   307
    /// \brief Clear the map.
deba@57
   308
    ///
deba@57
   309
    /// It erase all items from the map. It called by the observer notifier
alpar@209
   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