Better doc.
2 * lemon/static_map.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2006 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_extender.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";
67 typedef std::vector<_Value> Container;
71 /// The graph type of the map.
73 /// The reference map tag.
74 typedef True ReferenceMapTag;
76 /// The key type of the map.
78 /// The value type of the map.
80 /// The const reference type of the map.
81 typedef typename Container::const_reference ConstReference;
82 /// The reference type of the map.
83 typedef typename Container::reference Reference;
85 typedef const Value ConstValue;
86 typedef Value* Pointer;
87 typedef const Value* ConstPointer;
89 typedef AlterationNotifier<_Item> Registry;
92 typedef StaticMap Map;
93 /// The base class of the map.
94 typedef typename Registry::ObserverBase Parent;
96 /// \brief Constructor to attach the new map into the registry.
98 /// It constructs a map and attachs it into the registry.
99 /// It adds all the items of the graph to the map.
100 StaticMap(const Graph& _g) : graph(&_g) {
101 attach(_g.getNotifier(_Item()));
105 /// \brief Constructor uses given value to initialize the map.
107 /// It constructs a map uses a given value to initialize the map.
108 /// It adds all the items of the graph to the map.
109 StaticMap(const Graph& _g, const Value& _v) : graph(&_g) {
110 attach(_g.getNotifier(_Item()));
111 unsigned size = graph->maxId(_Item()) + 1;
112 container.reserve(size);
113 container.resize(size, _v);
116 /// \brief Copy constructor
118 /// Copy constructor.
119 StaticMap(const StaticMap& _copy) : Parent(), graph(_copy.getGraph()) {
120 if (_copy.attached()) {
121 attach(*_copy.getRegistry());
122 container = _copy.container;
126 /// \brief Destrcutor
129 virtual ~StaticMap() {
139 StaticMap& operator=(const StaticMap&);
143 using Parent::attach;
144 using Parent::detach;
145 using Parent::attached;
147 const Graph* getGraph() const {
153 /// \brief The subcript operator.
155 /// The subscript operator. The map can be subscripted by the
156 /// actual items of the graph.
158 Reference operator[](const Key& key) {
159 return container[graph->id(key)];
162 /// \brief The const subcript operator.
164 /// The const subscript operator. The map can be subscripted by the
165 /// actual items of the graph.
167 ConstReference operator[](const Key& key) const {
168 return container[graph->id(key)];
172 /// \brief The setter function of the map.
174 /// It the same as operator[](key) = value expression.
176 void set(const Key& key, const Value& value) {
177 (*this)[key] = value;
182 /// \brief Adds a new key to the map.
184 /// It adds a new key to the map. It called by the observer registry
185 /// and it overrides the add() member function of the observer base.
187 void add(const Key&) {
188 throw UnsupportedOperation();
191 /// \brief Erases a key from the map.
193 /// Erase a key from the map. It called by the observer registry
194 /// and it overrides the erase() member function of the observer base.
195 void erase(const Key&) {
196 throw UnsupportedOperation();
201 /// It buildes the map. It called by the observer registry
202 /// and it overrides the build() member function of the observer base.
205 int size = graph->maxId(_Item()) + 1;
206 container.reserve(size);
207 container.resize(size);
212 /// It erase all items from the map. It called by the observer registry
213 /// and it overrides the clear() member function of the observer base.
226 template <typename _Base>
227 class StaticMappableGraphExtender : public _Base {
230 typedef StaticMappableGraphExtender<_Base> Graph;
231 typedef _Base Parent;
233 typedef typename Parent::Node Node;
234 typedef typename Parent::NodeIt NodeIt;
236 typedef typename Parent::Edge Edge;
237 typedef typename Parent::EdgeIt EdgeIt;
240 template <typename _Value>
242 : public IterableMapExtender<StaticMap<Graph, Node, _Value> > {
244 typedef StaticMappableGraphExtender Graph;
245 typedef IterableMapExtender<StaticMap<Graph, Node, _Value> > Parent;
247 NodeMap(const Graph& _g)
249 NodeMap(const Graph& _g, const _Value& _v)
252 NodeMap& operator=(const NodeMap& cmap) {
253 return operator=<NodeMap>(cmap);
257 /// \brief Template assign operator.
259 /// The given parameter should be conform to the ReadMap
260 /// concecpt and could be indiced by the current item set of
261 /// the NodeMap. In this case the value for each item
262 /// is assigned by the value of the given ReadMap.
263 template <typename CMap>
264 NodeMap& operator=(const CMap& cmap) {
265 checkConcept<concept::ReadMap<Node, _Value>, CMap>();
266 const typename Parent::Graph* graph = Parent::getGraph();
268 for (graph->first(it); it != INVALID; graph->next(it)) {
269 Parent::set(it, cmap[it]);
276 template <typename _Value>
278 : public IterableMapExtender<StaticMap<Graph, Edge, _Value> > {
280 typedef StaticMappableGraphExtender Graph;
281 typedef IterableMapExtender<StaticMap<Graph, Edge, _Value> > Parent;
283 EdgeMap(const Graph& _g)
285 EdgeMap(const Graph& _g, const _Value& _v)
288 EdgeMap& operator=(const EdgeMap& cmap) {
289 return operator=<EdgeMap>(cmap);
292 template <typename CMap>
293 EdgeMap& operator=(const CMap& cmap) {
294 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
295 const typename Parent::Graph* graph = Parent::getGraph();
297 for (graph->first(it); it != INVALID; graph->next(it)) {
298 Parent::set(it, cmap[it]);
307 template <typename _Base>
308 class StaticMappableUndirGraphExtender :
309 public StaticMappableGraphExtender<_Base> {
312 typedef StaticMappableUndirGraphExtender Graph;
313 typedef StaticMappableGraphExtender<_Base> Parent;
315 typedef typename Parent::UndirEdge UndirEdge;
317 template <typename _Value>
319 : public IterableMapExtender<StaticMap<Graph, UndirEdge, _Value> > {
321 typedef StaticMappableUndirGraphExtender Graph;
322 typedef IterableMapExtender<
323 StaticMap<Graph, UndirEdge, _Value> > Parent;
325 UndirEdgeMap(const Graph& _g)
327 UndirEdgeMap(const Graph& _g, const _Value& _v)
330 UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
331 return operator=<UndirEdgeMap>(cmap);
334 template <typename CMap>
335 UndirEdgeMap& operator=(const CMap& cmap) {
336 checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
337 const typename Parent::Graph* graph = Parent::getGraph();
339 for (graph->first(it); it != INVALID; graph->next(it)) {
340 Parent::set(it, cmap[it]);
348 template <typename _Base>
349 class StaticMappableUndirBipartiteGraphExtender : public _Base {
352 typedef _Base Parent;
353 typedef StaticMappableUndirBipartiteGraphExtender Graph;
355 typedef typename Parent::Node Node;
356 typedef typename Parent::UpperNode UpperNode;
357 typedef typename Parent::LowerNode LowerNode;
358 typedef typename Parent::Edge Edge;
359 typedef typename Parent::UndirEdge UndirEdge;
361 template <typename _Value>
363 : public IterableMapExtender<StaticMap<Graph, UpperNode, _Value> > {
365 typedef StaticMappableUndirBipartiteGraphExtender Graph;
366 typedef IterableMapExtender<StaticMap<Graph, UpperNode, _Value> >
369 UpperNodeMap(const Graph& _g)
371 UpperNodeMap(const Graph& _g, const _Value& _v)
374 UpperNodeMap& operator=(const UpperNodeMap& cmap) {
375 return operator=<UpperNodeMap>(cmap);
379 /// \brief Template assign operator.
381 /// The given parameter should be conform to the ReadMap
382 /// concept and could be indiced by the current item set of
383 /// the UpperNodeMap. In this case the value for each item
384 /// is assigned by the value of the given ReadMap.
385 template <typename CMap>
386 UpperNodeMap& operator=(const CMap& cmap) {
387 checkConcept<concept::ReadMap<UpperNode, _Value>, CMap>();
388 const typename Parent::Graph* graph = Parent::getGraph();
390 for (graph->first(it); it != INVALID; graph->next(it)) {
391 Parent::set(it, cmap[it]);
398 template <typename _Value>
400 : public IterableMapExtender<StaticMap<Graph, LowerNode, _Value> > {
402 typedef StaticMappableUndirBipartiteGraphExtender Graph;
403 typedef IterableMapExtender<StaticMap<Graph, LowerNode, _Value> >
406 LowerNodeMap(const Graph& _g)
408 LowerNodeMap(const Graph& _g, const _Value& _v)
411 LowerNodeMap& operator=(const LowerNodeMap& cmap) {
412 return operator=<LowerNodeMap>(cmap);
416 /// \brief Template assign operator.
418 /// The given parameter should be conform to the ReadMap
419 /// concept and could be indiced by the current item set of
420 /// the LowerNodeMap. In this case the value for each item
421 /// is assigned by the value of the given ReadMap.
422 template <typename CMap>
423 LowerNodeMap& operator=(const CMap& cmap) {
424 checkConcept<concept::ReadMap<LowerNode, _Value>, CMap>();
425 const typename Parent::Graph* graph = Parent::getGraph();
427 for (graph->first(it); it != INVALID; graph->next(it)) {
428 Parent::set(it, cmap[it]);
437 template <typename _Value>
438 class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
440 typedef StaticMappableUndirBipartiteGraphExtender Graph;
443 typedef _Value Value;
445 /// The reference type of the map;
446 typedef typename LowerNodeMap<_Value>::Reference Reference;
447 /// The pointer type of the map;
448 typedef typename LowerNodeMap<_Value>::Pointer Pointer;
450 /// The const value type of the map.
451 typedef const Value ConstValue;
452 /// The const reference type of the map;
453 typedef typename LowerNodeMap<_Value>::ConstReference ConstReference;
454 /// The pointer type of the map;
455 typedef typename LowerNodeMap<_Value>::ConstPointer ConstPointer;
457 typedef True ReferenceMapTag;
459 NodeMapBase(const Graph& _g)
460 : graph(&_g), lowerMap(_g), upperMap(_g) {
461 Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
463 NodeMapBase(const Graph& _g, const _Value& _v)
464 : graph(&_g), lowerMap(_g, _v),
466 Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
469 virtual ~NodeMapBase() {
470 if (Parent::NodeNotifier::ObserverBase::attached()) {
471 Parent::NodeNotifier::ObserverBase::detach();
475 ConstReference operator[](const Key& node) const {
476 if (Parent::upper(node)) {
477 return upperMap[node];
479 return lowerMap[node];
483 Reference operator[](const Key& node) {
484 if (Parent::upper(node)) {
485 return upperMap[node];
487 return lowerMap[node];
491 void set(const Key& node, const Value& value) {
492 if (Parent::upper(node)) {
493 upperMap.set(node, value);
495 lowerMap.set(node, value);
501 virtual void add(const Node&) {}
502 virtual void erase(const Node&) {}
503 virtual void clear() {}
504 virtual void build() {}
506 const Graph* getGraph() const { return graph; }
510 LowerNodeMap<_Value> lowerMap;
511 UpperNodeMap<_Value> upperMap;
516 template <typename _Value>
518 : public IterableMapExtender<NodeMapBase<_Value> > {
520 typedef StaticMappableUndirBipartiteGraphExtender Graph;
521 typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
523 NodeMap(const Graph& _g)
525 NodeMap(const Graph& _g, const _Value& _v)
528 NodeMap& operator=(const NodeMap& cmap) {
529 return operator=<NodeMap>(cmap);
533 /// \brief Template assign operator.
535 /// The given parameter should be conform to the ReadMap
536 /// concept and could be indiced by the current item set of
537 /// the NodeMap. In this case the value for each item
538 /// is assigned by the value of the given ReadMap.
539 template <typename CMap>
540 NodeMap& operator=(const CMap& cmap) {
541 checkConcept<concept::ReadMap<Node, _Value>, CMap>();
542 const typename Parent::Graph* graph = Parent::getGraph();
544 for (graph->first(it); it != INVALID; graph->next(it)) {
545 Parent::set(it, cmap[it]);
554 template <typename _Value>
556 : public IterableMapExtender<StaticMap<Graph, Edge, _Value> > {
558 typedef StaticMappableUndirBipartiteGraphExtender Graph;
559 typedef IterableMapExtender<StaticMap<Graph, Edge, _Value> > Parent;
561 EdgeMap(const Graph& _g)
563 EdgeMap(const Graph& _g, const _Value& _v)
566 EdgeMap& operator=(const EdgeMap& cmap) {
567 return operator=<EdgeMap>(cmap);
570 template <typename CMap>
571 EdgeMap& operator=(const CMap& cmap) {
572 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
573 const typename Parent::Graph* graph = Parent::getGraph();
575 for (graph->first(it); it != INVALID; graph->next(it)) {
576 Parent::set(it, cmap[it]);
582 template <typename _Value>
584 : public IterableMapExtender<StaticMap<Graph, UndirEdge, _Value> > {
586 typedef StaticMappableUndirBipartiteGraphExtender Graph;
587 typedef IterableMapExtender<StaticMap<Graph, UndirEdge, _Value> >
590 UndirEdgeMap(const Graph& _g)
592 UndirEdgeMap(const Graph& _g, const _Value& _v)
595 UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
596 return operator=<UndirEdgeMap>(cmap);
599 template <typename CMap>
600 UndirEdgeMap& operator=(const CMap& cmap) {
601 checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
602 const typename Parent::Graph* graph = Parent::getGraph();
604 for (graph->first(it); it != INVALID; graph->next(it)) {
605 Parent::set(it, cmap[it]);