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