-#ifdef TEXREDBLACK
-// utilitie function for red-black tree
-// taken from Wikipedia
-// http://en.wikipedia.org/wiki/Red-black_tree
-#define BLACK 0
-#define RED 1
-//**** Family stuff ****
-texbsp *grandparent(texbsp *n)
-{
- if ((n != NULL) && (n->parent != NULL))
- return n->parent->parent;
- else
- return NULL;
-}
-texbsp *sibling(texbsp *n)
-{
- if ((n != NULL) && (n->parent != NULL))
- if (n == n->parent->left)
- return n->parent->right;
- else
- return n->parent->left;
- else
- return NULL;
-}
-texbsp *uncle(texbsp *n)
-{
- texbsp *g = grandparent(n);
- if (g == NULL)
- return NULL; // No grandparent means no uncle
- if (n->parent == g->left)
- return g->right;
- else
- return g->left;
-}
-//**** Search functions ****
-texbsp* lookup_node(GLuint key)
-{
- texbsp* n = texlist;
- while (n != NULL) {
- int comp_result = (int)key - (int)n->id;
- if (comp_result == 0) {
- return n;
- } else if (comp_result < 0) {
- n = n->left;
- } else {
- n = n->right;
- }
- }
- return n;
-}
-GLuint rbtree_lookup(GLuint key)
-{
- texbsp* n = lookup_node(key);
- return n == NULL ? NULL : n->id;
-}
-//**** Basic insertion / replace ****
-texbsp* new_node(GLuint id, int node_color, texbsp* left, texbsp* right)
-{
- texbsp* result = malloc(sizeof(texbsp));
- result->id = id;
- result->color = node_color;
- result->left = left;
- result->right = right;
- if (left != NULL) left->parent = result;
- if (right != NULL) right->parent = result;
- result->parent = NULL;
- return result;
-}
-void replace_node(texbsp* oldn, texbsp* newn)
-{
- if (oldn->parent == NULL) {
- list = newn;
- } else {
- if (oldn == oldn->parent->left)
- oldn->parent->left = newn;
- else
- oldn->parent->right = newn;
- }
- if (newn != NULL) {
- newn->parent = oldn->parent;
- }
-}
-//**** Rotation functions ****
-void rotate_left(texbsp* n)
-{
- texbsp* r = n->right;
- replace_node(n, r);
- n->right = r->left;
- if (r->left != NULL) {
- r->left->parent = n;
- }
- r->left = n;
- n->parent = r;
-}
-void rotate_right(texbsp* n)
-{
- node L = n->left;
- replace_node(n, L);
- n->left = L->right;
- if (L->right != NULL) {
- L->right->parent = n;
- }
- L->right = n;
- n->parent = L;
-}
-//**** Insertion cases ****
-void insert_case1(texbsp *n)
-{
- if (n->parent == NULL)
- n->color = BLACK;
- else
- insert_case2(n);
-}
-void insert_case2(texbsp *n)
-{
- if (n->parent->color == BLACK)
- return;
- else
- insert_case3(n);
-}
-void insert_case3(texbsp *n)
-{
- texbsp *u = uncle(n), *g;
-
- if ((u != NULL) && (u->color == RED)) {
- n->parent->color = BLACK;
- u->color = BLACK;
- g = grandparent(n);
- g->color = RED;
- insert_case1(g);
- } else {
- insert_case4(n);
- }
-}
-void insert_case4(texbsp *n)
-{
- texbsp *g = grandparent(n);
-
- if ((n == n->parent->right) && (n->parent == g->left)) {
- rotate_left(n->parent);
- n = n->left;
- } else if ((n == n->parent->left) && (n->parent == g->right)) {
- rotate_right(n->parent);
- n = n->right;
- }
- insert_case5(n);
-}
-void insert_case5(texbsp *n)
-{
- struct node *g = grandparent(n);
-
- n->parent->color = BLACK;
- g->color = RED;
- if (n == n->parent->left)
- rotate_right(g);
- else
- rotate_left(g);
-}
-void rbtree_insert(GLuint key)