2 * lemon/default_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_DEFAULT_MAP_H
18 #define LEMON_DEFAULT_MAP_H
21 #include <lemon/bits/array_map.h>
22 #include <lemon/bits/vector_map.h>
24 ///\ingroup graphmapfactory
26 ///\brief Graph maps that construct and destruct
27 ///their elements dynamically.
32 template <typename _Graph, typename _Item, typename _Value>
33 struct DefaultMapSelector {
34 typedef ArrayMap<_Graph, _Item, _Value> Map;
38 template <typename _Graph, typename _Item>
39 struct DefaultMapSelector<_Graph, _Item, bool> {
40 typedef VectorMap<_Graph, _Item, bool> Map;
44 template <typename _Graph, typename _Item>
45 struct DefaultMapSelector<_Graph, _Item, char> {
46 typedef VectorMap<_Graph, _Item, char> Map;
49 template <typename _Graph, typename _Item>
50 struct DefaultMapSelector<_Graph, _Item, signed char> {
51 typedef VectorMap<_Graph, _Item, signed char> Map;
54 template <typename _Graph, typename _Item>
55 struct DefaultMapSelector<_Graph, _Item, unsigned char> {
56 typedef VectorMap<_Graph, _Item, unsigned char> Map;
61 template <typename _Graph, typename _Item>
62 struct DefaultMapSelector<_Graph, _Item, signed int> {
63 typedef VectorMap<_Graph, _Item, signed int> Map;
66 template <typename _Graph, typename _Item>
67 struct DefaultMapSelector<_Graph, _Item, unsigned int> {
68 typedef VectorMap<_Graph, _Item, unsigned int> Map;
73 template <typename _Graph, typename _Item>
74 struct DefaultMapSelector<_Graph, _Item, signed short> {
75 typedef VectorMap<_Graph, _Item, signed short> Map;
78 template <typename _Graph, typename _Item>
79 struct DefaultMapSelector<_Graph, _Item, unsigned short> {
80 typedef VectorMap<_Graph, _Item, unsigned short> Map;
85 template <typename _Graph, typename _Item>
86 struct DefaultMapSelector<_Graph, _Item, signed long> {
87 typedef VectorMap<_Graph, _Item, signed long> Map;
90 template <typename _Graph, typename _Item>
91 struct DefaultMapSelector<_Graph, _Item, unsigned long> {
92 typedef VectorMap<_Graph, _Item, unsigned long> Map;
95 // \todo handling long long type
99 template <typename _Graph, typename _Item>
100 struct DefaultMapSelector<_Graph, _Item, float> {
101 typedef VectorMap<_Graph, _Item, float> Map;
106 template <typename _Graph, typename _Item>
107 struct DefaultMapSelector<_Graph, _Item, double> {
108 typedef VectorMap<_Graph, _Item, double> Map;
113 template <typename _Graph, typename _Item>
114 struct DefaultMapSelector<_Graph, _Item, long double> {
115 typedef VectorMap<_Graph, _Item, long double> Map;
120 template <typename _Graph, typename _Item, typename _Ptr>
121 struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
122 typedef VectorMap<_Graph, _Item, _Ptr*> Map;
131 : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
133 typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
134 typedef DefaultMap<_Graph, _Item, _Value> Map;
136 typedef typename Parent::Graph Graph;
137 typedef typename Parent::Value Value;
139 DefaultMap(const Graph& _g) : Parent(_g) {}
140 DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {}
146 template <typename _Base>
147 class MappableGraphExtender : public _Base {
150 typedef MappableGraphExtender<_Base> Graph;
151 typedef _Base Parent;
153 typedef typename Parent::Node Node;
154 typedef typename Parent::NodeIt NodeIt;
156 typedef typename Parent::Edge Edge;
157 typedef typename Parent::EdgeIt EdgeIt;
160 template <typename _Value>
162 : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {
164 typedef MappableGraphExtender Graph;
165 typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;
167 NodeMap(const Graph& _g)
169 NodeMap(const Graph& _g, const _Value& _v)
172 NodeMap& operator=(const NodeMap& cmap) {
173 return operator=<NodeMap>(cmap);
177 /// \brief Template assign operator.
179 /// The given parameter should be conform to the ReadMap
180 /// concecpt and could be indiced by the current item set of
181 /// the NodeMap. In this case the value for each item
182 /// is assigned by the value of the given ReadMap.
183 template <typename CMap>
184 NodeMap& operator=(const CMap& cmap) {
185 checkConcept<concept::ReadMap<Node, _Value>, CMap>();
186 const typename Parent::Graph* graph = Parent::getGraph();
188 for (graph->first(it); it != INVALID; graph->next(it)) {
189 Parent::set(it, cmap[it]);
196 template <typename _Value>
198 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
200 typedef MappableGraphExtender Graph;
201 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
203 EdgeMap(const Graph& _g)
205 EdgeMap(const Graph& _g, const _Value& _v)
208 EdgeMap& operator=(const EdgeMap& cmap) {
209 return operator=<EdgeMap>(cmap);
212 template <typename CMap>
213 EdgeMap& operator=(const CMap& cmap) {
214 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
215 const typename Parent::Graph* graph = Parent::getGraph();
217 for (graph->first(it); it != INVALID; graph->next(it)) {
218 Parent::set(it, cmap[it]);
227 template <typename _Base>
228 class MappableEdgeSetExtender : public _Base {
231 typedef MappableEdgeSetExtender<_Base> Graph;
232 typedef _Base Parent;
234 typedef typename Parent::Edge Edge;
235 typedef typename Parent::EdgeIt EdgeIt;
237 template <typename _Value>
239 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
241 typedef MappableEdgeSetExtender Graph;
242 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
244 EdgeMap(const Graph& _g)
246 EdgeMap(const Graph& _g, const _Value& _v)
249 EdgeMap& operator=(const EdgeMap& cmap) {
250 return operator=<EdgeMap>(cmap);
253 template <typename CMap>
254 EdgeMap& operator=(const CMap& cmap) {
255 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
256 const typename Parent::Graph* graph = Parent::getGraph();
258 for (graph->first(it); it != INVALID; graph->next(it)) {
259 Parent::set(it, cmap[it]);
268 template <typename _Base>
269 class MappableUndirGraphExtender :
270 public MappableGraphExtender<_Base> {
273 typedef MappableUndirGraphExtender Graph;
274 typedef MappableGraphExtender<_Base> Parent;
276 typedef typename Parent::UndirEdge UndirEdge;
278 template <typename _Value>
280 : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > {
282 typedef MappableUndirGraphExtender Graph;
283 typedef IterableMapExtender<
284 DefaultMap<Graph, UndirEdge, _Value> > Parent;
286 UndirEdgeMap(const Graph& _g)
288 UndirEdgeMap(const Graph& _g, const _Value& _v)
291 UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
292 return operator=<UndirEdgeMap>(cmap);
295 template <typename CMap>
296 UndirEdgeMap& operator=(const CMap& cmap) {
297 checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
298 const typename Parent::Graph* graph = Parent::getGraph();
300 for (graph->first(it); it != INVALID; graph->next(it)) {
301 Parent::set(it, cmap[it]);
311 template <typename _Base>
312 class MappableUndirEdgeSetExtender :
313 public MappableEdgeSetExtender<_Base> {
316 typedef MappableUndirEdgeSetExtender Graph;
317 typedef MappableEdgeSetExtender<_Base> Parent;
319 typedef typename Parent::UndirEdge UndirEdge;
321 template <typename _Value>
323 : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > {
325 typedef MappableUndirEdgeSetExtender Graph;
326 typedef IterableMapExtender<
327 DefaultMap<Graph, UndirEdge, _Value> > Parent;
329 UndirEdgeMap(const Graph& _g)
331 UndirEdgeMap(const Graph& _g, const _Value& _v)
334 UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
335 return operator=<UndirEdgeMap>(cmap);
338 template <typename CMap>
339 UndirEdgeMap& operator=(const CMap& cmap) {
340 checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
341 const typename Parent::Graph* graph = Parent::getGraph();
343 for (graph->first(it); it != INVALID; graph->next(it)) {
344 Parent::set(it, cmap[it]);
354 template <typename _Base>
355 class MappableUndirBipartiteGraphExtender : public _Base {
358 typedef _Base Parent;
359 typedef MappableUndirBipartiteGraphExtender Graph;
361 typedef typename Parent::Node Node;
362 typedef typename Parent::UpperNode UpperNode;
363 typedef typename Parent::LowerNode LowerNode;
364 typedef typename Parent::Edge Edge;
365 typedef typename Parent::UndirEdge UndirEdge;
367 template <typename _Value>
369 : public IterableMapExtender<DefaultMap<Graph, UpperNode, _Value> > {
371 typedef MappableUndirBipartiteGraphExtender Graph;
372 typedef IterableMapExtender<DefaultMap<Graph, UpperNode, _Value> >
375 UpperNodeMap(const Graph& _g)
377 UpperNodeMap(const Graph& _g, const _Value& _v)
380 UpperNodeMap& operator=(const UpperNodeMap& cmap) {
381 return operator=<UpperNodeMap>(cmap);
385 /// \brief Template assign operator.
387 /// The given parameter should be conform to the ReadMap
388 /// concept and could be indiced by the current item set of
389 /// the UpperNodeMap. In this case the value for each item
390 /// is assigned by the value of the given ReadMap.
391 template <typename CMap>
392 UpperNodeMap& operator=(const CMap& cmap) {
393 checkConcept<concept::ReadMap<UpperNode, _Value>, CMap>();
394 const typename Parent::Graph* graph = Parent::getGraph();
396 for (graph->first(it); it != INVALID; graph->next(it)) {
397 Parent::set(it, cmap[it]);
404 template <typename _Value>
406 : public IterableMapExtender<DefaultMap<Graph, LowerNode, _Value> > {
408 typedef MappableUndirBipartiteGraphExtender Graph;
409 typedef IterableMapExtender<DefaultMap<Graph, LowerNode, _Value> >
412 LowerNodeMap(const Graph& _g)
414 LowerNodeMap(const Graph& _g, const _Value& _v)
417 LowerNodeMap& operator=(const LowerNodeMap& cmap) {
418 return operator=<LowerNodeMap>(cmap);
422 /// \brief Template assign operator.
424 /// The given parameter should be conform to the ReadMap
425 /// concept and could be indiced by the current item set of
426 /// the LowerNodeMap. In this case the value for each item
427 /// is assigned by the value of the given ReadMap.
428 template <typename CMap>
429 LowerNodeMap& operator=(const CMap& cmap) {
430 checkConcept<concept::ReadMap<LowerNode, _Value>, CMap>();
431 const typename Parent::Graph* graph = Parent::getGraph();
433 for (graph->first(it); it != INVALID; graph->next(it)) {
434 Parent::set(it, cmap[it]);
443 template <typename _Value>
444 class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
446 typedef MappableUndirBipartiteGraphExtender Graph;
449 typedef _Value Value;
451 /// The reference type of the map;
452 typedef typename LowerNodeMap<_Value>::Reference Reference;
453 /// The pointer type of the map;
454 typedef typename LowerNodeMap<_Value>::Pointer Pointer;
456 /// The const value type of the map.
457 typedef const Value ConstValue;
458 /// The const reference type of the map;
459 typedef typename LowerNodeMap<_Value>::ConstReference ConstReference;
460 /// The pointer type of the map;
461 typedef typename LowerNodeMap<_Value>::ConstPointer ConstPointer;
463 typedef True ReferenceMapTag;
465 NodeMapBase(const Graph& _g)
466 : graph(&_g), lowerMap(_g), upperMap(_g) {
467 Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
469 NodeMapBase(const Graph& _g, const _Value& _v)
470 : graph(&_g), lowerMap(_g, _v),
472 Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
475 virtual ~NodeMapBase() {
476 if (Parent::NodeNotifier::ObserverBase::attached()) {
477 Parent::NodeNotifier::ObserverBase::detach();
481 ConstReference operator[](const Key& node) const {
482 if (Parent::upper(node)) {
483 return upperMap[node];
485 return lowerMap[node];
489 Reference operator[](const Key& node) {
490 if (Parent::upper(node)) {
491 return upperMap[node];
493 return lowerMap[node];
497 void set(const Key& node, const Value& value) {
498 if (Parent::upper(node)) {
499 upperMap.set(node, value);
501 lowerMap.set(node, value);
507 virtual void add(const Node&) {}
508 virtual void erase(const Node&) {}
509 virtual void clear() {}
510 virtual void build() {}
512 const Graph* getGraph() const { return graph; }
516 LowerNodeMap<_Value> lowerMap;
517 UpperNodeMap<_Value> upperMap;
522 template <typename _Value>
524 : public IterableMapExtender<NodeMapBase<_Value> > {
526 typedef MappableUndirBipartiteGraphExtender Graph;
527 typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
529 NodeMap(const Graph& _g)
531 NodeMap(const Graph& _g, const _Value& _v)
534 NodeMap& operator=(const NodeMap& cmap) {
535 return operator=<NodeMap>(cmap);
539 /// \brief Template assign operator.
541 /// The given parameter should be conform to the ReadMap
542 /// concept and could be indiced by the current item set of
543 /// the NodeMap. In this case the value for each item
544 /// is assigned by the value of the given ReadMap.
545 template <typename CMap>
546 NodeMap& operator=(const CMap& cmap) {
547 checkConcept<concept::ReadMap<Node, _Value>, CMap>();
548 const typename Parent::Graph* graph = Parent::getGraph();
550 for (graph->first(it); it != INVALID; graph->next(it)) {
551 Parent::set(it, cmap[it]);
560 template <typename _Value>
562 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
564 typedef MappableUndirBipartiteGraphExtender Graph;
565 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
567 EdgeMap(const Graph& _g)
569 EdgeMap(const Graph& _g, const _Value& _v)
572 EdgeMap& operator=(const EdgeMap& cmap) {
573 return operator=<EdgeMap>(cmap);
576 template <typename CMap>
577 EdgeMap& operator=(const CMap& cmap) {
578 checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
579 const typename Parent::Graph* graph = Parent::getGraph();
581 for (graph->first(it); it != INVALID; graph->next(it)) {
582 Parent::set(it, cmap[it]);
588 template <typename _Value>
590 : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > {
592 typedef MappableUndirBipartiteGraphExtender Graph;
593 typedef IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> >
596 UndirEdgeMap(const Graph& _g)
598 UndirEdgeMap(const Graph& _g, const _Value& _v)
601 UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
602 return operator=<UndirEdgeMap>(cmap);
605 template <typename CMap>
606 UndirEdgeMap& operator=(const CMap& cmap) {
607 checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
608 const typename Parent::Graph* graph = Parent::getGraph();
610 for (graph->first(it); it != INVALID; graph->next(it)) {
611 Parent::set(it, cmap[it]);