3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #ifndef LEMON_DEFAULT_MAP_H
20 #define LEMON_DEFAULT_MAP_H
23 #include <lemon/bits/array_map.h>
24 #include <lemon/bits/vector_map.h>
26 ///\ingroup graphmapfactory
28 ///\brief Graph maps that construct and destruct
29 ///their elements dynamically.
34 template <typename _Graph, typename _Item, typename _Value>
35 struct DefaultMapSelector {
36 typedef ArrayMap<_Graph, _Item, _Value> Map;
40 template <typename _Graph, typename _Item>
41 struct DefaultMapSelector<_Graph, _Item, bool> {
42 typedef VectorMap<_Graph, _Item, bool> Map;
46 template <typename _Graph, typename _Item>
47 struct DefaultMapSelector<_Graph, _Item, char> {
48 typedef VectorMap<_Graph, _Item, char> Map;
51 template <typename _Graph, typename _Item>
52 struct DefaultMapSelector<_Graph, _Item, signed char> {
53 typedef VectorMap<_Graph, _Item, signed char> Map;
56 template <typename _Graph, typename _Item>
57 struct DefaultMapSelector<_Graph, _Item, unsigned char> {
58 typedef VectorMap<_Graph, _Item, unsigned char> Map;
63 template <typename _Graph, typename _Item>
64 struct DefaultMapSelector<_Graph, _Item, signed int> {
65 typedef VectorMap<_Graph, _Item, signed int> Map;
68 template <typename _Graph, typename _Item>
69 struct DefaultMapSelector<_Graph, _Item, unsigned int> {
70 typedef VectorMap<_Graph, _Item, unsigned int> Map;
75 template <typename _Graph, typename _Item>
76 struct DefaultMapSelector<_Graph, _Item, signed short> {
77 typedef VectorMap<_Graph, _Item, signed short> Map;
80 template <typename _Graph, typename _Item>
81 struct DefaultMapSelector<_Graph, _Item, unsigned short> {
82 typedef VectorMap<_Graph, _Item, unsigned short> Map;
87 template <typename _Graph, typename _Item>
88 struct DefaultMapSelector<_Graph, _Item, signed long> {
89 typedef VectorMap<_Graph, _Item, signed long> Map;
92 template <typename _Graph, typename _Item>
93 struct DefaultMapSelector<_Graph, _Item, unsigned long> {
94 typedef VectorMap<_Graph, _Item, unsigned long> Map;
97 // \todo handling long long type
101 template <typename _Graph, typename _Item>
102 struct DefaultMapSelector<_Graph, _Item, float> {
103 typedef VectorMap<_Graph, _Item, float> Map;
108 template <typename _Graph, typename _Item>
109 struct DefaultMapSelector<_Graph, _Item, double> {
110 typedef VectorMap<_Graph, _Item, double> Map;
115 template <typename _Graph, typename _Item>
116 struct DefaultMapSelector<_Graph, _Item, long double> {
117 typedef VectorMap<_Graph, _Item, long double> Map;
122 template <typename _Graph, typename _Item, typename _Ptr>
123 struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
124 typedef VectorMap<_Graph, _Item, _Ptr*> Map;
133 : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
135 typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
136 typedef DefaultMap<_Graph, _Item, _Value> Map;
138 typedef typename Parent::Graph Graph;
139 typedef typename Parent::Value Value;
141 DefaultMap(const Graph& _g) : Parent(_g) {}
142 DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {}
148 template <typename _Base>
149 class MappableGraphExtender : public _Base {
152 typedef MappableGraphExtender<_Base> Graph;
153 typedef _Base Parent;
155 typedef typename Parent::Node Node;
156 typedef typename Parent::NodeIt NodeIt;
158 typedef typename Parent::Edge Edge;
159 typedef typename Parent::EdgeIt EdgeIt;
162 template <typename _Value>
164 : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {
166 typedef MappableGraphExtender Graph;
167 typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;
169 NodeMap(const Graph& _g)
171 NodeMap(const Graph& _g, const _Value& _v)
174 NodeMap& operator=(const NodeMap& cmap) {
175 return operator=<NodeMap>(cmap);
179 /// \brief Template assign operator.
181 /// The given parameter should be conform to the ReadMap
182 /// concecpt and could be indiced by the current item set of
183 /// the NodeMap. In this case the value for each item
184 /// is assigned by the value of the given ReadMap.
185 template <typename CMap>
186 NodeMap& operator=(const CMap& cmap) {
187 checkConcept<concept::ReadMap<Node, _Value>, CMap>();
188 const typename Parent::Graph* graph = Parent::getGraph();
190 for (graph->first(it); it != INVALID; graph->next(it)) {
191 Parent::set(it, cmap[it]);
198 template <typename _Value>
200 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
202 typedef MappableGraphExtender Graph;
203 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
205 EdgeMap(const Graph& _g)
207 EdgeMap(const Graph& _g, const _Value& _v)
210 EdgeMap& operator=(const EdgeMap& cmap) {
211 return operator=<EdgeMap>(cmap);
214 template <typename CMap>
215 EdgeMap& operator=(const CMap& cmap) {
216 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
217 const typename Parent::Graph* graph = Parent::getGraph();
219 for (graph->first(it); it != INVALID; graph->next(it)) {
220 Parent::set(it, cmap[it]);
229 template <typename _Base>
230 class MappableEdgeSetExtender : public _Base {
233 typedef MappableEdgeSetExtender<_Base> Graph;
234 typedef _Base Parent;
236 typedef typename Parent::Edge Edge;
237 typedef typename Parent::EdgeIt EdgeIt;
239 template <typename _Value>
241 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
243 typedef MappableEdgeSetExtender Graph;
244 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
246 EdgeMap(const Graph& _g)
248 EdgeMap(const Graph& _g, const _Value& _v)
251 EdgeMap& operator=(const EdgeMap& cmap) {
252 return operator=<EdgeMap>(cmap);
255 template <typename CMap>
256 EdgeMap& operator=(const CMap& cmap) {
257 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
258 const typename Parent::Graph* graph = Parent::getGraph();
260 for (graph->first(it); it != INVALID; graph->next(it)) {
261 Parent::set(it, cmap[it]);
270 template <typename _Base>
271 class MappableUGraphExtender :
272 public MappableGraphExtender<_Base> {
275 typedef MappableUGraphExtender Graph;
276 typedef MappableGraphExtender<_Base> Parent;
278 typedef typename Parent::UEdge UEdge;
280 template <typename _Value>
282 : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
284 typedef MappableUGraphExtender Graph;
285 typedef IterableMapExtender<
286 DefaultMap<Graph, UEdge, _Value> > Parent;
288 UEdgeMap(const Graph& _g)
290 UEdgeMap(const Graph& _g, const _Value& _v)
293 UEdgeMap& operator=(const UEdgeMap& cmap) {
294 return operator=<UEdgeMap>(cmap);
297 template <typename CMap>
298 UEdgeMap& operator=(const CMap& cmap) {
299 checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
300 const typename Parent::Graph* graph = Parent::getGraph();
302 for (graph->first(it); it != INVALID; graph->next(it)) {
303 Parent::set(it, cmap[it]);
313 template <typename _Base>
314 class MappableUEdgeSetExtender :
315 public MappableEdgeSetExtender<_Base> {
318 typedef MappableUEdgeSetExtender Graph;
319 typedef MappableEdgeSetExtender<_Base> Parent;
321 typedef typename Parent::UEdge UEdge;
323 template <typename _Value>
325 : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
327 typedef MappableUEdgeSetExtender Graph;
328 typedef IterableMapExtender<
329 DefaultMap<Graph, UEdge, _Value> > Parent;
331 UEdgeMap(const Graph& _g)
333 UEdgeMap(const Graph& _g, const _Value& _v)
336 UEdgeMap& operator=(const UEdgeMap& cmap) {
337 return operator=<UEdgeMap>(cmap);
340 template <typename CMap>
341 UEdgeMap& operator=(const CMap& cmap) {
342 checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
343 const typename Parent::Graph* graph = Parent::getGraph();
345 for (graph->first(it); it != INVALID; graph->next(it)) {
346 Parent::set(it, cmap[it]);
356 template <typename _Base>
357 class MappableBpUGraphExtender : public _Base {
360 typedef _Base Parent;
361 typedef MappableBpUGraphExtender Graph;
363 typedef typename Parent::Node Node;
364 typedef typename Parent::ANode ANode;
365 typedef typename Parent::BNode BNode;
366 typedef typename Parent::Edge Edge;
367 typedef typename Parent::UEdge UEdge;
369 template <typename _Value>
371 : public IterableMapExtender<DefaultMap<Graph, ANode, _Value> > {
373 typedef MappableBpUGraphExtender Graph;
374 typedef IterableMapExtender<DefaultMap<Graph, ANode, _Value> >
377 ANodeMap(const Graph& _g)
379 ANodeMap(const Graph& _g, const _Value& _v)
382 ANodeMap& operator=(const ANodeMap& cmap) {
383 return operator=<ANodeMap>(cmap);
387 /// \brief Template assign operator.
389 /// The given parameter should be conform to the ReadMap
390 /// concept and could be indiced by the current item set of
391 /// the ANodeMap. In this case the value for each item
392 /// is assigned by the value of the given ReadMap.
393 template <typename CMap>
394 ANodeMap& operator=(const CMap& cmap) {
395 checkConcept<concept::ReadMap<ANode, _Value>, CMap>();
396 const typename Parent::Graph* graph = Parent::getGraph();
398 for (graph->first(it); it != INVALID; graph->next(it)) {
399 Parent::set(it, cmap[it]);
406 template <typename _Value>
408 : public IterableMapExtender<DefaultMap<Graph, BNode, _Value> > {
410 typedef MappableBpUGraphExtender Graph;
411 typedef IterableMapExtender<DefaultMap<Graph, BNode, _Value> >
414 BNodeMap(const Graph& _g)
416 BNodeMap(const Graph& _g, const _Value& _v)
419 BNodeMap& operator=(const BNodeMap& cmap) {
420 return operator=<BNodeMap>(cmap);
424 /// \brief Template assign operator.
426 /// The given parameter should be conform to the ReadMap
427 /// concept and could be indiced by the current item set of
428 /// the BNodeMap. In this case the value for each item
429 /// is assigned by the value of the given ReadMap.
430 template <typename CMap>
431 BNodeMap& operator=(const CMap& cmap) {
432 checkConcept<concept::ReadMap<BNode, _Value>, CMap>();
433 const typename Parent::Graph* graph = Parent::getGraph();
435 for (graph->first(it); it != INVALID; graph->next(it)) {
436 Parent::set(it, cmap[it]);
445 template <typename _Value>
446 class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
448 typedef MappableBpUGraphExtender Graph;
451 typedef _Value Value;
453 /// The reference type of the map;
454 typedef typename BNodeMap<_Value>::Reference Reference;
455 /// The pointer type of the map;
456 typedef typename BNodeMap<_Value>::Pointer Pointer;
458 /// The const value type of the map.
459 typedef const Value ConstValue;
460 /// The const reference type of the map;
461 typedef typename BNodeMap<_Value>::ConstReference ConstReference;
462 /// The pointer type of the map;
463 typedef typename BNodeMap<_Value>::ConstPointer ConstPointer;
465 typedef True ReferenceMapTag;
467 NodeMapBase(const Graph& _g)
468 : graph(&_g), bNodeMap(_g), aNodeMap(_g) {
469 Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
471 NodeMapBase(const Graph& _g, const _Value& _v)
472 : graph(&_g), bNodeMap(_g, _v),
474 Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
477 virtual ~NodeMapBase() {
478 if (Parent::NodeNotifier::ObserverBase::attached()) {
479 Parent::NodeNotifier::ObserverBase::detach();
483 ConstReference operator[](const Key& node) const {
484 if (Parent::aNode(node)) {
485 return aNodeMap[node];
487 return bNodeMap[node];
491 Reference operator[](const Key& node) {
492 if (Parent::aNode(node)) {
493 return aNodeMap[node];
495 return bNodeMap[node];
499 void set(const Key& node, const Value& value) {
500 if (Parent::aNode(node)) {
501 aNodeMap.set(node, value);
503 bNodeMap.set(node, value);
509 virtual void add(const Node&) {}
510 virtual void add(const std::vector<Node>&) {}
511 virtual void erase(const Node&) {}
512 virtual void erase(const std::vector<Node>&) {}
513 virtual void clear() {}
514 virtual void build() {}
516 const Graph* getGraph() const { return graph; }
520 BNodeMap<_Value> bNodeMap;
521 ANodeMap<_Value> aNodeMap;
526 template <typename _Value>
528 : public IterableMapExtender<NodeMapBase<_Value> > {
530 typedef MappableBpUGraphExtender Graph;
531 typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
533 NodeMap(const Graph& _g)
535 NodeMap(const Graph& _g, const _Value& _v)
538 NodeMap& operator=(const NodeMap& cmap) {
539 return operator=<NodeMap>(cmap);
543 /// \brief Template assign operator.
545 /// The given parameter should be conform to the ReadMap
546 /// concept and could be indiced by the current item set of
547 /// the NodeMap. In this case the value for each item
548 /// is assigned by the value of the given ReadMap.
549 template <typename CMap>
550 NodeMap& operator=(const CMap& cmap) {
551 checkConcept<concept::ReadMap<Node, _Value>, CMap>();
552 const typename Parent::Graph* graph = Parent::getGraph();
554 for (graph->first(it); it != INVALID; graph->next(it)) {
555 Parent::set(it, cmap[it]);
564 template <typename _Value>
566 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
568 typedef MappableBpUGraphExtender Graph;
569 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
571 EdgeMap(const Graph& _g)
573 EdgeMap(const Graph& _g, const _Value& _v)
576 EdgeMap& operator=(const EdgeMap& cmap) {
577 return operator=<EdgeMap>(cmap);
580 template <typename CMap>
581 EdgeMap& operator=(const CMap& cmap) {
582 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
583 const typename Parent::Graph* graph = Parent::getGraph();
585 for (graph->first(it); it != INVALID; graph->next(it)) {
586 Parent::set(it, cmap[it]);
592 template <typename _Value>
594 : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
596 typedef MappableBpUGraphExtender Graph;
597 typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> >
600 UEdgeMap(const Graph& _g)
602 UEdgeMap(const Graph& _g, const _Value& _v)
605 UEdgeMap& operator=(const UEdgeMap& cmap) {
606 return operator=<UEdgeMap>(cmap);
609 template <typename CMap>
610 UEdgeMap& operator=(const CMap& cmap) {
611 checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
612 const typename Parent::Graph* graph = Parent::getGraph();
614 for (graph->first(it); it != INVALID; graph->next(it)) {
615 Parent::set(it, cmap[it]);