[Lemon-commits] [lemon_svn] alpar: r663 - in hugo/trunk/src: include include/skeletons test work/alpar
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:40:50 CET 2006
Author: alpar
Date: Mon May 3 09:27:29 2004
New Revision: 663
Added:
hugo/trunk/src/test/graph_test.cc
Modified:
hugo/trunk/src/include/skeletons/graph.h
hugo/trunk/src/include/smart_graph.h
hugo/trunk/src/work/alpar/list_graph.h
Log:
test/graph_test.cc added.
It discovered several bugs and warnings in 'include/smart_graph.h',
in 'include/skeletons/graph.h' and in 'work/alpar/list_graph.h'.
They have also been fixed.
Modified: hugo/trunk/src/include/skeletons/graph.h
==============================================================================
--- hugo/trunk/src/include/skeletons/graph.h (original)
+++ hugo/trunk/src/include/skeletons/graph.h Mon May 3 09:27:29 2004
@@ -1,6 +1,6 @@
// -*- c++ -*-
-#ifndef HUGO_GRAPH_H
-#define HUGO_GRAPH_H
+#ifndef HUGO_SKELETON_GRAPH_H
+#define HUGO_SKELETON_GRAPH_H
///\file
///\brief Declaration of GraphSkeleton.
@@ -58,13 +58,13 @@
/// Two iterators are equal if and only if they point to the
/// same object or both are invalid.
- bool operator==(Node n) const { return true; }
+ bool operator==(Node) const { return true; }
/// \sa \ref operator==(Node n)
///
- bool operator!=(Node n) const { return true; }
+ bool operator!=(Node) const { return true; }
- bool operator<(Node n) const { return true; }
+ bool operator<(Node) const { return true; }
};
/// This iterator goes through each node.
@@ -90,7 +90,7 @@
NodeIt(const GraphSkeleton &G) {}
/// @warning The default constructor sets the iterator
/// to an undefined value.
- NodeIt(const NodeIt &) {}
+ NodeIt(const NodeIt &n) : Node(n) {}
};
@@ -104,9 +104,9 @@
Edge(Invalid) {}
/// Two iterators are equal if and only if they point to the
/// same object or both are invalid.
- bool operator==(Edge n) const { return true; }
- bool operator!=(Edge n) const { return true; }
- bool operator<(Edge n) const { return true; }
+ bool operator==(Edge) const { return true; }
+ bool operator!=(Edge) const { return true; }
+ bool operator<(Edge) const { return true; }
};
/// This iterator goes trough the outgoing edges of a node.
@@ -187,9 +187,9 @@
NodeIt &first(NodeIt &i) const { return i;}
/// The first incoming edge.
- InEdgeIt &first(InEdgeIt &i, Node n) const { return i;}
+ InEdgeIt &first(InEdgeIt &i, Node) const { return i;}
/// The first outgoing edge.
- OutEdgeIt &first(OutEdgeIt &i, Node n) const { return i;}
+ OutEdgeIt &first(OutEdgeIt &i, Node) const { return i;}
// SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
/// The first edge of the Graph.
EdgeIt &first(EdgeIt &i) const { return i;}
@@ -258,7 +258,7 @@
///Add a new edge to the graph with tail node \c tail
///and head node \c head.
///\return the new edge.
- Edge addEdge(Node tail, Node head) { return INVALID;}
+ Edge addEdge(Node, Node) { return INVALID;}
/// Resets the graph.
@@ -294,11 +294,11 @@
/// Sets the value associated with node \c i to the value \c t.
///
- void set(Node i, T t) {}
- /// Gets the value of a node.
- T get(Node i) const {return *(T*)0;} //FIXME: Is it necessary
- T &operator[](Node i) {return *(T*)0;}
- const T &operator[](Node i) const {return *(T*)0;}
+ void set(Node, T) {}
+ // Gets the value of a node.
+ //T get(Node i) const {return *(T*)0;} //FIXME: Is it necessary?
+ T &operator[](Node) {return *(T*)0;}
+ const T &operator[](Node) const {return *(T*)0;}
/// Updates the map if the graph has been changed
@@ -326,9 +326,14 @@
EdgeMap(const GraphSkeleton &G) {}
EdgeMap(const GraphSkeleton &G, T t) {}
- void set(Edge i, T t) {}
- T get(Edge i) const {return *(T*)0;}
- T &operator[](Edge i) {return *(T*)0;}
+ ///\todo It can copy between different types.
+ ///
+ template<typename TT> EdgeMap(const EdgeMap<TT> &m) {}
+
+ void set(Edge, T) {}
+ //T get(Edge) const {return *(T*)0;}
+ T &operator[](Edge) {return *(T*)0;}
+ const T &operator[](Edge) const {return *(T*)0;}
void update() {}
void update(T a) {} //FIXME: Is it necessary
@@ -391,4 +396,4 @@
// }
-#endif // HUGO_GRAPH_H
+#endif // HUGO_SKELETON_GRAPH_H
Modified: hugo/trunk/src/include/smart_graph.h
==============================================================================
--- hugo/trunk/src/include/smart_graph.h (original)
+++ hugo/trunk/src/include/smart_graph.h Mon May 3 09:27:29 2004
@@ -80,6 +80,7 @@
public:
+
class NodeIt;
class EdgeIt;
class OutEdgeIt;
@@ -138,7 +139,17 @@
bool valid(Edge e) const { return e.n!=-1; }
bool valid(Node n) const { return n.n!=-1; }
+ ///\deprecated Use
+ ///\code
+ /// e=INVALID;
+ ///\endcode
+ ///instead.
void setInvalid(Edge &e) { e.n=-1; }
+ ///\deprecated Use
+ ///\code
+ /// e=INVALID;
+ ///\endcode
+ ///instead.
void setInvalid(Node &n) { n.n=-1; }
template <typename It> It getNext(It it) const
@@ -197,7 +208,7 @@
Node(int nn) {n=nn;}
public:
Node() {}
- Node (Invalid i) { n=-1; }
+ Node (Invalid) { n=-1; }
bool operator==(const Node i) const {return n==i.n;}
bool operator!=(const Node i) const {return n!=i.n;}
bool operator<(const Node i) const {return n<i.n;}
@@ -379,7 +390,7 @@
EdgeMap(const EdgeMap<T> &m) :
DynMapBase<Edge>(*m.G), container(m.container)
{
- G->dyn_node_maps.push_back(this);
+ G->dyn_edge_maps.push_back(this);
}
template<typename TT> friend class EdgeMap;
@@ -389,7 +400,7 @@
template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
DynMapBase<Edge>(*m.G)
{
- G->dyn_node_maps.push_back(this);
+ G->dyn_edge_maps.push_back(this);
typename std::vector<TT>::const_iterator i;
for(typename std::vector<TT>::const_iterator i=m.container.begin();
i!=m.container.end();
Added: hugo/trunk/src/test/graph_test.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/test/graph_test.cc Mon May 3 09:27:29 2004
@@ -0,0 +1,304 @@
+#include<iostream>
+#include<smart_graph.h>
+#include<skeletons/graph.h>
+#include<../work/alpar/list_graph.h>
+
+/*
+This test makes consistency checks of list graph structures.
+
+G.addNode(), G.addEdge(), G.valid(), G.tail(), G.head()
+
+*/
+
+using namespace hugo;
+
+// void check(bool rc, const char *msg) {
+// if(!rc) {
+// std::cerr << msg << std::endl;
+// exit(1);
+// }
+// }
+
+#define check(rc, msg) \
+ if(!rc) { \
+ std::cerr << __FILE__ ":" << __LINE__ << ": error: " << msg << std::endl; \
+ exit(1); \
+ } else { } \
+
+
+template<class Graph> void checkCompile(Graph &G)
+{
+ typedef typename Graph::Node Node;
+ typedef typename Graph::NodeIt NodeIt;
+ typedef typename Graph::Edge Edge;
+ typedef typename Graph::EdgeIt EdgeIt;
+ typedef typename Graph::InEdgeIt InEdgeIt;
+ typedef typename Graph::OutEdgeIt OutEdgeIt;
+
+ {
+ Node i; Node j(i); Node k(INVALID);
+ i=j;
+ bool b=G.valid(i); b=b;
+ b=(i==j); b=(i!=j); b=(i<j);
+ }
+ {
+ NodeIt i; NodeIt j(i); NodeIt k(INVALID); NodeIt l(G);
+ i=j;
+ j=G.first(i);
+ j=G.next(i);
+ bool b=G.valid(i); b=b;
+ Node n(i);
+ n=i;
+ b=(i==j); b=(i!=j); b=(i<j);
+ }
+ {
+ Edge i; Edge j(i); Edge k(INVALID);
+ i=j;
+ bool b=G.valid(i); b=b;
+ b=(i==j); b=(i!=j); b=(i<j);
+ }
+ {
+ EdgeIt i; EdgeIt j(i); EdgeIt k(INVALID); EdgeIt l(G);
+ i=j;
+ j=G.first(i);
+ j=G.next(i);
+ bool b=G.valid(i); b=b;
+ Edge e(i);
+ e=i;
+ b=(i==j); b=(i!=j); b=(i<j);
+ }
+ {
+ Node n;
+ InEdgeIt i; InEdgeIt j(i); InEdgeIt k(INVALID); InEdgeIt l(G,n);
+ i=j;
+ j=G.first(i,n);
+ j=G.next(i);
+ bool b=G.valid(i); b=b;
+ Edge e(i);
+ e=i;
+ b=(i==j); b=(i!=j); b=(i<j);
+ }
+ {
+ Node n;
+ OutEdgeIt i; OutEdgeIt j(i); OutEdgeIt k(INVALID); OutEdgeIt l(G,n);
+ i=j;
+ j=G.first(i,n);
+ j=G.next(i);
+ bool b=G.valid(i); b=b;
+ Edge e(i);
+ e=i;
+ b=(i==j); b=(i!=j); b=(i<j);
+ }
+
+ Node n,m;
+ n=G.addNode();
+ Edge e;
+ e=G.addEdge(n,m);
+ n=G.tail(e);
+ n=G.head(e);
+
+ //aNode, bNode ?
+
+ // id tests
+ { int i=G.id(n); i=i; }
+ { int i=G.id(e); i=i; }
+
+ G.clear();
+
+ //NodeMap tests
+ {
+ Node k;
+ typename Graph::NodeMap<int> m(G);
+ typename Graph::NodeMap<int> const &cm = m; //Const map
+ typename Graph::NodeMap<int> mdef(G,12); //Inicialize with default value
+ typename Graph::NodeMap<int> mm(cm); //Copy
+ typename Graph::NodeMap<double> dm(cm); //Copy from another type
+ int v;
+ v=m[k]; m[k]=v; m.set(k,v);
+ v=cm[k];
+
+ m=cm;
+ dm=cm; //Copy from another type
+ }
+ { //bool NodeMap
+ Node k;
+ typename Graph::NodeMap<bool> m(G);
+ typename Graph::NodeMap<bool> const &cm = m; //Const map
+ typename Graph::NodeMap<bool> mdef(G,12); //Inicialize with default value
+ typename Graph::NodeMap<bool> mm(cm); //Copy
+ typename Graph::NodeMap<int> dm(cm); //Copy from another type
+ bool v;
+ v=m[k]; m[k]=v; m.set(k,v);
+ v=cm[k];
+
+ m=cm;
+ dm=cm; //Copy from another type
+ }
+ //EdgeMap tests
+ {
+ Edge k;
+ typename Graph::EdgeMap<int> m(G);
+ typename Graph::EdgeMap<int> const &cm = m; //Const map
+ typename Graph::EdgeMap<int> mdef(G,12); //Inicialize with default value
+ typename Graph::EdgeMap<int> mm(cm); //Copy
+ typename Graph::EdgeMap<double> dm(cm); //Copy from another type
+ int v;
+ v=m[k]; m[k]=v; m.set(k,v);
+ v=cm[k];
+
+ m=cm;
+ dm=cm; //Copy from another type
+ }
+ { //bool EdgeMap
+ Edge k;
+ typename Graph::EdgeMap<bool> m(G);
+ typename Graph::EdgeMap<bool> const &cm = m; //Const map
+ typename Graph::EdgeMap<bool> mdef(G,12); //Inicialize with default value
+ typename Graph::EdgeMap<bool> mm(cm); //Copy
+ typename Graph::EdgeMap<int> dm(cm); //Copy from another type
+ bool v;
+ v=m[k]; m[k]=v; m.set(k,v);
+ v=cm[k];
+
+ m=cm;
+ dm=cm; //Copy from another type
+ }
+
+}
+
+
+
+template<class Graph> void addPetersen(Graph &G)
+{
+ std::vector<typename Graph::Node> outer, inner;
+
+ for(int i=0;i<5;i++) {
+ outer.push_back(G.addNode());
+ inner.push_back(G.addNode());
+ }
+
+ for(int i=0;i<5;i++) {
+ G.addEdge(outer[i],inner[i]);
+ G.addEdge(outer[i],outer[(i+1)%5]);
+ G.addEdge(inner[i],inner[(i+2)%5]);
+ }
+}
+
+template<class Graph> void checkNodeList(Graph &G, int nn)
+{
+ typename Graph::NodeIt n(G);
+ for(int i=0;i<nn;i++) {
+ check(G.valid(n),"Wrong Node list linking.");
+ G.next(n);
+ }
+ check(!G.valid(n),"Wrong Node list linking.");
+}
+
+template<class Graph> void checkEdgeList(Graph &G, int nn)
+{
+ typedef typename Graph::EdgeIt EdgeIt;
+
+ EdgeIt e(G);
+ for(int i=0;i<nn;i++) {
+ check(G.valid(e),"Wrong Edge list linking.");
+ G.next(e);
+ }
+ check(!G.valid(e),"Wrong Edge list linking.");
+}
+
+template<class Graph> void checkOutEdgeList(Graph &G,
+ typename Graph::Node n,
+ int nn)
+{
+ typename Graph::OutEdgeIt e(G,n);
+ for(int i=0;i<nn;i++) {
+ check(G.valid(e),"Wrong OutEdge list linking.");
+ G.next(e);
+ }
+ check(!G.valid(e),"Wrong OutEdge list linking.");
+}
+
+template<class Graph> void checkInEdgeList(Graph &G,
+ typename Graph::Node n,
+ int nn)
+{
+ typename Graph::InEdgeIt e(G,n);
+ for(int i=0;i<nn;i++) {
+ check(G.valid(e),"Wrong InEdge list linking.");
+ G.next(e);
+ }
+ check(!G.valid(e),"Wrong InEdge list linking.");
+}
+
+//Checks head(), tail() as well;
+template<class Graph> void bidirPetersen(Graph &G)
+{
+ typedef typename Graph::Edge Edge;
+ typedef typename Graph::EdgeIt EdgeIt;
+
+ checkEdgeList(G,15);
+
+ std::vector<Edge> ee;
+
+ for(EdgeIt e(G);G.valid(e);G.next(e)) ee.push_back(e);
+
+ for(typename std::vector<Edge>::iterator p=ee.begin();p!=ee.end();p++)
+ G.addEdge(G.head(*p),G.tail(*p));
+}
+
+template<class Graph> void checkPetersen(Graph &G)
+{
+ typedef typename Graph::Node Node;
+
+ typedef typename Graph::EdgeIt EdgeIt;
+ typedef typename Graph::NodeIt NodeIt;
+
+ checkNodeList(G,10);
+ checkEdgeList(G,30);
+
+ for(NodeIt n(G);G.valid(n);G.next(n)) {
+ checkInEdgeList(G,n,3);
+ checkOutEdgeList(G,n,3);
+ G.next(n);
+ }
+}
+
+template void checkCompile<GraphSkeleton>(GraphSkeleton &);
+template void checkCompile<SmartGraph>(SmartGraph &);
+template void checkCompile<SymSmartGraph>(SymSmartGraph &);
+template void checkCompile<ListGraph>(ListGraph &);
+template void checkCompile<SymListGraph>(SymListGraph &);
+//Due to some mysterious and some conceptual problems it does not work.
+//template void checkCompile<EdgeSet <ListGraph> >(EdgeSet <ListGraph> &);
+
+int main()
+{
+ {
+ SmartGraph G;
+ addPetersen(G);
+ bidirPetersen(G);
+ checkPetersen(G);
+ }
+ {
+ ListGraph G;
+ addPetersen(G);
+ bidirPetersen(G);
+ checkPetersen(G);
+ }
+ {
+ SymSmartGraph G;
+ addPetersen(G);
+ checkPetersen(G);
+ }
+ {
+ SymListGraph G;
+ addPetersen(G);
+ checkPetersen(G);
+ }
+
+ //\todo map tests.
+ //\todo copy constr tests.
+
+ std::cout << __FILE__ ": All tests passed.\n";
+
+}
Modified: hugo/trunk/src/work/alpar/list_graph.h
==============================================================================
--- hugo/trunk/src/work/alpar/list_graph.h (original)
+++ hugo/trunk/src/work/alpar/list_graph.h Mon May 3 09:27:29 2004
@@ -316,7 +316,7 @@
Node(int nn) {n=nn;}
public:
Node() {}
- Node (Invalid i) { n=-1; }
+ Node (Invalid) { n=-1; }
bool operator==(const Node i) const {return n==i.n;}
bool operator!=(const Node i) const {return n!=i.n;}
bool operator<(const Node i) const {return n<i.n;}
@@ -503,7 +503,7 @@
EdgeMap(const EdgeMap<T> &m) :
DynMapBase<Edge>(*m.G), container(m.container)
{
- G->dyn_node_maps.push_back(this);
+ G->dyn_edge_maps.push_back(this);
}
template<typename TT> friend class EdgeMap;
@@ -513,7 +513,7 @@
template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
DynMapBase<Edge>(*m.G)
{
- G->dyn_node_maps.push_back(this);
+ G->dyn_edge_maps.push_back(this);
typename std::vector<TT>::const_iterator i;
for(typename std::vector<TT>::const_iterator i=m.container.begin();
i!=m.container.end();
@@ -1294,11 +1294,11 @@
it.n=edges[it.n].next_in;
}
else {
- typename NodeGraphType::Node n;
- for(n=G.next(edges[it.n].head);
- G.valid(n) && nodes[n].first_in == -1;
- G.next(n)) ;
- it.n = (G.valid(n))?-1:nodes[n].first_in;
+ NodeIt n;
+ for(n=next(edges[it.n].head);
+ valid(n) && nodes[n].first_in == -1;
+ next(n)) ;
+ it.n = (valid(n))?-1:nodes[n].first_in;
}
return it;
}
@@ -1385,6 +1385,7 @@
// first_node=first_free_node=first_free_edge=-1;
// }
+ public:
class Node : public NodeGraphType::Node {
friend class EdgeSet;
// template <typename T> friend class NodeMap;
@@ -1444,10 +1445,11 @@
friend class EdgeSet;
public:
EdgeIt(const EdgeSet& G) : Edge() {
- typename NodeGraphType::Node m;
+ // typename NodeGraphType::Node m;
+ NodeIt m;
for(G.first(m);
- G.valid(m) && nodes[m].first_in == -1; G.next[m]);
- n = G.valid(m)?-1:nodes[m].first_in;
+ G.valid(m) && G.nodes[m].first_in == -1; G.next(m));
+ n = G.valid(m)?-1:G.nodes[m].first_in;
}
EdgeIt (Invalid i) : Edge(i) { }
EdgeIt() : Edge() { }
@@ -1514,7 +1516,7 @@
EdgeMap(const EdgeMap<T> &m) :
DynMapBase<Edge>(*m.G), container(m.container)
{
- G->dyn_node_maps.push_back(this);
+ G->dyn_edge_maps.push_back(this);
}
template<typename TT> friend class EdgeMap;
@@ -1524,7 +1526,7 @@
template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
DynMapBase<Edge>(*m.G)
{
- G->dyn_node_maps.push_back(this);
+ G->dyn_edge_maps.push_back(this);
typename std::vector<TT>::const_iterator i;
for(typename std::vector<TT>::const_iterator i=m.container.begin();
i!=m.container.end();
More information about the Lemon-commits
mailing list