-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframe.c
318 lines (277 loc) · 7.07 KB
/
frame.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
311
312
313
314
315
316
317
#include <stdlib.h>
#include "object.h"
#include "mem.h"
#include "config.h"
typedef enum color_type {
RED,
BLACK
} color_type;
/**
* var must be a symbol, a total order is maintained using
* simple pointer comparison. Thus no hash needed.
*/
typedef struct rbnode {
object *var;
object *val;
rbtree left;
rbtree right;
color_type color;
} rbnode;
#define rbleft(p) ((p)->left)
#define rbright(p) ((p)->right)
#define rbcolor(p) ((p)->color)
#define rbvar(p) ((p)->var)
#define rbval(p) ((p)->val)
#define VAR_MIN (void*)0
static rbnode *null_node;
int env_frame_init(void) {
null_node = sc_malloc(sizeof(rbnode));
if (null_node == NULL) {
return -1;
}
rbleft(null_node) = null_node;
rbright(null_node) = null_node;
rbcolor(null_node) = BLACK;
rbvar(null_node) = VAR_MIN; /* dummy */
rbval(null_node) = NULL;
return 0;
}
void env_frame_dispose() {
sc_free(null_node);
}
int is_env_frame(object *obj) {
return obj != NULL && type(obj) == ENV_FRAME;
}
object* make_env_frame(void) {
object *p;
rbtree t;
t = sc_malloc(sizeof(rbnode));
if (t == NULL) {
return NULL;
}
rbvar(t) = VAR_MIN;
rbval(t) = NULL;
rbleft(t) = null_node;
rbright(t) = null_node;
rbcolor(t) = BLACK;
p = alloc_object();
type(p) = ENV_FRAME;
obj_rbtv(p) = t;
return p;
}
static object* rb_find(rbtree t, object *var) {
if (t == null_node) {
return NULL;
}
if (var < rbvar(t)) {
return rb_find(rbleft(t), var);
} else if (var > rbvar(t)) {
return rb_find(rbright(t), var);
} else {
return rbval(t);
}
}
/**
* return NULL if not found
*/
object* env_frame_find(object *frame, object *var) {
rbtree t = obj_rbtv(frame);
return rb_find(t, var);
}
static void rb_free(rbtree t) {
if (t != null_node) {
rb_free(rbleft(t));
rb_free(rbright(t));
sc_free(t);
}
}
void env_frame_free(object *obj) {
rbtree t = obj_rbtv(obj);
rb_free(t);
}
/**
* This function can be called only if k2 has a left child.
* Perform a rotate between a node(k2) and its left child.
* Return new root.
*/
static position single_rotate_with_left(position k2) {
position k1;
k1 = rbleft(k2);
rbleft(k2) = rbright(k1);
rbright(k1) = k2;
return k1;
}
/**
* This function can be called only if k1 has a right child.
* Perform a rotate between a node(k1) and its right child.
* Return new root.
*/
static position single_rotate_with_right(position k1) {
position k2;
k2 = rbright(k1);
rbright(k1) = rbleft(k2);
rbleft(k2) = k1;
return k2;
}
/**
* Perform a rotation at node X
*/
static position rotate(position parent, object *var) {
if (var < rbvar(parent)) {
rbleft(parent) = var < rbvar(rbleft(parent)) ?
single_rotate_with_left(rbleft(parent)) :
single_rotate_with_right(rbleft(parent));
return rbleft(parent);
} else {
rbright(parent) = var < rbvar(rbright(parent)) ?
single_rotate_with_left(rbright(parent)) :
single_rotate_with_right(rbright(parent));
return rbright(parent);
}
}
static void handle_reorient(object *var, rbtree t, position *x,
position *p, position *gp, position *ggp) {
/* flip color */
rbcolor(*x) = RED;
rbcolor(rbleft(*x)) = BLACK;
rbcolor(rbright(*x)) = BLACK;
if (rbcolor(*p) == RED) {
/* need rotate */
rbcolor(*gp) = RED;
if ((var < rbvar(*gp)) != (var < rbvar(*p))) {
*p = rotate(*gp, var); /* start double rotate */
}
*x = rotate(*ggp, var);
rbcolor(*x) = BLACK;
}
rbcolor(rbright(t)) = BLACK; /* make root black */
}
static int rb_change(rbtree t, object *var, object *val) {
if (t == null_node) {
return -1;
}
if (var < rbvar(t)) {
return rb_change(rbleft(t), var, val);
} else if (var > rbvar(t)) {
return rb_change(rbright(t), var, val);
} else {
rbval(t) = val;
return 0;
}
}
int env_frame_change(object *frame, object *var, object *val) {
rbtree t = obj_rbtv(frame);
return rb_change(t, var, val);
}
#ifdef VERIFY_RB_TREE
static void rb_verify(rbtree tree);
#endif
int env_frame_insert(object *frame, object *var, object *val) {
position x, p, gp, ggp;
rbtree t = obj_rbtv(frame);
x = p = gp = t;
rbvar(null_node) = var;
while (rbvar(x) != var) {
ggp = gp;
gp = p;
p = x;
if (var < rbvar(x)) {
x = rbleft(x);
} else {
x = rbright(x);
}
if (rbcolor(rbleft(x)) == RED &&
rbcolor(rbright(x)) == RED) {
handle_reorient(var, t, &x, &p, &gp, &ggp);
}
}
if (x != null_node) {
/* change existing node */
rbval(x) = val;
} else if (x == null_node) {
x = sc_malloc(sizeof(rbnode));
if (x == NULL) {
return -2;
}
rbvar(x) = var;
rbval(x) = val;
rbleft(x) = null_node;
rbright(x) = null_node;
if (var < rbvar(p)) {
rbleft(p) = x;
} else {
rbright(p) = x;
}
/* color it red; maybe rotate */
handle_reorient(var, t, &x, &p, &gp, &ggp);
}
#ifdef VERIFY_RB_TREE
rb_verify(t);
#endif
return 0;
}
void rb_walk(rbtree t, env_frame_walk_fn walker) {
if (t == null_node) {
return;
}
walker(rbvar(t), rbval(t));
rb_walk(rbleft(t), walker);
rb_walk(rbright(t), walker);
}
void env_frame_walk(object *frame, env_frame_walk_fn walker) {
rbtree t = obj_rbtv(frame);
rb_walk(rbright(t), walker);
}
/* rbtree verification functions */
#ifdef VERIFY_RB_TREE
static int rb_verify_node(rbtree t) {
rbtree left, right;
if (t == null_node) {
return 1;
}
left = rbleft(t);
right = rbright(t);
if (rbcolor(t) == RED &&
(rbcolor(left) != BLACK || rbcolor(right) != BLACK)) {
return 0;
}
return rb_verify_node(left) && rb_verify_node(right);
}
static void rb_verify_black_node(rbtree t, int cnt, int *n) {
if (t == null_node) {
if (*n == -1) {
*n = cnt;
return;
} else if (*n == cnt) {
return;
} else {
fprintf(stderr,
"not every path from a node to a leaf \
contains the same number of black nodes\n");
exit(-1);
}
}
if (rbcolor(t) == BLACK) {
cnt++;
}
rb_verify_black_node(rbleft(t), cnt, n);
rb_verify_black_node(rbright(t), cnt, n);
}
static void rb_verify(rbtree tree) {
rbtree t = rbright(tree);
int n = -1;
if (rbcolor(t) != BLACK) {
fprintf(stderr, "root must be black\n");
return;
}
if (rbcolor(null_node) != BLACK) {
fprintf(stderr, "leaves must be black\n");
return;
}
if (!rb_verify_node(t)) {
fprintf(stderr, "children of red node must be black\n");
return;
}
rb_verify_black_node(t, 0, &n);
}
#endif /* VERIFY_RB_TREE */