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);
216 375b78fb 2009-08-23 rsc checkparents(Avl *a, Avl *p)
218 375b78fb 2009-08-23 rsc if(a == nil)
220 375b78fb 2009-08-23 rsc if(a->p != p)
221 375b78fb 2009-08-23 rsc print("bad parent\n");
222 375b78fb 2009-08-23 rsc checkparents(a->n[0], a);
223 375b78fb 2009-08-23 rsc checkparents(a->n[1], a);
227 375b78fb 2009-08-23 rsc struct Avltree
230 375b78fb 2009-08-23 rsc int (*cmp)(Avl*, Avl*);
231 375b78fb 2009-08-23 rsc Avlwalk *walks;
233 375b78fb 2009-08-23 rsc struct Avlwalk
235 375b78fb 2009-08-23 rsc int started;
237 375b78fb 2009-08-23 rsc Avlwalk *next;
238 375b78fb 2009-08-23 rsc Avltree *tree;
243 375b78fb 2009-08-23 rsc mkavltree(int (*cmp)(Avl*, Avl*))
247 375b78fb 2009-08-23 rsc t = malloc(sizeof *t);
248 375b78fb 2009-08-23 rsc if(t == nil)
250 375b78fb 2009-08-23 rsc memset(t, 0, sizeof *t);
251 375b78fb 2009-08-23 rsc t->cmp = cmp;
256 375b78fb 2009-08-23 rsc insertavl(Avltree *t, Avl *new, Avl **oldp)
258 375b78fb 2009-08-23 rsc *oldp = nil;
259 375b78fb 2009-08-23 rsc _insertavl(&t->root, nil, new, t->cmp, oldp);
263 375b78fb 2009-08-23 rsc lookupavl(Avltree *t, Avl *key)
265 375b78fb 2009-08-23 rsc return _lookupavl(t->root, key, t->cmp);
269 375b78fb 2009-08-23 rsc findpredecessor(Avl *a)
271 375b78fb 2009-08-23 rsc if(a == nil)
274 375b78fb 2009-08-23 rsc if(a->n[0] != nil){
275 375b78fb 2009-08-23 rsc /* predecessor is rightmost descendant of left child */
276 375b78fb 2009-08-23 rsc for(a = a->n[0]; a->n[1]; a = a->n[1])
280 375b78fb 2009-08-23 rsc /* we're at a leaf, successor is a parent we enter from the right */
281 375b78fb 2009-08-23 rsc while(a->p && a->p->n[0] == a)
283 375b78fb 2009-08-23 rsc return a->p;
288 375b78fb 2009-08-23 rsc findsuccessor(Avl *a)
290 375b78fb 2009-08-23 rsc if(a == nil)
293 375b78fb 2009-08-23 rsc if(a->n[1] != nil){
294 375b78fb 2009-08-23 rsc /* successor is leftmost descendant of right child */
295 375b78fb 2009-08-23 rsc for(a = a->n[1]; a->n[0]; a = a->n[0])
299 375b78fb 2009-08-23 rsc /* we're at a leaf, successor is a parent we enter from the left going up */
300 375b78fb 2009-08-23 rsc while(a->p && a->p->n[1] == a)
302 375b78fb 2009-08-23 rsc return a->p;
307 375b78fb 2009-08-23 rsc walkdel(Avl *a, void *v)
313 375b78fb 2009-08-23 rsc if(a == nil)
316 375b78fb 2009-08-23 rsc p = findpredecessor(a);
318 375b78fb 2009-08-23 rsc for(w = t->walks; w; w = w->next){
319 375b78fb 2009-08-23 rsc if(w->node == a){
320 375b78fb 2009-08-23 rsc /* back pointer to predecessor; not perfect but adequate */
321 375b78fb 2009-08-23 rsc w->moved = 1;
322 375b78fb 2009-08-23 rsc w->node = p;
323 375b78fb 2009-08-23 rsc if(p == nil)
324 375b78fb 2009-08-23 rsc w->started = 0;
330 375b78fb 2009-08-23 rsc deleteavl(Avltree *t, Avl *key, Avl **oldp)
332 375b78fb 2009-08-23 rsc *oldp = nil;
333 375b78fb 2009-08-23 rsc _deleteavl(&t->root, nil, key, t->cmp, oldp, walkdel, t);
337 375b78fb 2009-08-23 rsc avlwalk(Avltree *t)
341 375b78fb 2009-08-23 rsc w = malloc(sizeof *w);
342 375b78fb 2009-08-23 rsc if(w == nil)
344 375b78fb 2009-08-23 rsc memset(w, 0, sizeof *w);
345 375b78fb 2009-08-23 rsc w->tree = t;
346 375b78fb 2009-08-23 rsc w->next = t->walks;
347 375b78fb 2009-08-23 rsc t->walks = w;
352 375b78fb 2009-08-23 rsc avlnext(Avlwalk *w)
356 375b78fb 2009-08-23 rsc if(w->started==0){
357 375b78fb 2009-08-23 rsc for(a = w->tree->root; a && a->n[0]; a = a->n[0])
359 375b78fb 2009-08-23 rsc w->node = a;
360 375b78fb 2009-08-23 rsc w->started = 1;
362 375b78fb 2009-08-23 rsc a = findsuccessor(w->node);
363 375b78fb 2009-08-23 rsc if(a == w->node)
365 375b78fb 2009-08-23 rsc w->node = a;
367 375b78fb 2009-08-23 rsc return w->node;
371 375b78fb 2009-08-23 rsc avlprev(Avlwalk *w)
375 375b78fb 2009-08-23 rsc if(w->started == 0){
376 375b78fb 2009-08-23 rsc for(a = w->tree->root; a && a->n[1]; a = a->n[1])
378 375b78fb 2009-08-23 rsc w->node = a;
379 375b78fb 2009-08-23 rsc w->started = 1;
380 375b78fb 2009-08-23 rsc }else if(w->moved){
381 375b78fb 2009-08-23 rsc w->moved = 0;
382 375b78fb 2009-08-23 rsc return w->node;
384 375b78fb 2009-08-23 rsc a = findpredecessor(w->node);
385 375b78fb 2009-08-23 rsc if(a == w->node)
387 375b78fb 2009-08-23 rsc w->node = a;
389 375b78fb 2009-08-23 rsc return w->node;
393 375b78fb 2009-08-23 rsc endwalk(Avlwalk *w)
396 375b78fb 2009-08-23 rsc Avlwalk **l;
398 375b78fb 2009-08-23 rsc t = w->tree;
399 375b78fb 2009-08-23 rsc for(l = &t->walks; *l; l = &(*l)->next){
400 375b78fb 2009-08-23 rsc if(*l == w){
401 375b78fb 2009-08-23 rsc *l = w->next;
410 375b78fb 2009-08-23 rsc walkavl(Avl *t, void (*f)(Avl*, void*), void *v)
412 375b78fb 2009-08-23 rsc if(t == nil)
414 375b78fb 2009-08-23 rsc walkavl(t->n[0], f, v);
416 375b78fb 2009-08-23 rsc walkavl(t->n[1], f, v);