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 deba@1999: #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@1999: /// \brief BaseExtender for the UGraphs deba@1999: template deba@1999: class UGraphBaseExtender : 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@1999: friend class UGraphBaseExtender; 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(id & 1)); deba@1999: } deba@1999: deba@1999: UEdge uEdgeFromId(int id) const { deba@1999: return Parent::edgeFromId(id >> 1); 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@1999: Edge findEdge(Node source, Node target, Edge prev) const { deba@1999: if (prev == INVALID) { deba@1999: UEdge edge = Parent::findEdge(source, target); deba@1999: if (edge != INVALID) return direct(edge, true); deba@1999: edge = Parent::findEdge(target, source); deba@1999: if (edge != INVALID) return direct(edge, false); deba@1999: } else if (direction(prev)) { deba@1999: UEdge edge = Parent::findEdge(source, target, prev); deba@1999: if (edge != INVALID) return direct(edge, true); deba@1999: edge = Parent::findEdge(target, source); deba@1999: if (edge != INVALID) return direct(edge, false); deba@1999: } else { deba@1999: UEdge edge = Parent::findEdge(target, source, prev); deba@1999: if (edge != INVALID) return direct(edge, false); deba@1999: } deba@1999: return INVALID; deba@1999: } deba@1999: deba@1999: UEdge findUEdge(Node source, Node target, UEdge prev) const { deba@1999: if (prev == INVALID) { deba@1999: UEdge edge = Parent::findEdge(source, target); deba@1999: if (edge != INVALID) return edge; deba@1999: edge = Parent::findEdge(target, source); deba@1999: if (edge != INVALID) return edge; deba@1999: } else if (Parent::source(prev) == source) { deba@1999: UEdge edge = Parent::findEdge(source, target, prev); deba@1999: if (edge != INVALID) return edge; deba@1999: edge = Parent::findEdge(target, source); deba@1999: if (edge != INVALID) return edge; deba@1999: } else { deba@1999: UEdge edge = Parent::findEdge(target, source, prev); deba@1999: if (edge != INVALID) return edge; deba@1999: } deba@1999: return INVALID; deba@1999: } deba@1999: }; deba@1999: deba@1999: deba@1999: /// \ingroup graphbits deba@1999: /// deba@1999: /// \brief BaseExtender for the BpUGraphs deba@1999: template deba@1999: class BpUGraphBaseExtender : public Base { deba@1999: public: deba@1999: typedef Base Parent; deba@1999: typedef BpUGraphBaseExtender Graph; deba@1999: deba@1999: typedef typename Parent::Node Node; deba@1999: typedef typename Parent::Edge UEdge; deba@1999: deba@1999: deba@1999: using Parent::first; deba@1999: using Parent::next; deba@1999: deba@1999: using Parent::id; deba@1999: deba@1999: class ANode : public Node { deba@1999: friend class BpUGraphBaseExtender; deba@1999: public: deba@1999: ANode() {} deba@1999: ANode(const Node& node) : Node(node) { deba@1999: LEMON_ASSERT(Parent::aNode(node) || node == INVALID, deba@1999: typename Parent::NodeSetError()); deba@1999: } deba@1999: ANode(Invalid) : Node(INVALID) {} deba@1999: }; deba@1999: deba@1999: void first(ANode& node) const { deba@1999: Parent::firstANode(static_cast(node)); deba@1999: } deba@1999: void next(ANode& node) const { deba@1999: Parent::nextANode(static_cast(node)); deba@1999: } deba@1999: deba@1999: int id(const ANode& node) const { deba@1999: return Parent::aNodeId(node); deba@1999: } deba@1999: deba@1999: class BNode : public Node { deba@1999: friend class BpUGraphBaseExtender; deba@1999: public: deba@1999: BNode() {} deba@1999: BNode(const Node& node) : Node(node) { deba@1999: LEMON_ASSERT(Parent::bNode(node) || node == INVALID, deba@1999: typename Parent::NodeSetError()); deba@1999: } deba@1999: BNode(Invalid) : Node(INVALID) {} deba@1999: }; deba@1999: deba@1999: void first(BNode& node) const { deba@1999: Parent::firstBNode(static_cast(node)); deba@1999: } deba@1999: void next(BNode& node) const { deba@1999: Parent::nextBNode(static_cast(node)); deba@1999: } deba@1999: deba@1999: int id(const BNode& node) const { deba@1999: return Parent::aNodeId(node); deba@1999: } deba@1999: deba@1999: Node source(const UEdge& edge) const { deba@1999: return aNode(edge); deba@1999: } deba@1999: Node target(const UEdge& edge) const { deba@1999: return bNode(edge); deba@1999: } deba@1999: deba@1999: void firstInc(UEdge& edge, bool& direction, const Node& node) const { deba@1999: if (Parent::aNode(node)) { deba@1999: Parent::firstOut(edge, node); deba@1999: direction = true; deba@1999: } else { deba@1999: Parent::firstIn(edge, node); deba@1999: direction = static_cast(edge) == INVALID; deba@1999: } deba@1999: } deba@1999: void nextInc(UEdge& edge, bool& direction) const { deba@1999: if (direction) { deba@1999: Parent::nextOut(edge); deba@1999: } else { deba@1999: Parent::nextIn(edge); deba@1999: if (edge == INVALID) direction = true; deba@1999: } deba@1999: } deba@1999: deba@1999: int maxUEdgeId() const { deba@1999: return Parent::maxEdgeId(); deba@1999: } deba@1999: deba@1999: UEdge uEdgeFromId(int id) const { deba@1999: return Parent::edgeFromId(id); deba@1999: } deba@1999: deba@1999: class Edge : public UEdge { deba@1999: friend class BpUGraphBaseExtender; deba@1999: protected: deba@1999: bool forward; deba@1999: deba@1999: Edge(const UEdge& edge, bool _forward) deba@1999: : UEdge(edge), forward(_forward) {} deba@1999: deba@1999: public: deba@1999: Edge() {} deba@1999: Edge (Invalid) : UEdge(INVALID), forward(true) {} deba@1999: bool operator==(const Edge& i) const { deba@1999: return UEdge::operator==(i) && forward == i.forward; deba@1999: } deba@1999: bool operator!=(const Edge& i) const { deba@1999: return UEdge::operator!=(i) || forward != i.forward; deba@1999: } deba@1999: bool operator<(const Edge& i) const { deba@1999: return UEdge::operator<(i) || deba@1999: (!(i.forward(edge)); deba@1999: edge.forward = true; deba@1999: } deba@1999: deba@1999: void next(Edge& edge) const { deba@1999: if (!edge.forward) { deba@1999: Parent::next(static_cast(edge)); deba@1999: } deba@1999: edge.forward = !edge.forward; deba@1999: } deba@1999: deba@1999: void firstOut(Edge& edge, const Node& node) const { deba@1999: if (Parent::aNode(node)) { deba@1999: Parent::firstOut(edge, node); deba@1999: edge.forward = true; deba@1999: } else { deba@1999: Parent::firstIn(edge, node); deba@1999: edge.forward = static_cast(edge) == INVALID; deba@1999: } deba@1999: } deba@1999: void nextOut(Edge& edge) const { deba@1999: if (edge.forward) { deba@1999: Parent::nextOut(edge); deba@1999: } else { deba@1999: Parent::nextIn(edge); deba@1999: edge.forward = static_cast(edge) == INVALID; deba@1999: } deba@1999: } deba@1999: deba@1999: void firstIn(Edge& edge, const Node& node) const { deba@1999: if (Parent::bNode(node)) { deba@1999: Parent::firstIn(edge, node); deba@1999: edge.forward = true; deba@1999: } else { deba@1999: Parent::firstOut(edge, node); deba@1999: edge.forward = static_cast(edge) == INVALID; deba@1999: } deba@1999: } deba@1999: void nextIn(Edge& edge) const { deba@1999: if (edge.forward) { deba@1999: Parent::nextIn(edge); deba@1999: } else { deba@1999: Parent::nextOut(edge); deba@1999: edge.forward = static_cast(edge) == INVALID; deba@1999: } deba@1999: } deba@1999: deba@1999: Node source(const Edge& edge) const { deba@1999: return edge.forward ? Parent::aNode(edge) : Parent::bNode(edge); deba@1999: } deba@1999: Node target(const Edge& edge) const { deba@1999: return edge.forward ? Parent::bNode(edge) : Parent::aNode(edge); deba@1999: } deba@1999: deba@1999: int id(const Edge& edge) const { deba@1999: return (Parent::id(edge) << 1) + (edge.forward ? 0 : 1); deba@1999: } deba@1999: Edge edgeFromId(int id) const { deba@1999: return Edge(Parent::fromId(id >> 1, UEdge()), (id & 1) == 0); deba@1999: } deba@1999: int maxEdgeId() const { deba@1999: return (Parent::maxId(UEdge()) << 1) + 1; deba@1999: } deba@1999: deba@1999: bool direction(const Edge& edge) const { deba@1999: return edge.forward; deba@1999: } deba@1999: deba@1999: Edge direct(const UEdge& edge, bool direction) const { deba@1999: return Edge(edge, direction); deba@1999: } deba@2031: deba@2031: int edgeNum() const { deba@2031: return 2 * Parent::edgeNum(); deba@2031: } deba@2031: deba@2031: int uEdgeNum() const { deba@2031: return Parent::edgeNum(); deba@2031: } deba@2031: deba@1999: }; deba@1999: deba@1999: } deba@1999: deba@1999: #endif