COIN-OR::LEMON - Graph Library

Ticket #68: 68-2-f4b5c2d5449d.patch

File 68-2-f4b5c2d5449d.patch, 5.4 KB (added by Peter Kovacs, 15 years ago)
  • lemon/static_graph.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1251201523 -7200
    # Node ID f4b5c2d5449d41268f349c0e86ca295896dbde4a
    # Parent  cf360f758f25b4832d48984c9c8fbb2b21602df8
    Small improvements + add tests for StaticDigraph (#68)
    
    diff --git a/lemon/static_graph.h b/lemon/static_graph.h
    a b  
    3232  public:
    3333
    3434    StaticDigraphBase()
    35       : node_num(-1), arc_num(0),
     35      : built(false), node_num(0), arc_num(0),
    3636        node_first_out(NULL), node_first_in(NULL),
    3737        arc_source(NULL), arc_target(NULL),
    3838        arc_next_in(NULL), arc_next_out(NULL) {}
    3939   
    4040    ~StaticDigraphBase() {
    41       if (node_num != -1) {
     41      if (built) {
    4242        delete[] node_first_out;
    4343        delete[] node_first_in;
    4444        delete[] arc_source;
     
    128128
    129129    typedef True BuildTag;
    130130   
    131     template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
    132     void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
    133 
    134       if (node_num != -1) {
     131    void clear() {
     132      if (built) {
    135133        delete[] node_first_out;
    136134        delete[] node_first_in;
    137135        delete[] arc_source;
     
    139137        delete[] arc_next_out;
    140138        delete[] arc_next_in;
    141139      }
    142 
     140      built = false;
     141      node_num = 0;
     142      arc_num = 0;
     143    }
     144   
     145    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
     146    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
    143147      typedef typename Digraph::Node GNode;
    144148      typedef typename Digraph::Arc GArc;
    145149
     150      built = true;
     151
    146152      node_num = countNodes(digraph);
    147153      arc_num = countArcs(digraph);
    148154
     
    205211      e.id = node_first_out[n.id + 1];
    206212    }
    207213
    208   private:
     214  protected:
     215    bool built;
    209216    int node_num;
    210217    int arc_num;
    211218    int *node_first_out;
     
    223230  public:
    224231
    225232    typedef ExtendedStaticDigraphBase Parent;
     233 
     234  public:
     235 
     236    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
     237    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
     238      if (built) Parent::clear();
     239      Parent::build(digraph, nodeRef, arcRef);
     240    }
     241 
    226242
    227243  protected:
    228244
     
    261277      Arc last;
    262278    };
    263279
     280    Node baseNode(const OutArcIt &arc) const {
     281      return Parent::source(static_cast<const Arc&>(arc));
     282    }
     283
     284    Node runningNode(const OutArcIt &arc) const {
     285      return Parent::target(static_cast<const Arc&>(arc));
     286    }
     287
     288    Node baseNode(const InArcIt &arc) const {
     289      return Parent::target(static_cast<const Arc&>(arc));
     290    }
     291
     292    Node runningNode(const InArcIt &arc) const {
     293      return Parent::source(static_cast<const Arc&>(arc));
     294    }
     295
    264296  };
    265297
    266298}
  • test/digraph_test.cc

    diff --git a/test/digraph_test.cc b/test/digraph_test.cc
    a b  
    1919#include <lemon/concepts/digraph.h>
    2020#include <lemon/list_graph.h>
    2121#include <lemon/smart_graph.h>
     22#include <lemon/static_graph.h>
    2223#include <lemon/full_graph.h>
    2324
    2425#include "test_tools.h"
     
    317318    checkConcept<ExtendableDigraphComponent<>, SmartDigraph>();
    318319    checkConcept<ClearableDigraphComponent<>, SmartDigraph>();
    319320  }
     321  { // Checking StaticDigraph
     322    checkConcept<Digraph, StaticDigraph>();
     323    checkConcept<ClearableDigraphComponent<>, StaticDigraph>();
     324  }
    320325  { // Checking FullDigraph
    321326    checkConcept<Digraph, FullDigraph>();
    322327  }
     
    372377  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
    373378}
    374379
     380void checkStaticDigraph() {
     381  SmartDigraph g;
     382  SmartDigraph::NodeMap<StaticDigraph::Node> nref(g);
     383  SmartDigraph::ArcMap<StaticDigraph::Arc> aref(g);
     384 
     385  StaticDigraph G;
     386 
     387  checkGraphNodeList(G, 0);
     388  checkGraphArcList(G, 0);
     389
     390  G.build(g, nref, aref);
     391
     392  checkGraphNodeList(G, 0);
     393  checkGraphArcList(G, 0);
     394
     395  SmartDigraph::Node
     396    n1 = g.addNode(),
     397    n2 = g.addNode(),
     398    n3 = g.addNode();
     399
     400  G.build(g, nref, aref);
     401
     402  checkGraphNodeList(G, 3);
     403  checkGraphArcList(G, 0);
     404
     405  SmartDigraph::Arc a1 = g.addArc(n1, n2);
     406
     407  G.build(g, nref, aref);
     408
     409  check(G.source(aref[a1]) == nref[n1] && G.target(aref[a1]) == nref[n2],
     410        "Wrong arc or wrong references");
     411  checkGraphNodeList(G, 3);
     412  checkGraphArcList(G, 1);
     413
     414  checkGraphOutArcList(G, nref[n1], 1);
     415  checkGraphOutArcList(G, nref[n2], 0);
     416  checkGraphOutArcList(G, nref[n3], 0);
     417
     418  checkGraphInArcList(G, nref[n1], 0);
     419  checkGraphInArcList(G, nref[n2], 1);
     420  checkGraphInArcList(G, nref[n3], 0);
     421
     422  checkGraphConArcList(G, 1);
     423
     424  SmartDigraph::Arc
     425    a2 = g.addArc(n2, n1),
     426    a3 = g.addArc(n2, n3),
     427    a4 = g.addArc(n2, n3);
     428
     429  digraphCopy(g, G).nodeRef(nref).run();
     430
     431  checkGraphNodeList(G, 3);
     432  checkGraphArcList(G, 4);
     433
     434  checkGraphOutArcList(G, nref[n1], 1);
     435  checkGraphOutArcList(G, nref[n2], 3);
     436  checkGraphOutArcList(G, nref[n3], 0);
     437
     438  checkGraphInArcList(G, nref[n1], 1);
     439  checkGraphInArcList(G, nref[n2], 1);
     440  checkGraphInArcList(G, nref[n3], 2);
     441
     442  checkGraphConArcList(G, 4);
     443
     444  checkNodeIds(G);
     445  checkArcIds(G);
     446  checkGraphNodeMap(G);
     447  checkGraphArcMap(G);
     448}
     449
    375450void checkFullDigraph(int num) {
    376451  typedef FullDigraph Digraph;
    377452  DIGRAPH_TYPEDEFS(Digraph);
     
    419494    checkDigraphSnapshot<SmartDigraph>();
    420495    checkDigraphValidity<SmartDigraph>();
    421496  }
     497  { // Checking StaticDigraph
     498    checkStaticDigraph();
     499  }
    422500  { // Checking FullDigraph
    423501    checkFullDigraph(8);
    424502  }