COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/array_map.h @ 1020:f42cb3146ed4

Last change on this file since 1020:f42cb3146ed4 was 988:aa19ca32d9b0, checked in by Alpar Juttner, 19 years ago

Revome an obsolete #include line.

File size: 10.1 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/lemon/array_map.h - Part of LEMON, a generic C++ optimization library
[906]3 *
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
[921]17#ifndef LEMON_ARRAY_MAP_H
18#define LEMON_ARRAY_MAP_H
[822]19
20#include <memory>
21
22///\ingroup graphmaps
23///\file
24///\brief Graph maps that construates and destruates
25///their elements dynamically.
26
[921]27namespace lemon {
[822]28
29
30  /// \addtogroup graphmaps
31  /// @{
32       
33  /** The ArrayMap template class is graph map structure what
34   *  automatically updates the map when a key is added to or erased from
35   *  the map. This map factory uses the allocators to implement
36   *  the container functionality.
37   *
38   *  The template parameter is the MapRegistry that the maps
[987]39   *  will belong to and the Value.
[822]40   */
41
[946]42  template <typename _Graph,
43            typename _Item,
44            typename _ItemIt,
45            typename _Value>
46  class ArrayMap : public AlterationObserverRegistry<_Item>::ObserverBase {
[897]47
[822]48  public:
49               
50    /// The graph type of the maps.
[946]51    typedef _Graph Graph;
[822]52    /// The key type of the maps.
[987]53    typedef _Item Key;
[946]54
55    typedef AlterationObserverRegistry<_Item> Registry;
56
57  private:
[822]58    /// The iterator to iterate on the keys.
[946]59    typedef _ItemIt KeyIt;
60
[822]61    /// The MapBase of the Map which imlements the core regisitry function.
[946]62    typedef typename Registry::ObserverBase Parent;
[822]63               
64   
65  public:
66
67    /// The value type of the map.
[987]68    typedef _Value Value;
[822]69    /// The reference type of the map;
[987]70    typedef Value& Reference;
[822]71    /// The pointer type of the map;
[987]72    typedef Value* Pointer;
[822]73
74    /// The const value type of the map.
[987]75    typedef const Value ConstValue;
[822]76    /// The const reference type of the map;
[987]77    typedef const Value& ConstReference;
[822]78    /// The pointer type of the map;
[987]79    typedef const Value* ConstPointer;
[822]80
81
[946]82  private:
[822]83    typedef std::allocator<Value> Allocator;
84
[946]85
86  public:
87
[822]88    /** Graph and Registry initialized map constructor.
89     */
[980]90    ArrayMap(const Graph& _g) : graph(&_g) {
91      attach(_g.getObserverRegistry(_Item()));
[822]92      allocate_memory();
[946]93      for (KeyIt it(*graph); it != INVALID; ++it) {
[980]94        int id = graph->id(it);;
[822]95        allocator.construct(&(values[id]), Value());
96      }                                                         
97    }
98
[946]99    /// Constructor to use default value to initialize the map.
100
101    /// It constrates a map and initialize all of the the map.
102
[980]103    ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) {
104      attach(_g.getObserverRegistry(_Item()));
[822]105      allocate_memory();
[946]106      for (KeyIt it(*graph); it != INVALID; ++it) {
[980]107        int id = graph->id(it);;
[946]108        allocator.construct(&(values[id]), _v);
[822]109      }                                                         
110    }
111
112    /** Constructor to copy a map of the same map type.
113     */
[946]114    ArrayMap(const ArrayMap& copy) {
115      if (copy.attached()) {
116        attach(*copy.getRegistry());
117      }
[822]118      capacity = copy.capacity;
119      if (capacity == 0) return;
120      values = allocator.allocate(capacity);
[946]121      for (KeyIt it(*graph); it != INVALID; ++it) {
[980]122        int id = graph->id(it);;
[891]123        allocator.construct(&(values[id]), copy.values[id]);
[822]124      }
125    }
126
[978]127    using Parent::attach;
128    using Parent::detach;
129    using Parent::attached;
130
[822]131    /** Assign operator to copy a map of the same map type.
132     */
133    ArrayMap& operator=(const ArrayMap& copy) {
134      if (&copy == this) return *this;
[897]135     
[946]136      if (graph != copy.graph) {
137        if (attached()) {
138          clear();
139          detach();
[897]140        }
[946]141        if (copy.attached()) {
142          attach(*copy.getRegistry());
143        }
[897]144        capacity = copy.capacity;
145        if (capacity == 0) return *this;
146        values = allocator.allocate(capacity);     
[822]147      }
[891]148
[946]149      for (KeyIt it(*graph); it != INVALID; ++it) {
[980]150        int id = graph->id(it);;
[822]151        allocator.construct(&(values[id]), copy.values[id]);
152      }
[891]153
[822]154      return *this;
155    }
156
157    /** The destructor of the map.
158     */
[946]159    virtual ~ArrayMap() {     
160      if (attached()) {
161        clear();
162        detach();
[822]163      }
164    }
165       
166       
167    /**
168     * The subscript operator. The map can be subscripted by the
169     * actual keys of the graph.
170     */
[987]171    Reference operator[](const Key& key) {
[980]172      int id = graph->id(key);
[822]173      return values[id];
174    }
175               
176    /**
177     * The const subscript operator. The map can be subscripted by the
178     * actual keys of the graph.
179     */
[987]180    ConstReference operator[](const Key& key) const {
[980]181      int id = graph->id(key);
[822]182      return values[id];
183    }
184       
185    /** Setter function of the map. Equivalent with map[key] = val.
186     *  This is a compatibility feature with the not dereferable maps.
187     */
[987]188    void set(const Key& key, const Value& val) {
[946]189      (*this)[key] = val;
[822]190    }
191               
192    /** Add a new key to the map. It called by the map registry.
193     */
[987]194    void add(const Key& key) {
[980]195      int id = graph->id(key);
[822]196      if (id >= capacity) {
197        int new_capacity = (capacity == 0 ? 1 : capacity);
198        while (new_capacity <= id) {
199          new_capacity <<= 1;
200        }
[946]201        Value* new_values = allocator.allocate(new_capacity);
202        for (KeyIt it(*graph); it != INVALID; ++it) {
[980]203          int jd = graph->id(it);;
[822]204          if (id != jd) {
205            allocator.construct(&(new_values[jd]), values[jd]);
206            allocator.destroy(&(values[jd]));
207          }
208        }
209        if (capacity != 0) allocator.deallocate(values, capacity);
210        values = new_values;
211        capacity = new_capacity;
212      }
213      allocator.construct(&(values[id]), Value());
214    }
215               
216    /** Erase a key from the map. It called by the map registry.
217     */
[987]218    void erase(const Key& key) {
[980]219      int id = graph->id(key);
[822]220      allocator.destroy(&(values[id]));
221    }
222
[946]223    void build() {
224      allocate_memory();
225      for (KeyIt it(*graph); it != INVALID; ++it) {
[980]226        int id = graph->id(it);;
[946]227        allocator.construct(&(values[id]), Value());
228      }                                                         
229    }
230
[822]231    void clear() {     
232      if (capacity != 0) {
[946]233        for (KeyIt it(*graph); it != INVALID; ++it) {
[980]234          int id = graph->id(it);;
[946]235          allocator.destroy(&(values[id]));
236        }                                                               
[822]237        allocator.deallocate(values, capacity);
238        capacity = 0;
239      }
240    }
241
[946]242//     /// The stl compatible pair iterator of the map.
243//     typedef MapIterator<ArrayMap> Iterator;
244//     /// The stl compatible const pair iterator of the map.
245//     typedef MapConstIterator<ArrayMap> ConstIterator;
[822]246
[946]247//     /** Returns the begin iterator of the map.
248//      */
249//     Iterator begin() {
250//       return Iterator(*this, KeyIt(*MapBase::getGraph()));
251//     }
[822]252
[946]253//     /** Returns the end iterator of the map.
254//      */
255//     Iterator end() {
256//       return Iterator(*this, INVALID);
257//     }
[822]258
[946]259//     /** Returns the begin ConstIterator of the map.
260//      */
261//     ConstIterator begin() const {
262//       return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
263//     }
[822]264
[946]265//     /** Returns the end const_iterator of the map.
266//      */
267//     ConstIterator end() const {
268//       return ConstIterator(*this, INVALID);
269//     }
[822]270
[946]271//     /// The KeySet of the Map.
272//     typedef MapConstKeySet<ArrayMap> ConstKeySet;
[830]273
[946]274//     /// KeySet getter function.
275//     ConstKeySet keySet() const {
276//       return ConstKeySet(*this);
277//     }
[830]278
[946]279//     /// The ConstValueSet of the Map.
280//     typedef MapConstValueSet<ArrayMap> ConstValueSet;
[830]281
[946]282//     /// ConstValueSet getter function.
283//     ConstValueSet valueSet() const {
284//       return ConstValueSet(*this);
285//     }
[830]286
[946]287//     /// The ValueSet of the Map.
288//     typedef MapValueSet<ArrayMap> ValueSet;
[830]289
[946]290//     /// ValueSet getter function.
291//     ValueSet valueSet() {
292//       return ValueSet(*this);
293//     }
[830]294
[822]295  private:
296     
297    void allocate_memory() {
[980]298      int max_id = graph->maxId(_Item());
[822]299      if (max_id == -1) {
300        capacity = 0;
301        values = 0;
302        return;
303      }
304      capacity = 1;
305      while (capacity <= max_id) {
306        capacity <<= 1;
307      }
308      values = allocator.allocate(capacity);   
309    }     
310
[946]311    const Graph* graph;
[822]312    int capacity;
313    Value* values;
314    Allocator allocator;
[844]315
316  public:
[946]317//     // STL  compatibility typedefs.
318//     typedef Iterator iterator;
319//     typedef ConstIterator const_iterator;
[987]320//     typedef typename Iterator::PairValue value_type;
321//     typedef typename Iterator::Key key_type;
322//     typedef typename Iterator::Value data_type;
323//     typedef typename Iterator::PairReference reference;
324//     typedef typename Iterator::PairPointer pointer;
325//     typedef typename ConstIterator::PairReference const_reference;
326//     typedef typename ConstIterator::PairPointer const_pointer;
[946]327//     typedef int difference_type;             
[822]328  };           
329
[946]330  template <typename _Base>
331  class ArrayMappableGraphExtender : public _Base {
332  public:
333
334    typedef ArrayMappableGraphExtender<_Base> Graph;
335    typedef _Base Parent;
336
337    typedef typename Parent::Node Node;
338    typedef typename Parent::NodeIt NodeIt;
339    typedef typename Parent::NodeObserverRegistry NodeObserverRegistry;
340
341    typedef typename Parent::Edge Edge;
342    typedef typename Parent::EdgeIt EdgeIt;
343    typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
344
345   
346
347    template <typename _Value>
[980]348    class NodeMap : public ArrayMap<Graph, Node, NodeIt, _Value> {
[946]349    public:
350      typedef ArrayMappableGraphExtender<_Base> Graph;
351
352      typedef typename Graph::Node Node;
353      typedef typename Graph::NodeIt NodeIt;
354
[980]355      typedef ArrayMap<Graph, Node, NodeIt, _Value> Parent;
[946]356
[979]357      //typedef typename Parent::Graph Graph;
[946]358      typedef typename Parent::Value Value;
359
360      NodeMap(const Graph& g)
[980]361        : Parent(g) {}
[946]362      NodeMap(const Graph& g, const Value& v)
[980]363        : Parent(g, v) {}
[946]364
365    };
366
367    template <typename _Value>
[980]368    class EdgeMap : public ArrayMap<Graph, Edge, EdgeIt, _Value> {
[946]369    public:
370      typedef ArrayMappableGraphExtender<_Base> Graph;
371
372      typedef typename Graph::Edge Edge;
373      typedef typename Graph::EdgeIt EdgeIt;
374
[980]375      typedef ArrayMap<Graph, Edge, EdgeIt, _Value> Parent;
[946]376
[979]377      //typedef typename Parent::Graph Graph;
[946]378      typedef typename Parent::Value Value;
379
380      EdgeMap(const Graph& g)
[980]381        : Parent(g) {}
[946]382      EdgeMap(const Graph& g, const Value& v)
[980]383        : Parent(g, v) {}
[946]384
385    };
386   
387  };
388
[822]389/// @}
390
391}
392
[921]393#endif //LEMON_ARRAY_MAP_H
Note: See TracBrowser for help on using the repository browser.