1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #ifndef LEMON_BITS_VECTOR_MAP_H
20 #define LEMON_BITS_VECTOR_MAP_H
25 #include <lemon/bits/traits.h>
26 #include <lemon/bits/utility.h>
28 #include <lemon/bits/alteration_notifier.h>
30 #include <lemon/concept_check.h>
31 #include <lemon/concepts/maps.h>
36 ///\brief Vector based graph maps.
39 /// \ingroup graphbits
41 /// \brief Graph map based on the std::vector storage.
43 /// The VectorMap template class is graph map structure what
44 /// automatically updates the map when a key is added to or erased from
45 /// the map. This map type uses the std::vector to store the values.
47 /// \tparam _Notifier The AlterationNotifier that will notify this map.
48 /// \tparam _Item The item type of the graph items.
49 /// \tparam _Value The value type of the map.
50 /// \todo Fix the doc: there is _Graph parameter instead of _Notifier.
51 template <typename _Graph, typename _Item, typename _Value>
53 : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
56 /// The container type of the map.
57 typedef std::vector<_Value> Container;
61 /// The graph type of the map.
63 /// The item type of the map.
65 /// The reference map tag.
66 typedef True ReferenceMapTag;
68 /// The key type of the map.
70 /// The value type of the map.
73 /// The notifier type.
74 typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
77 typedef VectorMap Map;
78 /// The base class of the map.
79 typedef typename Notifier::ObserverBase Parent;
81 /// The reference type of the map;
82 typedef typename Container::reference Reference;
83 /// The const reference type of the map;
84 typedef typename Container::const_reference ConstReference;
87 /// \brief Constructor to attach the new map into the notifier.
89 /// It constructs a map and attachs it into the notifier.
90 /// It adds all the items of the graph to the map.
91 VectorMap(const Graph& graph) {
92 Parent::attach(graph.notifier(Item()));
93 container.resize(Parent::notifier()->maxId() + 1);
96 /// \brief Constructor uses given value to initialize the map.
98 /// It constructs a map uses a given value to initialize the map.
99 /// It adds all the items of the graph to the map.
100 VectorMap(const Graph& graph, const Value& value) {
101 Parent::attach(graph.notifier(Item()));
102 container.resize(Parent::notifier()->maxId() + 1, value);
105 /// \brief Copy constructor
107 /// Copy constructor.
108 VectorMap(const VectorMap& _copy) : Parent() {
109 if (_copy.attached()) {
110 Parent::attach(*_copy.notifier());
111 container = _copy.container;
115 /// \brief Assign operator.
117 /// This operator assigns for each item in the map the
118 /// value mapped to the same item in the copied map.
119 /// The parameter map should be indiced with the same
120 /// itemset because this assign operator does not change
121 /// the container of the map.
122 VectorMap& operator=(const VectorMap& cmap) {
123 return operator=<VectorMap>(cmap);
127 /// \brief Template assign operator.
129 /// The given parameter should be conform to the ReadMap
130 /// concecpt and could be indiced by the current item set of
131 /// the NodeMap. In this case the value for each item
132 /// is assigned by the value of the given ReadMap.
133 template <typename CMap>
134 VectorMap& operator=(const CMap& cmap) {
135 checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
136 const typename Parent::Notifier* nf = Parent::notifier();
138 for (nf->first(it); it != INVALID; nf->next(it)) {
146 /// \brief The subcript operator.
148 /// The subscript operator. The map can be subscripted by the
149 /// actual items of the graph.
150 Reference operator[](const Key& key) {
151 return container[Parent::notifier()->id(key)];
154 /// \brief The const subcript operator.
156 /// The const subscript operator. The map can be subscripted by the
157 /// actual items of the graph.
158 ConstReference operator[](const Key& key) const {
159 return container[Parent::notifier()->id(key)];
163 /// \brief The setter function of the map.
165 /// It the same as operator[](key) = value expression.
166 void set(const Key& key, const Value& value) {
167 (*this)[key] = value;
172 /// \brief Adds a new key to the map.
174 /// It adds a new key to the map. It called by the observer notifier
175 /// and it overrides the add() member function of the observer base.
176 virtual void add(const Key& key) {
177 int id = Parent::notifier()->id(key);
178 if (id >= int(container.size())) {
179 container.resize(id + 1);
183 /// \brief Adds more new keys to the map.
185 /// It adds more new keys to the map. It called by the observer notifier
186 /// and it overrides the add() member function of the observer base.
187 virtual void add(const std::vector<Key>& keys) {
188 int max = container.size() - 1;
189 for (int i = 0; i < int(keys.size()); ++i) {
190 int id = Parent::notifier()->id(keys[i]);
195 container.resize(max + 1);
198 /// \brief Erase a key from the map.
200 /// Erase a key from the map. It called by the observer notifier
201 /// and it overrides the erase() member function of the observer base.
202 virtual void erase(const Key& key) {
203 container[Parent::notifier()->id(key)] = Value();
206 /// \brief Erase more keys from the map.
208 /// Erase more keys from the map. It called by the observer notifier
209 /// and it overrides the erase() member function of the observer base.
210 virtual void erase(const std::vector<Key>& keys) {
211 for (int i = 0; i < int(keys.size()); ++i) {
212 container[Parent::notifier()->id(keys[i])] = Value();
216 /// \brief Buildes the map.
218 /// It buildes the map. It called by the observer notifier
219 /// and it overrides the build() member function of the observer base.
220 virtual void build() {
221 int size = Parent::notifier()->maxId() + 1;
222 container.reserve(size);
223 container.resize(size);
226 /// \brief Clear the map.
228 /// It erase all items from the map. It called by the observer notifier
229 /// and it overrides the clear() member function of the observer base.
230 virtual void clear() {