src/work/graphdemo.cc
changeset 276 b38f4cfa76cf
parent 3 272a5677bd6d
equal deleted inserted replaced
2:0c2a4df420bb 3:acede0ffd389
    41   
    41   
    42    //for(i=1;i<=10;i++) cout << (G.AddNode()->n=i) << ' ';
    42    //for(i=1;i<=10;i++) cout << (G.AddNode()->n=i) << ' ';
    43    //cout << '\n';
    43    //cout << '\n';
    44  
    44  
    45   i=0;
    45   i=0;
    46   for(G.GetFirst(n);n.isValid();n++)
    46   for(G.GetFirst(n);n.Valid();n++)
    47     for(G.GetFirst(m);m.isValid();++m)
    47     for(G.GetFirst(m);m.Valid();++m)
    48       if(n!=m) G.AddEdge(n,m)->id=++i;
    48       if(n!=m) G.AddEdge(n,m)->id=++i;
    49    
    49    
    50   cout << "Number of edges: " << i << "\n\n";
    50   cout << "Number of edges: " << i << "\n\n";
    51 
    51 
    52   TestGraph::AllEdgeIterator a;
    52   TestGraph::AllEdgeIterator a;
    53   for(G.GetFirst(a);a.isValid();++a)
    53   for(G.GetFirst(a);a.Valid();++a)
    54     cout << a->id << ":" << a.From()->id << "->" << a.To()->id << "   ";
    54     cout << a->id << ":" << a.From()->id << "->" << a.To()->id << "   ";
    55 
    55 
    56   cout << "\n\n\n";
    56   cout << "\n\n\n";
    57   
    57   
    58   for(G.GetFirst(n);n.isValid();++n)
    58   for(G.GetFirst(n);n.Valid();++n)
    59     {
    59     {
    60       cout << n->id << "->";
    60       cout << n->id << "->";
    61       for(G.GetFirst(e,n);e.isValid();++e)
    61       for(G.GetFirst(e,n);e.Valid();++e)
    62 	cout << e->id << ":" << e.To()->id << ' ';
    62 	cout << e->id << ":" << e.To()->id << ' ';
    63       cout << '\n';
    63       cout << '\n';
    64     }
    64     }
    65   
    65   
    66   cout << "\n\n\n\nB-verzio:\n\n\n";
    66   cout << "\n\n\n\nB-verzio:\n\n\n";
    68   G.Clean();
    68   G.Clean();
    69 
    69 
    70   for(i=1;i<=10;i++) G.AddNode()->id=i;
    70   for(i=1;i<=10;i++) G.AddNode()->id=i;
    71   
    71   
    72   i=0;
    72   i=0;
    73   for(n=G.First();n.isValid();n++)
    73   for(n=G.First();n.Valid();n++)
    74     for(m=G.First();m.isValid();++m)
    74     for(m=G.First();m.Valid();++m)
    75       if(n!=m) G.AddEdge(n,m)->id=++i;
    75       if(n!=m) G.AddEdge(n,m)->id=++i;
    76    
    76    
    77   ;
    77   ;
    78   for(n=G.First();n.isValid();++n) //Demo
    78   for(n=G.First();n.Valid();++n) //Demo
    79     {
    79     {
    80       e=G.First(n);
    80       e=G.First(n);
    81       while(e.isValid())
    81       while(e.Valid())
    82 	if((e->id)%2) G.Delete(e++);  //it may be nice to have a postfix ++
    82 	if((e->id)%2) G.Delete(e++);  //it may be nice to have a postfix ++
    83 	else ++e;
    83 	else ++e;
    84     }
    84     }
    85   
    85   
    86   // cout << "Number of edges: " << i << "\n\n";
    86   // cout << "Number of edges: " << i << "\n\n";
    87 
    87 
    88   for(a=G.First();a.isValid();++a)
    88   for(a=G.First();a.Valid();++a)
    89     cout << a->id << ": " << a.From()->id << "->" << a.To()->id << "   ";
    89     cout << a->id << ": " << a.From()->id << "->" << a.To()->id << "   ";
    90   
    90   
    91   cout << "\n\n\n";
    91   cout << "\n\n\n";
    92   
    92   
    93   for(n=G.First();n.isValid();++n)
    93   for(n=G.First();n.Valid();++n)
    94     {
    94     {
    95       cout << n->id << "->";
    95       cout << n->id << "->";
    96       for(e=G.First(n);e.isValid();++e)
    96       for(e=G.First(n);e.Valid();++e)
    97 	cout << e->id << ":" << e.To()->id << ' ';
    97 	cout << e->id << ":" << e.To()->id << ' ';
    98       cout << '\n';
    98       cout << '\n';
    99     }
    99     }
   100   
   100   
   101   // For Marci's sake
   101   // For Marci's sake
   105     
   105     
   106     for(int i=1;i<=10;i++) G.AddNode()->id=i;
   106     for(int i=1;i<=10;i++) G.AddNode()->id=i;
   107     
   107     
   108     
   108     
   109     {  //I would'n say I'm really happy with this.
   109     {  //I would'n say I'm really happy with this.
   110       int i;
   110       int i=0;
   111       for(TestGraph::NodeIterator n(G);n.isValid();n++)
   111       for(TestGraph::NodeIterator n(G);n.Valid();n++)
   112 	for(TestGraph::NodeIterator m(G);m.isValid();++m)
   112 	for(TestGraph::NodeIterator m(G);m.Valid();++m)
   113 	  if(n!=m) G.AddEdge(n,m)->id=++i;
   113 	  if(n!=m) G.AddEdge(n,m)->id=++i;
   114     }
   114     }
   115     
   115     
   116     for(TestGraph::NodeIterator n(G);n.isValid();++n) //Demo
   116     for(TestGraph::NodeIterator n(G);n.Valid();++n) //Demo
   117       {
   117       {
   118 	TestGraph::OutEdgeIterator e(G,n);
   118 	TestGraph::OutEdgeIterator e(G,n);
   119 	while(e.isValid())
   119 	while(e.Valid())
   120 	  if((e->id)%2) G.Delete(e++);  //it may be nice to have a postfix ++
   120 	  if((e->id)%2) G.Delete(e++);  //it may be nice to have a postfix ++
   121 	  else ++e;
   121 	  else ++e;
   122       }
   122       }
   123     
   123     
   124     for(TestGraph::AllEdgeIterator a(G);a.isValid();++a)
   124     for(TestGraph::AllEdgeIterator a(G);a.Valid();++a)
   125       cout << a->id << ": " << a.From()->id << "->" << a.To()->id << "   ";
   125       cout << a->id << ": " << a.From()->id << "->" << a.To()->id << "   ";
   126     
   126     
   127     cout << "\n\n\n";
   127     cout << "\n\n\n";
   128     
   128     
   129     for(TestGraph::NodeIterator n(G);n.isValid();++n)
   129     for(TestGraph::NodeIterator n(G);n.Valid();++n)
   130       {
   130       {
   131 	cout << n->id << "->";
   131 	cout << n->id << "->";
   132 	for(TestGraph::OutEdgeIterator e(G,n);e.isValid();++e)
   132 	for(TestGraph::OutEdgeIterator e(G,n);e.Valid();++e)
   133 	  cout << e->id << ":" << e.To()->id << ' ';
   133 	  cout << e->id << ":" << e.To()->id << ' ';
   134 	cout << '\n';
   134 	cout << '\n';
   135       }
   135       }
   136   }
   136   }
   137 }
   137 }