COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/bits/array_map.h @ 2391:14a343be7a5a

Last change on this file since 2391:14a343be7a5a was 2391:14a343be7a5a, checked in by Alpar Juttner, 17 years ago

Happy New Year to all source files!

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