[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 | |
---|
[1999] | 19 | #ifndef LEMON_BITS_VECTOR_MAP_H |
---|
| 20 | #define LEMON_BITS_VECTOR_MAP_H |
---|
[822] | 21 | |
---|
| 22 | #include <vector> |
---|
[946] | 23 | #include <algorithm> |
---|
[822] | 24 | |
---|
[1999] | 25 | #include <lemon/bits/traits.h> |
---|
[1993] | 26 | #include <lemon/bits/utility.h> |
---|
[1999] | 27 | |
---|
[1307] | 28 | #include <lemon/bits/alteration_notifier.h> |
---|
[822] | 29 | |
---|
[2031] | 30 | #include <lemon/concept_check.h> |
---|
| 31 | #include <lemon/concept/maps.h> |
---|
| 32 | |
---|
[1999] | 33 | ///\ingroup graphbits |
---|
[1669] | 34 | /// |
---|
[822] | 35 | ///\file |
---|
| 36 | ///\brief Vector based graph maps. |
---|
[921] | 37 | namespace lemon { |
---|
[1669] | 38 | |
---|
[1996] | 39 | /// \ingroup graphbits |
---|
[1669] | 40 | /// |
---|
| 41 | /// \brief Graph map based on the std::vector storage. |
---|
| 42 | /// |
---|
[946] | 43 | /// The VectorMap template class is graph map structure what |
---|
[1999] | 44 | /// automatically updates the map when a key is added to or erased from |
---|
[946] | 45 | /// the map. This map factory uses the allocators to implement |
---|
| 46 | /// the container functionality. This map factory |
---|
| 47 | /// uses the std::vector to implement the container function. |
---|
| 48 | /// |
---|
[1999] | 49 | /// \param Notifier The AlterationNotifier that will notify this map. |
---|
[1703] | 50 | /// \param Item The item type of the graph items. |
---|
[946] | 51 | /// \param Value The value type of the map. |
---|
| 52 | /// |
---|
[1999] | 53 | /// \author Balazs Dezso |
---|
| 54 | template <typename _Graph, typename _Item, typename _Value> |
---|
| 55 | class VectorMap |
---|
| 56 | : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
---|
[1719] | 57 | private: |
---|
| 58 | |
---|
| 59 | /// The container type of the map. |
---|
| 60 | typedef std::vector<_Value> Container; |
---|
| 61 | |
---|
[822] | 62 | public: |
---|
[1703] | 63 | |
---|
[946] | 64 | /// The graph type of the map. |
---|
| 65 | typedef _Graph Graph; |
---|
[1999] | 66 | /// The item type of the map. |
---|
| 67 | typedef _Item Item; |
---|
[1719] | 68 | /// The reference map tag. |
---|
| 69 | typedef True ReferenceMapTag; |
---|
| 70 | |
---|
[946] | 71 | /// The key type of the map. |
---|
[987] | 72 | typedef _Item Key; |
---|
[946] | 73 | /// The value type of the map. |
---|
| 74 | typedef _Value Value; |
---|
[1719] | 75 | |
---|
[1999] | 76 | /// The notifier type. |
---|
| 77 | typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
---|
[822] | 78 | |
---|
| 79 | /// The map type. |
---|
| 80 | typedef VectorMap Map; |
---|
[946] | 81 | /// The base class of the map. |
---|
[1999] | 82 | typedef typename Notifier::ObserverBase Parent; |
---|
[822] | 83 | |
---|
| 84 | /// The reference type of the map; |
---|
[987] | 85 | typedef typename Container::reference Reference; |
---|
[822] | 86 | /// The const reference type of the map; |
---|
[987] | 87 | typedef typename Container::const_reference ConstReference; |
---|
[822] | 88 | |
---|
[1267] | 89 | |
---|
[1999] | 90 | /// \brief Constructor to attach the new map into the notifier. |
---|
[1703] | 91 | /// |
---|
[1999] | 92 | /// It constructs a map and attachs it into the notifier. |
---|
[946] | 93 | /// It adds all the items of the graph to the map. |
---|
[1999] | 94 | VectorMap(const Graph& graph) { |
---|
| 95 | Parent::attach(graph.getNotifier(Item())); |
---|
| 96 | container.resize(Parent::getNotifier()->maxId() + 1); |
---|
[946] | 97 | } |
---|
[822] | 98 | |
---|
[1703] | 99 | /// \brief Constructor uses given value to initialize the map. |
---|
| 100 | /// |
---|
| 101 | /// It constructs a map uses a given value to initialize the map. |
---|
[946] | 102 | /// It adds all the items of the graph to the map. |
---|
[1999] | 103 | VectorMap(const Graph& graph, const Value& value) { |
---|
| 104 | Parent::attach(graph.getNotifier(Item())); |
---|
| 105 | container.resize(Parent::getNotifier()->maxId() + 1, value); |
---|
[946] | 106 | } |
---|
[822] | 107 | |
---|
[1703] | 108 | /// \brief Copy constructor |
---|
| 109 | /// |
---|
| 110 | /// Copy constructor. |
---|
[1999] | 111 | VectorMap(const VectorMap& _copy) : Parent() { |
---|
[980] | 112 | if (_copy.attached()) { |
---|
[1999] | 113 | Parent::attach(*_copy.getNotifier()); |
---|
[980] | 114 | container = _copy.container; |
---|
[822] | 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[2031] | 118 | /// \brief Assign operator. |
---|
| 119 | /// |
---|
| 120 | /// This operator assigns for each item in the map the |
---|
| 121 | /// value mapped to the same item in the copied map. |
---|
| 122 | /// The parameter map should be indiced with the same |
---|
| 123 | /// itemset because this assign operator does not change |
---|
| 124 | /// the container of the map. |
---|
| 125 | VectorMap& operator=(const VectorMap& cmap) { |
---|
| 126 | return operator=<VectorMap>(cmap); |
---|
| 127 | } |
---|
[1669] | 128 | |
---|
| 129 | |
---|
[2031] | 130 | /// \brief Template assign operator. |
---|
| 131 | /// |
---|
| 132 | /// The given parameter should be conform to the ReadMap |
---|
| 133 | /// concecpt and could be indiced by the current item set of |
---|
| 134 | /// the NodeMap. In this case the value for each item |
---|
| 135 | /// is assigned by the value of the given ReadMap. |
---|
| 136 | template <typename CMap> |
---|
| 137 | VectorMap& operator=(const CMap& cmap) { |
---|
| 138 | checkConcept<concept::ReadMap<Key, _Value>, CMap>(); |
---|
| 139 | const typename Parent::Notifier* notifier = Parent::getNotifier(); |
---|
| 140 | Item it; |
---|
| 141 | for (notifier->first(it); it != INVALID; notifier->next(it)) { |
---|
| 142 | set(it, cmap[it]); |
---|
| 143 | } |
---|
| 144 | return *this; |
---|
| 145 | } |
---|
| 146 | |
---|
[1669] | 147 | public: |
---|
| 148 | |
---|
[1703] | 149 | /// \brief The subcript operator. |
---|
| 150 | /// |
---|
[946] | 151 | /// The subscript operator. The map can be subscripted by the |
---|
[1703] | 152 | /// actual items of the graph. |
---|
[987] | 153 | Reference operator[](const Key& key) { |
---|
[1999] | 154 | return container[Parent::getNotifier()->id(key)]; |
---|
[822] | 155 | } |
---|
| 156 | |
---|
[1703] | 157 | /// \brief The const subcript operator. |
---|
| 158 | /// |
---|
[946] | 159 | /// The const subscript operator. The map can be subscripted by the |
---|
| 160 | /// actual items of the graph. |
---|
[987] | 161 | ConstReference operator[](const Key& key) const { |
---|
[1999] | 162 | return container[Parent::getNotifier()->id(key)]; |
---|
[822] | 163 | } |
---|
| 164 | |
---|
[937] | 165 | |
---|
[1703] | 166 | /// \brief The setter function of the map. |
---|
| 167 | /// |
---|
[946] | 168 | /// It the same as operator[](key) = value expression. |
---|
[987] | 169 | void set(const Key& key, const Value& value) { |
---|
[946] | 170 | (*this)[key] = value; |
---|
[822] | 171 | } |
---|
[946] | 172 | |
---|
[1669] | 173 | protected: |
---|
| 174 | |
---|
| 175 | /// \brief Adds a new key to the map. |
---|
| 176 | /// |
---|
[1999] | 177 | /// It adds a new key to the map. It called by the observer notifier |
---|
[1703] | 178 | /// and it overrides the add() member function of the observer base. |
---|
| 179 | virtual void add(const Key& key) { |
---|
[1999] | 180 | int id = Parent::getNotifier()->id(key); |
---|
[822] | 181 | if (id >= (int)container.size()) { |
---|
| 182 | container.resize(id + 1); |
---|
| 183 | } |
---|
| 184 | } |
---|
[937] | 185 | |
---|
[1832] | 186 | /// \brief Adds more new keys to the map. |
---|
| 187 | /// |
---|
[1999] | 188 | /// It adds more new keys to the map. It called by the observer notifier |
---|
[1832] | 189 | /// and it overrides the add() member function of the observer base. |
---|
| 190 | virtual void add(const std::vector<Key>& keys) { |
---|
[1999] | 191 | int max = container.size() - 1; |
---|
[1832] | 192 | for (int i = 0; i < (int)keys.size(); ++i) { |
---|
[1999] | 193 | int id = Parent::getNotifier()->id(keys[i]); |
---|
| 194 | if (id >= max) { |
---|
| 195 | max = id; |
---|
| 196 | } |
---|
[1832] | 197 | } |
---|
[1999] | 198 | container.resize(max + 1); |
---|
[1832] | 199 | } |
---|
| 200 | |
---|
[1703] | 201 | /// \brief Erase a key from the map. |
---|
| 202 | /// |
---|
[1999] | 203 | /// Erase a key from the map. It called by the observer notifier |
---|
[946] | 204 | /// and it overrides the erase() member function of the observer base. |
---|
[1999] | 205 | virtual void erase(const Key& key) { |
---|
| 206 | container[Parent::getNotifier()->id(key)] = Value(); |
---|
| 207 | } |
---|
[822] | 208 | |
---|
[1832] | 209 | /// \brief Erase more keys from the map. |
---|
| 210 | /// |
---|
[1999] | 211 | /// Erase more keys from the map. It called by the observer notifier |
---|
[1832] | 212 | /// and it overrides the erase() member function of the observer base. |
---|
[1999] | 213 | virtual void erase(const std::vector<Key>& keys) { |
---|
| 214 | for (int i = 0; i < (int)keys.size(); ++i) { |
---|
| 215 | container[Parent::getNotifier()->id(keys[i])] = Value(); |
---|
| 216 | } |
---|
| 217 | } |
---|
[1832] | 218 | |
---|
[1703] | 219 | /// \brief Buildes the map. |
---|
| 220 | /// |
---|
[1999] | 221 | /// It buildes the map. It called by the observer notifier |
---|
[946] | 222 | /// and it overrides the build() member function of the observer base. |
---|
[1703] | 223 | virtual void build() { |
---|
[1999] | 224 | int size = Parent::getNotifier()->maxId() + 1; |
---|
| 225 | container.reserve(size); |
---|
| 226 | container.resize(size); |
---|
[946] | 227 | } |
---|
[937] | 228 | |
---|
[1703] | 229 | /// \brief Clear the map. |
---|
| 230 | /// |
---|
[1999] | 231 | /// It erase all items from the map. It called by the observer notifier |
---|
[946] | 232 | /// and it overrides the clear() member function of the observer base. |
---|
[1703] | 233 | virtual void clear() { |
---|
[822] | 234 | container.clear(); |
---|
| 235 | } |
---|
[1267] | 236 | |
---|
[822] | 237 | private: |
---|
| 238 | |
---|
| 239 | Container container; |
---|
| 240 | |
---|
[946] | 241 | }; |
---|
| 242 | |
---|
[822] | 243 | } |
---|
| 244 | |
---|
| 245 | #endif |
---|