File: tree.c

package info (click to toggle)
mgt 2.31-7
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 460 kB
  • ctags: 582
  • sloc: ansic: 4,852; sh: 411; pascal: 329; makefile: 88; csh: 3
file content (351 lines) | stat: -rw-r--r-- 5,442 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* "mgt" Copyright (c) 1991 Shodan  */

#include "mgt.h"


static int newNodeNum;


FUNCTION boolean getCoord(x, y, list)
int x, y;
coordList *list;
{
   for (; list; list = list->next)
      if (list->x == x && list->y == y)
	 return true;
   return false;
}



FUNCTION coordList *addCoord(x, y)
int x, y;
{
   coordList *co;

   if (!(co = (coordList *) malloc(sizeof(coordList))))
      barf("Memory allocation failure (addCoord)");
   co->next = 0;
   co->x = x;
   co->y = y;
   return co;
}



FUNCTION void setCoord(x, y, startlist)
int x, y;
coordList **startlist;
{
   coordList *list;
   if (!*startlist)
      *startlist = addCoord(x, y);
   else {
      for (list = *startlist; list->next; list = list->next)
	 if (x == list->x && y == list->y)
	    return;
      list->next = addCoord(x, y);
   }
}


FUNCTION void clearCoord(x, y, startlist)
int x, y;
coordList **startlist;

{
   coordList *last, *list;

   last = 0;
   list = *startlist;
   while (list && (x != list->x || y != list->y)) {
      last = list;
      list = list->next;
   }
   if (list) {
      if (last)
	 last->next = list->next;
      else
	 *startlist = list->next;
      free(list);
   }
}


FUNCTION void initNodes()
{
   newNodeNum = 0;
}

FUNCTION nodep newNode()
{
   nodep new;
#ifdef DEBUG
   totalmemory += sizeof(node);
   fprintf(debug, "%ld %ld\n", totalmemory, coreleft());
#endif
   new = (nodep) calloc(1, sizeof(node));
   if (!new)
      barf("Memory allocation failure (newnode)");
   new->nodeNum = newNodeNum++;
   return new;
}

FUNCTION void freeNode(n)
nodep n;
{
   if (n) {
      freeNode(n->nextSibling);
      freeNode(n->child);
      delNode(n);
   }
}

FUNCTION char *dupStr(s)
char *s;
{
   char *c;
#ifdef DEBUG
   totalmemory += strlen(s) + 1;
   fprintf(debug, "%ld %ld\n", totalmemory, coreleft());
#endif
   c = (char *) malloc((unsigned) strlen(s) + 1);
   if (!c)
      barf("Memory allocation failure (dupstr)");
   strcpy(c, s);
   return c;
}



FUNCTION void freeProps(n)
nodep n;
{
   property *prop, *lastprop;
   coordList *cl, *lastcl;
   prop = n->p;
   while (prop) {
      switch (prop->t) {
	 case t_AddEmpty:
	 case t_AddBlack:
	 case t_AddWhite:
	 case t_Mark:
	 case t_Letter:
	 case t_Black:
	 case t_White:
	    cl = prop->data.stones;
	    while (cl) {
	       lastcl = cl;
	       cl = cl->next;
	       free(lastcl);
	    }
	    break;
	 case t_Name:
	 case t_Comment:
	    free(prop->data.comment);
	    break;

      }

      lastprop = prop;
      prop = prop->next;
      free(lastprop);
   }
}


FUNCTION void delNode(n)
nodep n;
{
   freeProps(n);
   free(n);
}

FUNCTION void addprop(n, p)
nodep n;
property *p;
{
   p->next = n->p;
   n->p = p;
}

FUNCTION property *getprop(n, t)
nodep n;
Token t;
{
   property *p;
   p = n->p;
   while (p && p->t != t)
      p = p->next;
   return p;
}

FUNCTION int treeCountSiblings(n)
nodep n;
{
   int i;
   nodep n1;

   n1 = n->child;
   i = 0;
   while (n1) {
      n1 = n1->nextSibling;
      i++;
   }
   return i;
}

FUNCTION nodep nthChild(n, c)	/* nodep, int */
nodep n;
int c;
{

   if (n->child) {
      n = child(n);
      while (c-- && n)
	 n = nextSibling(n);
   } else {
      n = 0;
   }
   return n;
}

/* TREE WALK functions KEEP TRACK OF DEPTH AS WELL */

FUNCTION nodep parent(n)
nodep n;
{
   return n->parent;
}

FUNCTION nodep child(n)
nodep n;
{
   return n->child;
}

FUNCTION nodep lastSibling(n)
nodep n;
{
   return n->lastSibling;
}

FUNCTION nodep nextSibling(n)
nodep n;
{
   return n->nextSibling;
}

FUNCTION nodep treeLastSibling(n)
nodep n;
{
   while (n->nextSibling) {
      n = n->nextSibling;
   }
   return n;
}

/* go to next node down the tree. Stop if at bottom. */
FUNCTION nodep treeDown(n)
nodep n;
{
   if (n->child) {
      BUG("treeDown: going down\n");
      return child(n);
   } else {
      BUG("treeDown: stop\n");
      return n;
   }
}

/* backup, stop if at top */
FUNCTION nodep treeUp(n)
nodep n;
{
   if (n->parent) {
      BUG("treeUp: going up\n");
      return parent(n);
   } else {
      BUG("treeUp: staying\n");
      return n;
   }
}


/* go to next node backing up the tree only */
FUNCTION nodep treeNextUp(n)
nodep n;
{
   if (n->nextSibling) {
      BUG("nextSibling ");
      return nextSibling(n);
   } else if (n->parent) {
      BUG("nextup-parent ");
      while (n->parent && !n->nextSibling)
	 n = parent(n);
      if (n->nextSibling)
	 return nextSibling(n);
      else
	 return n;
   } else if (n->child) {
      BUG("child ");
      return child(n);
   } else {
      BUG("current ");
      return n;
   }
}


/* go to next node, backup if neccessary */
FUNCTION nodep treeNext(n)
nodep n;
{
   nodep r;

   BUG("treeNext:");
   if (n != (r = treeDown(n))) {
      BUG("going down ");
   } else {
      BUG("going up ");
      r = treeNextUp(n);
   }
   BUG("\n");
   return r;
}



FUNCTION nodep lastChildOfLastSibling(n)
nodep n;
{

   while (n->nextSibling)
      n = nextSibling(n);
   if (n->child)
      return lastChildOfLastSibling(child(n));
   else
      return n;
}

/* go to next node, backup if neccessary */
FUNCTION nodep treeLast(n)
nodep n;
{
   nodep r;

   if (n->lastSibling) {
      n = lastSibling(n);
      if (n->child)
	 r = lastChildOfLastSibling(child(n));
      else
	 r = n;
   } else if (n->parent) {
      r = parent(n);
   } else if (n->child) {
      r = lastChildOfLastSibling(child(n));
   } else {
      r = n;
   }
   return r;
}