COIN-OR::LEMON - Graph Library

Ticket #23: item_validity_check.patch

File item_validity_check.patch, 11.2 KB (added by Balazs Dezso, 11 years ago)
  • lemon/graph_utils.h

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1208958160 -7200
    # Node ID ee2042a0422f344cd56c30f0556d1ddc0e81be6a
    # Parent  19666d918a51d37985fb946e6937266e87dd8770
    Item validity checking for ListGraph and SmartGraph
    
    diff -r 19666d918a51 -r ee2042a0422f lemon/graph_utils.h
    a b  
    128128  typedef typename ::lemon::_graph_utils_bits::                         \
    129129  Node<Digraph>::type Node;                                             \
    130130  typedef typename ::lemon::_graph_utils_bits::                         \
    131   NodeIt<Digraph>::type NodeIt;                                         \
     131  NodeIt<Digraph>::type NodeIt;                                         \
    132132  typedef typename ::lemon::_graph_utils_bits::                         \
    133133  Arc<Digraph>::type Arc;                                               \
    134134  typedef typename ::lemon::_graph_utils_bits::                         \
  • lemon/list_graph.h

    diff -r 19666d918a51 -r ee2042a0422f lemon/list_graph.h
    a b  
    152152    static Node nodeFromId(int id) { return Node(id);}
    153153    static Arc arcFromId(int id) { return Arc(id);}
    154154
     155    bool valid(Node n) const {
     156      return n.id >= 0 && n.id < static_cast<int>(nodes.size()) &&
     157        nodes[n.id].prev != -2;
     158    }
     159
     160    bool valid(Arc a) const {
     161      return a.id >= 0 && a.id < static_cast<int>(arcs.size()) &&
     162        arcs[a.id].prev_in != -2;
     163    }
     164
    155165    Node addNode() {     
    156166      int n;
    157167     
     
    219229     
    220230      nodes[n].next = first_free_node;
    221231      first_free_node = n;
     232      nodes[n].prev = -2;
    222233
    223234    }
    224235   
     
    247258      }
    248259     
    249260      arcs[n].next_in = first_free_arc;
    250       first_free_arc = n;     
    251 
     261      first_free_arc = n;
     262      arcs[n].prev_in = -2;
    252263    }
    253264
    254265    void clear() {
     
    349360    Arc addArc(const Node& s, const Node& t) {
    350361      return Parent::addArc(s, t);
    351362    }
     363
     364    /// Node validity checking
     365
     366    /// The function gives back true, when the given node is valid,
     367    /// ie. it is a real node of the graph. 
     368    ///
     369    /// \warning A removed node could become valid, if new nodes are
     370    /// added to the graph.
     371    bool valid(Node n) const { return Parent::valid(n); }
     372
     373    /// Arc validity checking
     374
     375    /// The function gives back true, when the given arc is valid,
     376    /// ie. it is a real arc of the graph. 
     377    ///
     378    /// \warning A removed arc could become valid, if new arcs are
     379    /// added to the graph.
     380    bool valid(Arc a) const { return Parent::valid(a); }
    352381
    353382    /// Change the target of \c e to \c n
    354383
     
    945974    static Arc arcFromId(int id) { return Arc(id);}
    946975    static Edge edgeFromId(int id) { return Edge(id);}
    947976
     977    bool valid(Node n) const {
     978      return n.id >= 0 && n.id < static_cast<int>(nodes.size()) &&
     979        nodes[n.id].prev != -2;
     980    }
     981
     982    bool valid(Arc a) const {
     983      return a.id >= 0 && a.id < static_cast<int>(arcs.size()) &&
     984        arcs[a.id].prev_out != -2;
     985    }
     986
     987    bool valid(Edge e) const {
     988      return e.id >= 0 && 2 * e.id < static_cast<int>(arcs.size()) &&
     989        arcs[2 * e.id].prev_out != -2;
     990    }
     991
    948992    Node addNode() {     
    949993      int n;
    950994     
     
    10131057     
    10141058      nodes[n].next = first_free_node;
    10151059      first_free_node = n;
    1016 
     1060      nodes[n].prev = -2;
    10171061    }
    10181062   
    10191063    void erase(const Edge& edge) {
     
    10411085     
    10421086      arcs[n].next_out = first_free_arc;
    10431087      first_free_arc = n;     
     1088      arcs[n].prev_out = -2;
     1089      arcs[n | 1].prev_out = -2;
    10441090
    10451091    }
    10461092
     
    11571203    Edge addEdge(const Node& s, const Node& t) {
    11581204      return Parent::addEdge(s, t);
    11591205    }
     1206    /// \brief Node validity checking
     1207    ///
     1208    /// The function gives back true, when the given node is valid,
     1209    /// ie. it is a real node of the graph. 
     1210    ///
     1211    /// \warning A removed node could become valid, if new nodes are
     1212    /// added to the graph.
     1213    bool valid(Node n) const { return Parent::valid(n); }
     1214    /// \brief Arc validity checking
     1215    ///
     1216    /// The function gives back true, when the given arc is valid,
     1217    /// ie. it is a real arc of the graph. 
     1218    ///
     1219    /// \warning A removed arc could become valid, if new edges are
     1220    /// added to the graph.
     1221    bool valid(Arc a) const { return Parent::valid(a); }
     1222    /// \brief Edge validity checking
     1223    ///
     1224    /// The function gives back true, when the given edge is valid,
     1225    /// ie. it is a real edge of the graph. 
     1226    ///
     1227    /// \warning A removed edge could become valid, if new edges are
     1228    /// added to the graph.
     1229    bool valid(Edge e) const { return Parent::valid(e); }
    11601230    /// \brief Change the source of \c e to \c n
    11611231    ///
    11621232    /// This function changes the source of \c e to \c n.
  • lemon/smart_graph.h

    diff -r 19666d918a51 -r ee2042a0422f lemon/smart_graph.h
    a b  
    114114
    115115    static Node nodeFromId(int id) { return Node(id);}
    116116    static Arc arcFromId(int id) { return Arc(id);}
     117
     118    bool valid(Node n) const {
     119      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
     120    }
     121    bool valid(Arc a) const {
     122      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
     123    }
    117124
    118125    class Node {
    119126      friend class SmartDigraphBase;
     
    261268    /// \sa reserveNode
    262269    void reserveArc(int m) { arcs.reserve(m); };
    263270
     271    /// \brief Node validity checking
     272    ///
     273    /// The function gives back true, when the given node is valid,
     274    /// ie. it is a real node of the graph. 
     275    ///
     276    /// \warning A removed node (using Snapshot) could become valid,
     277    /// if new nodes are added to the graph.
     278    bool valid(Node n) const { return Parent::valid(n); }
     279
     280    /// \brief Arc validity checking
     281    ///
     282    /// The function gives back true, when the given arc is valid,
     283    /// ie. it is a real arc of the graph. 
     284    ///
     285    /// \warning A removed arc (using Snapshot) could become valid,
     286    /// if new arcs are added to the graph.
     287    bool valid(Arc a) const { return Parent::valid(a); }
     288
    264289    ///Clear the digraph.
    265290   
    266291    ///Erase all the nodes and arcs from the digraph.
     
    550575    static Arc arcFromId(int id) { return Arc(id);}
    551576    static Edge edgeFromId(int id) { return Edge(id);}
    552577
     578    bool valid(Node n) const {
     579      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
     580    }
     581    bool valid(Arc a) const {
     582      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
     583    }
     584    bool valid(Edge e) const {
     585      return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
     586    }
     587
    553588    Node addNode() {     
    554589      int n = nodes.size();
    555590      nodes.push_back(NodeT());
     
    642677      return Parent::addEdge(s, t);
    643678    }
    644679
     680    /// \brief Node validity checking
     681    ///
     682    /// The function gives back true, when the given node is valid,
     683    /// ie. it is a real node of the graph. 
     684    ///
     685    /// \warning A removed node (using Snapshot) could become valid,
     686    /// if new nodes are added to the graph.
     687    bool valid(Node n) const { return Parent::valid(n); }
     688
     689    /// \brief Arc validity checking
     690    ///
     691    /// The function gives back true, when the given arc is valid,
     692    /// ie. it is a real arc of the graph. 
     693    ///
     694    /// \warning A removed arc (using Snapshot) could become valid,
     695    /// if new edges are added to the graph.
     696    bool valid(Arc a) const { return Parent::valid(a); }
     697
     698    /// \brief Edge validity checking
     699    ///
     700    /// The function gives back true, when the given edge is valid,
     701    /// ie. it is a real edge of the graph. 
     702    ///
     703    /// \warning A removed edge (using Snapshot) could become valid,
     704    /// if new edges are added to the graph.
     705    bool valid(Edge e) const { return Parent::valid(e); }
     706
    645707    ///Clear the graph.
    646708   
    647709    ///Erase all the nodes and edges from the graph.
  • test/graph_test.cc

    diff -r 19666d918a51 -r ee2042a0422f test/graph_test.cc
    a b  
    2222// #include <lemon/full_graph.h>
    2323// #include <lemon/grid_graph.h>
    2424
    25 //#include <lemon/graph_utils.h>
     25#include <lemon/graph_utils.h>
    2626
    2727#include "test_tools.h"
    2828
     
    8282}
    8383
    8484template <typename Graph>
    85 void print_items(Graph &g) {
     85void check_graph_counts() {
    8686
    87   typedef typename Graph::NodeIt NodeIt;
    88   typedef typename Graph::EdgeIt EdgeIt;
    89   typedef typename Graph::ArcIt ArcIt;
    90 
    91   std::cout << "Nodes" << std::endl;
    92   int i=0;
    93   for(NodeIt it(g); it!=INVALID; ++it, ++i) {
    94     std::cout << "  " << i << ": " << g.id(it) << std::endl;
    95   }
    96 
    97   std::cout << "Edge" << std::endl;
    98   i=0;
    99   for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
    100     std::cout << "  " << i << ": " << g.id(it)
    101          << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it))
    102          << ")" << std::endl;
    103   }
    104 
    105   std::cout << "Arc" << std::endl;
    106   i=0;
    107   for(ArcIt it(g); it!=INVALID; ++it, ++i) {
    108     std::cout << "  " << i << ": " << g.id(it)
    109          << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it))
    110          << ")" << std::endl;
    111   }
    112 
    113 }
    114 
    115 template <typename Graph>
    116 void check_graph() {
    117 
    118   typedef typename Graph::Node Node;
    119   typedef typename Graph::Edge Edge;
    120   typedef typename Graph::Arc Arc;
    121   typedef typename Graph::NodeIt NodeIt;
    122   typedef typename Graph::EdgeIt EdgeIt;
    123   typedef typename Graph::ArcIt ArcIt;
    124 
     87  GRAPH_TYPEDEFS(Graph);
    12588  Graph g;
    12689
    12790  check_item_counts(g,0,0);
     
    13598    e1 = g.addEdge(n1, n2),
    13699    e2 = g.addEdge(n2, n3);
    137100
    138   // print_items(g);
    139 
    140101  check_item_counts(g,3,2);
    141102}
     103
     104template <typename Graph>
     105void check_graph_validity() {
     106
     107  GRAPH_TYPEDEFS(Graph);
     108  Graph g;
     109
     110  check_item_counts(g,0,0);
     111
     112  Node
     113    n1 = g.addNode(),
     114    n2 = g.addNode(),
     115    n3 = g.addNode();
     116
     117  Edge
     118    e1 = g.addEdge(n1, n2),
     119    e2 = g.addEdge(n2, n3);
     120
     121  check(g.valid(n1), "Validity check");
     122  check(g.valid(e1), "Validity check");
     123  check(g.valid(g.direct(e1, true)), "Validity check");
     124
     125  check(!g.valid(g.nodeFromId(-1)), "Validity check");
     126  check(!g.valid(g.edgeFromId(-1)), "Validity check");
     127  check(!g.valid(g.arcFromId(-1)), "Validity check");
     128   
     129}
     130
     131template <typename Graph>
     132void check_graph_validity_erase() {
     133
     134  GRAPH_TYPEDEFS(Graph);
     135  Graph g;
     136
     137  check_item_counts(g,0,0);
     138
     139  Node
     140    n1 = g.addNode(),
     141    n2 = g.addNode(),
     142    n3 = g.addNode();
     143
     144  Edge
     145    e1 = g.addEdge(n1, n2),
     146    e2 = g.addEdge(n2, n3);
     147
     148  check(g.valid(n1), "Validity check");
     149  check(g.valid(e1), "Validity check");
     150  check(g.valid(g.direct(e1, true)), "Validity check");
     151
     152  g.erase(n1);
     153
     154  check(!g.valid(n1), "Validity check");
     155  check(g.valid(n2), "Validity check");
     156  check(g.valid(n3), "Validity check");
     157  check(!g.valid(e1), "Validity check");
     158  check(g.valid(e2), "Validity check");
     159
     160  check(!g.valid(g.nodeFromId(-1)), "Validity check");
     161  check(!g.valid(g.edgeFromId(-1)), "Validity check");
     162  check(!g.valid(g.arcFromId(-1)), "Validity check");
     163   
     164}
     165
     166
    142167
    143168// void checkGridGraph(const GridGraph& g, int w, int h) {
    144169//   check(g.width() == w, "Wrong width");
     
    187212int main() {
    188213  check_concepts();
    189214
    190   check_graph<ListGraph>();
    191   check_graph<SmartGraph>();
     215  check_graph_counts<ListGraph>();
     216  check_graph_counts<SmartGraph>();
     217
     218  check_graph_validity_erase<ListGraph>();
     219  check_graph_validity<SmartGraph>();
    192220
    193221//   {
    194222//     FullGraph g(5);