[Lemon-commits] Peter Kovacs: Small improvements + add tests for...

Lemon HG hg at lemon.cs.elte.hu
Thu Nov 5 10:24:28 CET 2009


details:   http://lemon.cs.elte.hu/hg/lemon/rev/f4b5c2d5449d
changeset: 834:f4b5c2d5449d
user:      Peter Kovacs <kpeter [at] inf.elte.hu>
date:      Tue Aug 25 13:58:43 2009 +0200
description:
	Small improvements + add tests for StaticDigraph (#68)

diffstat:

 lemon/static_graph.h |  48 ++++++++++++++++++++----
 test/digraph_test.cc |  78 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 118 insertions(+), 8 deletions(-)

diffs (210 lines):

diff --git a/lemon/static_graph.h b/lemon/static_graph.h
--- a/lemon/static_graph.h
+++ b/lemon/static_graph.h
@@ -32,13 +32,13 @@
   public:
 
     StaticDigraphBase() 
-      : node_num(-1), arc_num(0), 
+      : built(false), node_num(0), arc_num(0), 
         node_first_out(NULL), node_first_in(NULL),
         arc_source(NULL), arc_target(NULL), 
         arc_next_in(NULL), arc_next_out(NULL) {}
     
     ~StaticDigraphBase() {
-      if (node_num != -1) {
+      if (built) {
         delete[] node_first_out;
         delete[] node_first_in;
         delete[] arc_source;
@@ -128,10 +128,8 @@
 
     typedef True BuildTag;
     
-    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
-    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
-
-      if (node_num != -1) {
+    void clear() {
+      if (built) {
         delete[] node_first_out;
         delete[] node_first_in;
         delete[] arc_source;
@@ -139,10 +137,18 @@
         delete[] arc_next_out;
         delete[] arc_next_in;
       }
-
+      built = false;
+      node_num = 0;
+      arc_num = 0;
+    }
+    
+    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
+    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
       typedef typename Digraph::Node GNode;
       typedef typename Digraph::Arc GArc;
 
+      built = true;
+
       node_num = countNodes(digraph);
       arc_num = countArcs(digraph);
 
@@ -205,7 +211,8 @@
       e.id = node_first_out[n.id + 1];
     }
 
-  private:
+  protected:
+    bool built;
     int node_num;
     int arc_num;
     int *node_first_out;
@@ -223,6 +230,15 @@
   public:
 
     typedef ExtendedStaticDigraphBase Parent;
+  
+  public:
+  
+    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
+    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
+      if (built) Parent::clear();
+      Parent::build(digraph, nodeRef, arcRef);
+    }
+  
 
   protected:
 
@@ -261,6 +277,22 @@
       Arc last;
     };
 
+    Node baseNode(const OutArcIt &arc) const {
+      return Parent::source(static_cast<const Arc&>(arc));
+    }
+
+    Node runningNode(const OutArcIt &arc) const {
+      return Parent::target(static_cast<const Arc&>(arc));
+    }
+
+    Node baseNode(const InArcIt &arc) const {
+      return Parent::target(static_cast<const Arc&>(arc));
+    }
+
+    Node runningNode(const InArcIt &arc) const {
+      return Parent::source(static_cast<const Arc&>(arc));
+    }
+
   };
 
 }
diff --git a/test/digraph_test.cc b/test/digraph_test.cc
--- a/test/digraph_test.cc
+++ b/test/digraph_test.cc
@@ -19,6 +19,7 @@
 #include <lemon/concepts/digraph.h>
 #include <lemon/list_graph.h>
 #include <lemon/smart_graph.h>
+#include <lemon/static_graph.h>
 #include <lemon/full_graph.h>
 
 #include "test_tools.h"
@@ -317,6 +318,10 @@
     checkConcept<ExtendableDigraphComponent<>, SmartDigraph>();
     checkConcept<ClearableDigraphComponent<>, SmartDigraph>();
   }
+  { // Checking StaticDigraph
+    checkConcept<Digraph, StaticDigraph>();
+    checkConcept<ClearableDigraphComponent<>, StaticDigraph>();
+  }
   { // Checking FullDigraph
     checkConcept<Digraph, FullDigraph>();
   }
@@ -372,6 +377,76 @@
   check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
 }
 
+void checkStaticDigraph() {
+  SmartDigraph g;
+  SmartDigraph::NodeMap<StaticDigraph::Node> nref(g);
+  SmartDigraph::ArcMap<StaticDigraph::Arc> aref(g);
+  
+  StaticDigraph G;
+  
+  checkGraphNodeList(G, 0);
+  checkGraphArcList(G, 0);
+
+  G.build(g, nref, aref);
+
+  checkGraphNodeList(G, 0);
+  checkGraphArcList(G, 0);
+
+  SmartDigraph::Node
+    n1 = g.addNode(),
+    n2 = g.addNode(),
+    n3 = g.addNode();
+
+  G.build(g, nref, aref);
+
+  checkGraphNodeList(G, 3);
+  checkGraphArcList(G, 0);
+
+  SmartDigraph::Arc a1 = g.addArc(n1, n2);
+
+  G.build(g, nref, aref);
+
+  check(G.source(aref[a1]) == nref[n1] && G.target(aref[a1]) == nref[n2],
+        "Wrong arc or wrong references");
+  checkGraphNodeList(G, 3);
+  checkGraphArcList(G, 1);
+
+  checkGraphOutArcList(G, nref[n1], 1);
+  checkGraphOutArcList(G, nref[n2], 0);
+  checkGraphOutArcList(G, nref[n3], 0);
+
+  checkGraphInArcList(G, nref[n1], 0);
+  checkGraphInArcList(G, nref[n2], 1);
+  checkGraphInArcList(G, nref[n3], 0);
+
+  checkGraphConArcList(G, 1);
+
+  SmartDigraph::Arc
+    a2 = g.addArc(n2, n1),
+    a3 = g.addArc(n2, n3),
+    a4 = g.addArc(n2, n3);
+
+  digraphCopy(g, G).nodeRef(nref).run();
+
+  checkGraphNodeList(G, 3);
+  checkGraphArcList(G, 4);
+
+  checkGraphOutArcList(G, nref[n1], 1);
+  checkGraphOutArcList(G, nref[n2], 3);
+  checkGraphOutArcList(G, nref[n3], 0);
+
+  checkGraphInArcList(G, nref[n1], 1);
+  checkGraphInArcList(G, nref[n2], 1);
+  checkGraphInArcList(G, nref[n3], 2);
+
+  checkGraphConArcList(G, 4);
+
+  checkNodeIds(G);
+  checkArcIds(G);
+  checkGraphNodeMap(G);
+  checkGraphArcMap(G);
+}
+
 void checkFullDigraph(int num) {
   typedef FullDigraph Digraph;
   DIGRAPH_TYPEDEFS(Digraph);
@@ -419,6 +494,9 @@
     checkDigraphSnapshot<SmartDigraph>();
     checkDigraphValidity<SmartDigraph>();
   }
+  { // Checking StaticDigraph
+    checkStaticDigraph();
+  }
   { // Checking FullDigraph
     checkFullDigraph(8);
   }



More information about the Lemon-commits mailing list