COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/bits/vector_map.h @ 1669:66ae78d29f1e

Last change on this file since 1669:66ae78d29f1e was 1669:66ae78d29f1e, checked in by Balazs Dezso, 19 years ago

Template assign operator for graph maps.

Some naming and coding conventions.

File size: 5.6 KB
RevLine 
[906]1/* -*- C++ -*-
[1435]2 * lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
[906]3 *
[1164]4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[906]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_VECTOR_MAP_H
18#define LEMON_VECTOR_MAP_H
[822]19
20#include <vector>
[946]21#include <algorithm>
[822]22
[1267]23#include <lemon/utility.h>
[1307]24#include <lemon/bits/map_iterator.h>
25#include <lemon/bits/alteration_notifier.h>
[1669]26#include <lemon/concept_check.h>
27#include <lemon/concept/maps.h>
[822]28
[1669]29/// \ingroup graphmapfactory
30///
[822]31///\file
32///\brief Vector based graph maps.
33
[921]34namespace lemon {
[1669]35
36  /// \ingroup graphmapfactory
37  ///
38  /// \brief Graph map based on the std::vector storage.
39  ///
[946]40  /// The VectorMap template class is graph map structure what
41  /// automatically updates the map when a key is added to or erased from
42  /// the map. This map factory uses the allocators to implement
43  /// the container functionality. This map factory
44  /// uses the std::vector to implement the container function.
45  ///
[1039]46  /// \param Registry The AlterationNotifier that will notify this map.
[946]47  /// \param IdMap The IdMap type of the graph items.
48  /// \param Value The value type of the map.
49  ///
50  /// \author Balazs Dezso
51       
52
[1267]53  template <
54    typename _Graph,
55    typename _Item,   
56    typename _Value
57    >
[1039]58  class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
[822]59  public:
60               
[946]61    /// The graph type of the map.
62    typedef _Graph Graph;
63    /// The key type of the map.
[987]64    typedef _Item Key;
[946]65    /// The id map type of the map.
[1039]66    typedef AlterationNotifier<_Item> Registry;
[946]67    /// The value type of the map.
68    typedef _Value Value;
[822]69
70    /// The map type.
71    typedef VectorMap Map;
[946]72    /// The base class of the map.
73    typedef typename Registry::ObserverBase Parent;
[822]74
75  private:
76               
77    /// The container type of the map.
78    typedef std::vector<Value> Container;       
79
80  public:
81
82    /// The reference type of the map;
[987]83    typedef typename Container::reference Reference;
[822]84    /// The pointer type of the map;
[987]85    typedef typename Container::pointer Pointer;
[822]86
87    /// The const value type of the map.
[987]88    typedef const Value ConstValue;
[822]89    /// The const reference type of the map;
[987]90    typedef typename Container::const_reference ConstReference;
[822]91    /// The pointer type of the map;
[987]92    typedef typename Container::const_pointer ConstPointer;
[822]93
[1267]94    typedef True FullTypeTag;
95
[946]96    /// Constructor to attach the new map into the registry.
[937]97
[946]98    /// It construates a map and attachs it into the registry.
99    /// It adds all the items of the graph to the map.
100     
[980]101    VectorMap(const Graph& _g) : graph(&_g) {
[1039]102      attach(_g.getNotifier(_Item()));
[946]103      build();
104    }
[822]105
[937]106    /// Constructor uses given value to initialize the map.
107
[946]108    /// It construates a map uses a given value to initialize the map.
109    /// It adds all the items of the graph to the map.
110     
[980]111    VectorMap(const Graph& _g, const Value& _v) : graph(&_g) {
[1039]112      attach(_g.getNotifier(_Item()));
[980]113      container.resize(graph->maxId(_Item()) + 1, _v);
[946]114    }
[822]115
[1374]116    VectorMap(const VectorMap& _copy)
117      : Parent(), graph(_copy.getGraph()) {
[980]118      if (_copy.attached()) {
119        attach(*_copy.getRegistry());
120        container = _copy.container;
[822]121      }
122    }
123
[946]124    virtual ~VectorMap() {
125      if (attached()) {
126        detach();
[822]127      }
[946]128    }
129
[1669]130
131  private:
132
133    VectorMap& operator=(const VectorMap&);
134
135  protected:
136
137    using Parent::attach;
138    using Parent::detach;
139    using Parent::attached;
140
[946]141    const Graph* getGraph() const {
142      return graph;
[822]143    }
[937]144
[1669]145  public:
146
[937]147    /// The subcript operator.
148
[946]149    /// The subscript operator. The map can be subscripted by the
150    /// actual items of the graph.
151     
[987]152    Reference operator[](const Key& key) {
[980]153      return container[graph->id(key)];
[822]154    }
155               
[937]156    /// The const subcript operator.
157
[946]158    /// The const subscript operator. The map can be subscripted by the
159    /// actual items of the graph.
160     
[987]161    ConstReference operator[](const Key& key) const {
[980]162      return container[graph->id(key)];
[822]163    }
164
[937]165
[946]166    /// The setter function of the map.
167
168    /// It the same as operator[](key) = value expression.
169    ///
[987]170    void set(const Key& key, const Value& value) {
[946]171      (*this)[key] = value;
[822]172    }
[946]173
[1669]174  protected:
175
176    /// \brief Adds a new key to the map.
177    ///         
[946]178    /// It adds a new key to the map. It called by the observer registry
179    /// and it overrides the add() member function of the observer base.
180     
[987]181    void add(const Key& key) {
[980]182      int id = graph->id(key);
[822]183      if (id >= (int)container.size()) {
184        container.resize(id + 1);
185      }
186    }
[937]187
188    /// Erases a key from the map.
[822]189               
[946]190    /// Erase a key from the map. It called by the observer registry
191    /// and it overrides the erase() member function of the observer base.     
[987]192    void erase(const Key&) {}
[822]193
[946]194    /// Buildes the map.
195               
196    /// It buildes the map. It called by the observer registry
197    /// and it overrides the build() member function of the observer base.
[937]198
[946]199    void build() {
[980]200      container.resize(graph->maxId(_Item()) + 1);
[946]201    }
[937]202
[946]203    /// Clear the map.
204
205    /// It erase all items from the map. It called by the observer registry
206    /// and it overrides the clear() member function of the observer base.     
[822]207    void clear() {
208      container.clear();
209    }
[1267]210   
[822]211  private:
212               
213    Container container;
[946]214    const Graph *graph;
[822]215
[946]216  };
217
[822]218}
219
220#endif
Note: See TracBrowser for help on using the repository browser.