-
Notifications
You must be signed in to change notification settings - Fork 1
/
btree.c
310 lines (270 loc) · 8.26 KB
/
btree.c
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
/* btree.c - balanced tree handling on a linear stack.
* Copyright Mike Arnautov 2001-2008.
* Licensed under GPL, version 3 or later (see the supplied LICENCE file).
*
* 14 Jul 09 MLA Fixed gcc --pedantic warnings.
* 12 May 08 MLA Removed 32-bit dependency.
* 07 May 07 Stuart Munro bug: fix declaration and casting of btact args.
* 03 Sep 06 MLA Bug: All longs should be ints!
* 23 Dec 05 MLA roots[] must be long, not int!
* 06 Mar 03 Stuart Munro bug: fix non-ASCII btfind args declaration;
* include stdlib.h; remove unused variables.
* 07 Jan 03 MLA Adapted for use by acdc.
* 24 Mar 01 MLA Initial coding.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "text.h"
#include "const.h"
#include "symbol.h"
#include "btree.h"
#define BT_LSIB -1
#define BT_UP 1
#define BT_RSIB 1
#define BT_BAL 3
#define BT_PTR 4
#define BT_STP 4096
#define PTRLEN sizeof(int *)/sizeof(int);
/*====================================================================*/
static void sing_rot (int *root, int parent, int child, int dir)
{
int gparent = *(root + parent + BT_UP);
int gchild = *(root + child + BT_UP - dir);
*(root + parent + BT_UP + dir) = *(root + child + BT_UP - dir);
*(root + child + BT_UP - dir) = parent;
*(root + parent + BT_BAL) -= *(root + child + BT_BAL);
*(root + child + BT_BAL) = -(*(root + parent + BT_BAL));
if (gparent > 0)
*(root + gparent + BT_UP +
(*(root + gparent + BT_UP + BT_LSIB) == parent ? BT_LSIB : BT_RSIB)) =
child;
else
*(root + 1) = child;
*(root + child + BT_UP) = gparent;
*(root + parent + BT_UP) = child;
if (gchild)
*(root + gchild + BT_UP) = parent;
}
/*--------------------------------------------------------------------*/
static void dub_rot (int *root, int parent, int child, int dir)
{
int gparent = *(root + parent + BT_UP);
int gchild = *(root + child + BT_UP - dir);
*(root + child + BT_UP - dir) = *(root + gchild + BT_UP + dir);
if (*(root + gchild + BT_UP + dir))
*(root + *(root + gchild + BT_UP + dir) + BT_UP) = child;
*(root + parent + BT_UP + dir) = *(root + gchild + BT_UP - dir);
if (*(root + gchild + BT_UP - dir))
*(root + *(root + gchild + BT_UP - dir) + BT_UP) = parent;
*(root + gchild + BT_UP + dir) = child;
*(root + gchild + BT_UP - dir) = parent;
if (*(root + gchild + BT_BAL) == *(root + child + BT_BAL))
*(root + child + BT_BAL) *= -1;
else
*(root + child + BT_BAL) = 0;
if (*(root + gchild + BT_BAL) == *(root + parent + BT_BAL))
*(root + parent + BT_BAL) *= -1;
else
*(root + parent + BT_BAL) = 0;
*(root + gchild + BT_BAL) = 0;
*(root + child + BT_UP) = gchild;
*(root + parent + BT_UP) = gchild;
*(root + gchild + BT_UP) = gparent;
if (gparent > 0)
*(root + gparent + BT_UP +
(*(root + gparent + BT_UP + BT_LSIB) == parent ? BT_LSIB : BT_RSIB)) =
gchild;
else
*(root + 1) = gchild;
}
/*====================================================================*/
int btcmpa (struct node *itemptr, struct node *nodeptr)
{
char *n1;
char *n2;
int result;
if (*(n1 = itemptr -> name) == '!')
n1++;
if (*(n2 = nodeptr -> name) == '!')
n2++;
result = strcmp (n1, n2);
if (result)
return (result > 0 ? 1 : -1);
else
return (0);
}
/*--------------------------------------------------------------------*/
int btcmpf (char *word, struct node *nodeptr)
{
char *name;
int result;
if (*(name = nodeptr -> name) == '!')
name++;
result = strcmp (word, name);
if (result)
return (result > 0 ? 1 : -1);
else
return (0);
}
/*====================================================================*/
void btinit (int type)
{
int *root = *(roots + type);
if (root == NULL)
{
if ((root = (int *)malloc(BT_STP * sizeof(int))) == NULL)
{
fprintf (stderr, "*ERROR* Unable to allocate dictionary root %d!\n", type);
exit (1);
}
*root = 3;
*(root + 1) = 0;
*(root + 2) = BT_STP;
}
else
{
if ((root = (int *)realloc
(root, (*(root + 2) + BT_STP) * sizeof(int))) == NULL)
{
fprintf (stderr, "*ERROR* Unable to re-allocate dictionary root %d!\n", type);
exit (1);
}
*(root + 2) += BT_STP;
}
*(roots + type) = root;
}
/*--------------------------------------------------------------------*/
#ifdef BT_DEBUG
void btshow (int *root, struct node *nodeptr)
{
int *iptr;
printf ("Show: %s, free: %hd, root: %hd\n", text, *root, *(root + 1));
iptr = root + 3;
while (iptr < root + *root)
{
fprintf (stderr, "Offset %d: Up %hd, L %hd, R %hd, B %hd, T: %s\n",
iptr - root, *(iptr + BT_UP), *(iptr + BT_UP + BT_LSIB),
*(iptr + BT_UP + BT_RSIB), *(iptr + BT_BAL),
nodeptr -> name);
iptr += BT_PTR + PTRLEN;
}
}
#endif /* BT_DEBUG */
/*--------------------------------------------------------------------*/
int btadd (int type, void *record)
{
int *root = roots [type];
int parent = 0;
int child = *(root + 1);
int dir;
int *newrec;
int reclen = BT_PTR + PTRLEN;
if (*(root + 1) > 0)
{
while (child > 0)
{
struct node *np;
memcpy (&np, root + child + BT_PTR, sizeof (np));
if ((dir = btcmpa (record, np)) == 0)
return (0);
parent = child;
child = *(root + child + BT_UP + dir);
}
}
if (*root + reclen > *(root + 2))
{
btinit (type);
if ((root = roots [type]) == NULL)
return (-1);
}
newrec = root + (child = *root);
*(newrec + BT_UP) = parent;
*(newrec + BT_UP + BT_LSIB) = *(newrec + BT_UP + BT_RSIB) = 0;
*(newrec + BT_BAL) = 0;
memcpy ((struct node *)(newrec + BT_PTR), &record, sizeof (record));
*root += reclen;
if (parent)
{
*(root + parent + BT_UP + dir) = child;
while (1)
{
dir = *(root + parent + BT_UP + BT_LSIB) == child ? BT_LSIB : BT_RSIB;
if (*(root + parent + BT_BAL) == dir)
{
if (*(root + child + BT_BAL) == -dir)
dub_rot (root, parent, child, dir);
else
sing_rot (root, parent, child, dir);
break;
}
if ((*(root + parent + BT_BAL) += dir) == 0)
break;
child = parent;
parent = *(root + parent + BT_UP);
if (parent == 0) break;
}
}
else
*(root + 1) = child;
return (1);
}
/*--------------------------------------------------------------------*/
struct node *btfind (int type, char *word)
{
int node;
int dir;
int *root = roots [type];
if ((node = *(root + 1)) == 0)
return (NULL);
while (node)
{
struct node *np;
memcpy (&np, root + node + BT_PTR, sizeof (np));
if ((dir = btcmpf (word, np)) == 0)
return (np);
node = *(root + node + BT_UP + dir);
}
return (NULL);
}
/*====================================================================*/
void btspan (int type, void (*btact)(struct node *))
{
struct node *np;
int state = 0;
int *root = roots [type];
int node = *(root + 1);
int next;
if (node == 0)
return;
while (node)
{
switch (state)
{
case 0:
if ((next = *(root + node + BT_UP + BT_LSIB)) != 0)
node = next;
else
state = (*(root + node + BT_UP + BT_RSIB)) ? 1 : 2;
break;
case 1:
memcpy (&np, root + node + BT_PTR, sizeof (np));
btact (np);
state = (next = *(root + node + BT_UP + BT_RSIB)) ? 0 : 3;
if (state == 0)
node = next;
break;
case 2:
memcpy (&np, root + node + BT_PTR, sizeof (np));
btact (np);
/* And just fall through! */
case 3:
if ((next = *(root + node + BT_UP)) != 0)
state = (*(root + next + BT_UP + BT_LSIB) == node) ? 1 : 3;
node = next;
break;
}
}
}
/*====================================================================*/