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