# HG changeset patch # User alpar # Date 1071330290 0 # Node ID 272a5677bd6d9be669d5a009556c300fa58279a5 # Parent 37117ebbabe299dbf1a87256f11b0041563b6791 - Marci type iterator constructors - src/demo/bfsdemo.cc: demo for bfs.h - cosmetical changes diff -r 37117ebbabe2 -r 272a5677bd6d src/include/bfs.h --- a/src/include/bfs.h Thu Dec 11 07:24:53 2003 +0000 +++ b/src/include/bfs.h Sat Dec 13 15:44:50 2003 +0000 @@ -38,14 +38,14 @@ // }; -// Nem jo! Osszeakad a masik Set-tel +// Nem jo! Osszeakad a masik Put-tel // class do_nothing_map {}; // template -// void Set(const do_nothing_map &p,const V &v,const T &t) {} +// void Put(const do_nothing_map &p,const V &v,const T &t) {} struct do_nothing_map { template - static void Set(V v,T t) {} + static void Put(V v,T t) {} }; @@ -53,23 +53,23 @@ class class_element_map { public: typedef T value_type; - static void Set(const I &i, const T &t) {(*i).*M=t;} + static void Put(const I &i, const T &t) {(*i).*M=t;} static T Get(const I i) {return (*i).*M;} T &operator[](I i) {return (*i).*M;} }; /* template - void Set(class_element_map p,I i, T t) + void Put(class_element_map p,I i, T t) { i->*M=t; }; */ template - inline void Set(P &p,const I &i, const T &t) + inline void Put(P &p,const I &i, const T &t) { - p.Set(i,t); + p.Put(i,t); }; template inline typename P::value_type Get(const P &p,const I &i) @@ -125,7 +125,7 @@ public: bfs_node_data NodeType::*d; typedef bool value_type; - void Set(typename G::NodeIterator &i, const value_type &t) + void Put(typename G::NodeIterator &i, const value_type &t) {((*i).*d).visited=t;} value_type Get(const typename G::NodeIterator &i) const {return ((*i).*d).visited;} @@ -136,7 +136,7 @@ public: bfs_node_data NodeType::*d; typedef typename G::EdgeIterator value_type; - void Set(typename G::NodeIterator &i, const value_type &t) + void Put(typename G::NodeIterator &i, const value_type &t) {((*i).*d).tree=t;} value_type Get(const typename G::NodeIterator &i) const {return ((*i).*d).tree;} @@ -147,7 +147,7 @@ public: bfs_node_data NodeType::*d; typedef int value_type; - void Set(typename G::NodeIterator &i, const value_type &t) + void Put(typename G::NodeIterator &i, const value_type &t) {((*i).*d).dist=t;} value_type Get(const typename G::NodeIterator &i) const {return ((*i).*d).dist;} @@ -158,7 +158,7 @@ public: bfs_node_data NodeType::*d; typedef int value_type; - void Set(typename G::NodeIterator &i, const value_type &t) + void Put(typename G::NodeIterator &i, const value_type &t) {((*i).*d).priority=t;} value_type Get(const typename G::NodeIterator &i) const {return ((*i).*d).priority;} @@ -175,7 +175,7 @@ bfs_static_maps(const bfs_node_data NodeType::*dd) { - SetDataField(dd); + PutDataField(dd); } bfs_static_maps(const bfs_static_maps &B) @@ -210,17 +210,17 @@ int d; for(Gr.GetFirst(n);n.isValid();++n) - Set(maps.visited,n,false); + Put(maps.visited,n,false); queue Q; q.n=start_node; q.dist=0; Q.push(q); - Set(maps.visited,start_node,true); - // Set(maps::tree,start_node,?????); - Set(maps.dist,start_node,0); - Set(maps.priority,start_node,pr++); + Put(maps.visited,start_node,true); + // Put(maps::tree,start_node,?????); + Put(maps.dist,start_node,0); + Put(maps.priority,start_node,pr++); do { n=Q.front().n;d=Q.front().dist+1; @@ -230,10 +230,10 @@ q.n=m; q.dist=d; Q.push(q); - Set(maps.visited,m,true); - Set(maps.tree,m,e); - Set(maps.dist,m,d); - Set(maps.priority,m,pr++); + Put(maps.visited,m,true); + Put(maps.tree,m,e); + Put(maps.dist,m,d); + Put(maps.priority,m,pr++); } } while(!Q.empty()); }; diff -r 37117ebbabe2 -r 272a5677bd6d src/include/graph.h --- a/src/include/graph.h Thu Dec 11 07:24:53 2003 +0000 +++ b/src/include/graph.h Sat Dec 13 15:44:50 2003 +0000 @@ -57,6 +57,8 @@ public: NodeIterator() {;} + NodeIterator(Graph &Gr)//'const Graph &G' would be wrong. + {G=&Gr;n=Gr.OldGraph::FirstNode();} NodeIterator(const NodeIterator &i) {G=i.G;n=i.n;} NodeIterator &GoNext() { n=G->NextNode(n); return *this;} @@ -148,6 +150,10 @@ //Ne a BiEdgeIterator-bol szarmazzon? { public: + InEdgeIterator() {} + InEdgeIterator(const Graph &Gr,const NodeIterator &n) + { G=&Gr; e=Gr.OldGraph::FirstIn(n.n);} + InEdgeIterator &GoNext() { e=e->NextIn(); return *this;} InEdgeIterator Next() const {return InEdgeIterator(*this).GoNext();} InEdgeIterator &operator++() { return GoNext();} @@ -169,6 +175,10 @@ class OutEdgeIterator : public EdgeIterator { public: + OutEdgeIterator() {} + OutEdgeIterator(Graph &Gr,const NodeIterator &n) + { G=&Gr; e=Gr.OldGraph::FirstOut(n.n);} + OutEdgeIterator &GoNext() { e=e->NextOut(); return *this;} OutEdgeIterator Next() const {return OutEdgeIterator(*this).GoNext();} OutEdgeIterator &operator++() { return GoNext();} @@ -192,6 +202,10 @@ NodeIterator n; // Itt ketszer van a G public: + SymEdgeIterator() {} + SymEdgeIterator(const Graph &Gr,const NodeIterator &nn) + { G=&Gr; n=nn; e=Gr.FirstSym(nn.n); } + SymEdgeIterator &GoNext() { e=e->NextEdge(n.n); return *this;} SymEdgeIterator Next() const {return SymEdgeIterator(*this).GoNext();} SymEdgeIterator &operator++() { return GoNext();} @@ -216,6 +230,12 @@ NodeIterator n; // Itt ketszer van a G public: + AllEdgeIterator() {} + AllEdgeIterator(Graph &Gr) : n(Gr) + { + e=n.isValid()?Gr.OldGraph::FirstOut(n.n):NULL; + } + AllEdgeIterator &GoNext() { e=e->NextOut(); @@ -248,7 +268,7 @@ typedef InEdgeIterator DeletingInEdgeIterator; typedef SymEdgeIterator DeletingSymEdgeIterator; - const NodeIterator &FirstNode() + const NodeIterator FirstNode() { NodeIterator i; i.G=this;i.n=OldGraph::FirstNode(); @@ -284,19 +304,19 @@ //Vagy beginnode()? - const DeletingEdgeIterator &FirstOut(const NodeIterator &n) + const DeletingEdgeIterator FirstOut(const NodeIterator &n) { EdgeIterator i; i.G=n.G;i.edge=n.G->OldGraph::FirstOut(n.n); return i; } - const DeletingEdgeIterator &FirstIn(const NodeIterator &n) + const DeletingEdgeIterator FirstIn(const NodeIterator &n) { EdgeIterator i; i.G=n.G;i.edge=n.G->OldGraph::FirstIn(n.n); return i; } - const DeletingSymEdgeIterator &FirstSym(const NodeIterator &n) + const DeletingSymEdgeIterator FirstSym(const NodeIterator &n) { EdgeIterator i; i.G=n.G;i.n=n.n; @@ -368,7 +388,7 @@ void Delete(DeletingEdgeIterator e) {e.G->OldGraph::Delete(e.e);} int NodeNum() { OldGraph::NodeNum(); } - int Clean() { OldGraph::Clean(); } + void Clean() { OldGraph::Clean(); } Graph() : _FST(this) {} }; diff -r 37117ebbabe2 -r 272a5677bd6d src/include/oldgraph.h --- a/src/include/oldgraph.h Thu Dec 11 07:24:53 2003 +0000 +++ b/src/include/oldgraph.h Sat Dec 13 15:44:50 2003 +0000 @@ -111,51 +111,52 @@ public: int NodeNum() {return nodenum;}; int EdgeNum(); - int MaxNode() {return nodes_size;}; - int FirstNode() {return firstnode;}; - int NextNode(int n) {return nodes[n].next;}; - int PrevNode(int n) {return nodes[n].prev;}; - N& operator()(int n) {return *(N*)(nodes[n].data);}; - N& Data (int n) {return *(N*)(nodes[n].data);}; + int MaxNode() const {return nodes_size;}; + int FirstNode() const {return firstnode;}; + int NextNode(int n) const {return nodes[n].next;}; + int PrevNode(int n) const {return nodes[n].prev;}; + N& operator()(int n) const {return *(N*)(nodes[n].data);}; + N& Data (int n) const {return *(N*)(nodes[n].data);}; int AddNode(); - void AddNodeBlock(int n) {for(int i=0;i=0&&n=0&&ndata);}; - E& Data (EdgePoint e) {return *(E*)(((edge_t*)e)->data);}; - int From(EdgePoint e) {return e->from;}; - int To(EdgePoint e) {return e->to;}; - EdgePoint NextIn(EdgePoint e) + E& operator()(EdgePoint e) const {return *(E*)(((edge_t*)e)->data);}; + E& Data (EdgePoint e) const {return *(E*)(((edge_t*)e)->data);}; + int From(EdgePoint e) const {return e->from;}; + int To(EdgePoint e) const {return e->to;}; + EdgePoint NextIn(EdgePoint e) const {return e->nextin;}; - EdgePoint NextOut(EdgePoint e) + EdgePoint NextOut(EdgePoint e)const {return e->nextout;}; EdgePoint AddEdge(int f, int t); void Delete(EdgePoint e); EdgePoint Edge(int f,int t); // EdgePoint Edge(E &d) // {return (EdgePoint)(((char*)&d)-(char*)&(((edge_t*)NULL)->data));}; - E& operator()(int f, int t) {return *(E*)(((edge_t*)Edge(f,t))->data);}; - E& Data(int f, int t) {return *(E*)(((edge_t*)Edge(f,t))->data);}; + E& operator()(int f, int t) const {return *(E*)(((edge_t*)Edge(f,t))->data);}; + E& Data(int f, int t) const {return *(E*)(((edge_t*)Edge(f,t))->data);}; void Delete(int f, int t) {Delete(Edge(f,t));}; void Reverse(EdgePoint e); // Functions for EdgeIndex - EdgePoint Edge(EdgeIndex i) + EdgePoint Edge(EdgeIndex i) const { return (EdgePoint)(edges[i.block]->fields+i.index);}; - EdgeIndex Index(EdgePoint e) { return e->index;}; - EdgeIndex Index(int f, int t) { EdgePoint e; return Edge(f,t)->index; } + EdgeIndex Index(EdgePoint e) const { return e->index;}; + EdgeIndex Index(int f, int t) const { EdgePoint e; return Edge(f,t)->index; } void Delete(EdgeIndex i) { Delete(Edge(i));}; - E& operator()(EdgeIndex i) + E& operator()(EdgeIndex i) const {return *(E*)(edges[i.block]->fields[i.index].data);}; - E& Data(EdgeIndex i) + E& Data(EdgeIndex i) const {return *(E*)(edges[i.block]->fields[i.index].data);}; EdgePoint AddEdge(int f, int t,EdgeIndex in); @@ -163,11 +164,11 @@ // Operators for symmetric graphs: - EdgePoint FirstEdge(int n) + EdgePoint FirstEdge(int n) const { return (EdgePoint)(FirstIn(n)?FirstIn(n):FirstOut(n));}; - EdgePoint NextEdge(int n,EdgePoint e) + EdgePoint NextEdge(int n,EdgePoint e) const { return From(e)==n?NextOut(e):(NextIn(e)?NextIn(e):FirstOut(n)); }; - int Opposite(EdgePoint e,int n) + int Opposite(EdgePoint e,int n) const { return From(e)+To(e)-n; }; // Initializers, destructors @@ -222,7 +223,6 @@ template void OldGraph::destroy() { - edge_block *oe; int i; while(firstnode!=INVALID) Delete(firstnode); diff -r 37117ebbabe2 -r 272a5677bd6d src/work/bfsdemo.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/bfsdemo.cc Sat Dec 13 15:44:50 2003 +0000 @@ -0,0 +1,83 @@ +#include +#include +#include + +using namespace NEGRO; +using namespace std; + +class IGraph +{ +public: + + // struct NodeType {bfs_node_data bfs;}; + struct NodeType {bool isVis;}; + + vector nodes; + + class NodeIterator + { + public: + IGraph *G; + int n; + NodeIterator &operator ++() { n++; return *this;} + NodeType &operator *() const { return G->nodes[n];} + NodeType *operator ->() const { return &(G->nodes[n]);} + bool isValid() const {return n<=5000;} + int Index() {return n;} //csak a kiirashoz kell + }; + + void GetFirst(NodeIterator &i) {i.G=this;i.n=1;} + + class OutEdgeIterator + { + public: + IGraph *G; + int f,t; + int gcd() { int a=f;int b=t;int c;while((c=a%b)) {a=b;b=c;} ; return b;} + OutEdgeIterator &operator ++() {while(++t<=5000&&gcd()==1);return *this;} + bool isValid() const {return t<=5000;} + NodeIterator From() const {NodeIterator i; i.G=G;i.n=f;return i;} + NodeIterator To() const {NodeIterator i; i.G=G;i.n=t;return i;} + NodeIterator Anode() const {return From();} + NodeIterator Bnode() const {return To();} + }; + + typedef OutEdgeIterator EdgeIterator; + void GetFirst(OutEdgeIterator &i,const NodeIterator &n) + {i.G=this;i.f=n.n;i.t=0;++i;} + + IGraph() : nodes(5001) {} +}; + +class IMaps_t +{ +public: +// class_element_map visited; + struct _visited_map_t { + typedef bool value_type; + void Put(const IGraph::NodeIterator &n,const value_type &t) { n->isVis=t; } + value_type Get(const IGraph::NodeIterator &n) const { return n->isVis; } + } visited; + struct _tree_map_t { + typedef IGraph::EdgeIterator value_type; + void Put(const IGraph::NodeIterator &n,const value_type &t) + { cout << t.From().Index() << "->" << t.To().Index() << '\n'; } + } tree; + do_nothing_map dist; //node->int (W) + do_nothing_map priority; //node->int (W) +}; + + +int main() +{ + IGraph IG; + IMaps_t IMaps; + + IGraph::NodeIterator in; + IG.GetFirst(in); + ++in; + bfs(IG,in,IMaps); +} diff -r 37117ebbabe2 -r 272a5677bd6d src/work/graphdemo.cc --- a/src/work/graphdemo.cc Thu Dec 11 07:24:53 2003 +0000 +++ b/src/work/graphdemo.cc Sat Dec 13 15:44:50 2003 +0000 @@ -26,106 +26,12 @@ typedef Graph TestGraph; -/* -struct isVis_map {}; -bool Get(isVis_map p,TestGraph::NodeIterator i) { return i->isVis;} -void Set(isVis_map p,TestGraph::NodeIterator i,bool b) { i->isVis=b;} -*/ - -class my_bfs_maps -{ -public: - //isVis_map visited; //node->bool (RW) - class_element_map visited; - struct _tree_map_t { - typedef TestGraph::EdgeIterator value_type; - void Set(const TestGraph::NodeIterator &n,const value_type &t) - { - cout << t.From()->id << "->" << t.To()->id << '\n'; - } - } tree; - do_nothing_map dist; //node->int (W) - do_nothing_map priority; //node->int (W) - //priority_map priority; //node->int (W) -}; - - - - -class IGraph -{ -public: - - // struct NodeType {bfs_node_data bfs;}; - struct NodeType {bool isVis;}; - - vector nodes; - - class NodeIterator - { - public: - IGraph *G; - int n; - NodeIterator &operator ++() { n++; return *this;} - NodeType &operator *() const { return G->nodes[n];} - NodeType *operator ->() const { return &(G->nodes[n]);} - bool isValid() const {return n<=5000;} - int Index() {return n;} //csak a kiirashoz kell - }; - - void GetFirst(NodeIterator &i) {i.G=this;i.n=1;} - - class OutEdgeIterator - { - public: - IGraph *G; - int f,t; - int gcd() { int a=f;int b=t;int c;while(c=a%b) {a=b;b=c;} ; return b;} - OutEdgeIterator &operator ++() {while(++t<=5000&&gcd()==1);return *this;} - bool isValid() const {return t<=5000;} - NodeIterator From() const {NodeIterator i; i.G=G;i.n=f;return i;} - NodeIterator To() const {NodeIterator i; i.G=G;i.n=t;return i;} - NodeIterator Anode() const {return From();} - NodeIterator Bnode() const {return To();} - }; - - typedef OutEdgeIterator EdgeIterator; - void GetFirst(OutEdgeIterator &i,const NodeIterator &n) - {i.G=this;i.f=n.n;i.t=0;++i;} - - IGraph() : nodes(5000) {} -}; - -class IMaps_t -{ -public: -// class_element_map visited; - struct _visited_map_t { - typedef bool value_type; - void Set(const IGraph::NodeIterator &n,const value_type &t) { n->isVis=t; } - value_type Get(const IGraph::NodeIterator &n) const { return n->isVis; } - } visited; - struct _tree_map_t { - typedef IGraph::EdgeIterator value_type; - void Set(const IGraph::NodeIterator &n,const value_type &t) - { cout << t.From().Index() << "->" << t.To().Index() << '\n'; } - } tree; - do_nothing_map dist; //node->int (W) - do_nothing_map priority; //node->int (W) -}; - -void main() +int main() { TestGraph G; - TestGraph::NodeIterator n,m,o,p,q; - TestGraph::OutEdgeIterator e,f,g,h; - int i,j,k; + TestGraph::NodeIterator n,m; + TestGraph::OutEdgeIterator e; + int i; //for(i=1;i<=10;i++) G.AddNode().n=i; //Ez nagyon rossz!!!!!!!! @@ -169,11 +75,11 @@ if(n!=m) G.AddEdge(n,m)->id=++i; ; - for(n=G.First();n.isValid();++n) + for(n=G.First();n.isValid();++n) //Demo { e=G.First(n); while(e.isValid()) - if((e->id)%2) G.Delete(e++); + if((e->id)%2) G.Delete(e++); //it may be nice to have a postfix ++ else ++e; } @@ -192,26 +98,40 @@ cout << '\n'; } - n=G.First(); - - - //G.Clean(); - - cout << "\n\n\n BFS \n\n\n"; - //my_bfs_maps Maps; - // bfs_static_maps Maps(&NodeData::bfs); + // For Marci's sake - /// bfs(G,n,Maps); - - cout << '\n'; - - IGraph IG; - IMaps_t IMaps; - - IGraph::NodeIterator in; - IG.GetFirst(in); - ++in; - bfs(IG,in,IMaps); - - + { + G.Clean(); + + for(int i=1;i<=10;i++) G.AddNode()->id=i; + + + { //I would'n say I'm really happy with this. + int i; + for(TestGraph::NodeIterator n(G);n.isValid();n++) + for(TestGraph::NodeIterator m(G);m.isValid();++m) + if(n!=m) G.AddEdge(n,m)->id=++i; + } + + for(TestGraph::NodeIterator n(G);n.isValid();++n) //Demo + { + TestGraph::OutEdgeIterator e(G,n); + while(e.isValid()) + if((e->id)%2) G.Delete(e++); //it may be nice to have a postfix ++ + else ++e; + } + + for(TestGraph::AllEdgeIterator a(G);a.isValid();++a) + cout << a->id << ": " << a.From()->id << "->" << a.To()->id << " "; + + cout << "\n\n\n"; + + for(TestGraph::NodeIterator n(G);n.isValid();++n) + { + cout << n->id << "->"; + for(TestGraph::OutEdgeIterator e(G,n);e.isValid();++e) + cout << e->id << ":" << e.To()->id << ' '; + cout << '\n'; + } + } } diff -r 37117ebbabe2 -r 272a5677bd6d src/work/makefile --- a/src/work/makefile Thu Dec 11 07:24:53 2003 +0000 +++ b/src/work/makefile Sat Dec 13 15:44:50 2003 +0000 @@ -1,3 +1,20 @@ -graphdemo: graphdemo.cc ../include/graph.h ../include/bfs.h \ +CXXFLAGS=-g -Wall -I../include + +none: + @echo 'Please use one of these forms:' + @echo ' make all' + @echo ' make clean' + @echo ' make graphdemo' + @echo ' make bfsdemo' + +all: graphdemo bfsdemo + +clean: + rm graphdemo bfsdemo +graphdemo: graphdemo.cc ../include/graph.h \ ../include/oldgraph.h makefile - g++ -g -I../include -o graphdemo graphdemo.cc + g++ $(CXXFLAGS) -o graphdemo graphdemo.cc + +bfsdemo: bfsdemo.cc ../include/graph.h ../include/bfs.h \ + ../include/oldgraph.h makefile + g++ $(CXXFLAGS) -o bfsdemo bfsdemo.cc