alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@57:  *
alpar@209:  * This file is a part of LEMON, a generic C++ optimization library.
deba@57:  *
alpar@440:  * Copyright (C) 2003-2009
deba@57:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57:  *
deba@57:  * Permission to use, modify and distribute this software is granted
deba@57:  * provided that this copyright notice appears in all copies. For
deba@57:  * precise terms see the accompanying LICENSE file.
deba@57:  *
deba@57:  * This software is provided "AS IS" with no warranty of any kind,
deba@57:  * express or implied, and with no claim as to its suitability for any
deba@57:  * purpose.
deba@57:  *
deba@57:  */
deba@57: 
deba@57: #ifndef LEMON_BITS_BASE_EXTENDER_H
deba@57: #define LEMON_BITS_BASE_EXTENDER_H
deba@57: 
deba@220: #include <lemon/core.h>
deba@57: #include <lemon/error.h>
deba@57: 
deba@57: #include <lemon/bits/map_extender.h>
deba@57: #include <lemon/bits/default_map.h>
deba@57: 
deba@57: #include <lemon/concept_check.h>
deba@57: #include <lemon/concepts/maps.h>
deba@57: 
kpeter@314: //\ingroup digraphbits
kpeter@314: //\file
kpeter@361: //\brief Extenders for the graph types
deba@57: namespace lemon {
deba@57: 
kpeter@314:   // \ingroup digraphbits
kpeter@314:   //
kpeter@314:   // \brief BaseDigraph to BaseGraph extender
deba@57:   template <typename Base>
deba@57:   class UndirDigraphExtender : public Base {
deba@57: 
deba@57:   public:
deba@57: 
deba@57:     typedef Base Parent;
deba@57:     typedef typename Parent::Arc Edge;
deba@57:     typedef typename Parent::Node Node;
deba@57: 
deba@57:     typedef True UndirectedTag;
deba@57: 
deba@57:     class Arc : public Edge {
deba@57:       friend class UndirDigraphExtender;
deba@57: 
deba@57:     protected:
deba@57:       bool forward;
deba@57: 
deba@57:       Arc(const Edge &ue, bool _forward) :
deba@57:         Edge(ue), forward(_forward) {}
deba@57: 
deba@57:     public:
deba@57:       Arc() {}
deba@57: 
kpeter@256:       // Invalid arc constructor
deba@57:       Arc(Invalid i) : Edge(i), forward(true) {}
deba@57: 
deba@57:       bool operator==(const Arc &that) const {
alpar@209:         return forward==that.forward && Edge(*this)==Edge(that);
deba@57:       }
deba@57:       bool operator!=(const Arc &that) const {
alpar@209:         return forward!=that.forward || Edge(*this)!=Edge(that);
deba@57:       }
deba@57:       bool operator<(const Arc &that) const {
alpar@209:         return forward<that.forward ||
alpar@209:           (!(that.forward<forward) && Edge(*this)<Edge(that));
deba@57:       }
deba@57:     };
deba@57: 
kpeter@314:     // First node of the edge
kpeter@256:     Node u(const Edge &e) const {
kpeter@256:       return Parent::source(e);
kpeter@256:     }
deba@57: 
kpeter@314:     // Source of the given arc
deba@57:     Node source(const Arc &e) const {
deba@57:       return e.forward ? Parent::source(e) : Parent::target(e);
deba@57:     }
deba@57: 
kpeter@314:     // Second node of the edge
kpeter@256:     Node v(const Edge &e) const {
kpeter@256:       return Parent::target(e);
kpeter@256:     }
deba@57: 
kpeter@314:     // Target of the given arc
deba@57:     Node target(const Arc &e) const {
deba@57:       return e.forward ? Parent::target(e) : Parent::source(e);
deba@57:     }
deba@57: 
kpeter@314:     // \brief Directed arc from an edge.
kpeter@314:     //
kpeter@314:     // Returns a directed arc corresponding to the specified edge.
kpeter@314:     // If the given bool is true, the first node of the given edge and
kpeter@314:     // the source node of the returned arc are the same.
kpeter@256:     static Arc direct(const Edge &e, bool d) {
kpeter@256:       return Arc(e, d);
deba@57:     }
deba@57: 
kpeter@314:     // Returns whether the given directed arc has the same orientation
kpeter@314:     // as the corresponding edge.
kpeter@256:     static bool direction(const Arc &a) { return a.forward; }
deba@57: 
deba@57:     using Parent::first;
deba@57:     using Parent::next;
deba@57: 
deba@57:     void first(Arc &e) const {
deba@57:       Parent::first(e);
deba@57:       e.forward=true;
deba@57:     }
deba@57: 
deba@57:     void next(Arc &e) const {
deba@57:       if( e.forward ) {
alpar@209:         e.forward = false;
deba@57:       }
deba@57:       else {
alpar@209:         Parent::next(e);
alpar@209:         e.forward = true;
deba@57:       }
deba@57:     }
deba@57: 
deba@57:     void firstOut(Arc &e, const Node &n) const {
deba@57:       Parent::firstIn(e,n);
deba@57:       if( Edge(e) != INVALID ) {
alpar@209:         e.forward = false;
deba@57:       }
deba@57:       else {
alpar@209:         Parent::firstOut(e,n);
alpar@209:         e.forward = true;
deba@57:       }
deba@57:     }
deba@57:     void nextOut(Arc &e) const {
deba@57:       if( ! e.forward ) {
alpar@209:         Node n = Parent::target(e);
alpar@209:         Parent::nextIn(e);
alpar@209:         if( Edge(e) == INVALID ) {
alpar@209:           Parent::firstOut(e, n);
alpar@209:           e.forward = true;
alpar@209:         }
deba@57:       }
deba@57:       else {
alpar@209:         Parent::nextOut(e);
deba@57:       }
deba@57:     }
deba@57: 
deba@57:     void firstIn(Arc &e, const Node &n) const {
deba@57:       Parent::firstOut(e,n);
deba@57:       if( Edge(e) != INVALID ) {
alpar@209:         e.forward = false;
deba@57:       }
deba@57:       else {
alpar@209:         Parent::firstIn(e,n);
alpar@209:         e.forward = true;
deba@57:       }
deba@57:     }
deba@57:     void nextIn(Arc &e) const {
deba@57:       if( ! e.forward ) {
alpar@209:         Node n = Parent::source(e);
alpar@209:         Parent::nextOut(e);
alpar@209:         if( Edge(e) == INVALID ) {
alpar@209:           Parent::firstIn(e, n);
alpar@209:           e.forward = true;
alpar@209:         }
deba@57:       }
deba@57:       else {
alpar@209:         Parent::nextIn(e);
deba@57:       }
deba@57:     }
deba@57: 
deba@57:     void firstInc(Edge &e, bool &d, const Node &n) const {
deba@57:       d = true;
deba@57:       Parent::firstOut(e, n);
deba@57:       if (e != INVALID) return;
deba@57:       d = false;
deba@57:       Parent::firstIn(e, n);
deba@57:     }
deba@57: 
deba@57:     void nextInc(Edge &e, bool &d) const {
deba@57:       if (d) {
alpar@209:         Node s = Parent::source(e);
alpar@209:         Parent::nextOut(e);
alpar@209:         if (e != INVALID) return;
alpar@209:         d = false;
alpar@209:         Parent::firstIn(e, s);
deba@57:       } else {
alpar@209:         Parent::nextIn(e);
deba@57:       }
deba@57:     }
deba@57: 
deba@57:     Node nodeFromId(int ix) const {
deba@57:       return Parent::nodeFromId(ix);
deba@57:     }
deba@57: 
deba@57:     Arc arcFromId(int ix) const {
deba@57:       return direct(Parent::arcFromId(ix >> 1), bool(ix & 1));
deba@57:     }
deba@57: 
deba@57:     Edge edgeFromId(int ix) const {
deba@57:       return Parent::arcFromId(ix);
deba@57:     }
deba@57: 
deba@57:     int id(const Node &n) const {
deba@57:       return Parent::id(n);
deba@57:     }
deba@57: 
deba@57:     int id(const Edge &e) const {
deba@57:       return Parent::id(e);
deba@57:     }
deba@57: 
deba@57:     int id(const Arc &e) const {
deba@57:       return 2 * Parent::id(e) + int(e.forward);
deba@57:     }
deba@57: 
deba@57:     int maxNodeId() const {
deba@57:       return Parent::maxNodeId();
deba@57:     }
deba@57: 
deba@57:     int maxArcId() const {
deba@57:       return 2 * Parent::maxArcId() + 1;
deba@57:     }
deba@57: 
deba@57:     int maxEdgeId() const {
deba@57:       return Parent::maxArcId();
deba@57:     }
deba@57: 
deba@57:     int arcNum() const {
deba@57:       return 2 * Parent::arcNum();
deba@57:     }
deba@57: 
deba@57:     int edgeNum() const {
deba@57:       return Parent::arcNum();
deba@57:     }
deba@57: 
deba@57:     Arc findArc(Node s, Node t, Arc p = INVALID) const {
deba@57:       if (p == INVALID) {
alpar@209:         Edge arc = Parent::findArc(s, t);
alpar@209:         if (arc != INVALID) return direct(arc, true);
alpar@209:         arc = Parent::findArc(t, s);
alpar@209:         if (arc != INVALID) return direct(arc, false);
deba@57:       } else if (direction(p)) {
alpar@209:         Edge arc = Parent::findArc(s, t, p);
alpar@209:         if (arc != INVALID) return direct(arc, true);
alpar@209:         arc = Parent::findArc(t, s);
alpar@209:         if (arc != INVALID) return direct(arc, false);
deba@57:       } else {
alpar@209:         Edge arc = Parent::findArc(t, s, p);
alpar@209:         if (arc != INVALID) return direct(arc, false);
deba@57:       }
deba@57:       return INVALID;
deba@57:     }
deba@57: 
deba@57:     Edge findEdge(Node s, Node t, Edge p = INVALID) const {
deba@57:       if (s != t) {
deba@57:         if (p == INVALID) {
deba@57:           Edge arc = Parent::findArc(s, t);
deba@57:           if (arc != INVALID) return arc;
deba@57:           arc = Parent::findArc(t, s);
deba@57:           if (arc != INVALID) return arc;
deba@57:         } else if (Parent::s(p) == s) {
deba@57:           Edge arc = Parent::findArc(s, t, p);
deba@57:           if (arc != INVALID) return arc;
deba@57:           arc = Parent::findArc(t, s);
alpar@209:           if (arc != INVALID) return arc;
deba@57:         } else {
deba@57:           Edge arc = Parent::findArc(t, s, p);
alpar@209:           if (arc != INVALID) return arc;
deba@57:         }
deba@57:       } else {
deba@57:         return Parent::findArc(s, t, p);
deba@57:       }
deba@57:       return INVALID;
deba@57:     }
deba@57:   };
deba@57: 
deba@57:   template <typename Base>
deba@57:   class BidirBpGraphExtender : public Base {
deba@57:   public:
deba@57:     typedef Base Parent;
deba@57:     typedef BidirBpGraphExtender Digraph;
deba@57: 
deba@57:     typedef typename Parent::Node Node;
deba@57:     typedef typename Parent::Edge Edge;
deba@57: 
deba@57: 
deba@57:     using Parent::first;
deba@57:     using Parent::next;
deba@57: 
deba@57:     using Parent::id;
deba@57: 
deba@57:     class Red : public Node {
deba@57:       friend class BidirBpGraphExtender;
deba@57:     public:
deba@57:       Red() {}
deba@57:       Red(const Node& node) : Node(node) {
kpeter@289:         LEMON_DEBUG(Parent::red(node) || node == INVALID,
kpeter@289:                     typename Parent::NodeSetError());
deba@57:       }
deba@57:       Red& operator=(const Node& node) {
kpeter@289:         LEMON_DEBUG(Parent::red(node) || node == INVALID,
kpeter@289:                     typename Parent::NodeSetError());
deba@57:         Node::operator=(node);
deba@57:         return *this;
deba@57:       }
deba@57:       Red(Invalid) : Node(INVALID) {}
deba@57:       Red& operator=(Invalid) {
deba@57:         Node::operator=(INVALID);
deba@57:         return *this;
deba@57:       }
deba@57:     };
deba@57: 
deba@57:     void first(Red& node) const {
deba@57:       Parent::firstRed(static_cast<Node&>(node));
deba@57:     }
deba@57:     void next(Red& node) const {
deba@57:       Parent::nextRed(static_cast<Node&>(node));
deba@57:     }
deba@57: 
deba@57:     int id(const Red& node) const {
deba@57:       return Parent::redId(node);
deba@57:     }
deba@57: 
deba@57:     class Blue : public Node {
deba@57:       friend class BidirBpGraphExtender;
deba@57:     public:
deba@57:       Blue() {}
deba@57:       Blue(const Node& node) : Node(node) {
kpeter@289:         LEMON_DEBUG(Parent::blue(node) || node == INVALID,
kpeter@289:                     typename Parent::NodeSetError());
deba@57:       }
deba@57:       Blue& operator=(const Node& node) {
kpeter@289:         LEMON_DEBUG(Parent::blue(node) || node == INVALID,
kpeter@289:                     typename Parent::NodeSetError());
deba@57:         Node::operator=(node);
deba@57:         return *this;
deba@57:       }
deba@57:       Blue(Invalid) : Node(INVALID) {}
deba@57:       Blue& operator=(Invalid) {
deba@57:         Node::operator=(INVALID);
deba@57:         return *this;
deba@57:       }
deba@57:     };
deba@57: 
deba@57:     void first(Blue& node) const {
deba@57:       Parent::firstBlue(static_cast<Node&>(node));
deba@57:     }
deba@57:     void next(Blue& node) const {
deba@57:       Parent::nextBlue(static_cast<Node&>(node));
deba@57:     }
alpar@209: 
deba@57:     int id(const Blue& node) const {
deba@57:       return Parent::redId(node);
deba@57:     }
deba@57: 
deba@57:     Node source(const Edge& arc) const {
deba@57:       return red(arc);
deba@57:     }
deba@57:     Node target(const Edge& arc) const {
deba@57:       return blue(arc);
deba@57:     }
deba@57: 
deba@57:     void firstInc(Edge& arc, bool& dir, const Node& node) const {
deba@57:       if (Parent::red(node)) {
alpar@209:         Parent::firstFromRed(arc, node);
alpar@209:         dir = true;
deba@57:       } else {
alpar@209:         Parent::firstFromBlue(arc, node);
alpar@209:         dir = static_cast<Edge&>(arc) == INVALID;
deba@57:       }
deba@57:     }
deba@57:     void nextInc(Edge& arc, bool& dir) const {
deba@57:       if (dir) {
alpar@209:         Parent::nextFromRed(arc);
deba@57:       } else {
alpar@209:         Parent::nextFromBlue(arc);
alpar@209:         if (arc == INVALID) dir = true;
deba@57:       }
deba@57:     }
deba@57: 
deba@57:     class Arc : public Edge {
deba@57:       friend class BidirBpGraphExtender;
deba@57:     protected:
deba@57:       bool forward;
deba@57: 
deba@57:       Arc(const Edge& arc, bool _forward)
alpar@209:         : Edge(arc), forward(_forward) {}
deba@57: 
deba@57:     public:
deba@57:       Arc() {}
deba@57:       Arc (Invalid) : Edge(INVALID), forward(true) {}
deba@57:       bool operator==(const Arc& i) const {
alpar@209:         return Edge::operator==(i) && forward == i.forward;
deba@57:       }
deba@57:       bool operator!=(const Arc& i) const {
alpar@209:         return Edge::operator!=(i) || forward != i.forward;
deba@57:       }
deba@57:       bool operator<(const Arc& i) const {
alpar@209:         return Edge::operator<(i) ||
alpar@209:           (!(i.forward<forward) && Edge(*this)<Edge(i));
deba@57:       }
deba@57:     };
deba@57: 
deba@57:     void first(Arc& arc) const {
deba@57:       Parent::first(static_cast<Edge&>(arc));
deba@57:       arc.forward = true;
deba@57:     }
deba@57: 
deba@57:     void next(Arc& arc) const {
deba@57:       if (!arc.forward) {
alpar@209:         Parent::next(static_cast<Edge&>(arc));
deba@57:       }
deba@57:       arc.forward = !arc.forward;
deba@57:     }
deba@57: 
deba@57:     void firstOut(Arc& arc, const Node& node) const {
deba@57:       if (Parent::red(node)) {
alpar@209:         Parent::firstFromRed(arc, node);
alpar@209:         arc.forward = true;
deba@57:       } else {
alpar@209:         Parent::firstFromBlue(arc, node);
alpar@209:         arc.forward = static_cast<Edge&>(arc) == INVALID;
deba@57:       }
deba@57:     }
deba@57:     void nextOut(Arc& arc) const {
deba@57:       if (arc.forward) {
alpar@209:         Parent::nextFromRed(arc);
deba@57:       } else {
alpar@209:         Parent::nextFromBlue(arc);
deba@57:         arc.forward = static_cast<Edge&>(arc) == INVALID;
deba@57:       }
deba@57:     }
deba@57: 
deba@57:     void firstIn(Arc& arc, const Node& node) const {
deba@57:       if (Parent::blue(node)) {
alpar@209:         Parent::firstFromBlue(arc, node);
alpar@209:         arc.forward = true;
deba@57:       } else {
alpar@209:         Parent::firstFromRed(arc, node);
alpar@209:         arc.forward = static_cast<Edge&>(arc) == INVALID;
deba@57:       }
deba@57:     }
deba@57:     void nextIn(Arc& arc) const {
deba@57:       if (arc.forward) {
alpar@209:         Parent::nextFromBlue(arc);
deba@57:       } else {
alpar@209:         Parent::nextFromRed(arc);
alpar@209:         arc.forward = static_cast<Edge&>(arc) == INVALID;
deba@57:       }
deba@57:     }
deba@57: 
deba@57:     Node source(const Arc& arc) const {
deba@57:       return arc.forward ? Parent::red(arc) : Parent::blue(arc);
deba@57:     }
deba@57:     Node target(const Arc& arc) const {
deba@57:       return arc.forward ? Parent::blue(arc) : Parent::red(arc);
deba@57:     }
deba@57: 
deba@57:     int id(const Arc& arc) const {
alpar@209:       return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
deba@57:         (arc.forward ? 0 : 1);
deba@57:     }
deba@57:     Arc arcFromId(int ix) const {
deba@57:       return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
deba@57:     }
deba@57:     int maxArcId() const {
deba@57:       return (Parent::maxEdgeId() << 1) + 1;
deba@57:     }
deba@57: 
deba@57:     bool direction(const Arc& arc) const {
deba@57:       return arc.forward;
deba@57:     }
deba@57: 
deba@57:     Arc direct(const Edge& arc, bool dir) const {
deba@57:       return Arc(arc, dir);
deba@57:     }
deba@57: 
deba@57:     int arcNum() const {
deba@57:       return 2 * Parent::edgeNum();
deba@57:     }
deba@57: 
deba@57:     int edgeNum() const {
deba@57:       return Parent::edgeNum();
deba@57:     }
deba@57: 
deba@57: 
deba@57:   };
deba@57: }
deba@57: 
deba@57: #endif