[Lemon-commits] Balazs Dezso: Type safe red and blue node set (#69)

Lemon HG hg at lemon.cs.elte.hu
Fri Mar 1 17:49:47 CET 2013


details:   http://lemon.cs.elte.hu/hg/lemon/rev/c8fa41fcc4a7
changeset: 1193:c8fa41fcc4a7
user:      Balazs Dezso <deba [at] inf.elte.hu>
date:      Thu Dec 01 09:05:47 2011 +0100
description:
	Type safe red and blue node set (#69)

diffstat:

 lemon/bits/graph_extender.h       |  110 ++++++++++--------------
 lemon/concepts/bpgraph.h          |   85 +++++++++++-------
 lemon/concepts/graph_components.h |  163 ++++++++++++++++++++++---------------
 lemon/core.h                      |   99 +++++++++++++++++-----
 lemon/full_graph.h                |   75 +++++++++++------
 lemon/list_graph.h                |   86 +++++++++++++------
 lemon/smart_graph.h               |   80 ++++++++++++------
 test/bpgraph_test.cc              |   36 ++++---
 test/graph_copy_test.cc           |   60 +++++++++----
 test/graph_test.h                 |   30 +++++-
 10 files changed, 512 insertions(+), 312 deletions(-)

diffs (truncated from 1750 to 300 lines):

diff --git a/lemon/bits/graph_extender.h b/lemon/bits/graph_extender.h
--- a/lemon/bits/graph_extender.h
+++ b/lemon/bits/graph_extender.h
@@ -760,55 +760,17 @@
     typedef True UndirectedTag;
 
     typedef typename Parent::Node Node;
+    typedef typename Parent::RedNode RedNode;
+    typedef typename Parent::BlueNode BlueNode;
     typedef typename Parent::Arc Arc;
     typedef typename Parent::Edge Edge;
 
     // BpGraph extension
 
-    class RedNode : public Node {
-    public:
-      RedNode() {}
-      RedNode(const RedNode& node) : Node(node) {}
-      RedNode(Invalid) : Node(INVALID){}
-      RedNode(const Node& node) : Node(node) {}
-    };
-    class BlueNode : public Node {
-    public:
-      BlueNode() {}
-      BlueNode(const BlueNode& node) : Node(node) {}
-      BlueNode(Invalid) : Node(INVALID){}
-      BlueNode(const Node& node) : Node(node) {}
-    };
-
     using Parent::first;
     using Parent::next;
-
-    void first(RedNode& node) const {
-      Parent::firstRed(node);
-    }
-    
-    void next(RedNode& node) const {
-      Parent::nextRed(node);
-    }
-
-    void first(BlueNode& node) const {
-      Parent::firstBlue(node);
-    }
-
-    void next(BlueNode& node) const {
-      Parent::nextBlue(node);
-    }
-
     using Parent::id;
 
-    int id(const RedNode& node) const {
-      return Parent::redId(node);
-    }
-
-    int id(const BlueNode& node) const {
-      return Parent::blueId(node);
-    }
-
     int maxId(Node) const {
       return Parent::maxNodeId();
     }
@@ -862,6 +824,32 @@
       return Parent::direct(edge, Parent::redNode(edge) == node);
     }
 
+    RedNode asRedNode(const Node& node) const {
+      if (node == INVALID || Parent::blue(node)) {
+        return INVALID;
+      } else {
+        return Parent::asRedNodeUnsafe(node);
+      }
+    }
+
+    BlueNode asBlueNode(const Node& node) const {
+      if (node == INVALID || Parent::red(node)) {
+        return INVALID;
+      } else {
+        return Parent::asBlueNodeUnsafe(node);
+      }
+    }
+
+    std::pair<RedNode, BlueNode> asRedBlueNode(const Node& node) const {
+      if (node == INVALID) {
+        return std::make_pair(RedNode(INVALID), BlueNode(INVALID));
+      } else if (Parent::red(node)) {
+        return std::make_pair(Parent::asRedNodeUnsafe(node), BlueNode(INVALID));
+      } else {
+        return std::make_pair(RedNode(INVALID), Parent::asBlueNodeUnsafe(node));
+      }
+    }
+
     // Alterable extension
 
     typedef AlterationNotifier<BpGraphExtender, Node> NodeNotifier;
@@ -925,49 +913,45 @@
 
     };
 
-    class RedIt : public Node {
+    class RedIt : public RedNode {
       const BpGraph* _graph;
     public:
 
       RedIt() {}
 
-      RedIt(Invalid i) : Node(i) { }
+      RedIt(Invalid i) : RedNode(i) { }
 
       explicit RedIt(const BpGraph& graph) : _graph(&graph) {
-        _graph->firstRed(static_cast<Node&>(*this));
+        _graph->first(static_cast<RedNode&>(*this));
       }
 
-      RedIt(const BpGraph& graph, const Node& node)
-        : Node(node), _graph(&graph) {
-        LEMON_DEBUG(_graph->red(node), "Node has to be red.");
-      }
+      RedIt(const BpGraph& graph, const RedNode& node)
+        : RedNode(node), _graph(&graph) {}
 
       RedIt& operator++() {
-        _graph->nextRed(*this);
+        _graph->next(static_cast<RedNode&>(*this));
         return *this;
       }
 
     };
 
-    class BlueIt : public Node {
+    class BlueIt : public BlueNode {
       const BpGraph* _graph;
     public:
 
       BlueIt() {}
 
-      BlueIt(Invalid i) : Node(i) { }
+      BlueIt(Invalid i) : BlueNode(i) { }
 
       explicit BlueIt(const BpGraph& graph) : _graph(&graph) {
-        _graph->firstBlue(static_cast<Node&>(*this));
+        _graph->first(static_cast<BlueNode&>(*this));
       }
 
-      BlueIt(const BpGraph& graph, const Node& node)
-        : Node(node), _graph(&graph) {
-        LEMON_DEBUG(_graph->blue(node), "Node has to be blue.");
-      }
+      BlueIt(const BpGraph& graph, const BlueNode& node)
+        : BlueNode(node), _graph(&graph) {}
 
       BlueIt& operator++() {
-        _graph->nextBlue(*this);
+        _graph->next(static_cast<BlueNode&>(*this));
         return *this;
       }
 
@@ -1258,21 +1242,21 @@
 
     // Alteration extension
 
-    Node addRedNode() {
-      Node node = Parent::addRedNode();
+    RedNode addRedNode() {
+      RedNode node = Parent::addRedNode();
       notifier(RedNode()).add(node);
       notifier(Node()).add(node);
       return node;
     }
 
-    Node addBlueNode() {
-      Node node = Parent::addBlueNode();
+    BlueNode addBlueNode() {
+      BlueNode node = Parent::addBlueNode();
       notifier(BlueNode()).add(node);
       notifier(Node()).add(node);
       return node;
     }
 
-    Edge addEdge(const Node& from, const Node& to) {
+    Edge addEdge(const RedNode& from, const BlueNode& to) {
       Edge edge = Parent::addEdge(from, to);
       notifier(Edge()).add(edge);
       std::vector<Arc> av;
@@ -1317,9 +1301,9 @@
       }
 
       if (Parent::red(node)) {
-        notifier(RedNode()).erase(node);
+        notifier(RedNode()).erase(this->asRedNodeUnsafe(node));
       } else {
-        notifier(BlueNode()).erase(node);        
+        notifier(BlueNode()).erase(this->asBlueNodeUnsafe(node));
       }
 
       notifier(Node()).erase(node);
diff --git a/lemon/concepts/bpgraph.h b/lemon/concepts/bpgraph.h
--- a/lemon/concepts/bpgraph.h
+++ b/lemon/concepts/bpgraph.h
@@ -149,12 +149,6 @@
         /// \sa Invalid for more details.
         RedNode(Invalid) { }
 
-        /// Constructor for conversion from a node.
-
-        /// Constructor for conversion from a node. The conversion can
-        /// be invalid, since the Node can be member of the blue
-        /// set.
-        RedNode(const Node&) {}
       };
 
       /// Class to represent blue nodes.
@@ -182,12 +176,6 @@
         /// \sa Invalid for more details.
         BlueNode(Invalid) { }
 
-        /// Constructor for conversion from a node.
-
-        /// Constructor for conversion from a node. The conversion can
-        /// be invalid, since the Node can be member of the red
-        /// set.
-        BlueNode(const Node&) {}
       };
 
       /// Iterator class for the red nodes.
@@ -199,7 +187,7 @@
       /// int count=0;
       /// for (BpGraph::RedNodeIt n(g); n!=INVALID; ++n) ++count;
       ///\endcode
-      class RedIt : public Node {
+      class RedIt : public RedNode {
       public:
         /// Default constructor
 
@@ -210,7 +198,7 @@
 
         /// Copy constructor.
         ///
-        RedIt(const RedIt& n) : Node(n) { }
+        RedIt(const RedIt& n) : RedNode(n) { }
         /// %Invalid constructor \& conversion.
 
         /// Initializes the iterator to be invalid.
@@ -225,7 +213,7 @@
 
         /// Sets the iterator to the given red node of the given
         /// digraph.
-        RedIt(const BpGraph&, const Node&) { }
+        RedIt(const BpGraph&, const RedNode&) { }
         /// Next node.
 
         /// Assign the iterator to the next red node.
@@ -242,7 +230,7 @@
       /// int count=0;
       /// for (BpGraph::BlueNodeIt n(g); n!=INVALID; ++n) ++count;
       ///\endcode
-      class BlueIt : public Node {
+      class BlueIt : public BlueNode {
       public:
         /// Default constructor
 
@@ -253,7 +241,7 @@
 
         /// Copy constructor.
         ///
-        BlueIt(const BlueIt& n) : Node(n) { }
+        BlueIt(const BlueIt& n) : BlueNode(n) { }
         /// %Invalid constructor \& conversion.
 
         /// Initializes the iterator to be invalid.
@@ -268,7 +256,7 @@
 
         /// Sets the iterator to the given blue node of the given
         /// digraph.
-        BlueIt(const BpGraph&, const Node&) { }
+        BlueIt(const BpGraph&, const BlueNode&) { }
         /// Next node.
 
         /// Assign the iterator to the next blue node.
@@ -784,15 +772,54 @@
       /// Gives back %true for blue nodes.
       bool blue(const Node&) const { return true; }
 
+      /// \brief Converts the node to red node object.
+      ///
+      /// This class is converts unsafely the node to red node
+      /// object. It should be called only if the node is from the red
+      /// partition or INVALID.
+      RedNode asRedNodeUnsafe(const Node&) const { return RedNode(); }
+
+      /// \brief Converts the node to blue node object.
+      ///
+      /// This class is converts unsafely the node to blue node
+      /// object. It should be called only if the node is from the red
+      /// partition or INVALID.
+      BlueNode asBlueNodeUnsafe(const Node&) const { return BlueNode(); }
+
+      /// \brief Converts the node to red node object.
+      ///
+      /// This class is converts safely the node to red node
+      /// object. If the node is not from the red partition, then it



More information about the Lemon-commits mailing list