COIN-OR::LEMON - Graph Library

Changeset 1205:d3dcc49e6403 in lemon for test


Ignore:
Timestamp:
02/28/13 17:13:14 (7 years ago)
Author:
Peter Kovacs <kpeter@…>
Branch:
default
Phase:
public
Message:

Use output iterator instead of a container (#386)
in tourNodes() functions of TSP algorithms

File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/tsp_test.cc

    r1203 r1205  
    6262
    6363// Checks tour validity
    64 bool checkTour(const FullGraph &gr, const std::vector<FullGraph::Node> &p) {
     64template <typename Container>
     65bool checkTour(const FullGraph &gr, const Container &p) {
    6566  FullGraph::NodeMap<bool> used(gr, false);
    6667 
    67   int nodes = 0;
    68   for (int i = 0; i < int(p.size()); ++i) {
    69     if (used[p[i]]) return false;
    70     used[p[i]] = true;
    71     ++nodes;
     68  int node_cnt = 0;
     69  for (typename Container::const_iterator it = p.begin(); it != p.end(); ++it) {
     70    FullGraph::Node node = *it;
     71    if (used[node]) return false;
     72    used[node] = true;
     73    ++node_cnt;
    7274  }
    7375 
    74   return (nodes == gr.nodeNum());
     76  return (node_cnt == gr.nodeNum());
    7577}
    7678
    7779// Checks tour validity
    78 bool checkTour(const FullGraph &gr, const Path<FullGraph> &p) {
     80bool checkTourPath(const FullGraph &gr, const Path<FullGraph> &p) {
    7981  FullGraph::NodeMap<bool> used(gr, false);
    8082 
     
    135137    check(alg.tourCost() == esize, alg_name + ": Wrong total cost");
    136138
    137     std::list<Node> list;
    138     std::vector<Node> vec;
    139     alg.tourNodes(list);
    140     alg.tourNodes(vec);
    141     check(list.size() == nsize, alg_name + ": Wrong node sequence");
    142     check(vec.size() == nsize,  alg_name + ": Wrong node sequence");
    143     check(alg.tourNodes().size() == nsize, alg_name + ": Wrong node sequence");
    144     check(checkTour(g, vec), alg_name + ": Wrong node sequence");
    145     check(checkCost(g, vec, constMap<Edge, int>(1), esize),
     139    std::list<Node> list1(nsize), list2;
     140    std::vector<Node> vec1(nsize), vec2;
     141    alg.tourNodes(list1.begin());
     142    alg.tourNodes(vec1.begin());
     143    alg.tourNodes(std::front_inserter(list2));
     144    alg.tourNodes(std::back_inserter(vec2));
     145    check(checkTour(g, alg.tourNodes()), alg_name + ": Wrong node sequence");
     146    check(checkTour(g, list1), alg_name + ": Wrong node sequence");
     147    check(checkTour(g, vec1), alg_name + ": Wrong node sequence");
     148    check(checkTour(g, list2), alg_name + ": Wrong node sequence");
     149    check(checkTour(g, vec2), alg_name + ": Wrong node sequence");
     150    check(checkCost(g, vec1, constMap<Edge, int>(1), esize),
    146151      alg_name + ": Wrong tour cost");
    147152
     
    149154    alg.tour(path);
    150155    check(path.length() == esize, alg_name + ": Wrong tour");
    151     check(checkTour(g, path), alg_name + ": Wrong tour");
     156    check(checkTourPath(g, path), alg_name + ": Wrong tour");
    152157    check(checkCost(g, path, constMap<Edge, int>(1), esize),
    153158      alg_name + ": Wrong tour cost");
     
    181186
    182187    std::vector<Node> vec;
    183     alg.tourNodes(vec);
     188    alg.tourNodes(std::back_inserter(vec));
    184189    check(checkTour(g, vec), alg_name + ": Wrong node sequence");
    185190    check(checkCost(g, vec, cost, alg.tourCost()),
     
    188193    SimplePath<FullGraph> path;
    189194    alg.tour(path);
    190     check(checkTour(g, path), alg_name + ": Wrong tour");
     195    check(checkTourPath(g, path), alg_name + ": Wrong tour");
    191196    check(checkCost(g, path, cost, alg.tourCost()),
    192197      alg_name + ": Wrong tour cost");
Note: See TracChangeset for help on using the changeset viewer.