COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/bits/vector_map.h @ 1993:2115143eceea

Last change on this file since 1993:2115143eceea was 1993:2115143eceea, checked in by Balazs Dezso, 14 years ago

utility, invalid and traits moved to bits

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