// Written by Nic Roets. This code is in the public domain. #ifndef ARTREE_H_INCLUDED #define ARTREE_H_INCLUDED #define ARTREE_VERSION_MAJOR 0 #define ARTREE_VERSION_MINOR 1 template struct arnode { arnode *parent, *left, *right; struct { int branchsize : 31; int red : 1; } s; t node; }; template struct artree { arnode *root; artree(void) : root(NULL) {} void InsertBefore (arnode *me, arnode *_new); void Remove (arnode *old); int Next (arnode **ptr); /* TRUE if another element was found */ int Search (arnode **ptr, int referenceMinusPtr); /* 0: not done, 1: it does not exist, but it's neighbours has been found 2: it has been found */ arnode &operator[](int i); }; #define ArtreeSearch(tree,condition) ({ typeof (tree.root) x = tree.root; \ while (x && !tree.Search (&x, condition)) {} x; }) /* If not found, returns the position where it should be inserted */ #define ArtreeFind(tree,condition) ({ typeof (tree.root) x = tree.root; \ int _done; while (x && !(_done = tree.Search (&x, condition))) {} \ _done == 2 ? x : NULL; }) /* If not found, returns NULL */ #endif