[915] | 1 | /* -*- C++ -*- |
---|
| 2 | * src/hugo/graph_wrapper.h - Part of HUGOlib, a generic C++ optimization library |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
| 6 | * |
---|
| 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. |
---|
| 10 | * |
---|
| 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 |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #include <hugo/invalid.h> |
---|
| 18 | #include <hugo/maps.h> |
---|
| 19 | #include <hugo/map_defines.h> |
---|
| 20 | #include <hugo/graph_wrapper.h> |
---|
| 21 | #include <iostream> |
---|
| 22 | |
---|
| 23 | #ifndef HUGO_MERGE_NODE_GRAPH_WRAPPER_H |
---|
| 24 | #define HUGO_MERGE_NODE_GRAPH_WRAPPER_H |
---|
| 25 | |
---|
| 26 | namespace hugo { |
---|
| 27 | |
---|
| 28 | template <typename Graph1, typename Graph2> |
---|
| 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; |
---|
| 35 | public: |
---|
| 36 | class Node; |
---|
| 37 | class NodeIt; |
---|
| 38 | friend class Node; |
---|
| 39 | friend class NodeIt; |
---|
| 40 | |
---|
| 41 | MergeNodeGraphWrapper(Graph1& _graph1, Graph2& _graph2) : |
---|
| 42 | Parent1(_graph1), Parent2(_graph2) { } |
---|
| 43 | |
---|
| 44 | class Node : public Graph1Node, public Graph2Node { |
---|
| 45 | friend class MergeNodeGraphWrapper<Graph1, Graph2>; |
---|
| 46 | //template<typename T> friend class NodeMap; |
---|
| 47 | protected: |
---|
| 48 | bool backward; //true, iff backward |
---|
| 49 | public: |
---|
| 50 | Node() { } |
---|
| 51 | /// \todo =false is needed, or causes problems? |
---|
| 52 | /// If \c _backward is false, then we get an edge corresponding to the |
---|
| 53 | /// original one, otherwise its oppositely directed pair is obtained. |
---|
| 54 | Node(const Graph1Node& n1, |
---|
| 55 | const Graph2Node& n2, bool _backward) : |
---|
| 56 | Graph1Node(n1), Graph2Node(n2), backward(_backward) { } |
---|
| 57 | Node(Invalid i) : Graph1Node(i), Graph2Node(i), backward(true) { } |
---|
| 58 | bool operator==(const Node& v) const { |
---|
| 59 | return (this->backward==v.backward && |
---|
| 60 | static_cast<Graph1Node>(*this)== |
---|
| 61 | static_cast<Graph1Node>(v) && |
---|
| 62 | static_cast<Graph2Node>(*this)== |
---|
| 63 | static_cast<Graph2Node>(v)); |
---|
| 64 | } |
---|
| 65 | bool operator!=(const Node& v) const { |
---|
| 66 | return (this->backward!=v.backward || |
---|
| 67 | static_cast<Graph1Node>(*this)!= |
---|
| 68 | static_cast<Graph1Node>(v) || |
---|
| 69 | static_cast<Graph2Node>(*this)!= |
---|
| 70 | static_cast<Graph2Node>(v)); |
---|
| 71 | } |
---|
| 72 | }; |
---|
| 73 | |
---|
| 74 | class NodeIt : public Node { |
---|
| 75 | friend class MergeNodeGraphWrapper<Graph1, Graph2>; |
---|
| 76 | protected: |
---|
| 77 | const MergeNodeGraphWrapper<Graph1, Graph2>* gw; |
---|
| 78 | public: |
---|
| 79 | NodeIt() { } |
---|
| 80 | NodeIt(Invalid i) : Node(i) { } |
---|
| 81 | NodeIt(const MergeNodeGraphWrapper<Graph1, Graph2>& _gw) : |
---|
| 82 | Node(typename Graph1::NodeIt(*_gw.Parent1::graph), |
---|
| 83 | typename Graph2::NodeIt(), |
---|
| 84 | false), gw(&_gw) { |
---|
| 85 | if (*static_cast<Graph1Node*>(this)==INVALID) { |
---|
| 86 | *static_cast<Node*>(this)= |
---|
| 87 | Node(Graph1Node(INVALID), |
---|
| 88 | typename Graph2::NodeIt(*_gw.Parent2::graph), |
---|
| 89 | true); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | NodeIt(const MergeNodeGraphWrapper<Graph1, Graph2>& _gw, |
---|
| 93 | const Node& n) : |
---|
| 94 | Node(n), gw(&_gw) { } |
---|
| 95 | NodeIt& operator++() { |
---|
| 96 | if (!this->backward) { |
---|
| 97 | *(static_cast<Graph1Node*>(this))= |
---|
| 98 | ++(typename Graph1::NodeIt(*gw->Parent1::graph, *this)); |
---|
| 99 | if (*static_cast<Graph1Node*>(this)==INVALID) { |
---|
| 100 | *static_cast<Node*>(this)= |
---|
| 101 | Node(typename Graph1::Node(INVALID), |
---|
| 102 | typename Graph2::NodeIt(*gw->Parent2::graph), true); |
---|
| 103 | } |
---|
| 104 | } else { |
---|
| 105 | *(static_cast<Graph2Node*>(this))= |
---|
| 106 | ++(typename Graph2::NodeIt(*gw->Parent2::graph, *this)); |
---|
| 107 | } |
---|
| 108 | return *this; |
---|
| 109 | } |
---|
| 110 | }; |
---|
| 111 | |
---|
| 112 | int id(const Node& n) const { |
---|
| 113 | if (!n.backward) |
---|
| 114 | return this->Parent1::graph->id(n); |
---|
| 115 | else |
---|
| 116 | return this->Parent2::graph->id(n); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | }; |
---|
| 120 | |
---|
| 121 | } //namespace hugo |
---|
| 122 | |
---|
| 123 | #endif //HUGO_MERGE_NODE_GRAPH_WRAPPER_H |
---|