src/work/alpar/list_graph_demo.cc
author ladanyi
Thu, 16 Sep 2004 19:51:28 +0000
changeset 875 fda944f15ca7
parent 401 2d0cccf7cc94
child 880 9d0bfd35b97c
permissions -rw-r--r--
Changed to conform to the new iterator style.
alpar@397
     1
#include<list_graph.h>
alpar@397
     2
#include<skeletons/graph.h>
alpar@397
     3
alpar@397
     4
#include <iostream>
alpar@397
     5
#include <vector>
alpar@397
     6
alpar@397
     7
using namespace hugo;
alpar@397
     8
alpar@397
     9
typedef ListGraph Graph;
alpar@397
    10
//typedef GraphSkeleton Graph;
alpar@397
    11
alpar@397
    12
alpar@397
    13
Graph::OutEdgeIt safeFirstOut(const Graph &G, Graph::Node n)
alpar@397
    14
{
alpar@397
    15
  return G.valid(n) ? Graph::OutEdgeIt(G,n):INVALID;
alpar@397
    16
}
alpar@397
    17
alpar@397
    18
int main()
alpar@397
    19
{
alpar@397
    20
alpar@397
    21
  typedef Graph::Edge Edge;
alpar@397
    22
  typedef Graph::InEdgeIt InEdgeIt;
alpar@397
    23
  typedef Graph::OutEdgeIt OutEdgeIt;
alpar@397
    24
  typedef Graph::EdgeIt EdgeIt;
alpar@397
    25
  typedef Graph::Node Node;
alpar@397
    26
  typedef Graph::NodeIt NodeIt;
alpar@397
    27
  
alpar@397
    28
  Graph G;
alpar@397
    29
  
alpar@397
    30
  {
alpar@397
    31
    NodeIt n;
alpar@397
    32
alpar@397
    33
    for(int i=0;i<10;i++) G.addNode();
alpar@397
    34
    for(G.first(n);G.valid(n);G.next(n)) 
alpar@397
    35
      for(NodeIt m(G);m!=INVALID;G.next(m)) 
alpar@397
    36
	if(n!=m) G.addEdge(n,m);
alpar@397
    37
    
alpar@397
    38
    OutEdgeIt e = safeFirstOut(G,n);
alpar@397
    39
    OutEdgeIt f = safeFirstOut(G,NodeIt(G));
alpar@397
    40
    
alpar@397
    41
    
alpar@397
    42
    InEdgeIt i(INVALID), j;
alpar@397
    43
    InEdgeIt ii(i);
alpar@397
    44
    ii=G.first(i,n);
alpar@397
    45
    ii=G.next(i);
alpar@397
    46
    
alpar@397
    47
    OutEdgeIt o(INVALID), oo;
alpar@397
    48
    OutEdgeIt ooo(oo);
alpar@397
    49
    oo=G.first(o,n);
alpar@397
    50
    oo=G.next(o);
alpar@397
    51
    
alpar@397
    52
    EdgeIt ei(INVALID), eie;
alpar@397
    53
    EdgeIt eiee(ei);
alpar@397
    54
    eie=G.first(ei);
alpar@397
    55
    eie=G.next(ei);
alpar@397
    56
    
alpar@397
    57
    Edge eee(i);
alpar@397
    58
    eee=o;
alpar@397
    59
    eee=eie;
alpar@397
    60
    
alpar@397
    61
    
alpar@397
    62
    bool tm;
alpar@397
    63
    tm = G.valid(n) && G.valid(i) && G.valid(o) && G.valid(ei);
alpar@397
    64
    
alpar@397
    65
    std::vector<InEdgeIt> v(10);
alpar@397
    66
    std::vector<InEdgeIt> w(10,INVALID);
alpar@397
    67
    
alpar@397
    68
  }
alpar@397
    69
  
alpar@397
    70
  // Test of maps
alpar@397
    71
alpar@397
    72
  G.clear();
alpar@397
    73
  
alpar@397
    74
  for(int i=0;i<10;i++) G.addNode();
alpar@397
    75
  for(NodeIt i(G);G.valid(i);G.next(i)) 
alpar@397
    76
    for(NodeIt j(G);G.valid(j);G.next(j)) 
alpar@397
    77
      if(i<j) G.addEdge(i,j);           //The iterators are comparable
alpar@397
    78
  
alpar@397
    79
  Graph::NodeMap<int> n(G);
alpar@397
    80
  int count=0;
alpar@397
    81
  for(NodeIt i(G);G.valid(i);G.next(i)) n[i]=count++;
alpar@397
    82
  
alpar@397
    83
  Graph::NodeMap<int> nn=n;
alpar@397
    84
  Graph::NodeMap<double> dd=n;
alpar@397
    85
alpar@397
    86
  n = nn;
alpar@397
    87
  
alpar@397
    88
  dd = nn;
alpar@397
    89
  
alpar@397
    90
  Graph::EdgeMap<int> emap(G);
alpar@397
    91
alpar@397
    92
  // Test of SymListGraph
alpar@397
    93
  
alpar@397
    94
  {
alpar@397
    95
    typedef SymListGraph Graph;
alpar@397
    96
    typedef Graph::Edge Edge;
alpar@397
    97
    typedef Graph::InEdgeIt InEdgeIt;
alpar@397
    98
    typedef Graph::OutEdgeIt OutEdgeIt;
alpar@397
    99
    typedef Graph::EdgeIt EdgeIt;
alpar@397
   100
    typedef Graph::Node Node;
alpar@397
   101
    typedef Graph::NodeIt NodeIt;
alpar@397
   102
alpar@397
   103
    Graph G;
alpar@397
   104
alpar@397
   105
    for(int i=0;i<10;i++) G.addNode();
alpar@397
   106
    for(NodeIt i(G);G.valid(i);G.next(i)) 
alpar@397
   107
      for(NodeIt j(G);G.valid(j);G.next(j)) 
alpar@397
   108
	if(i<j) G.addEdge(i,j);           //The iterators are comparable
alpar@397
   109
  
alpar@397
   110
    Graph::EdgeMap<int> em(G);
alpar@397
   111
    Graph::SymEdgeMap<int> sm(G);
alpar@397
   112
    for(EdgeIt e(G);G.valid(e);G.next(e)) em[e]=G.id(e);
alpar@397
   113
    for(EdgeIt e(G);G.valid(e);G.next(e))
alpar@397
   114
      if(G.tail(e)<G.head(e)) sm[e]=G.id(e);
alpar@397
   115
    
alpar@397
   116
    for(EdgeIt e(G);G.valid(e);G.next(e))
alpar@397
   117
      std::cout << G.id(G.tail(e)) << "->" << G.id(G.head(e))
alpar@397
   118
		<< ": id=" << G.id(e) << " oppid=" << G.id(G.opposite(e))
alpar@397
   119
		<< " em=" << em[e]
alpar@397
   120
		<< " sm=" << sm[e] << "\n";
alpar@397
   121
    
alpar@426
   122
    //Delete all nodes
alpar@426
   123
    NodeIt n;
alpar@426
   124
    while(G.valid(G.first(n))) G.erase(n);
alpar@397
   125
  }
alpar@401
   126
alpar@401
   127
  // Tests for NodeSet and EdgeSet
alpar@401
   128
  
alpar@401
   129
  {
alpar@401
   130
    NodeSet N;
alpar@401
   131
    
alpar@401
   132
    typedef EdgeSet<NodeSet> ES;
alpar@401
   133
    
alpar@401
   134
    ES E(N);
alpar@401
   135
    ES F(N);
alpar@401
   136
    for(int i=0;i<10;i++) G.addNode();
alpar@401
   137
    
alpar@401
   138
    for(ES::NodeIt n(E);E.valid(n);E.next(n))
alpar@401
   139
      for(ES::NodeIt m(E);E.valid(m);E.next(m))
alpar@401
   140
	if(n!=m) F.addEdge(n,m);
alpar@401
   141
    for(ES::NodeIt n(F);F.valid(n);F.next(n))
alpar@401
   142
      for(ES::NodeIt m(F);F.valid(m);F.next(m))
alpar@401
   143
	if(n<m) F.addEdge(n,m);
alpar@401
   144
    
alpar@401
   145
alpar@401
   146
    NodeSet::NodeMap<int> nm1(N);
alpar@401
   147
    ES::NodeMap<int> nm2(E);
alpar@401
   148
    ES::EdgeMap<int> eme(E);
alpar@401
   149
    ES::EdgeMap<int> emf(F);
alpar@401
   150
    
alpar@401
   151
       
alpar@401
   152
  }
alpar@397
   153
  
alpar@397
   154
}