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