lemon/bits/array_map.h
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 16 Sep 2008 16:07:35 +0100
changeset 262 0181b7f12a2a
parent 107 31a2e6d28f61
child 263 be8a861d3bb7
permissions -rw-r--r--
Update NEWS file (ticket #121)
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
deba@57
   106
    /// \brief Constructor to copy a map of the same map type.
deba@57
   107
    ///
alpar@209
   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()) {
alpar@209
   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)) {
alpar@209
   119
        int id = nf->id(it);;
alpar@209
   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
alpar@209
   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
alpar@209
   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
alpar@209
   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.
alpar@209
   154
    ///
deba@57
   155
    /// The destructor of the map.
alpar@209
   156
    virtual ~ArrayMap() {
deba@57
   157
      if (attached()) {
alpar@209
   158
        clear();
alpar@209
   159
        detach();
deba@57
   160
      }
deba@57
   161
    }
alpar@209
   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
alpar@209
   171
    /// \brief The subscript operator.
deba@57
   172
    ///
deba@57
   173
    /// The subscript operator. The map can be subscripted by the
alpar@209
   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];
alpar@209
   178
    }
alpar@209
   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
alpar@209
   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.
alpar@209
   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.
alpar@209
   200
    ///
deba@57
   201
    /// It adds a new key to the map. It called by the observer notifier
alpar@209
   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) {
alpar@209
   207
        int new_capacity = (capacity == 0 ? 1 : capacity);
alpar@209
   208
        while (new_capacity <= id) {
alpar@209
   209
          new_capacity <<= 1;
alpar@209
   210
        }
alpar@209
   211
        Value* new_values = allocator.allocate(new_capacity);
alpar@209
   212
        Item it;
alpar@209
   213
        for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@209
   214
          int jd = nf->id(it);;
alpar@209
   215
          if (id != jd) {
alpar@209
   216
            allocator.construct(&(new_values[jd]), values[jd]);
alpar@209
   217
            allocator.destroy(&(values[jd]));
alpar@209
   218
          }
alpar@209
   219
        }
alpar@209
   220
        if (capacity != 0) allocator.deallocate(values, capacity);
alpar@209
   221
        values = new_values;
alpar@209
   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.
alpar@209
   228
    ///
deba@57
   229
    /// It adds more new keys to the map. It called by the observer notifier
alpar@209
   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) {
alpar@209
   235
        int id = nf->id(keys[i]);
alpar@209
   236
        if (id > max_id) {
alpar@209
   237
          max_id = id;
alpar@209
   238
        }
deba@57
   239
      }
deba@57
   240
      if (max_id >= capacity) {
alpar@209
   241
        int new_capacity = (capacity == 0 ? 1 : capacity);
alpar@209
   242
        while (new_capacity <= max_id) {
alpar@209
   243
          new_capacity <<= 1;
alpar@209
   244
        }
alpar@209
   245
        Value* new_values = allocator.allocate(new_capacity);
alpar@209
   246
        Item it;
alpar@209
   247
        for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@209
   248
          int id = nf->id(it);
alpar@209
   249
          bool found = false;
alpar@209
   250
          for (int i = 0; i < int(keys.size()); ++i) {
alpar@209
   251
            int jd = nf->id(keys[i]);
alpar@209
   252
            if (id == jd) {
alpar@209
   253
              found = true;
alpar@209
   254
              break;
alpar@209
   255
            }
alpar@209
   256
          }
alpar@209
   257
          if (found) continue;
alpar@209
   258
          allocator.construct(&(new_values[id]), values[id]);
alpar@209
   259
          allocator.destroy(&(values[id]));
alpar@209
   260
        }
alpar@209
   261
        if (capacity != 0) allocator.deallocate(values, capacity);
alpar@209
   262
        values = new_values;
alpar@209
   263
        capacity = new_capacity;
deba@57
   264
      }
deba@57
   265
      for (int i = 0; i < int(keys.size()); ++i) {
alpar@209
   266
        int id = nf->id(keys[i]);
alpar@209
   267
        allocator.construct(&(values[id]), Value());
deba@57
   268
      }
deba@57
   269
    }
alpar@209
   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
alpar@209
   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
alpar@209
   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) {
alpar@209
   286
        int id = Parent::notifier()->id(keys[i]);
alpar@209
   287
        allocator.destroy(&(values[id]));
deba@57
   288
      }
deba@57
   289
    }
deba@57
   290
deba@57
   291
    /// \brief Buildes the map.
alpar@209
   292
    ///
deba@57
   293
    /// It buildes the map. It called by the observer notifier
alpar@209
   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)) {
alpar@209
   300
        int id = nf->id(it);;
alpar@209
   301
        allocator.construct(&(values[id]), Value());
alpar@209
   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
alpar@209
   308
    /// and it overrides the clear() member function of the observer base.
alpar@209
   309
    virtual void clear() {
deba@57
   310
      Notifier* nf = Parent::notifier();
deba@57
   311
      if (capacity != 0) {
alpar@209
   312
        Item it;
alpar@209
   313
        for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@209
   314
          int id = nf->id(it);
alpar@209
   315
          allocator.destroy(&(values[id]));
alpar@209
   316
        }
alpar@209
   317
        allocator.deallocate(values, capacity);
alpar@209
   318
        capacity = 0;
deba@57
   319
      }
deba@57
   320
    }
deba@57
   321
deba@57
   322
  private:
alpar@209
   323
deba@57
   324
    void allocate_memory() {
deba@57
   325
      int max_id = Parent::notifier()->maxId();
deba@57
   326
      if (max_id == -1) {
alpar@209
   327
        capacity = 0;
alpar@209
   328
        values = 0;
alpar@209
   329
        return;
deba@57
   330
      }
deba@57
   331
      capacity = 1;
deba@57
   332
      while (capacity <= max_id) {
alpar@209
   333
        capacity <<= 1;
deba@57
   334
      }
alpar@209
   335
      values = allocator.allocate(capacity);
alpar@209
   336
    }
deba@57
   337
deba@57
   338
    int capacity;
deba@57
   339
    Value* values;
deba@57
   340
    Allocator allocator;
deba@57
   341
alpar@209
   342
  };
deba@57
   343
deba@57
   344
}
deba@57
   345
alpar@209
   346
#endif