00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #pragma once
00013
00014 # include <stack>
00015 # include <realroot/Seq.hpp>
00016
00017 # define NODESTACK std::stack<gNode<T> *>
00018 # define Viewer viewer<axel, K>
00019
00020
00021 namespace mmx {
00022 namespace shape {
00023
00024 template<class T> class Graph;
00025 template<class T> class GraphDfsIter;
00026
00027
00029
00031 template<class T>
00032 class gNode
00033 {
00034
00035 friend class Graph<T>;
00036 friend class GraphDfsIter<T>;
00037
00038 private:
00039 T data;
00040 gNode<T>* vlist;
00041 gNode<T>* next;
00042 gNode<T>* me;
00043 gNode<T>* start;
00044
00045 int m_status;
00046 int m_aux;
00047
00048 public:
00049 gNode(): vlist (NULL), next (NULL), me (this), start (this) {};
00050
00051 gNode(T val)
00052 {
00053 this->data =val;
00054 this->vlist=NULL;
00055 this->next =NULL;
00056 this->me =this;
00057 this->start=this;
00058 this->m_status =2009;
00059 this->m_aux =0;
00060 }
00061
00062 inline T get_data() {return this->data;};
00063
00064
00066 inline bool adj(T& v) {
00067
00068 gNode<T>* t;
00069 t= this->next;
00070
00071 while(t!=NULL)
00072 {
00073 if(t->vlist->data==v)
00074 return true;
00075 t=t->next;
00076 }
00077 return false;
00078 };
00079
00081 inline void status(int i)
00082 {
00083 if (this->start == NULL) {
00084 std::cerr << "internal bug\n";
00085 exit (-1);
00086 }
00087 this->start->m_status=i;
00088 };
00089
00091 inline int status()
00092 {
00093 return this->start->m_status;
00094 };
00095
00097 inline void aux(int i)
00098 {
00099 this->start->m_aux=i;
00100 };
00101
00103 inline int aux()
00104 {
00105 return this->start->m_aux;
00106 };
00107
00108 };
00109
00110
00111
00113
00115 template<class T>
00116 class Graph
00117 {
00118 friend class GraphDfsIter<T>;
00119 typedef void (*func)(gNode<T> *);
00120 typedef bool (*test_node)(gNode<T> *);
00121
00122 private:
00123 gNode<T>* head;
00124 unsigned nvertices, nedges;
00125
00126
00127 public:
00128
00129
00130 Graph()
00131 {
00132 nvertices=nedges=0;
00133 head=NULL;
00134 }
00135
00136 void vertex_list ( Seq<T> & v) const ;
00137 void edge_list ( Seq<T> & e) const ;
00138 Seq<T> neighbors( T e);
00139
00140 gNode<T>* push_vertex(T val, int i=0);
00141 void push_edge(T v_val,T e_val);
00142 inline void push_uedge(T v_val,T e_val)
00143 {push_edge(v_val,e_val); push_edge(e_val,v_val);};
00144
00145
00146 inline unsigned nbv() const { return nvertices;};
00147 inline unsigned nbe() const { return nedges; };
00148
00149 inline T next(T val) const ;
00150 inline bool member(T val) const ;
00151
00152 void delete_edge(T v_val,T e_val);
00153 void delete_vertex(T val);
00154
00155
00156 void dfs(func fp);
00157 void dfs_walk(gNode<T> * s, func fp);
00158
00159
00160 gNode<T>* dfs( test_node fp );
00161
00162
00163 void dfs(Seq<T> & out);
00164 void dfs_walk(gNode<T> * s, Seq<T> & out);
00165
00166
00167 bool set_start_node(T& val);
00168 bool set_start_node(T& v_val,T& e_val);
00169 gNode<T> * get_start_node();
00170
00171 GraphDfsIter<T> * createIterator() const;
00172
00173
00175 void print()
00176 {
00177 std::cout<< "Graph: "<< std::endl;
00178 gNode<T> *s, *t=head;
00179 while(t!=NULL)
00180 {
00181 std::cout<< t->data<<" ===> ";
00182 s=t->next;
00183 while(s!=NULL)
00184 { std::cout<< s->data<<", "; s=s->next;}
00185 std::cout<< std::endl;
00186 t=t->vlist;
00187 }
00188 std::cout<< std::endl;
00189 }
00190
00191
00192 private:
00193
00194 static void discover(gNode<T> * v);
00195
00196 };
00197
00198
00200 template<class T>
00201 class GraphDfsIter
00202 {
00203 const Graph<T> *g;
00204 gNode<T> * current, * next_start;
00205 NODESTACK q;
00206
00207 public:
00208
00209 GraphDfsIter(const Graph<T> s)
00210 {
00211 g = &s;
00212 }
00213
00214 void first()
00215 {
00216 current = g->head;
00217 next_start=current->vlist;
00218
00219 gNode<T>* t=current;
00220 while(t!=NULL)
00221 {
00222 t->status(-1);
00223 t=t->vlist;
00224 }
00225 current->status(0);
00226 }
00227
00228 bool isLast()
00229 {
00230 if ( !q.empty() )
00231 return false;
00232
00233 while( next_start!=NULL)
00234 {
00235 if ( next_start->status()==-1)
00236 {
00237 next_start->status(0);
00238 q.push(next_start);
00239 next_start=next_start->vlist;
00240 break;
00241 }
00242 next_start=next_start->vlist;
00243 }
00244
00245 if (q.empty() && next_start==NULL)
00246 return true;
00247 else
00248 return false;
00249 }
00250
00251 void next()
00252 {
00253
00254 if ( q.empty() )
00255 {
00256
00257 while( next_start!=NULL)
00258 {
00259 if ( next_start->status()==-1)
00260 {
00261 next_start->status(0);
00262 q.push(next_start);
00263 next_start=next_start->vlist;
00264 break;
00265 }
00266 next_start=next_start->vlist;
00267 }
00268
00269 if (q.empty() && next_start==NULL)
00270 {
00271 current= NULL;
00272 return;
00273 }
00274
00275 }
00276
00277
00278 current= q.top();
00279 q.pop();
00280
00281 gNode<T>* t=current;
00282 while(t->next!=NULL)
00283 {
00284 if ( t->next->status() == -1 )
00285 {
00286 t->next->status(0);
00287 q.push(t->next->start);
00288 }
00289 t=t->next;
00290 }
00291
00292 return;
00293 }
00294
00295 bool isDone()
00296 {
00297 return current == NULL;
00298 }
00299
00300 gNode<T> * currentItem()
00301 {
00302 return current;
00303 }
00304 };
00305
00306 template<class T>
00307 GraphDfsIter<T> *Graph<T>::createIterator() const
00308 {
00309 return new GraphDfsIter<T>(this);
00310 }
00311
00313 template<class T>
00314 gNode<T>* Graph<T>::push_vertex(T val,int i)
00315 {
00316 gNode<T>* temp=new gNode<T>(val);
00317 temp->aux(i);
00318
00319 nvertices++;
00320 if(head==NULL)
00321 {
00322 head=temp;
00323 head->me=temp;
00324 return temp;
00325 }
00326
00327 gNode<T>* t=head;
00328 while(t->vlist!=NULL)
00329 t=t->vlist;
00330
00331 t->vlist=temp;
00332 temp->start=temp;
00333 temp->me=temp;
00334 return temp;
00335 }
00336
00338 template<class T>
00339 void Graph<T>::push_edge(T v_val,T e_val)
00340 {
00341 if(head==NULL)
00342 return;
00343 nedges++;
00344
00345 gNode<T>* t=head;
00346
00347 while(t!=NULL)
00348 {
00349 if(t->data==v_val)
00350 {
00351 gNode<T>* temp=new gNode<T>(e_val);
00352
00353 while(t->next!=NULL)
00354 t=t->next;
00355 t->next=temp;
00356
00357 t=head;
00358 while(t!=NULL)
00359 {
00360 if(t->data==e_val)
00361 {
00362 temp->me=t->me;
00363 t->me=temp;
00364
00365 temp->start=t;
00366 }
00367 t=t->vlist;
00368 }
00369
00370 return;
00371 }
00372 t=t->vlist;
00373 }
00374 }
00375
00377 template<class T>
00378 void Graph<T>::vertex_list( Seq<T>& v) const
00379 {
00380 gNode<T>* t= head;
00381
00382 while(t!=NULL)
00383 {
00384 v << t->data;
00385 t=t->vlist;
00386 }
00387 }
00388
00389
00390 template<class T>
00391 void Graph<T>::edge_list (Seq<T>& e) const
00392 {
00393 gNode<T>*s, *t=head;
00394 while( t!=NULL)
00395 {
00396 s=t;
00397 while ( s->next!=NULL)
00398 {
00399 e<< t->data << s->next->data ;
00400 s=s->next;
00401 }
00402 t=t->vlist;
00403 }
00404 }
00405
00406
00407 template<class T>
00408 Seq<T> Graph<T>::neighbors ( T v_val)
00409 {
00410 Seq<T> e;
00411 gNode<T>*s, *t=head;
00412 while( t!=NULL)
00413 {
00414 if(t->data==v_val)
00415 {
00416 s=t;
00417 while ( s->next!=NULL)
00418 {
00419
00420 e<< s->next->data ;
00421 s=s->next;
00422 }
00423 }
00424 t=t->vlist;
00425 }
00426 return e;
00427 }
00428
00429
00430 template<class T> inline T
00431 Graph<T>::next(T val) const {
00432 gNode<T> *t=head;
00433 while( t!=NULL){
00434 if(t->data==val)
00435 { if (t->next==NULL)
00436 std::cout<<"no neighbor!"<<std::endl;
00437 return t->next->data;
00438 }
00439 t=t->vlist;
00440 }
00441 std::cout<< "Node not in graph."<<std::endl;
00442 return NULL;
00443 }
00444
00445
00446 template<class T> inline bool
00447 Graph<T>::member(T val) const {
00448 gNode<T> *t=head;
00449 while( t!=NULL)
00450 if(t->data==val)
00451 return true;
00452 else
00453 t=t->vlist;
00454 return false;
00455 }
00456
00457
00458
00459
00460 template<class T>
00461 void Graph<T>::discover(gNode<T> * v)
00462 {
00463
00464 }
00465
00467 template<class T>
00468 void Graph<T>::delete_edge(T v_val,T e_val)
00469 {
00470 if(head==NULL)
00471 return;
00472
00473 gNode<T> *s, *t=head;
00474
00475 while(t!=NULL)
00476 {
00477 if(t->data==v_val)
00478 {
00479 s=t;
00480 t=t->next;
00481 while(t!=NULL)
00482 {
00483 if(t->data==e_val)
00484 {
00485 s->next=t->next;
00486 t->vlist=NULL;
00487 delete t;
00488 nedges--;
00489 return;
00490 }
00491 s=t;
00492 t=t->next;
00493 }
00494 return;
00495 }
00496 t=t->vlist;
00497 }
00498
00499 }
00500
00502 template<class T>
00503 void Graph<T>::delete_vertex(T v_val)
00504 {
00505 if(head==NULL)
00506 return;
00507 nvertices--;
00508
00509 gNode<T>* e=head;
00510
00511
00512 while(e!=NULL)
00513 {
00514 delete_edge(e->data,v_val);
00515 e=e->vlist;
00516 }
00517
00518
00519 e=head;
00520 while(e!=NULL)
00521 {
00522 delete_edge(v_val,e->data);
00523 e=e->vlist;
00524 }
00525
00526
00527 if(head->data==v_val)
00528 {
00529 gNode<T>* temp=head;
00530 head=head->vlist;
00531 delete temp;
00532 return;
00533 }
00534
00535 gNode<T>* p1=head->vlist;
00536 gNode<T>* p2=head;
00537
00538 while(p1!=NULL)
00539 {
00540 if(p1->data==v_val)
00541 {
00542 p2->vlist=p1->vlist;
00543 p1=NULL;
00544 delete p1;
00545 return;
00546 }
00547 p2=p1;
00548 p1=p1->vlist;
00549 }
00550 }
00551
00552
00553 template<class T>
00554 gNode<T> * Graph<T>::get_start_node()
00555 {
00556 return head;
00557 }
00558
00559 template<class T>
00560 bool Graph<T>::set_start_node(T& val)
00561 {
00562 gNode<T>* k=head;
00563
00564 while(k->vlist!=NULL)
00565 {
00566 if(k->vlist->data==val)
00567 break;
00568 k=k->vlist;
00569 }
00570 if (k->vlist==NULL) return false;
00571
00572 gNode<T> *u= k->vlist;
00573 k->vlist = u->vlist;
00574 u->vlist = this->head;
00575 this->head= u;
00576 }
00577
00578 template<class T>
00579 bool Graph<T>::set_start_node(T& v_val,T& e_val)
00580 {
00581 gNode<T>* k=this->head->next;
00582
00583 if (!set_start_node(v_val)) return false;
00584
00585 while(k->next!=NULL)
00586 {
00587 if(k->next->data==e_val)
00588 break;
00589 k=k->next;
00590 }
00591 if (k->next==NULL) return false;
00592
00593 gNode<T> *u= k->next;
00594 k->next = u->next;
00595 u->next = this->head->next;
00596 this->head->next= u;
00597 }
00598
00599
00601
00603 template<class T>
00604 void Graph<T>::dfs( func fp )
00605 {
00606 gNode<T>* t=head;
00607
00608 while(t!=NULL)
00609 {
00610 t->status(-1);
00611 t=t->vlist;
00612 }
00613
00614 t=head;
00615 while(t!=NULL)
00616 {
00617 if (t->status()==-1) dfs_walk(t,fp);
00618 t=t->vlist;
00619 }
00620 }
00621
00622 template<class T>
00623 void Graph<T>::dfs_walk(gNode<T> * s, func fp)
00624 {
00625 NODESTACK q;
00626 gNode<T> * t;
00627
00628 q.push(s);
00629 s->status(0);
00630
00631 while ( !q.empty() )
00632 {
00633 t=q.top();
00634 q.pop();
00635
00636 if (fp!=NULL) (*fp)(t);
00637
00638
00639 while(t->next!=NULL)
00640 {
00641 if (t->next->status()==-1)
00642 {
00643 t->status(0);
00644 q.push(t->next->start);
00645 }
00646 t=t->next;
00647 }
00648 }
00649 }
00650
00652
00654 template<class T>
00655 void Graph<T>::dfs(Seq<T>& out)
00656 {
00657 gNode<T>* t=head;
00658
00659 while(t!=NULL)
00660 {
00661 t->status(-1);
00662 t=t->vlist;
00663 }
00664
00665 t=head;
00666 while(t!=NULL)
00667 {
00668 if (t->status()==-1)
00669 dfs_walk(t,out);
00670 t=t->vlist;
00671 }
00672 }
00673
00674 template<class T>
00675 void Graph<T>::dfs_walk(gNode<T> * s, Seq<T>& out)
00676 {
00677 NODESTACK q;
00678 gNode<T> * t;
00679
00680 q.push(s);
00681
00682 while ( !q.empty() )
00683 {
00684 t=q.top();
00685 q.pop();
00686 if ( t->status()==-1)
00687 {
00688 out<< t->data;
00689 t->status(0);
00690 }
00691 while(t->next!=NULL)
00692 {
00693 if ( t->next->status() == -1 )
00694 {
00695 q.push(t->next->start);
00696 }
00697 t=t->next;
00698 }
00699
00700 }
00701 }
00702
00704
00706 template<class T>
00707 gNode<T>* Graph<T>::dfs( test_node fp )
00708 {
00709 gNode<T>* t=head;
00710
00711 while(t!=NULL)
00712 {
00713 t->status(-1);
00714 t=t->vlist;
00715 }
00716
00717 t=head;
00718 while(t!=NULL)
00719 {
00720 if (t->status()==-1)
00721 {
00722 NODESTACK q;
00723 gNode<T> * s;
00724
00725 q.push(t);
00726 t->status(0);
00727
00728 while ( !q.empty() )
00729 {
00730 s=q.top();
00731 q.pop();
00732
00733
00734 if (fp!=NULL)
00735 if ( (*fp)(s) )
00736 return s;
00737
00738 while(t->next!=NULL)
00739 {
00740 if (t->next->status()==-1)
00741 {
00742 t->status(0);
00743 q.push(t->next->start);
00744 }
00745
00746 t=t->next;
00747 }
00748 }
00749 }
00750 t=t->vlist;
00751 }
00752
00753 return NULL;
00754 }
00756
00757
00758 template<class T>
00759 void write_edge(gNode<T>* v)
00760 {
00761 std::cout<<"Ok"<<std::endl;
00762 }
00763
00764 template<class O, class V> struct viewer; struct axel;
00765
00766 template<class K, class T> Viewer&
00767 operator<<(Viewer& out, const Graph<T>& g) {
00768
00769
00770
00771
00772 if (g.nbe()==0) return out;
00773
00774 Seq<T> edges;
00775 g.edge_list(edges);
00776
00777 out<<" <curve type=\"mesh\">\n<vect>\nVECT\n";
00778 out<<g.nbe()<<" "
00779
00780 <<2*g.nbe()<<" "
00781 <<g.nbe()<<"\n";
00782
00783 for(unsigned i=0; i<g.nbe();i+=2) out<<"2 ";
00784 out<<"\n";
00785
00786
00787
00788 for(unsigned i=0; i<g.nbe();i+=2) out<<"1 ";
00789 out<<"\n";
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806 for (unsigned i=0;i<edges.size(); i+=2)
00807 {
00808 out <<edges[i]->x() <<" "<<edges[i]->y() <<" 0 "
00809 <<edges[i+1]->x()<<" "<<edges[i+1]->y()<<" 0 "
00810 <<"\n";
00811 }
00812
00813
00814 for(unsigned i=0; i<g.nbe();i++)
00815 out<< "0.314 0.979 1 1\n";
00816
00817 out<<" </vect>\n </curve>\n";
00818
00819 return out;
00820 }
00821
00822
00823 } ;
00824 } ;
00825
00826 #undef NODESTACK
00827 #undef Viewer