lemon/bits/graph_extender.h
changeset 1025 c8fa41fcc4a7
parent 1020 5ef0ab7b61cd
child 1026 699c7eac2c6d
     1.1 --- a/lemon/bits/graph_extender.h	Thu Nov 25 22:45:29 2010 +0100
     1.2 +++ b/lemon/bits/graph_extender.h	Thu Dec 01 09:05:47 2011 +0100
     1.3 @@ -760,55 +760,17 @@
     1.4      typedef True UndirectedTag;
     1.5  
     1.6      typedef typename Parent::Node Node;
     1.7 +    typedef typename Parent::RedNode RedNode;
     1.8 +    typedef typename Parent::BlueNode BlueNode;
     1.9      typedef typename Parent::Arc Arc;
    1.10      typedef typename Parent::Edge Edge;
    1.11  
    1.12      // BpGraph extension
    1.13  
    1.14 -    class RedNode : public Node {
    1.15 -    public:
    1.16 -      RedNode() {}
    1.17 -      RedNode(const RedNode& node) : Node(node) {}
    1.18 -      RedNode(Invalid) : Node(INVALID){}
    1.19 -      RedNode(const Node& node) : Node(node) {}
    1.20 -    };
    1.21 -    class BlueNode : public Node {
    1.22 -    public:
    1.23 -      BlueNode() {}
    1.24 -      BlueNode(const BlueNode& node) : Node(node) {}
    1.25 -      BlueNode(Invalid) : Node(INVALID){}
    1.26 -      BlueNode(const Node& node) : Node(node) {}
    1.27 -    };
    1.28 -
    1.29      using Parent::first;
    1.30      using Parent::next;
    1.31 -
    1.32 -    void first(RedNode& node) const {
    1.33 -      Parent::firstRed(node);
    1.34 -    }
    1.35 -    
    1.36 -    void next(RedNode& node) const {
    1.37 -      Parent::nextRed(node);
    1.38 -    }
    1.39 -
    1.40 -    void first(BlueNode& node) const {
    1.41 -      Parent::firstBlue(node);
    1.42 -    }
    1.43 -
    1.44 -    void next(BlueNode& node) const {
    1.45 -      Parent::nextBlue(node);
    1.46 -    }
    1.47 -
    1.48      using Parent::id;
    1.49  
    1.50 -    int id(const RedNode& node) const {
    1.51 -      return Parent::redId(node);
    1.52 -    }
    1.53 -
    1.54 -    int id(const BlueNode& node) const {
    1.55 -      return Parent::blueId(node);
    1.56 -    }
    1.57 -
    1.58      int maxId(Node) const {
    1.59        return Parent::maxNodeId();
    1.60      }
    1.61 @@ -862,6 +824,32 @@
    1.62        return Parent::direct(edge, Parent::redNode(edge) == node);
    1.63      }
    1.64  
    1.65 +    RedNode asRedNode(const Node& node) const {
    1.66 +      if (node == INVALID || Parent::blue(node)) {
    1.67 +        return INVALID;
    1.68 +      } else {
    1.69 +        return Parent::asRedNodeUnsafe(node);
    1.70 +      }
    1.71 +    }
    1.72 +
    1.73 +    BlueNode asBlueNode(const Node& node) const {
    1.74 +      if (node == INVALID || Parent::red(node)) {
    1.75 +        return INVALID;
    1.76 +      } else {
    1.77 +        return Parent::asBlueNodeUnsafe(node);
    1.78 +      }
    1.79 +    }
    1.80 +
    1.81 +    std::pair<RedNode, BlueNode> asRedBlueNode(const Node& node) const {
    1.82 +      if (node == INVALID) {
    1.83 +        return std::make_pair(RedNode(INVALID), BlueNode(INVALID));
    1.84 +      } else if (Parent::red(node)) {
    1.85 +        return std::make_pair(Parent::asRedNodeUnsafe(node), BlueNode(INVALID));
    1.86 +      } else {
    1.87 +        return std::make_pair(RedNode(INVALID), Parent::asBlueNodeUnsafe(node));
    1.88 +      }
    1.89 +    }
    1.90 +
    1.91      // Alterable extension
    1.92  
    1.93      typedef AlterationNotifier<BpGraphExtender, Node> NodeNotifier;
    1.94 @@ -925,49 +913,45 @@
    1.95  
    1.96      };
    1.97  
    1.98 -    class RedIt : public Node {
    1.99 +    class RedIt : public RedNode {
   1.100        const BpGraph* _graph;
   1.101      public:
   1.102  
   1.103        RedIt() {}
   1.104  
   1.105 -      RedIt(Invalid i) : Node(i) { }
   1.106 +      RedIt(Invalid i) : RedNode(i) { }
   1.107  
   1.108        explicit RedIt(const BpGraph& graph) : _graph(&graph) {
   1.109 -        _graph->firstRed(static_cast<Node&>(*this));
   1.110 +        _graph->first(static_cast<RedNode&>(*this));
   1.111        }
   1.112  
   1.113 -      RedIt(const BpGraph& graph, const Node& node)
   1.114 -        : Node(node), _graph(&graph) {
   1.115 -        LEMON_DEBUG(_graph->red(node), "Node has to be red.");
   1.116 -      }
   1.117 +      RedIt(const BpGraph& graph, const RedNode& node)
   1.118 +        : RedNode(node), _graph(&graph) {}
   1.119  
   1.120        RedIt& operator++() {
   1.121 -        _graph->nextRed(*this);
   1.122 +        _graph->next(static_cast<RedNode&>(*this));
   1.123          return *this;
   1.124        }
   1.125  
   1.126      };
   1.127  
   1.128 -    class BlueIt : public Node {
   1.129 +    class BlueIt : public BlueNode {
   1.130        const BpGraph* _graph;
   1.131      public:
   1.132  
   1.133        BlueIt() {}
   1.134  
   1.135 -      BlueIt(Invalid i) : Node(i) { }
   1.136 +      BlueIt(Invalid i) : BlueNode(i) { }
   1.137  
   1.138        explicit BlueIt(const BpGraph& graph) : _graph(&graph) {
   1.139 -        _graph->firstBlue(static_cast<Node&>(*this));
   1.140 +        _graph->first(static_cast<BlueNode&>(*this));
   1.141        }
   1.142  
   1.143 -      BlueIt(const BpGraph& graph, const Node& node)
   1.144 -        : Node(node), _graph(&graph) {
   1.145 -        LEMON_DEBUG(_graph->blue(node), "Node has to be blue.");
   1.146 -      }
   1.147 +      BlueIt(const BpGraph& graph, const BlueNode& node)
   1.148 +        : BlueNode(node), _graph(&graph) {}
   1.149  
   1.150        BlueIt& operator++() {
   1.151 -        _graph->nextBlue(*this);
   1.152 +        _graph->next(static_cast<BlueNode&>(*this));
   1.153          return *this;
   1.154        }
   1.155  
   1.156 @@ -1258,21 +1242,21 @@
   1.157  
   1.158      // Alteration extension
   1.159  
   1.160 -    Node addRedNode() {
   1.161 -      Node node = Parent::addRedNode();
   1.162 +    RedNode addRedNode() {
   1.163 +      RedNode node = Parent::addRedNode();
   1.164        notifier(RedNode()).add(node);
   1.165        notifier(Node()).add(node);
   1.166        return node;
   1.167      }
   1.168  
   1.169 -    Node addBlueNode() {
   1.170 -      Node node = Parent::addBlueNode();
   1.171 +    BlueNode addBlueNode() {
   1.172 +      BlueNode node = Parent::addBlueNode();
   1.173        notifier(BlueNode()).add(node);
   1.174        notifier(Node()).add(node);
   1.175        return node;
   1.176      }
   1.177  
   1.178 -    Edge addEdge(const Node& from, const Node& to) {
   1.179 +    Edge addEdge(const RedNode& from, const BlueNode& to) {
   1.180        Edge edge = Parent::addEdge(from, to);
   1.181        notifier(Edge()).add(edge);
   1.182        std::vector<Arc> av;
   1.183 @@ -1317,9 +1301,9 @@
   1.184        }
   1.185  
   1.186        if (Parent::red(node)) {
   1.187 -        notifier(RedNode()).erase(node);
   1.188 +        notifier(RedNode()).erase(this->asRedNodeUnsafe(node));
   1.189        } else {
   1.190 -        notifier(BlueNode()).erase(node);        
   1.191 +        notifier(BlueNode()).erase(this->asBlueNodeUnsafe(node));
   1.192        }
   1.193  
   1.194        notifier(Node()).erase(node);