2 * lemon/static_map.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
17 #ifndef LEMON_STATIC_MAP_H
18 #define LEMON_STATIC_MAP_H
23 #include <lemon/utility.h>
24 #include <lemon/bits/map_iterator.h>
25 #include <lemon/bits/alteration_notifier.h>
26 #include <lemon/error.h>
27 #include <lemon/concept_check.h>
28 #include <lemon/concept/maps.h>
30 /// \ingroup graphmaps
33 ///\brief Static sized graph maps.
37 /// \ingroup graphmaps
39 /// \brief Graph map with static sized storage.
41 /// The StaticMap template class is graph map structure what
42 /// does not update automatically the map when a key is added to or
43 /// erased from the map rather it throws an exception. This map factory
44 /// uses the allocators to implement the container functionality.
46 /// \param Registry The AlterationNotifier that will notify this map.
47 /// \param Item The item type of the graph items.
48 /// \param Value The value type of the map.
50 /// \author Balazs Dezso
53 template <typename _Graph, typename _Item, typename _Value>
54 class StaticMap : public AlterationNotifier<_Item>::ObserverBase {
57 /// \brief Exception class for unsupported exceptions.
58 class UnsupportedOperation : public lemon::LogicError {
60 virtual const char* exceptionName() const {
61 return "lemon::StaticMap::UnsupportedOperation";
65 typedef True AdaptibleTag;
67 /// The graph type of the map.
69 /// The key type of the map.
71 /// The id map type of the map.
72 typedef AlterationNotifier<_Item> Registry;
73 /// The value type of the map.
77 typedef StaticMap Map;
78 /// The base class of the map.
79 typedef typename Registry::ObserverBase Parent;
83 typedef std::vector<Value> Container;
87 /// \brief Constructor to attach the new map into the registry.
89 /// It constructs a map and attachs it into the registry.
90 /// It adds all the items of the graph to the map.
91 StaticMap(const Graph& _g) : graph(&_g) {
92 attach(_g.getNotifier(_Item()));
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 StaticMap(const Graph& _g, const Value& _v) : graph(&_g) {
101 attach(_g.getNotifier(_Item()));
102 unsigned size = graph->maxId(_Item()) + 1;
103 container.reserve(size);
104 container.resize(size, _v);
107 /// \brief Copy constructor
109 /// Copy constructor.
110 StaticMap(const StaticMap& _copy) : Parent(), graph(_copy.getGraph()) {
111 if (_copy.attached()) {
112 attach(*_copy.getRegistry());
113 container = _copy.container;
117 /// \brief Destrcutor
120 virtual ~StaticMap() {
130 StaticMap& operator=(const StaticMap&);
134 using Parent::attach;
135 using Parent::detach;
136 using Parent::attached;
138 const Graph* getGraph() const {
144 typedef typename Container::reference Reference;
145 typedef typename Container::pointer Pointer;
146 typedef const Value ConstValue;
147 typedef typename Container::const_reference ConstReference;
148 typedef typename Container::const_pointer ConstPointer;
150 /// \brief The subcript operator.
152 /// The subscript operator. The map can be subscripted by the
153 /// actual items of the graph.
155 Reference operator[](const Key& key) {
156 return container[graph->id(key)];
159 /// \brief The const subcript operator.
161 /// The const subscript operator. The map can be subscripted by the
162 /// actual items of the graph.
164 ConstReference operator[](const Key& key) const {
165 return container[graph->id(key)];
169 /// \brief The setter function of the map.
171 /// It the same as operator[](key) = value expression.
173 void set(const Key& key, const Value& value) {
174 (*this)[key] = value;
179 /// \brief Adds a new key to the map.
181 /// It adds a new key to the map. It called by the observer registry
182 /// and it overrides the add() member function of the observer base.
184 void add(const Key&) {
185 throw UnsupportedOperation();
188 /// \brief Erases a key from the map.
190 /// Erase a key from the map. It called by the observer registry
191 /// and it overrides the erase() member function of the observer base.
192 void erase(const Key&) {
193 throw UnsupportedOperation();
198 /// It buildes the map. It called by the observer registry
199 /// and it overrides the build() member function of the observer base.
202 int size = graph->maxId(_Item()) + 1;
203 container.reserve(size);
204 container.resize(size);
209 /// It erase all items from the map. It called by the observer registry
210 /// and it overrides the clear() member function of the observer base.
223 template <typename _Base>
224 class StaticMappableGraphExtender : public _Base {
227 typedef StaticMappableGraphExtender<_Base> Graph;
228 typedef _Base Parent;
230 typedef typename Parent::Node Node;
231 typedef typename Parent::NodeIt NodeIt;
233 typedef typename Parent::Edge Edge;
234 typedef typename Parent::EdgeIt EdgeIt;
237 template <typename _Value>
239 : public IterableMapExtender<StaticMap<Graph, Node, _Value> > {
241 typedef StaticMappableGraphExtender Graph;
242 typedef IterableMapExtender<StaticMap<Graph, Node, _Value> > Parent;
244 NodeMap(const Graph& _g)
246 NodeMap(const Graph& _g, const _Value& _v)
249 NodeMap& operator=(const NodeMap& cmap) {
250 return operator=<NodeMap>(cmap);
254 /// \brief Template assign operator.
256 /// The given parameter should be conform to the ReadMap
257 /// concecpt and could be indiced by the current item set of
258 /// the NodeMap. In this case the value for each item
259 /// is assigned by the value of the given ReadMap.
260 template <typename CMap>
261 NodeMap& operator=(const CMap& cmap) {
262 checkConcept<concept::ReadMap<Node, _Value>, CMap>();
263 const typename Parent::Graph* graph = Parent::getGraph();
265 for (graph->first(it); it != INVALID; graph->next(it)) {
266 Parent::set(it, cmap[it]);
273 template <typename _Value>
275 : public IterableMapExtender<StaticMap<Graph, Edge, _Value> > {
277 typedef StaticMappableGraphExtender Graph;
278 typedef IterableMapExtender<StaticMap<Graph, Edge, _Value> > Parent;
280 EdgeMap(const Graph& _g)
282 EdgeMap(const Graph& _g, const _Value& _v)
285 EdgeMap& operator=(const EdgeMap& cmap) {
286 return operator=<EdgeMap>(cmap);
289 template <typename CMap>
290 EdgeMap& operator=(const CMap& cmap) {
291 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
292 const typename Parent::Graph* graph = Parent::getGraph();
294 for (graph->first(it); it != INVALID; graph->next(it)) {
295 Parent::set(it, cmap[it]);
304 template <typename _Base>
305 class StaticMappableUndirGraphExtender :
306 public StaticMappableGraphExtender<_Base> {
309 typedef StaticMappableUndirGraphExtender Graph;
310 typedef StaticMappableGraphExtender<_Base> Parent;
312 typedef typename Parent::UndirEdge UndirEdge;
314 template <typename _Value>
316 : public IterableMapExtender<StaticMap<Graph, UndirEdge, _Value> > {
318 typedef StaticMappableUndirGraphExtender Graph;
319 typedef IterableMapExtender<
320 StaticMap<Graph, UndirEdge, _Value> > Parent;
322 UndirEdgeMap(const Graph& _g)
324 UndirEdgeMap(const Graph& _g, const _Value& _v)
327 UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
328 return operator=<UndirEdgeMap>(cmap);
331 template <typename CMap>
332 UndirEdgeMap& operator=(const CMap& cmap) {
333 checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
334 const typename Parent::Graph* graph = Parent::getGraph();
336 for (graph->first(it); it != INVALID; graph->next(it)) {
337 Parent::set(it, cmap[it]);