# HG changeset patch # User Peter Kovacs # Date 1251201523 -7200 # Node ID f4b5c2d5449d41268f349c0e86ca295896dbde4a # Parent cf360f758f25b4832d48984c9c8fbb2b21602df8 Small improvements + add tests for StaticDigraph (#68) diff -r cf360f758f25 -r f4b5c2d5449d lemon/static_graph.h --- a/lemon/static_graph.h Tue Aug 25 11:09:02 2009 +0200 +++ b/lemon/static_graph.h Tue Aug 25 13:58:43 2009 +0200 @@ -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 - 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 + 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 + 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(arc)); + } + + Node runningNode(const OutArcIt &arc) const { + return Parent::target(static_cast(arc)); + } + + Node baseNode(const InArcIt &arc) const { + return Parent::target(static_cast(arc)); + } + + Node runningNode(const InArcIt &arc) const { + return Parent::source(static_cast(arc)); + } + }; } diff -r cf360f758f25 -r f4b5c2d5449d test/digraph_test.cc --- a/test/digraph_test.cc Tue Aug 25 11:09:02 2009 +0200 +++ b/test/digraph_test.cc Tue Aug 25 13:58:43 2009 +0200 @@ -19,6 +19,7 @@ #include #include #include +#include #include #include "test_tools.h" @@ -317,6 +318,10 @@ checkConcept, SmartDigraph>(); checkConcept, SmartDigraph>(); } + { // Checking StaticDigraph + checkConcept(); + checkConcept, StaticDigraph>(); + } { // Checking FullDigraph checkConcept(); } @@ -372,6 +377,76 @@ check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); } +void checkStaticDigraph() { + SmartDigraph g; + SmartDigraph::NodeMap nref(g); + SmartDigraph::ArcMap 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(); checkDigraphValidity(); } + { // Checking StaticDigraph + checkStaticDigraph(); + } { // Checking FullDigraph checkFullDigraph(8); }