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