1 375b78fb 2009-08-23 rsc #include <u.h>
2 375b78fb 2009-08-23 rsc #include <libc.h>
3 375b78fb 2009-08-23 rsc #include <bio.h>
4 375b78fb 2009-08-23 rsc #include <avl.h>
7 375b78fb 2009-08-23 rsc * In-memory database stored as self-balancing AVL tree.
8 375b78fb 2009-08-23 rsc * See Lewis & Denenberg, Data Structures and Their Algorithms.
12 375b78fb 2009-08-23 rsc singleleft(Avl **tp, Avl *p)
21 375b78fb 2009-08-23 rsc l = (r2 > 0? r2: 0)+1 - a->bal;
23 375b78fb 2009-08-23 rsc if((a->n[1] = c->n[0]) != nil)
24 375b78fb 2009-08-23 rsc a->n[1]->p = a;
26 375b78fb 2009-08-23 rsc if((c->n[0] = a) != nil)
27 375b78fb 2009-08-23 rsc c->n[0]->p = c;
29 375b78fb 2009-08-23 rsc if((*tp = c) != nil)
30 375b78fb 2009-08-23 rsc (*tp)->p = p;
33 375b78fb 2009-08-23 rsc c->bal = r2 - ((l > 0? l: 0)+1);
38 375b78fb 2009-08-23 rsc singleright(Avl **tp, Avl *p)
45 375b78fb 2009-08-23 rsc l2 = - c->bal;
46 375b78fb 2009-08-23 rsc r = a->bal + ((l2 > 0? l2: 0)+1);
48 375b78fb 2009-08-23 rsc if((a->n[0] = c->n[1]) != nil)
49 375b78fb 2009-08-23 rsc a->n[0]->p = a;
51 375b78fb 2009-08-23 rsc if((c->n[1] = a) != nil)
52 375b78fb 2009-08-23 rsc c->n[1]->p = c;
54 375b78fb 2009-08-23 rsc if((*tp = c) != nil)
55 375b78fb 2009-08-23 rsc (*tp)->p = p;
58 375b78fb 2009-08-23 rsc c->bal = ((r > 0? r: 0)+1) - l2;
62 375b78fb 2009-08-23 rsc doublerightleft(Avl **tp, Avl *p)
64 375b78fb 2009-08-23 rsc singleright(&(*tp)->n[1], *tp);
65 375b78fb 2009-08-23 rsc singleleft(tp, p);
69 375b78fb 2009-08-23 rsc doubleleftright(Avl **tp, Avl *p)
71 375b78fb 2009-08-23 rsc singleleft(&(*tp)->n[0], *tp);
72 375b78fb 2009-08-23 rsc singleright(tp, p);
76 375b78fb 2009-08-23 rsc balance(Avl **tp, Avl *p)
78 375b78fb 2009-08-23 rsc switch((*tp)->bal){
80 375b78fb 2009-08-23 rsc if((*tp)->n[0]->bal <= 0)
81 375b78fb 2009-08-23 rsc singleright(tp, p);
82 375b78fb 2009-08-23 rsc else if((*tp)->n[0]->bal == 1)
83 375b78fb 2009-08-23 rsc doubleleftright(tp, p);
89 375b78fb 2009-08-23 rsc if((*tp)->n[1]->bal >= 0)
90 375b78fb 2009-08-23 rsc singleleft(tp, p);
91 375b78fb 2009-08-23 rsc else if((*tp)->n[1]->bal == -1)
92 375b78fb 2009-08-23 rsc doublerightleft(tp, p);
100 375b78fb 2009-08-23 rsc _insertavl(Avl **tp, Avl *p, Avl *r, int (*cmp)(Avl*,Avl*), Avl **rfree)
104 375b78fb 2009-08-23 rsc if(*tp == nil){
106 375b78fb 2009-08-23 rsc r->n[0] = nil;
107 375b78fb 2009-08-23 rsc r->n[1] = nil;
112 375b78fb 2009-08-23 rsc ob = (*tp)->bal;
113 375b78fb 2009-08-23 rsc if((i = cmp(r, *tp)) != 0){
114 375b78fb 2009-08-23 rsc (*tp)->bal += i * _insertavl(&(*tp)->n[(i+1)/2], *tp, r, cmp,
116 375b78fb 2009-08-23 rsc balance(tp, p);
117 375b78fb 2009-08-23 rsc return ob == 0 && (*tp)->bal != 0;
120 375b78fb 2009-08-23 rsc /* install new entry */
121 375b78fb 2009-08-23 rsc *rfree = *tp; /* save old node for freeing */
122 375b78fb 2009-08-23 rsc *tp = r; /* insert new node */
123 375b78fb 2009-08-23 rsc **tp = **rfree; /* copy old node's Avl contents */
124 375b78fb 2009-08-23 rsc if(r->n[0]) /* fix node's children's parent pointers */
125 375b78fb 2009-08-23 rsc r->n[0]->p = r;
127 375b78fb 2009-08-23 rsc r->n[1]->p = r;
133 375b78fb 2009-08-23 rsc _lookupavl(Avl *t, Avl *r, int (*cmp)(Avl*,Avl*))
139 375b78fb 2009-08-23 rsc while(t != nil){
140 375b78fb 2009-08-23 rsc assert(t->p == p);
141 375b78fb 2009-08-23 rsc if((i = cmp(r, t)) == 0)
144 375b78fb 2009-08-23 rsc t = t->n[(i+1)/2];
150 375b78fb 2009-08-23 rsc successor(Avl **tp, Avl *p, Avl **r)
154 375b78fb 2009-08-23 rsc if((*tp)->n[0] == nil){
156 375b78fb 2009-08-23 rsc *tp = (*r)->n[1];
158 375b78fb 2009-08-23 rsc (*tp)->p = p;
161 375b78fb 2009-08-23 rsc ob = (*tp)->bal;
162 375b78fb 2009-08-23 rsc (*tp)->bal -= successor(&(*tp)->n[0], *tp, r);
163 375b78fb 2009-08-23 rsc balance(tp, p);
164 375b78fb 2009-08-23 rsc return -(ob != 0 && (*tp)->bal == 0);
168 375b78fb 2009-08-23 rsc _deleteavl(Avl **tp, Avl *p, Avl *rx, int(*cmp)(Avl*,Avl*), Avl **del,
169 375b78fb 2009-08-23 rsc void (*predel)(Avl*, void*), void *arg)
172 375b78fb 2009-08-23 rsc Avl *r, *or;
174 375b78fb 2009-08-23 rsc if(*tp == nil)
177 375b78fb 2009-08-23 rsc ob = (*tp)->bal;
178 375b78fb 2009-08-23 rsc if((i=cmp(rx, *tp)) != 0){
179 375b78fb 2009-08-23 rsc (*tp)->bal += i * _deleteavl(&(*tp)->n[(i+1)/2], *tp, rx, cmp,
180 375b78fb 2009-08-23 rsc del, predel, arg);
181 375b78fb 2009-08-23 rsc balance(tp, p);
182 375b78fb 2009-08-23 rsc return -(ob != 0 && (*tp)->bal == 0);
186 375b78fb 2009-08-23 rsc (*predel)(*tp, arg);
189 375b78fb 2009-08-23 rsc if(or->n[i=0] == nil || or->n[i=1] == nil){
190 375b78fb 2009-08-23 rsc *tp = or->n[1-i];
192 375b78fb 2009-08-23 rsc (*tp)->p = p;
197 375b78fb 2009-08-23 rsc /* deleting node with two kids, find successor */
198 375b78fb 2009-08-23 rsc or->bal += successor(&or->n[1], or, &r);
199 375b78fb 2009-08-23 rsc r->bal = or->bal;
200 375b78fb 2009-08-23 rsc r->n[0] = or->n[0];
201 375b78fb 2009-08-23 rsc r->n[1] = or->n[1];
203 375b78fb 2009-08-23 rsc (*tp)->p = p;
204 375b78fb 2009-08-23 rsc /* node has changed; fix children's parent pointers */
206 375b78fb 2009-08-23 rsc r->n[0]->p = r;
208 375b78fb 2009-08-23 rsc r->n[1]->p = r;
210 375b78fb 2009-08-23 rsc balance(tp, p);
211 375b78fb 2009-08-23 rsc return -(ob != 0 && (*tp)->bal == 0);
215 375b78fb 2009-08-23 rsc checkparents(Avl *a, Avl *p)
217 375b78fb 2009-08-23 rsc if(a == nil)
219 375b78fb 2009-08-23 rsc if(a->p != p)
220 375b78fb 2009-08-23 rsc print("bad parent\n");
221 375b78fb 2009-08-23 rsc checkparents(a->n[0], a);
222 375b78fb 2009-08-23 rsc checkparents(a->n[1], a);
225 375b78fb 2009-08-23 rsc struct Avltree
228 375b78fb 2009-08-23 rsc int (*cmp)(Avl*, Avl*);
229 375b78fb 2009-08-23 rsc Avlwalk *walks;
231 375b78fb 2009-08-23 rsc struct Avlwalk
233 375b78fb 2009-08-23 rsc int started;
235 375b78fb 2009-08-23 rsc Avlwalk *next;
236 375b78fb 2009-08-23 rsc Avltree *tree;
241 375b78fb 2009-08-23 rsc mkavltree(int (*cmp)(Avl*, Avl*))
245 375b78fb 2009-08-23 rsc t = malloc(sizeof *t);
246 375b78fb 2009-08-23 rsc if(t == nil)
248 375b78fb 2009-08-23 rsc memset(t, 0, sizeof *t);
249 375b78fb 2009-08-23 rsc t->cmp = cmp;
254 375b78fb 2009-08-23 rsc insertavl(Avltree *t, Avl *new, Avl **oldp)
256 375b78fb 2009-08-23 rsc *oldp = nil;
257 375b78fb 2009-08-23 rsc _insertavl(&t->root, nil, new, t->cmp, oldp);
261 375b78fb 2009-08-23 rsc lookupavl(Avltree *t, Avl *key)
263 375b78fb 2009-08-23 rsc return _lookupavl(t->root, key, t->cmp);
267 375b78fb 2009-08-23 rsc findpredecessor(Avl *a)
269 375b78fb 2009-08-23 rsc if(a == nil)
272 375b78fb 2009-08-23 rsc if(a->n[0] != nil){
273 375b78fb 2009-08-23 rsc /* predecessor is rightmost descendant of left child */
274 375b78fb 2009-08-23 rsc for(a = a->n[0]; a->n[1]; a = a->n[1])
278 375b78fb 2009-08-23 rsc /* we're at a leaf, successor is a parent we enter from the right */
279 375b78fb 2009-08-23 rsc while(a->p && a->p->n[0] == a)
281 375b78fb 2009-08-23 rsc return a->p;
286 375b78fb 2009-08-23 rsc findsuccessor(Avl *a)
288 375b78fb 2009-08-23 rsc if(a == nil)
291 375b78fb 2009-08-23 rsc if(a->n[1] != nil){
292 375b78fb 2009-08-23 rsc /* successor is leftmost descendant of right child */
293 375b78fb 2009-08-23 rsc for(a = a->n[1]; a->n[0]; a = a->n[0])
297 375b78fb 2009-08-23 rsc /* we're at a leaf, successor is a parent we enter from the left going up */
298 375b78fb 2009-08-23 rsc while(a->p && a->p->n[1] == a)
300 375b78fb 2009-08-23 rsc return a->p;
305 375b78fb 2009-08-23 rsc walkdel(Avl *a, void *v)
311 375b78fb 2009-08-23 rsc if(a == nil)
314 375b78fb 2009-08-23 rsc p = findpredecessor(a);
316 375b78fb 2009-08-23 rsc for(w = t->walks; w; w = w->next){
317 375b78fb 2009-08-23 rsc if(w->node == a){
318 375b78fb 2009-08-23 rsc /* back pointer to predecessor; not perfect but adequate */
319 375b78fb 2009-08-23 rsc w->moved = 1;
320 375b78fb 2009-08-23 rsc w->node = p;
321 375b78fb 2009-08-23 rsc if(p == nil)
322 375b78fb 2009-08-23 rsc w->started = 0;
328 375b78fb 2009-08-23 rsc deleteavl(Avltree *t, Avl *key, Avl **oldp)
330 375b78fb 2009-08-23 rsc *oldp = nil;
331 375b78fb 2009-08-23 rsc _deleteavl(&t->root, nil, key, t->cmp, oldp, walkdel, t);
335 375b78fb 2009-08-23 rsc avlwalk(Avltree *t)
339 375b78fb 2009-08-23 rsc w = malloc(sizeof *w);
340 375b78fb 2009-08-23 rsc if(w == nil)
342 375b78fb 2009-08-23 rsc memset(w, 0, sizeof *w);
343 375b78fb 2009-08-23 rsc w->tree = t;
344 375b78fb 2009-08-23 rsc w->next = t->walks;
345 375b78fb 2009-08-23 rsc t->walks = w;
350 375b78fb 2009-08-23 rsc avlnext(Avlwalk *w)
354 375b78fb 2009-08-23 rsc if(w->started==0){
355 375b78fb 2009-08-23 rsc for(a = w->tree->root; a && a->n[0]; a = a->n[0])
357 375b78fb 2009-08-23 rsc w->node = a;
358 375b78fb 2009-08-23 rsc w->started = 1;
360 375b78fb 2009-08-23 rsc a = findsuccessor(w->node);
361 375b78fb 2009-08-23 rsc if(a == w->node)
363 375b78fb 2009-08-23 rsc w->node = a;
365 375b78fb 2009-08-23 rsc return w->node;
369 375b78fb 2009-08-23 rsc avlprev(Avlwalk *w)
373 375b78fb 2009-08-23 rsc if(w->started == 0){
374 375b78fb 2009-08-23 rsc for(a = w->tree->root; a && a->n[1]; a = a->n[1])
376 375b78fb 2009-08-23 rsc w->node = a;
377 375b78fb 2009-08-23 rsc w->started = 1;
378 375b78fb 2009-08-23 rsc }else if(w->moved){
379 375b78fb 2009-08-23 rsc w->moved = 0;
380 375b78fb 2009-08-23 rsc return w->node;
382 375b78fb 2009-08-23 rsc a = findpredecessor(w->node);
383 375b78fb 2009-08-23 rsc if(a == w->node)
385 375b78fb 2009-08-23 rsc w->node = a;
387 375b78fb 2009-08-23 rsc return w->node;
391 375b78fb 2009-08-23 rsc endwalk(Avlwalk *w)
394 375b78fb 2009-08-23 rsc Avlwalk **l;
396 375b78fb 2009-08-23 rsc t = w->tree;
397 375b78fb 2009-08-23 rsc for(l = &t->walks; *l; l = &(*l)->next){
398 375b78fb 2009-08-23 rsc if(*l == w){
399 375b78fb 2009-08-23 rsc *l = w->next;
407 375b78fb 2009-08-23 rsc walkavl(Avl *t, void (*f)(Avl*, void*), void *v)
409 375b78fb 2009-08-23 rsc if(t == nil)
411 375b78fb 2009-08-23 rsc walkavl(t->n[0], f, v);
413 375b78fb 2009-08-23 rsc walkavl(t->n[1], f, v);