COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/array_map.h @ 987:87f7c54892df

Last change on this file since 987:87f7c54892df was 987:87f7c54892df, checked in by Alpar Juttner, 19 years ago

Naming changes:

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