[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> |
---|
[1810] | 24 | #include <lemon/bits/map_extender.h> |
---|
[1307] | 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] | 34 | namespace 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. |
---|
[1703] | 47 | /// \param Item The item type of the graph items. |
---|
[946] | 48 | /// \param Value The value type of the map. |
---|
| 49 | /// |
---|
| 50 | /// \author Balazs Dezso |
---|
| 51 | |
---|
[1267] | 52 | template < |
---|
| 53 | typename _Graph, |
---|
| 54 | typename _Item, |
---|
| 55 | typename _Value |
---|
| 56 | > |
---|
[1039] | 57 | class VectorMap : public AlterationNotifier<_Item>::ObserverBase { |
---|
[1719] | 58 | private: |
---|
| 59 | |
---|
| 60 | /// The container type of the map. |
---|
| 61 | typedef std::vector<_Value> Container; |
---|
| 62 | |
---|
[822] | 63 | public: |
---|
[1703] | 64 | |
---|
[946] | 65 | /// The graph type of the map. |
---|
| 66 | typedef _Graph Graph; |
---|
[1719] | 67 | /// The reference map tag. |
---|
| 68 | typedef True ReferenceMapTag; |
---|
| 69 | |
---|
[946] | 70 | /// The key type of the map. |
---|
[987] | 71 | typedef _Item Key; |
---|
[946] | 72 | /// The value type of the map. |
---|
| 73 | typedef _Value Value; |
---|
[1719] | 74 | |
---|
| 75 | typedef AlterationNotifier<_Item> Registry; |
---|
[822] | 76 | |
---|
| 77 | /// The map type. |
---|
| 78 | typedef VectorMap Map; |
---|
[946] | 79 | /// The base class of the map. |
---|
| 80 | typedef typename Registry::ObserverBase Parent; |
---|
[822] | 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 | |
---|
[1703] | 95 | /// \brief Constructor to attach the new map into the registry. |
---|
| 96 | /// |
---|
| 97 | /// It constructs a map and attachs it into the registry. |
---|
[946] | 98 | /// It adds all the items of the graph to the map. |
---|
[980] | 99 | VectorMap(const Graph& _g) : graph(&_g) { |
---|
[1039] | 100 | attach(_g.getNotifier(_Item())); |
---|
[946] | 101 | build(); |
---|
| 102 | } |
---|
[822] | 103 | |
---|
[1703] | 104 | /// \brief Constructor uses given value to initialize the map. |
---|
| 105 | /// |
---|
| 106 | /// It constructs a map uses a given value to initialize the map. |
---|
[946] | 107 | /// It adds all the items of the graph to the map. |
---|
[980] | 108 | VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { |
---|
[1039] | 109 | attach(_g.getNotifier(_Item())); |
---|
[980] | 110 | container.resize(graph->maxId(_Item()) + 1, _v); |
---|
[946] | 111 | } |
---|
[822] | 112 | |
---|
[1703] | 113 | /// \brief Copy constructor |
---|
| 114 | /// |
---|
| 115 | /// Copy constructor. |
---|
[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 | |
---|
[1703] | 124 | /// \brief Destrcutor |
---|
| 125 | /// |
---|
| 126 | /// Destructor. |
---|
[946] | 127 | virtual ~VectorMap() { |
---|
| 128 | if (attached()) { |
---|
| 129 | detach(); |
---|
[822] | 130 | } |
---|
[946] | 131 | } |
---|
| 132 | |
---|
[1669] | 133 | |
---|
| 134 | private: |
---|
| 135 | |
---|
| 136 | VectorMap& operator=(const VectorMap&); |
---|
| 137 | |
---|
| 138 | protected: |
---|
| 139 | |
---|
| 140 | using Parent::attach; |
---|
| 141 | using Parent::detach; |
---|
| 142 | using Parent::attached; |
---|
| 143 | |
---|
[946] | 144 | const Graph* getGraph() const { |
---|
| 145 | return graph; |
---|
[822] | 146 | } |
---|
[937] | 147 | |
---|
[1669] | 148 | public: |
---|
| 149 | |
---|
[1703] | 150 | /// \brief The subcript operator. |
---|
| 151 | /// |
---|
[946] | 152 | /// The subscript operator. The map can be subscripted by the |
---|
[1703] | 153 | /// actual items of the graph. |
---|
[987] | 154 | Reference operator[](const Key& key) { |
---|
[980] | 155 | return container[graph->id(key)]; |
---|
[822] | 156 | } |
---|
| 157 | |
---|
[1703] | 158 | /// \brief The const subcript operator. |
---|
| 159 | /// |
---|
[946] | 160 | /// The const subscript operator. The map can be subscripted by the |
---|
| 161 | /// actual items of the graph. |
---|
[987] | 162 | ConstReference operator[](const Key& key) const { |
---|
[980] | 163 | return container[graph->id(key)]; |
---|
[822] | 164 | } |
---|
| 165 | |
---|
[937] | 166 | |
---|
[1703] | 167 | /// \brief The setter function of the map. |
---|
| 168 | /// |
---|
[946] | 169 | /// It the same as operator[](key) = value expression. |
---|
[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 |
---|
[1703] | 179 | /// and it overrides the add() member function of the observer base. |
---|
| 180 | virtual void add(const Key& key) { |
---|
[980] | 181 | int id = graph->id(key); |
---|
[822] | 182 | if (id >= (int)container.size()) { |
---|
| 183 | container.resize(id + 1); |
---|
| 184 | } |
---|
| 185 | } |
---|
[937] | 186 | |
---|
[1703] | 187 | /// \brief Erase a key from the map. |
---|
| 188 | /// |
---|
[946] | 189 | /// Erase a key from the map. It called by the observer registry |
---|
| 190 | /// and it overrides the erase() member function of the observer base. |
---|
[1703] | 191 | virtual void erase(const Key&) {} |
---|
[822] | 192 | |
---|
[1703] | 193 | /// \brief Buildes the map. |
---|
| 194 | /// |
---|
[946] | 195 | /// It buildes the map. It called by the observer registry |
---|
| 196 | /// and it overrides the build() member function of the observer base. |
---|
[1703] | 197 | virtual void build() { |
---|
[980] | 198 | container.resize(graph->maxId(_Item()) + 1); |
---|
[946] | 199 | } |
---|
[937] | 200 | |
---|
[1703] | 201 | /// \brief Clear the map. |
---|
| 202 | /// |
---|
[946] | 203 | /// It erase all items from the map. It called by the observer registry |
---|
| 204 | /// and it overrides the clear() member function of the observer base. |
---|
[1703] | 205 | virtual void clear() { |
---|
[822] | 206 | container.clear(); |
---|
| 207 | } |
---|
[1267] | 208 | |
---|
[822] | 209 | private: |
---|
| 210 | |
---|
| 211 | Container container; |
---|
[946] | 212 | const Graph *graph; |
---|
[822] | 213 | |
---|
[946] | 214 | }; |
---|
| 215 | |
---|
[822] | 216 | } |
---|
| 217 | |
---|
| 218 | #endif |
---|