]> git.lizzy.rs Git - plan9front.git/commitdiff
libavl: lookup can return the closest match
authorspew <devnull@localhost>
Sat, 22 Apr 2017 18:59:37 +0000 (13:59 -0500)
committerspew <devnull@localhost>
Sat, 22 Apr 2017 18:59:37 +0000 (13:59 -0500)
sys/include/avl.h
sys/man/2/avl
sys/src/cmd/upas/fs/mtree.c
sys/src/cmd/upas/imap4d/fstree.c
sys/src/cmd/upas/imap4d/imp.c
sys/src/cmd/venti/copy.c
sys/src/games/galaxy/simulate.c
sys/src/games/mix/mix.c
sys/src/libavl/avl.c

index 49b61fbb6e71f5fc30f9ecf0440d06787374df6b..be7b18a5c8ac813c0f3f50017cbb434574962a7b 100644 (file)
@@ -15,8 +15,9 @@ struct Avltree {
        Avl *root;
 };
 
-Avltree *avlcreate(int(*cmp)(Avl*, Avl*));
-Avl *avllookup(Avltree*, Avl*);
+Avltree *avlinit(Avltree*, int(*)(Avl*, Avl*));
+Avltree *avlcreate(int(*)(Avl*, Avl*));
+Avl *avllookup(Avltree*, Avl*, int);
 Avl *avldelete(Avltree*, Avl*);
 Avl *avlinsert(Avltree*, Avl*);
 Avl *avlnext(Avl*);
index 388b8b3f44e0f84a77036650692972812e737b6c..41a4e93ed0fcf4e950d08598c74e812b2738b6b3 100644 (file)
@@ -30,7 +30,7 @@ struct Avltree {
 Avltree *avlcreate(int(*cmp)(Avl*, Avl*));
 Avl     *avlinsert(Avltree *tree, Avl *new);
 Avl     *avldelete(Avltree *tree, Avl *key);
-Avl     *avllookup(Avltree *tree, Avl *key);
+Avl     *avllookup(Avltree *tree, Avl *key, int dir);
 Avl     *avlnext(Avl *n);
 Avl     *avlprev(Avl *n);
 
@@ -55,13 +55,15 @@ node into the tree and returns an existing
 node with the same key that has been removed
 from the tree and may be freed.
 .I Avllookup
-returns the
-node that matches the key or
-.B nil
-if no node matches.
+searches for a given key and returns
+the closest node less than the given key, 
+.BR nil ,
+or the closest node greater than the key depending on whether
+.I dir
+is less than, equal to, or greater than zero, respectively.
 .I Avldelete
 removes the node matching the key from the tree and returns
-it. It returns nil of no matching key is found.
+it. It returns nil if no matching key is found.
 .PP
 .I Avlnext
 returns the next 
index 4f2af56552ba653e4fb5a66f61531c1b9d0b0803..d50deaef5f38d3b368966eed35c856e610a7a812 100644 (file)
@@ -22,7 +22,7 @@ mtreeisdup(Mailbox *mb, Message *m)
                return 0;
        memset(&t, 0, sizeof t);
        t.m = m;
-       if(avllookup(mb->mtree, &t))
+       if(avllookup(mb->mtree, &t, 0))
                return 1;
        return 0;
 }
@@ -36,7 +36,7 @@ mtreefind(Mailbox *mb, uchar *digest)
        m0.digest = digest;
        memset(&t, 0, sizeof t);
        t.m = &m0;
-       if(p = (Mtree*)avllookup(mb->mtree, &t))
+       if(p = (Mtree*)avllookup(mb->mtree, &t, 0))
                return p->m;
        return nil;
 }
@@ -65,7 +65,7 @@ mtreedelete(Mailbox *mb, Message *m)
        if(m->deleted & ~Deleted){
                if(m->digest == nil)
                        return;
-               p = (Mtree*)avllookup(mb->mtree, &t);
+               p = (Mtree*)avllookup(mb->mtree, &t, 0);
                if(p == nil || p->m != m)
                        return;
                p = (Mtree*)avldelete(mb->mtree, &t);
index 13ef32b7c810350d2aa2d81dd0afbcd7b63421bb..ee491c3fcdd09c223f06fabb4477e50fc262be34 100644 (file)
@@ -25,7 +25,7 @@ fstreefind(Box *mb, int id)
        memset(&t, 0, sizeof t);
        m0.id = id;
        t.m = &m0;
-       if(p = (Fstree*)avllookup(mb->fstree, &t))
+       if(p = (Fstree*)avllookup(mb->fstree, &t, 0))
                return p->m;
        return nil;
 }
index c5542eab7417e09b44fa1aff24534ff10e89f655..a8a96fb2806b01a49a204e2ecdbf4ed50bbb6876 100644 (file)
@@ -100,7 +100,7 @@ rdimp(Biobuf *b, Box *box)
                memset(&t, 0, sizeof t);
                m0.info[Idigest] = f[0];
                t.m = &m0;
-               p = (Mtree*)avllookup(mtree, &t);
+               p = (Mtree*)avllookup(mtree, &t, 0);
                if(p){
                        m = p->m;
                        if(m->uid && m->uid != u){
index 436ce418a7cf2205c0f52a514ca6b2881e6d29c4..c9364fc4bcdcaf5532b3adc54d66a40306b493d1 100644 (file)
@@ -51,7 +51,7 @@ havevisited(uchar score[VtScoreSize], int type)
                return 0;
        memmove(a.score, score, VtScoreSize);
        a.type = type;
-       return avllookup(scoretree, &a) != nil;
+       return avllookup(scoretree, &a, 0) != nil;
 }
 
 static void
index 4622b64215780dcb6a15225b5669c540ef5a8ca4..9fbf135ac7ab64c9e573d666bc7d3a0145038c25 100644 (file)
@@ -6,9 +6,9 @@
 
 int extraproc = -1, throttle;
 
-static QLockgolock;
-static Rendezgorend;
-static intgo;
+static QLock *golock;
+static Rendez *gorend;
+static int *go;
 
 static QLock runninglock;
 static Rendez runningrend;
index 1672afaaf99146f84f1624a3620446469369a984..2eb993fcab7a6d2236378a1a91dec347b33a242d 100644 (file)
@@ -413,7 +413,7 @@ sym(char *name)
        Sym *s, l;
 
        l.name = name;
-       s = (Sym*)avllookup(syms, &l);
+       s = (Sym*)avllookup(syms, &l, 0);
        if(s != nil)
                return s;
 
index 615de495717200e0b3ceb10a946ca349ccd47352..8f1c8b0754c0c7cedaa9d948ab52550b9f075279 100644 (file)
@@ -4,10 +4,6 @@
 
 /* See Knuth Volume 3, 6.2.3 */
 
-Avl *avllookup(Avltree*, Avl*);
-Avl *avldelete(Avltree*, Avl*);
-Avl *avlinsert(Avltree*, Avl*);
-
 Avltree*
 avlcreate(int (*cmp)(Avl*, Avl*))
 {
@@ -16,32 +12,42 @@ avlcreate(int (*cmp)(Avl*, Avl*))
        t = malloc(sizeof(*t));
        if(t == nil)
                return nil;
+       return avlinit(t, cmp);
+}
 
+Avltree*
+avlinit(Avltree *t, int (*cmp)(Avl*, Avl*))
+{
        t->cmp = cmp;
        t->root = nil;
        return t;
 }
 
 Avl*
-avllookup(Avltree *t, Avl *k)
+avllookup(Avltree *t, Avl *k, int d)
 {
-       Avl *h;
+       Avl *h, *n;
        int c;
 
+       n = nil;
        h = t->root;
        while(h != nil){
                c = (t->cmp)(k, h);
                if(c < 0){
+                       if(d > 0)
+                               n = h;
                        h = h->c[0];
                        continue;
                }
                if(c > 0){
+                       if(d < 0)
+                               n = h;
                        h = h->c[1];
                        continue;
                }
                return h;
        }
-       return nil;
+       return n;
 }
 
 static int insert(int (*)(Avl*, Avl*), Avl*, Avl**, Avl*, Avl**);