1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2008
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9  * Permission to use, modify and distribute this software is granted
 
    10  * provided that this copyright notice appears in all copies. For
 
    11  * precise terms see the accompanying LICENSE file.
 
    13  * This software is provided "AS IS" with no warranty of any kind,
 
    14  * express or implied, and with no claim as to its suitability for any
 
    19 #ifndef LEMON_BITS_BASE_EXTENDER_H
 
    20 #define LEMON_BITS_BASE_EXTENDER_H
 
    22 #include <lemon/core.h>
 
    23 #include <lemon/error.h>
 
    25 #include <lemon/bits/map_extender.h>
 
    26 #include <lemon/bits/default_map.h>
 
    28 #include <lemon/concept_check.h>
 
    29 #include <lemon/concepts/maps.h>
 
    31 ///\ingroup digraphbits
 
    33 ///\brief Extenders for the digraph types
 
    36   /// \ingroup digraphbits
 
    38   /// \brief BaseDigraph to BaseGraph extender
 
    39   template <typename Base>
 
    40   class UndirDigraphExtender : public Base {
 
    45     typedef typename Parent::Arc Edge;
 
    46     typedef typename Parent::Node Node;
 
    48     typedef True UndirectedTag;
 
    50     class Arc : public Edge {
 
    51       friend class UndirDigraphExtender;
 
    56       Arc(const Edge &ue, bool _forward) :
 
    57         Edge(ue), forward(_forward) {}
 
    62       // Invalid arc constructor
 
    63       Arc(Invalid i) : Edge(i), forward(true) {}
 
    65       bool operator==(const Arc &that) const {
 
    66         return forward==that.forward && Edge(*this)==Edge(that);
 
    68       bool operator!=(const Arc &that) const {
 
    69         return forward!=that.forward || Edge(*this)!=Edge(that);
 
    71       bool operator<(const Arc &that) const {
 
    72         return forward<that.forward ||
 
    73           (!(that.forward<forward) && Edge(*this)<Edge(that));
 
    77     /// First node of the edge
 
    78     Node u(const Edge &e) const {
 
    79       return Parent::source(e);
 
    82     /// Source of the given arc
 
    83     Node source(const Arc &e) const {
 
    84       return e.forward ? Parent::source(e) : Parent::target(e);
 
    87     /// Second node of the edge
 
    88     Node v(const Edge &e) const {
 
    89       return Parent::target(e);
 
    92     /// Target of the given arc
 
    93     Node target(const Arc &e) const {
 
    94       return e.forward ? Parent::target(e) : Parent::source(e);
 
    97     /// \brief Directed arc from an edge.
 
    99     /// Returns a directed arc corresponding to the specified edge.
 
   100     /// If the given bool is true, the first node of the given edge and
 
   101     /// the source node of the returned arc are the same.
 
   102     static Arc direct(const Edge &e, bool d) {
 
   106     /// Returns whether the given directed arc has the same orientation
 
   107     /// as the corresponding edge.
 
   109     /// \todo reference to the corresponding point of the undirected digraph
 
   110     /// concept. "What does the direction of an edge mean?"
 
   111     static bool direction(const Arc &a) { return a.forward; }
 
   116     void first(Arc &e) const {
 
   121     void next(Arc &e) const {
 
   131     void firstOut(Arc &e, const Node &n) const {
 
   132       Parent::firstIn(e,n);
 
   133       if( Edge(e) != INVALID ) {
 
   137         Parent::firstOut(e,n);
 
   141     void nextOut(Arc &e) const {
 
   143         Node n = Parent::target(e);
 
   145         if( Edge(e) == INVALID ) {
 
   146           Parent::firstOut(e, n);
 
   155     void firstIn(Arc &e, const Node &n) const {
 
   156       Parent::firstOut(e,n);
 
   157       if( Edge(e) != INVALID ) {
 
   161         Parent::firstIn(e,n);
 
   165     void nextIn(Arc &e) const {
 
   167         Node n = Parent::source(e);
 
   169         if( Edge(e) == INVALID ) {
 
   170           Parent::firstIn(e, n);
 
   179     void firstInc(Edge &e, bool &d, const Node &n) const {
 
   181       Parent::firstOut(e, n);
 
   182       if (e != INVALID) return;
 
   184       Parent::firstIn(e, n);
 
   187     void nextInc(Edge &e, bool &d) const {
 
   189         Node s = Parent::source(e);
 
   191         if (e != INVALID) return;
 
   193         Parent::firstIn(e, s);
 
   199     Node nodeFromId(int ix) const {
 
   200       return Parent::nodeFromId(ix);
 
   203     Arc arcFromId(int ix) const {
 
   204       return direct(Parent::arcFromId(ix >> 1), bool(ix & 1));
 
   207     Edge edgeFromId(int ix) const {
 
   208       return Parent::arcFromId(ix);
 
   211     int id(const Node &n) const {
 
   212       return Parent::id(n);
 
   215     int id(const Edge &e) const {
 
   216       return Parent::id(e);
 
   219     int id(const Arc &e) const {
 
   220       return 2 * Parent::id(e) + int(e.forward);
 
   223     int maxNodeId() const {
 
   224       return Parent::maxNodeId();
 
   227     int maxArcId() const {
 
   228       return 2 * Parent::maxArcId() + 1;
 
   231     int maxEdgeId() const {
 
   232       return Parent::maxArcId();
 
   236       return 2 * Parent::arcNum();
 
   239     int edgeNum() const {
 
   240       return Parent::arcNum();
 
   243     Arc findArc(Node s, Node t, Arc p = INVALID) const {
 
   245         Edge arc = Parent::findArc(s, t);
 
   246         if (arc != INVALID) return direct(arc, true);
 
   247         arc = Parent::findArc(t, s);
 
   248         if (arc != INVALID) return direct(arc, false);
 
   249       } else if (direction(p)) {
 
   250         Edge arc = Parent::findArc(s, t, p);
 
   251         if (arc != INVALID) return direct(arc, true);
 
   252         arc = Parent::findArc(t, s);
 
   253         if (arc != INVALID) return direct(arc, false);
 
   255         Edge arc = Parent::findArc(t, s, p);
 
   256         if (arc != INVALID) return direct(arc, false);
 
   261     Edge findEdge(Node s, Node t, Edge p = INVALID) const {
 
   264           Edge arc = Parent::findArc(s, t);
 
   265           if (arc != INVALID) return arc;
 
   266           arc = Parent::findArc(t, s);
 
   267           if (arc != INVALID) return arc;
 
   268         } else if (Parent::s(p) == s) {
 
   269           Edge arc = Parent::findArc(s, t, p);
 
   270           if (arc != INVALID) return arc;
 
   271           arc = Parent::findArc(t, s);
 
   272           if (arc != INVALID) return arc;
 
   274           Edge arc = Parent::findArc(t, s, p);
 
   275           if (arc != INVALID) return arc;
 
   278         return Parent::findArc(s, t, p);
 
   284   template <typename Base>
 
   285   class BidirBpGraphExtender : public Base {
 
   288     typedef BidirBpGraphExtender Digraph;
 
   290     typedef typename Parent::Node Node;
 
   291     typedef typename Parent::Edge Edge;
 
   299     class Red : public Node {
 
   300       friend class BidirBpGraphExtender;
 
   303       Red(const Node& node) : Node(node) {
 
   304         LEMON_ASSERT(Parent::red(node) || node == INVALID,
 
   305                      typename Parent::NodeSetError());
 
   307       Red& operator=(const Node& node) {
 
   308         LEMON_ASSERT(Parent::red(node) || node == INVALID,
 
   309                      typename Parent::NodeSetError());
 
   310         Node::operator=(node);
 
   313       Red(Invalid) : Node(INVALID) {}
 
   314       Red& operator=(Invalid) {
 
   315         Node::operator=(INVALID);
 
   320     void first(Red& node) const {
 
   321       Parent::firstRed(static_cast<Node&>(node));
 
   323     void next(Red& node) const {
 
   324       Parent::nextRed(static_cast<Node&>(node));
 
   327     int id(const Red& node) const {
 
   328       return Parent::redId(node);
 
   331     class Blue : public Node {
 
   332       friend class BidirBpGraphExtender;
 
   335       Blue(const Node& node) : Node(node) {
 
   336         LEMON_ASSERT(Parent::blue(node) || node == INVALID,
 
   337                      typename Parent::NodeSetError());
 
   339       Blue& operator=(const Node& node) {
 
   340         LEMON_ASSERT(Parent::blue(node) || node == INVALID,
 
   341                      typename Parent::NodeSetError());
 
   342         Node::operator=(node);
 
   345       Blue(Invalid) : Node(INVALID) {}
 
   346       Blue& operator=(Invalid) {
 
   347         Node::operator=(INVALID);
 
   352     void first(Blue& node) const {
 
   353       Parent::firstBlue(static_cast<Node&>(node));
 
   355     void next(Blue& node) const {
 
   356       Parent::nextBlue(static_cast<Node&>(node));
 
   359     int id(const Blue& node) const {
 
   360       return Parent::redId(node);
 
   363     Node source(const Edge& arc) const {
 
   366     Node target(const Edge& arc) const {
 
   370     void firstInc(Edge& arc, bool& dir, const Node& node) const {
 
   371       if (Parent::red(node)) {
 
   372         Parent::firstFromRed(arc, node);
 
   375         Parent::firstFromBlue(arc, node);
 
   376         dir = static_cast<Edge&>(arc) == INVALID;
 
   379     void nextInc(Edge& arc, bool& dir) const {
 
   381         Parent::nextFromRed(arc);
 
   383         Parent::nextFromBlue(arc);
 
   384         if (arc == INVALID) dir = true;
 
   388     class Arc : public Edge {
 
   389       friend class BidirBpGraphExtender;
 
   393       Arc(const Edge& arc, bool _forward)
 
   394         : Edge(arc), forward(_forward) {}
 
   398       Arc (Invalid) : Edge(INVALID), forward(true) {}
 
   399       bool operator==(const Arc& i) const {
 
   400         return Edge::operator==(i) && forward == i.forward;
 
   402       bool operator!=(const Arc& i) const {
 
   403         return Edge::operator!=(i) || forward != i.forward;
 
   405       bool operator<(const Arc& i) const {
 
   406         return Edge::operator<(i) ||
 
   407           (!(i.forward<forward) && Edge(*this)<Edge(i));
 
   411     void first(Arc& arc) const {
 
   412       Parent::first(static_cast<Edge&>(arc));
 
   416     void next(Arc& arc) const {
 
   418         Parent::next(static_cast<Edge&>(arc));
 
   420       arc.forward = !arc.forward;
 
   423     void firstOut(Arc& arc, const Node& node) const {
 
   424       if (Parent::red(node)) {
 
   425         Parent::firstFromRed(arc, node);
 
   428         Parent::firstFromBlue(arc, node);
 
   429         arc.forward = static_cast<Edge&>(arc) == INVALID;
 
   432     void nextOut(Arc& arc) const {
 
   434         Parent::nextFromRed(arc);
 
   436         Parent::nextFromBlue(arc);
 
   437         arc.forward = static_cast<Edge&>(arc) == INVALID;
 
   441     void firstIn(Arc& arc, const Node& node) const {
 
   442       if (Parent::blue(node)) {
 
   443         Parent::firstFromBlue(arc, node);
 
   446         Parent::firstFromRed(arc, node);
 
   447         arc.forward = static_cast<Edge&>(arc) == INVALID;
 
   450     void nextIn(Arc& arc) const {
 
   452         Parent::nextFromBlue(arc);
 
   454         Parent::nextFromRed(arc);
 
   455         arc.forward = static_cast<Edge&>(arc) == INVALID;
 
   459     Node source(const Arc& arc) const {
 
   460       return arc.forward ? Parent::red(arc) : Parent::blue(arc);
 
   462     Node target(const Arc& arc) const {
 
   463       return arc.forward ? Parent::blue(arc) : Parent::red(arc);
 
   466     int id(const Arc& arc) const {
 
   467       return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
 
   468         (arc.forward ? 0 : 1);
 
   470     Arc arcFromId(int ix) const {
 
   471       return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
 
   473     int maxArcId() const {
 
   474       return (Parent::maxEdgeId() << 1) + 1;
 
   477     bool direction(const Arc& arc) const {
 
   481     Arc direct(const Edge& arc, bool dir) const {
 
   482       return Arc(arc, dir);
 
   486       return 2 * Parent::edgeNum();
 
   489     int edgeNum() const {
 
   490       return Parent::edgeNum();