doc: Add serial to list of ci file reserved words
[charm.git] / examples / pose / LBSim / edgelist.c
1 /* The data structure/ ADT for the edge-list */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "typedefs.h"
5
6 extern VerticesListType graph;
7
8 void * InitEdgeList(E)
9 int E;
10 {
11   EdgeListType * edgesRec;
12
13   edgesRec = (EdgeListType *) malloc(sizeof(EdgeListType));
14   _MEMCHECK(edgesRec);
15   edgesRec->next = 0;
16   edgesRec->edges = (Edge *) malloc(E*sizeof(Edge));
17   _MEMCHECK(edgesRec->edges);
18   return(edgesRec);
19 }
20
21 void addEdge(EdgeList, v,w)
22      EdgeListType * EdgeList;
23      int v;
24      int w;
25 { int n, index;
26   n = EdgeList->next;
27   EdgeList->next++;
28
29   // printf("adding edge: (%d, %d)\n", v, w); 
30   (EdgeList->edges[n]).node1 = v;
31   (EdgeList->edges[n]).node2 = w;
32    index =  graph.vertexArray[v].next++;
33    graph.adjArray[ index ] = w;
34    index =  graph.vertexArray[w].next++;
35    graph.adjArray[ index ] = v;
36
37    graph.vertexArray[v].degree++;
38    graph.vertexArray[w].degree++;
39 }
40
41 void printEdges(EdgeList)
42      EdgeListType * EdgeList;
43 {int i;
44  Edge * edges;
45  edges = EdgeList->edges;
46  for (i=0; i< (EdgeList->next ); i++)
47    {printf("%d\t%d\n", edges[i].node1, edges[i].node2);
48   }
49 }
50
51 int edgeExists(x,y)
52 {
53   int i, ind;
54   ind = graph.vertexArray[x].adjListInd; 
55   
56   for(i=0; i< graph.vertexArray[x].degree; i++)
57     { if (graph.adjArray[ind + i] == y) return 1;}
58   
59   return 0;
60 }
61
62 /*
63   If we are adding an edge between two nodes already connected then we 
64   need special changes. We open some distinct edge say x,y and 
65   connect v with y and x with w
66 */
67
68 void addspEdge(EdgeList, v,w)
69      EdgeListType * EdgeList;
70      int v;
71      int w;
72 { int n, index,i,x,y,ind;
73   n = EdgeList->next;
74   EdgeList->next++;
75
76   // printf("adding special  edge: (%d, %d)\n", v, w); 
77   /*((EdgeList->edges)[n]).node1 = v;*/
78   /*(EdgeList->edges[n]).node2 = w;*/
79    for (i=0;i<n-1;i++)
80      if (((EdgeList->edges[i]).node1!=v) && ((EdgeList->edges[i]).node2!=w)) 
81         {
82            // x=(EdgeList->edges[i]).node1;
83
84              //printf("%d\n",graph.vertexArray[x].degree);
85
86             x=(EdgeList->edges[i]).node1;
87             y=(EdgeList->edges[i]).node2;       
88             (EdgeList->edges[i]).node2=w;
89             (EdgeList->edges[n]).node1=v;
90             (EdgeList->edges[n]).node2=y;                
91
92             ind =  graph.vertexArray[x].adjListInd;
93            // printf("%d %d %d\n",x,y,graph.vertexArray[x].degree);
94             for(i=0; i< graph.vertexArray[x].degree; i++)
95                     { if (graph.adjArray[ind + i] == y) graph.adjArray[ind+i]=w;}
96
97             ind =  graph.vertexArray[y].adjListInd;
98             for(i=0; i< graph.vertexArray[y].degree; i++)
99                     { if (graph.adjArray[ind + i] == x) graph.adjArray[ind+i]=v;}
100         
101             index =  graph.vertexArray[v].next++;
102             graph.adjArray[ index ] = y;
103             index =  graph.vertexArray[w].next++;
104             graph.adjArray[ index ] = x;
105             graph.vertexArray[v].degree++;
106             graph.vertexArray[w].degree++;
107             break;
108          
109         }
110 }
111