|
|
1.1 root 1: #include <stdio.h>
2: #include "tsort.h"
3:
4:
5: /*
6: * Remove removes the word pointed to by "wrdp". The only effect
7: * of this is to set its "name" to the empty string.
8: */
9:
10: #define remove(wrdp) ((wrdp)->name[0] = '\0')
11:
12:
13: /*
14: * Gone is used to determine if a word has already been
15: * written out. The mechanism used is that when a word
16: * has been written out, its "name" is set to the empty
17: * string.
18: */
19:
20: #define gone(wrdp) ((wrdp)->name[0] == '\0')
21:
22:
23: /*
24: * Clean is used to remove elements of a wordlist. It takes
25: * the wordlist pointed to by *"wrdlp" and removes any elements
26: * for which gone returns true.
27: */
28:
29: static void
30: clean(wrdlp)
31: register struct wordlist **wrdlp;
32: {
33: register struct worlist *wrdl;
34:
35: for (wrdl = *wrdlp; wrdl != NULL; wrdlp = &wrdl->next, wrdl = *wrdlp)
36: if (gone(wrdl->element)) {
37: do {
38: wrdl = wrdl->next;
39: } while (wrdl != NULL && gone(wrdl->element));
40: *wrdlp = wrdl;
41: if (wrdl == NULL)
42: break;
43: }
44: }
45:
46:
47: /*
48: * Pass makes one pass thru the wordlist pointed to by words,
49: * cleaning up all ancestor lists and writeing out all words
50: * with no ancestors. When it writes out a word, it "remove"s
51: * it. When the pass is completed, it makes another pass
52: * to actually take all written out words out of the list
53: * of words.
54: */
55:
56: static
57: pass()
58: {
59: register struct wordlist *wrdlp;
60: register progress = FALSE;
61:
62: for (wrdlp = words; wrdlp != NULL; wrdlp = wrdlp->next) {
63: clean(&wrdlp->element->ancestors);
64: if (wrdlp->element->ancestors == NULL) {
65: progress = TRUE;
66: printf("%s\n", wrdlp->element->name);
67: remove(wrdlp->element);
68: }
69: }
70: clean(&words);
71: return (progress);
72: }
73:
74:
75: /*
76: * Dump is used to dump out the remaining words if we find any
77: * cycles. It simply prints out all the remaining words in
78: * a random order.
79: */
80:
81: static void
82: dump()
83: {
84: register struct wordlist *wrdlp;
85:
86: for (wrdlp = words; wrdlp != NULL; wrdlp = wrdlp->next)
87: printf("%s\n", wrdlp->element->name);
88: }
89:
90:
91: /*
92: * Order is the routine which actually controls the dumping
93: * of all entries in the tree. It repeatedly calls pass until
94: * the tree is empty or nothing new can be dumped (indicateing
95: * a cycle).
96: */
97:
98: void
99: order()
100: {
101: while (pass())
102: ;
103: dump();
104: if (words != NULL)
105: die("cycles");
106: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.