deba@1999: /* -*- C++ -*- deba@1999: * deba@1999: * This file is a part of LEMON, a generic C++ optimization library deba@1999: * deba@1999: * Copyright (C) 2003-2006 deba@1999: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@1999: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@1999: * deba@1999: * Permission to use, modify and distribute this software is granted deba@1999: * provided that this copyright notice appears in all copies. For deba@1999: * precise terms see the accompanying LICENSE file. deba@1999: * deba@1999: * This software is provided "AS IS" with no warranty of any kind, deba@1999: * express or implied, and with no claim as to its suitability for any deba@1999: * purpose. deba@1999: * deba@1999: */ deba@1999: deba@1999: #ifndef LEMON_BITS_BASE_EXTENDER_H deba@1999: #define LEMON_BITS_BASE_EXTENDER_H deba@1999: deba@1999: #include deba@1999: #include deba@1999: deba@1999: #include deba@1999: #include deba@1999: deba@1999: #include alpar@2260: #include deba@1999: deba@1999: ///\ingroup graphbits deba@1999: ///\file deba@1999: ///\brief Extenders for the graph types deba@1999: namespace lemon { deba@1999: deba@1999: /// \ingroup graphbits deba@1999: /// deba@2222: /// \brief BaseGraph to BaseUGraph extender deba@1999: template deba@2076: class UndirGraphExtender : public Base { deba@1999: deba@1999: public: deba@1999: deba@1999: typedef Base Parent; deba@1999: typedef typename Parent::Edge UEdge; deba@1999: typedef typename Parent::Node Node; deba@1999: deba@1999: typedef True UndirectedTag; deba@1999: deba@1999: class Edge : public UEdge { deba@2076: friend class UndirGraphExtender; deba@1999: deba@1999: protected: deba@1999: bool forward; deba@1999: deba@1999: Edge(const UEdge &ue, bool _forward) : deba@1999: UEdge(ue), forward(_forward) {} deba@1999: deba@1999: public: deba@1999: Edge() {} deba@1999: deba@1999: /// Invalid edge constructor deba@1999: Edge(Invalid i) : UEdge(i), forward(true) {} deba@1999: deba@1999: bool operator==(const Edge &that) const { deba@1999: return forward==that.forward && UEdge(*this)==UEdge(that); deba@1999: } deba@1999: bool operator!=(const Edge &that) const { deba@1999: return forward!=that.forward || UEdge(*this)!=UEdge(that); deba@1999: } deba@1999: bool operator<(const Edge &that) const { deba@1999: return forward> 1), bool(ix & 1)); deba@1999: } deba@1999: deba@2386: UEdge uEdgeFromId(int ix) const { deba@2386: return Parent::edgeFromId(ix); deba@1999: } deba@1999: deba@1999: int id(const Node &n) const { deba@1999: return Parent::id(n); deba@1999: } deba@1999: deba@1999: int id(const UEdge &e) const { deba@1999: return Parent::id(e); deba@1999: } deba@1999: deba@1999: int id(const Edge &e) const { deba@1999: return 2 * Parent::id(e) + int(e.forward); deba@1999: } deba@1999: deba@1999: int maxNodeId() const { deba@1999: return Parent::maxNodeId(); deba@1999: } deba@1999: deba@1999: int maxEdgeId() const { deba@1999: return 2 * Parent::maxEdgeId() + 1; deba@1999: } deba@1999: deba@1999: int maxUEdgeId() const { deba@1999: return Parent::maxEdgeId(); deba@1999: } deba@1999: deba@1999: deba@1999: int edgeNum() const { deba@1999: return 2 * Parent::edgeNum(); deba@1999: } deba@1999: deba@1999: int uEdgeNum() const { deba@1999: return Parent::edgeNum(); deba@1999: } deba@1999: deba@2386: Edge findEdge(Node s, Node t, Edge p = INVALID) const { deba@2386: if (p == INVALID) { deba@2386: UEdge edge = Parent::findEdge(s, t); deba@1999: if (edge != INVALID) return direct(edge, true); deba@2386: edge = Parent::findEdge(t, s); deba@1999: if (edge != INVALID) return direct(edge, false); deba@2386: } else if (direction(p)) { deba@2386: UEdge edge = Parent::findEdge(s, t, p); deba@1999: if (edge != INVALID) return direct(edge, true); deba@2386: edge = Parent::findEdge(t, s); deba@1999: if (edge != INVALID) return direct(edge, false); deba@1999: } else { deba@2386: UEdge edge = Parent::findEdge(t, s, p); deba@1999: if (edge != INVALID) return direct(edge, false); deba@1999: } deba@1999: return INVALID; deba@1999: } deba@1999: deba@2386: UEdge findUEdge(Node s, Node t, UEdge p = INVALID) const { deba@2386: if (s != t) { deba@2386: if (p == INVALID) { deba@2386: UEdge edge = Parent::findEdge(s, t); deba@2222: if (edge != INVALID) return edge; deba@2386: edge = Parent::findEdge(t, s); deba@2222: if (edge != INVALID) return edge; deba@2386: } else if (Parent::s(p) == s) { deba@2386: UEdge edge = Parent::findEdge(s, t, p); deba@2222: if (edge != INVALID) return edge; deba@2386: edge = Parent::findEdge(t, s); deba@2222: if (edge != INVALID) return edge; deba@2222: } else { deba@2386: UEdge edge = Parent::findEdge(t, s, p); deba@2222: if (edge != INVALID) return edge; deba@2222: } deba@1999: } else { deba@2386: return Parent::findEdge(s, t, p); deba@1999: } deba@1999: return INVALID; deba@1999: } deba@1999: }; deba@1999: deba@2231: template deba@2231: class BidirBpUGraphExtender : public Base { deba@2231: public: deba@2231: typedef Base Parent; deba@2231: typedef BidirBpUGraphExtender Graph; deba@2231: deba@2231: typedef typename Parent::Node Node; deba@2231: typedef typename Parent::UEdge UEdge; deba@2231: deba@2231: deba@2231: using Parent::first; deba@2231: using Parent::next; deba@2231: deba@2231: using Parent::id; deba@2231: deba@2231: class ANode : public Node { deba@2231: friend class BidirBpUGraphExtender; deba@2231: public: deba@2231: ANode() {} deba@2231: ANode(const Node& node) : Node(node) { deba@2231: LEMON_ASSERT(Parent::aNode(node) || node == INVALID, deba@2231: typename Parent::NodeSetError()); deba@2231: } deba@2231: ANode& operator=(const Node& node) { deba@2231: LEMON_ASSERT(Parent::aNode(node) || node == INVALID, deba@2231: typename Parent::NodeSetError()); deba@2231: Node::operator=(node); deba@2231: return *this; deba@2231: } deba@2231: ANode(Invalid) : Node(INVALID) {} deba@2231: }; deba@2231: deba@2231: void first(ANode& node) const { deba@2231: Parent::firstANode(static_cast(node)); deba@2231: } deba@2231: void next(ANode& node) const { deba@2231: Parent::nextANode(static_cast(node)); deba@2231: } deba@2231: deba@2231: int id(const ANode& node) const { deba@2231: return Parent::aNodeId(node); deba@2231: } deba@2231: deba@2231: class BNode : public Node { deba@2231: friend class BidirBpUGraphExtender; deba@2231: public: deba@2231: BNode() {} deba@2231: BNode(const Node& node) : Node(node) { deba@2231: LEMON_ASSERT(Parent::bNode(node) || node == INVALID, deba@2231: typename Parent::NodeSetError()); deba@2231: } deba@2231: BNode& operator=(const Node& node) { deba@2231: LEMON_ASSERT(Parent::bNode(node) || node == INVALID, deba@2231: typename Parent::NodeSetError()); deba@2231: Node::operator=(node); deba@2231: return *this; deba@2231: } deba@2231: BNode(Invalid) : Node(INVALID) {} deba@2231: }; deba@2231: deba@2231: void first(BNode& node) const { deba@2231: Parent::firstBNode(static_cast(node)); deba@2231: } deba@2231: void next(BNode& node) const { deba@2231: Parent::nextBNode(static_cast(node)); deba@2231: } deba@2231: deba@2231: int id(const BNode& node) const { deba@2231: return Parent::aNodeId(node); deba@2231: } deba@2231: deba@2231: Node source(const UEdge& edge) const { deba@2231: return aNode(edge); deba@2231: } deba@2231: Node target(const UEdge& edge) const { deba@2231: return bNode(edge); deba@2231: } deba@2231: deba@2386: void firstInc(UEdge& edge, bool& dir, const Node& node) const { deba@2231: if (Parent::aNode(node)) { deba@2231: Parent::firstFromANode(edge, node); deba@2386: dir = true; deba@2231: } else { deba@2231: Parent::firstFromBNode(edge, node); deba@2386: dir = static_cast(edge) == INVALID; deba@2231: } deba@2231: } deba@2386: void nextInc(UEdge& edge, bool& dir) const { deba@2386: if (dir) { deba@2231: Parent::nextFromANode(edge); deba@2231: } else { deba@2231: Parent::nextFromBNode(edge); deba@2386: if (edge == INVALID) dir = true; deba@2231: } deba@2231: } deba@2231: deba@2231: class Edge : public UEdge { deba@2231: friend class BidirBpUGraphExtender; deba@2231: protected: deba@2231: bool forward; deba@2231: deba@2231: Edge(const UEdge& edge, bool _forward) deba@2231: : UEdge(edge), forward(_forward) {} deba@2231: deba@2231: public: deba@2231: Edge() {} deba@2231: Edge (Invalid) : UEdge(INVALID), forward(true) {} deba@2231: bool operator==(const Edge& i) const { deba@2231: return UEdge::operator==(i) && forward == i.forward; deba@2231: } deba@2231: bool operator!=(const Edge& i) const { deba@2231: return UEdge::operator!=(i) || forward != i.forward; deba@2231: } deba@2231: bool operator<(const Edge& i) const { deba@2231: return UEdge::operator<(i) || deba@2231: (!(i.forward(edge)); deba@2231: edge.forward = true; deba@2231: } deba@2231: deba@2231: void next(Edge& edge) const { deba@2231: if (!edge.forward) { deba@2231: Parent::next(static_cast(edge)); deba@2231: } deba@2231: edge.forward = !edge.forward; deba@2231: } deba@2231: deba@2231: void firstOut(Edge& edge, const Node& node) const { deba@2231: if (Parent::aNode(node)) { deba@2231: Parent::firstFromANode(edge, node); deba@2231: edge.forward = true; deba@2231: } else { deba@2231: Parent::firstFromBNode(edge, node); deba@2231: edge.forward = static_cast(edge) == INVALID; deba@2231: } deba@2231: } deba@2231: void nextOut(Edge& edge) const { deba@2231: if (edge.forward) { deba@2231: Parent::nextFromANode(edge); deba@2231: } else { deba@2231: Parent::nextFromBNode(edge); deba@2231: edge.forward = static_cast(edge) == INVALID; deba@2231: } deba@2231: } deba@2231: deba@2231: void firstIn(Edge& edge, const Node& node) const { deba@2231: if (Parent::bNode(node)) { deba@2231: Parent::firstFromBNode(edge, node); deba@2231: edge.forward = true; deba@2231: } else { deba@2231: Parent::firstFromANode(edge, node); deba@2231: edge.forward = static_cast(edge) == INVALID; deba@2231: } deba@2231: } deba@2231: void nextIn(Edge& edge) const { deba@2231: if (edge.forward) { deba@2231: Parent::nextFromBNode(edge); deba@2231: } else { deba@2231: Parent::nextFromANode(edge); deba@2231: edge.forward = static_cast(edge) == INVALID; deba@2231: } deba@2231: } deba@2231: deba@2231: Node source(const Edge& edge) const { deba@2231: return edge.forward ? Parent::aNode(edge) : Parent::bNode(edge); deba@2231: } deba@2231: Node target(const Edge& edge) const { deba@2231: return edge.forward ? Parent::bNode(edge) : Parent::aNode(edge); deba@2231: } deba@2231: deba@2231: int id(const Edge& edge) const { deba@2231: return (Parent::id(static_cast(edge)) << 1) + deba@2231: (edge.forward ? 0 : 1); deba@2231: } deba@2386: Edge edgeFromId(int ix) const { deba@2386: return Edge(Parent::fromUEdgeId(ix >> 1), (ix & 1) == 0); deba@2231: } deba@2231: int maxEdgeId() const { deba@2231: return (Parent::maxUEdgeId() << 1) + 1; deba@2231: } deba@2231: deba@2231: bool direction(const Edge& edge) const { deba@2231: return edge.forward; deba@2231: } deba@2231: deba@2386: Edge direct(const UEdge& edge, bool dir) const { deba@2386: return Edge(edge, dir); deba@2231: } deba@2231: deba@2231: int edgeNum() const { deba@2231: return 2 * Parent::uEdgeNum(); deba@2231: } deba@2231: deba@2231: int uEdgeNum() const { deba@2231: return Parent::uEdgeNum(); deba@2231: } deba@2231: deba@2231: deba@2231: }; deba@1999: } deba@1999: deba@1999: #endif