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@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(id & 1)); deba@1999: } deba@1999: deba@1999: UEdge uEdgeFromId(int id) const { deba@2187: return Parent::edgeFromId(id); 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@2222: Edge findEdge(Node source, Node target, Edge prev = INVALID) 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@2222: UEdge findUEdge(Node source, Node target, UEdge prev = INVALID) const { deba@2222: if (source != target) { deba@2222: if (prev == INVALID) { deba@2222: UEdge edge = Parent::findEdge(source, target); deba@2222: if (edge != INVALID) return edge; deba@2222: edge = Parent::findEdge(target, source); deba@2222: if (edge != INVALID) return edge; deba@2222: } else if (Parent::source(prev) == source) { deba@2222: UEdge edge = Parent::findEdge(source, target, prev); deba@2222: if (edge != INVALID) return edge; deba@2222: edge = Parent::findEdge(target, source); deba@2222: if (edge != INVALID) return edge; deba@2222: } else { deba@2222: UEdge edge = Parent::findEdge(target, source, prev); deba@2222: if (edge != INVALID) return edge; deba@2222: } deba@1999: } else { deba@2222: return Parent::findEdge(source, target, prev); deba@1999: } deba@1999: return INVALID; deba@1999: } deba@1999: }; deba@1999: deba@1999: } deba@1999: deba@1999: #endif