cscvs to tla changeset 1
[onak.git] / stats.c
1 /*
2  * stats.c - various routines to do stats on the key graph
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2000-2002 Project Purple
7  */
8
9 #include <stdlib.h>
10
11 #include "hash.h"
12 #include "keydb.h"
13 #include "ll.h"
14 #include "stats.h"
15
16 /**
17  *      initcolour - Clear the key graph ready for use.
18  *      @parent: Do we want to clear the parent pointers too?
19  *
20  *      Clears the parent and colour information on all elements in the key
21  *      graph.
22  */
23 void initcolour(bool parent)
24 {
25         unsigned long loop;
26         struct ll *curkey;
27
28         /*
29          * Init the colour/parent values. We get each entry list from the hash
30          * table and walk along it, zeroing the values.
31          */
32         for (loop = 0; loop < HASHSIZE; loop++) {
33                 curkey = gethashtableentry(loop);
34                 while (curkey != NULL) {
35                         ((struct stats_key *)curkey->object)->colour = 0;
36                         if (parent) {
37                                 ((struct stats_key *)curkey->object)->parent =
38                                         0;
39                         }
40                         curkey = curkey->next;
41                 }
42         }
43 }
44
45 /**
46  *      findpath - Given 2 keys finds a path between them.
47  *      @have: The key we have.
48  *      @want: The key we want to get to.
49  *
50  *      This does a breadth first search on the key tree, starting with the
51  *      key we have. It returns as soon as a path is found or when we run out
52  *      of keys; whichever comes sooner.
53  */
54 unsigned long findpath(struct stats_key *have, struct stats_key *want)
55 {
56         struct ll *keys = NULL;
57         struct ll *sigs = NULL;
58         struct ll *nextkeys = NULL;
59         long curdegree = 0;
60         long count = 0;
61         
62         curdegree = 1;
63         keys = lladd(NULL, want);
64
65         while (keys != NULL && have->colour == 0) {
66                 sigs = hash_getkeysigs(((struct stats_key *)
67                                         keys->object)->keyid);
68                 while (sigs != NULL && have->colour == 0) {
69                         /*
70                          * Check if we've seen this key before and if not mark
71                          * it and add its sigs to the list we want to look at.
72                          */
73                         if (((struct stats_key *)sigs->object)->colour == 0) {
74                                 count++;
75                                 ((struct stats_key *)sigs->object)->colour =
76                                         curdegree;
77                                 ((struct stats_key *)sigs->object)->parent =
78                                         ((struct stats_key *)
79                                          keys->object)->keyid;
80                                 nextkeys = lladd(nextkeys, sigs->object);
81                         }
82                         sigs = sigs->next;
83                 }
84                 keys = keys->next;
85                 if (keys == NULL) {
86                         keys = nextkeys;
87                         nextkeys = NULL;
88                         curdegree++;
89                 }
90         }
91
92         return count;
93 }
94
95 struct stats_key *furthestkey(struct stats_key *have)
96 {
97         unsigned long count = 0;
98         unsigned long curdegree = 0;
99         struct ll *curll, *nextll, *tmp;
100         struct ll *sigs = NULL;
101         struct stats_key *max;
102
103         if (have == NULL) {
104                 return NULL;
105         }
106
107         ++curdegree;
108
109         nextll = NULL;
110         max = have;
111         curll = lladd(NULL, have);
112
113         while (curll != NULL) {
114                 sigs = hash_getkeysigs(((struct stats_key *)
115                                 curll->object)->keyid);
116                 while (sigs != NULL) {
117                         if (((struct stats_key *) sigs->object)->colour == 0) {
118                                 /*
119                                  * We've never seen it. Count it, mark it and
120                                  * explore its subtree.
121                                  */
122                                 count++;
123                                 max = (struct stats_key *)sigs->object;
124                                 ((struct stats_key *)sigs->object)->colour = 
125                                         curdegree;
126                                 ((struct stats_key *)sigs->object)->parent = 
127                                         ((struct stats_key *)
128                                          curll->object)->keyid;
129                                 
130                                 nextll=lladd(nextll, sigs->object);
131                         }
132                         sigs=sigs->next;
133                 }
134                 tmp = curll->next;
135                 free(curll);
136                 curll = tmp;
137                 if (curll == NULL) {
138                         curll = nextll;
139                         nextll = NULL;
140                         ++curdegree;
141                 };
142         }
143
144         return max;
145 }