2 * src/lemon/merge_node_graph_wrapper.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, 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_MERGE_NODE_GRAPH_WRAPPER_H
18 #define LEMON_MERGE_NODE_GRAPH_WRAPPER_H
20 #include <lemon/invalid.h>
21 #include <lemon/maps.h>
22 #include <lemon/map_defines.h>
23 #include <lemon/graph_wrapper.h>
28 template <typename Graph1, typename Graph2, typename Enable=void>
29 class MergeNodeGraphWrapper :
30 public GraphWrapper<Graph1>, public GraphWrapper<Graph2> {
31 typedef GraphWrapper<Graph1> Parent1;
32 typedef GraphWrapper<Graph2> Parent2;
33 typedef typename GraphWrapper<Graph1>::Node Graph1Node;
34 typedef typename GraphWrapper<Graph2>::Node Graph2Node;
40 template<typename Value> class NodeMap;
42 MergeNodeGraphWrapper(Graph1& _graph1, Graph2& _graph2) :
43 Parent1(_graph1), Parent2(_graph2) { }
45 class Node : public Graph1Node, public Graph2Node {
46 friend class MergeNodeGraphWrapper<Graph1, Graph2>;
47 template<typename Value> friend class NodeMap;
49 bool backward; //true, iff backward
52 /// \todo =false is needed, or causes problems?
53 /// If \c _backward is false, then we get an edge corresponding to the
54 /// original one, otherwise its oppositely directed pair is obtained.
55 Node(const Graph1Node& n1,
56 const Graph2Node& n2, bool _backward) :
57 Graph1Node(n1), Graph2Node(n2), backward(_backward) { }
58 Node(Invalid i) : Graph1Node(i), Graph2Node(i), backward(true) { }
59 bool operator==(const Node& v) const {
60 return (this->backward==v.backward &&
61 static_cast<Graph1Node>(*this)==
62 static_cast<Graph1Node>(v) &&
63 static_cast<Graph2Node>(*this)==
64 static_cast<Graph2Node>(v));
66 bool operator!=(const Node& v) const {
67 return (this->backward!=v.backward ||
68 static_cast<Graph1Node>(*this)!=
69 static_cast<Graph1Node>(v) ||
70 static_cast<Graph2Node>(*this)!=
71 static_cast<Graph2Node>(v));
75 class NodeIt : public Node {
76 friend class MergeNodeGraphWrapper<Graph1, Graph2>;
78 const MergeNodeGraphWrapper<Graph1, Graph2>* gw;
81 NodeIt(Invalid i) : Node(i) { }
82 NodeIt(const MergeNodeGraphWrapper<Graph1, Graph2>& _gw) :
83 Node(typename Graph1::NodeIt(*_gw.Parent1::graph),
84 typename Graph2::Node(),
86 if (*static_cast<Graph1Node*>(this)==INVALID) {
87 // *static_cast<Node*>(this)=
88 // Node(Graph1Node(INVALID),
89 // typename Graph2::NodeIt(*_gw.Parent2::graph),
91 *static_cast<Graph2Node*>(this)=
92 typename Graph2::NodeIt(*_gw.Parent2::graph);
96 NodeIt(const MergeNodeGraphWrapper<Graph1, Graph2>& _gw,
99 NodeIt& operator++() {
100 if (!this->backward) {
101 *(static_cast<Graph1Node*>(this))=
102 ++(typename Graph1::NodeIt(*gw->Parent1::graph, *this));
103 if (*static_cast<Graph1Node*>(this)==INVALID) {
104 // *static_cast<Node*>(this)=
105 // Node(typename Graph1::Node(INVALID),
106 // typename Graph2::NodeIt(*gw->Parent2::graph), true);
107 *static_cast<Graph2Node*>(this)=
108 typename Graph2::NodeIt(*gw->Parent2::graph);
112 *(static_cast<Graph2Node*>(this))=
113 ++(typename Graph2::NodeIt(*gw->Parent2::graph, *this));
119 int id(const Node& n) const {
121 return this->Parent1::graph->id(n);
123 return this->Parent2::graph->id(n);
126 template <typename Value>
127 class NodeMap : public Parent1::template NodeMap<Value>,
128 public Parent2::template NodeMap<Value> {
129 typedef typename Parent1::template NodeMap<Value> ParentMap1;
130 typedef typename Parent2::template NodeMap<Value> ParentMap2;
132 NodeMap(const MergeNodeGraphWrapper<Graph1, Graph2>& gw) :
135 NodeMap(const MergeNodeGraphWrapper<Graph1, Graph2>& gw,
137 : ParentMap1(gw, value),
138 ParentMap2(gw, value) { }
139 NodeMap(const NodeMap& copy)
142 template <typename TT>
143 NodeMap(const NodeMap<TT>& copy)
146 NodeMap& operator=(const NodeMap& copy) {
147 ParentMap1::operator=(copy);
148 ParentMap2::operator=(copy);
151 template <typename TT>
152 NodeMap& operator=(const NodeMap<TT>& copy) {
153 ParentMap1::operator=(copy);
154 ParentMap2::operator=(copy);
157 Value operator[](const Node& n) const {
159 return ParentMap1::operator[](n);
161 return ParentMap2::operator[](n);
163 void set(const Node& n, const Value& value) {
165 ParentMap1::set(n, value);
167 ParentMap2::set(n, value);
169 using ParentMap1::operator[];
170 using ParentMap2::operator[];
177 #endif //LEMON_MERGE_NODE_GRAPH_WRAPPER_H