qse/lib/cmn/rbt.c

1017 lines
22 KiB
C
Raw Normal View History

/*
* $Id$
*
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
2014-11-19 14:42:24 +00:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
2014-11-19 14:42:24 +00:00
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <qse/cmn/rbt.h>
2016-04-29 03:55:42 +00:00
#include "mem-prv.h"
2010-10-31 06:49:18 +00:00
#define rbt_t qse_rbt_t
#define pair_t qse_rbt_pair_t
#define id_t qse_rbt_id_t
#define copier_t qse_rbt_copier_t
#define freeer_t qse_rbt_freeer_t
#define comper_t qse_rbt_comper_t
#define keeper_t qse_rbt_keeper_t
#define walker_t qse_rbt_walker_t
#define cbserter_t qse_rbt_cbserter_t
#define style_t qse_rbt_style_t
#define style_kind_t qse_rbt_style_kind_t
2010-07-14 05:18:30 +00:00
#define KPTR(p) QSE_RBT_KPTR(p)
#define KLEN(p) QSE_RBT_KLEN(p)
#define VPTR(p) QSE_RBT_VPTR(p)
#define VLEN(p) QSE_RBT_VLEN(p)
#define SIZEOF(x) QSE_SIZEOF(x)
#define size_t qse_size_t
#define byte_t qse_byte_t
#define mmgr_t qse_mmgr_t
#define KTOB(rbt,len) ((len)*(rbt)->scale[QSE_RBT_KEY])
#define VTOB(rbt,len) ((len)*(rbt)->scale[QSE_RBT_VAL])
#define UPSERT 1
#define UPDATE 2
#define ENSERT 3
#define INSERT 4
#define IS_NIL(rbt,x) ((x) == &((rbt)->xnil))
#define LEFT 0
#define RIGHT 1
#define left child[LEFT]
#define right child[RIGHT]
#define rotate_left(rbt,pivot) rotate(rbt,pivot,1);
#define rotate_right(rbt,pivot) rotate(rbt,pivot,0);
QSE_INLINE pair_t* qse_rbt_allocpair (
rbt_t* rbt, void* kptr, size_t klen, void* vptr, size_t vlen)
2010-07-14 05:18:30 +00:00
{
pair_t* n;
copier_t kcop = rbt->style->copier[QSE_RBT_KEY];
copier_t vcop = rbt->style->copier[QSE_RBT_VAL];
2010-07-14 05:18:30 +00:00
size_t as = SIZEOF(pair_t);
if (kcop == QSE_RBT_COPIER_INLINE) as += QSE_ALIGNTO_POW2(KTOB(rbt,klen), QSE_SIZEOF_VOID_P);
2010-07-14 05:18:30 +00:00
if (vcop == QSE_RBT_COPIER_INLINE) as += VTOB(rbt,vlen);
n = (pair_t*) QSE_MMGR_ALLOC (rbt->mmgr, as);
if (n == QSE_NULL) return QSE_NULL;
n->color = QSE_RBT_RED;
n->parent = QSE_NULL;
n->child[LEFT] = &rbt->xnil;
n->child[RIGHT] = &rbt->xnil;
2010-07-14 05:18:30 +00:00
KLEN(n) = klen;
if (kcop == QSE_RBT_COPIER_SIMPLE)
{
KPTR(n) = kptr;
}
else if (kcop == QSE_RBT_COPIER_INLINE)
{
KPTR(n) = n + 1;
if (kptr) QSE_MEMCPY (KPTR(n), kptr, KTOB(rbt,klen));
2010-07-14 05:18:30 +00:00
}
else
2010-07-14 05:18:30 +00:00
{
KPTR(n) = kcop (rbt, kptr, klen);
if (KPTR(n) == QSE_NULL)
{
QSE_MMGR_FREE (rbt->mmgr, n);
2010-07-14 05:18:30 +00:00
return QSE_NULL;
}
}
VLEN(n) = vlen;
if (vcop == QSE_RBT_COPIER_SIMPLE)
{
VPTR(n) = vptr;
}
else if (vcop == QSE_RBT_COPIER_INLINE)
{
VPTR(n) = n + 1;
if (kcop == QSE_RBT_COPIER_INLINE)
VPTR(n) = (byte_t*)VPTR(n) + QSE_ALIGNTO_POW2(KTOB(rbt,klen), QSE_SIZEOF_VOID_P);
if (vptr) QSE_MEMCPY (VPTR(n), vptr, VTOB(rbt,vlen));
2010-07-14 05:18:30 +00:00
}
else
2010-07-14 05:18:30 +00:00
{
VPTR(n) = vcop (rbt, vptr, vlen);
if (VPTR(n) != QSE_NULL)
{
if (rbt->style->freeer[QSE_RBT_KEY] != QSE_NULL)
rbt->style->freeer[QSE_RBT_KEY] (rbt, KPTR(n), KLEN(n));
QSE_MMGR_FREE (rbt->mmgr, n);
2010-07-14 05:18:30 +00:00
return QSE_NULL;
}
}
return n;
}
QSE_INLINE void qse_rbt_freepair (rbt_t* rbt, pair_t* pair)
{
if (rbt->style->freeer[QSE_RBT_KEY] != QSE_NULL)
rbt->style->freeer[QSE_RBT_KEY] (rbt, KPTR(pair), KLEN(pair));
if (rbt->style->freeer[QSE_RBT_VAL] != QSE_NULL)
rbt->style->freeer[QSE_RBT_VAL] (rbt, VPTR(pair), VLEN(pair));
2010-07-14 05:18:30 +00:00
QSE_MMGR_FREE (rbt->mmgr, pair);
}
static style_t style[] =
{
{
{
QSE_RBT_COPIER_DEFAULT,
QSE_RBT_COPIER_DEFAULT
},
{
QSE_RBT_FREEER_DEFAULT,
QSE_RBT_FREEER_DEFAULT
},
QSE_RBT_COMPER_DEFAULT,
QSE_RBT_KEEPER_DEFAULT
},
{
{
QSE_RBT_COPIER_INLINE,
QSE_RBT_COPIER_INLINE
},
{
QSE_RBT_FREEER_DEFAULT,
QSE_RBT_FREEER_DEFAULT
},
QSE_RBT_COMPER_DEFAULT,
QSE_RBT_KEEPER_DEFAULT
},
{
{
QSE_RBT_COPIER_INLINE,
QSE_RBT_COPIER_DEFAULT
},
{
QSE_RBT_FREEER_DEFAULT,
QSE_RBT_FREEER_DEFAULT
},
QSE_RBT_COMPER_DEFAULT,
QSE_RBT_KEEPER_DEFAULT
},
{
{
QSE_RBT_COPIER_DEFAULT,
QSE_RBT_COPIER_INLINE
},
{
QSE_RBT_FREEER_DEFAULT,
QSE_RBT_FREEER_DEFAULT
},
QSE_RBT_COMPER_DEFAULT,
QSE_RBT_KEEPER_DEFAULT
}
};
const style_t* qse_getrbtstyle (style_kind_t kind)
{
return &style[kind];
2012-04-30 10:03:55 +00:00
}
rbt_t* qse_rbt_open (mmgr_t* mmgr, size_t xtnsize, int kscale, int vscale)
2010-07-14 05:18:30 +00:00
{
rbt_t* rbt;
rbt = (rbt_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(rbt_t) + xtnsize);
if (rbt == QSE_NULL) return QSE_NULL;
2011-09-01 09:43:46 +00:00
if (qse_rbt_init (rbt, mmgr, kscale, vscale) <= -1)
{
QSE_MMGR_FREE (mmgr, rbt);
return QSE_NULL;
}
QSE_MEMSET (rbt + 1, 0, xtnsize);
return rbt;
}
2010-07-14 05:18:30 +00:00
void qse_rbt_close (rbt_t* rbt)
{
qse_rbt_fini (rbt);
QSE_MMGR_FREE (rbt->mmgr, rbt);
}
2011-09-01 09:43:46 +00:00
int qse_rbt_init (rbt_t* rbt, mmgr_t* mmgr, int kscale, int vscale)
{
/* do not zero out the extension */
2010-07-14 05:18:30 +00:00
QSE_MEMSET (rbt, 0, SIZEOF(*rbt));
rbt->mmgr = mmgr;
rbt->scale[QSE_RBT_KEY] = (kscale < 1)? 1: kscale;
rbt->scale[QSE_RBT_VAL] = (vscale < 1)? 1: vscale;
2010-07-14 05:18:30 +00:00
rbt->size = 0;
rbt->style = &style[0];
2010-07-14 05:18:30 +00:00
/* self-initializing nil */
QSE_MEMSET(&rbt->xnil, 0, QSE_SIZEOF(rbt->xnil));
rbt->xnil.color = QSE_RBT_BLACK;
rbt->xnil.left = &rbt->xnil;
rbt->xnil.right = &rbt->xnil;
2010-07-14 05:18:30 +00:00
/* root is set to nil initially */
rbt->root = &rbt->xnil;
2010-07-14 05:18:30 +00:00
2011-09-01 09:43:46 +00:00
return 0;
}
2010-07-14 05:18:30 +00:00
void qse_rbt_fini (rbt_t* rbt)
{
qse_rbt_clear (rbt);
}
qse_mmgr_t* qse_rbt_getmmgr (qse_rbt_t* rbt)
{
return rbt->mmgr;
}
void* qse_rbt_getxtn (qse_rbt_t* rbt)
{
return QSE_XTN (rbt);
}
const style_t* qse_rbt_getstyle (const rbt_t* rbt)
{
return rbt->style;
2010-07-14 05:18:30 +00:00
}
void qse_rbt_setstyle (rbt_t* rbt, const style_t* style)
2010-07-14 05:18:30 +00:00
{
QSE_ASSERT (style != QSE_NULL);
rbt->style = style;
2010-07-14 05:18:30 +00:00
}
size_t qse_rbt_getsize (const rbt_t* rbt)
2010-07-14 05:18:30 +00:00
{
return rbt->size;
}
pair_t* qse_rbt_search (const rbt_t* rbt, const void* kptr, size_t klen)
2010-07-14 05:18:30 +00:00
{
pair_t* pair = rbt->root;
2010-07-14 05:18:30 +00:00
while (!IS_NIL(rbt,pair))
{
int n = rbt->style->comper (rbt, kptr, klen, KPTR(pair), KLEN(pair));
2010-07-14 05:18:30 +00:00
if (n == 0) return pair;
2010-07-14 05:18:30 +00:00
if (n > 0) pair = pair->right;
else /* if (n < 0) */ pair = pair->left;
}
2010-07-14 05:18:30 +00:00
return QSE_NULL;
}
2010-07-14 05:25:24 +00:00
static void rotate (rbt_t* rbt, pair_t* pivot, int leftwise)
{
/*
* == leftwise rotation
* move the pivot pair down to the poistion of the pivot's original
* left child(x). move the pivot's right child(y) to the pivot's original
* position. as 'c1' is between 'y' and 'pivot', move it to the right
* of the new pivot position.
* parent parent
* | | (left or right?) | |
* pivot y
* / \ / \
* x y =====> pivot c2
* / \ / \
* c1 c2 x c1
*
* == rightwise rotation
* move the pivot pair down to the poistion of the pivot's original
* right child(y). move the pivot's left child(x) to the pivot's original
* position. as 'c2' is between 'x' and 'pivot', move it to the left
* of the new pivot position.
*
* parent parent
* | | (left or right?) | |
* pivot x
* / \ / \
* x y =====> c1 pivot
* / \ / \
* c1 c2 c2 y
*
*
* the actual implementation here resolves the pivot's relationship to
2010-07-14 05:18:30 +00:00
* its parent by comparaing pointers as it is not known if the pivot pair
* is the left child or the right child of its parent,
*/
2010-07-14 05:25:24 +00:00
pair_t* parent, * z, * c;
int cid1, cid2;
QSE_ASSERT (pivot != QSE_NULL);
if (leftwise)
{
cid1 = RIGHT;
cid2 = LEFT;
}
else
{
cid1 = LEFT;
cid2 = RIGHT;
}
parent = pivot->parent;
/* y for leftwise rotation, x for rightwise rotation */
z = pivot->child[cid1];
/* c1 for leftwise rotation, c2 for rightwise rotation */
c = z->child[cid2];
z->parent = parent;
if (parent)
{
if (parent->left == pivot)
{
parent->left = z;
}
else
{
QSE_ASSERT (parent->right == pivot);
parent->right = z;
}
}
else
{
QSE_ASSERT (rbt->root == pivot);
rbt->root = z;
}
z->child[cid2] = pivot;
if (!IS_NIL(rbt,pivot)) pivot->parent = z;
pivot->child[cid1] = c;
if (!IS_NIL(rbt,c)) c->parent = pivot;
}
2010-07-14 05:25:24 +00:00
static void adjust (rbt_t* rbt, pair_t* pair)
{
2010-07-14 05:18:30 +00:00
while (pair != rbt->root)
{
pair_t* tmp, * tmp2, * x_par;
int leftwise;
x_par = pair->parent;
if (x_par->color == QSE_RBT_BLACK) break;
QSE_ASSERT (x_par->parent != QSE_NULL);
if (x_par == x_par->parent->child[LEFT])
{
tmp = x_par->parent->child[RIGHT];
tmp2 = x_par->child[RIGHT];
leftwise = 1;
}
else
{
tmp = x_par->parent->child[LEFT];
tmp2 = x_par->child[LEFT];
leftwise = 0;
}
if (tmp->color == QSE_RBT_RED)
{
x_par->color = QSE_RBT_BLACK;
tmp->color = QSE_RBT_BLACK;
x_par->parent->color = QSE_RBT_RED;
pair = x_par->parent;
}
else
{
2010-07-14 05:18:30 +00:00
if (pair == tmp2)
{
pair = x_par;
2010-07-14 05:18:30 +00:00
rotate (rbt, pair, leftwise);
x_par = pair->parent;
}
x_par->color = QSE_RBT_BLACK;
x_par->parent->color = QSE_RBT_RED;
rotate (rbt, x_par->parent, !leftwise);
}
}
}
2010-07-14 05:18:30 +00:00
static pair_t* change_pair_val (
rbt_t* rbt, pair_t* pair, void* vptr, size_t vlen)
{
if (VPTR(pair) == vptr && VLEN(pair) == vlen)
2010-07-14 05:18:30 +00:00
{
/* if the old value and the new value are the same,
* it just calls the handler for this condition.
2010-07-14 05:18:30 +00:00
* No value replacement occurs. */
if (rbt->style->keeper != QSE_NULL)
2010-07-14 05:18:30 +00:00
{
rbt->style->keeper (rbt, vptr, vlen);
2010-07-14 05:18:30 +00:00
}
}
else
{
copier_t vcop = rbt->style->copier[QSE_RBT_VAL];
2010-07-14 05:18:30 +00:00
void* ovptr = VPTR(pair);
size_t ovlen = VLEN(pair);
2010-07-14 05:18:30 +00:00
/* place the new value according to the copier */
if (vcop == QSE_RBT_COPIER_SIMPLE)
{
VPTR(pair) = vptr;
VLEN(pair) = vlen;
}
else if (vcop == QSE_RBT_COPIER_INLINE)
{
if (ovlen == vlen)
{
2010-10-31 06:49:18 +00:00
if (vptr) QSE_MEMCPY (VPTR(pair), vptr, VTOB(rbt,vlen));
2010-07-14 05:18:30 +00:00
}
else
{
/* need to reconstruct the pair */
pair_t* p = qse_rbt_allocpair (rbt,
2010-07-14 05:18:30 +00:00
KPTR(pair), KLEN(pair),
vptr, vlen);
if (p == QSE_NULL) return QSE_NULL;
p->color = pair->color;
p->left = pair->left;
p->right = pair->right;
p->parent = pair->parent;
if (pair->parent)
{
if (pair->parent->left == pair)
{
pair->parent->left = p;
}
else
2010-07-14 05:18:30 +00:00
{
2010-07-14 07:29:02 +00:00
QSE_ASSERT (pair->parent->right == pair);
2010-07-14 05:18:30 +00:00
pair->parent->right = p;
}
}
if (!IS_NIL(rbt,pair->left)) pair->left->parent = p;
if (!IS_NIL(rbt,pair->right)) pair->right->parent = p;
2010-07-15 06:54:48 +00:00
if (pair == rbt->root) rbt->root = p;
qse_rbt_freepair (rbt, pair);
2010-07-14 05:18:30 +00:00
return p;
}
}
else
2010-07-14 05:18:30 +00:00
{
void* nvptr = vcop (rbt, vptr, vlen);
if (nvptr == QSE_NULL) return QSE_NULL;
VPTR(pair) = nvptr;
VLEN(pair) = vlen;
}
2010-07-14 05:18:30 +00:00
/* free up the old value */
if (rbt->style->freeer[QSE_RBT_VAL] != QSE_NULL)
2010-07-14 05:18:30 +00:00
{
rbt->style->freeer[QSE_RBT_VAL] (rbt, ovptr, ovlen);
2010-07-14 05:18:30 +00:00
}
}
return pair;
}
2010-07-14 05:18:30 +00:00
static pair_t* insert (
rbt_t* rbt, void* kptr, size_t klen, void* vptr, size_t vlen, int opt)
{
pair_t* x_cur = rbt->root;
pair_t* x_par = QSE_NULL;
pair_t* x_new;
while (!IS_NIL(rbt,x_cur))
{
int n = rbt->style->comper (rbt, kptr, klen, KPTR(x_cur), KLEN(x_cur));
if (n == 0)
{
2010-07-14 05:18:30 +00:00
switch (opt)
{
case UPSERT:
case UPDATE:
return change_pair_val (rbt, x_cur, vptr, vlen);
2010-07-14 05:18:30 +00:00
case ENSERT:
/* return existing pair */
return x_cur;
2010-07-14 05:18:30 +00:00
case INSERT:
/* return failure */
return QSE_NULL;
}
}
x_par = x_cur;
if (n > 0) x_cur = x_cur->right;
else /* if (n < 0) */ x_cur = x_cur->left;
}
2010-07-14 05:18:30 +00:00
if (opt == UPDATE) return QSE_NULL;
x_new = qse_rbt_allocpair (rbt, kptr, klen, vptr, vlen);
if (x_new == QSE_NULL) return QSE_NULL;
if (x_par == QSE_NULL)
{
2010-07-14 05:18:30 +00:00
/* the tree contains no pair */
QSE_ASSERT (rbt->root == &rbt->xnil);
rbt->root = x_new;
}
else
{
/* perform normal binary insert */
int n = rbt->style->comper (rbt, kptr, klen, KPTR(x_par), KLEN(x_par));
2010-07-14 05:18:30 +00:00
if (n > 0)
{
QSE_ASSERT (x_par->right == &rbt->xnil);
x_par->right = x_new;
}
else
{
QSE_ASSERT (x_par->left == &rbt->xnil);
x_par->left = x_new;
}
x_new->parent = x_par;
adjust (rbt, x_new);
}
rbt->root->color = QSE_RBT_BLACK;
2012-10-22 09:36:15 +00:00
rbt->size++;
return x_new;
}
2010-07-14 05:18:30 +00:00
pair_t* qse_rbt_upsert (
rbt_t* rbt, void* kptr, size_t klen, void* vptr, size_t vlen)
{
return insert (rbt, kptr, klen, vptr, vlen, UPSERT);
}
pair_t* qse_rbt_ensert (
rbt_t* rbt, void* kptr, size_t klen, void* vptr, size_t vlen)
{
return insert (rbt, kptr, klen, vptr, vlen, ENSERT);
}
pair_t* qse_rbt_insert (
rbt_t* rbt, void* kptr, size_t klen, void* vptr, size_t vlen)
{
return insert (rbt, kptr, klen, vptr, vlen, INSERT);
}
pair_t* qse_rbt_update (
rbt_t* rbt, void* kptr, size_t klen, void* vptr, size_t vlen)
{
return insert (rbt, kptr, klen, vptr, vlen, UPDATE);
}
2010-10-31 06:49:18 +00:00
pair_t* qse_rbt_cbsert (
rbt_t* rbt, void* kptr, size_t klen, cbserter_t cbserter, void* ctx)
{
pair_t* x_cur = rbt->root;
pair_t* x_par = QSE_NULL;
pair_t* x_new;
2010-10-31 06:49:18 +00:00
while (!IS_NIL(rbt,x_cur))
2010-10-31 06:49:18 +00:00
{
int n = rbt->style->comper (rbt, kptr, klen, KPTR(x_cur), KLEN(x_cur));
if (n == 0)
2010-10-31 06:49:18 +00:00
{
/* back up the contents of the current pair
2010-10-31 06:49:18 +00:00
* in case it is reallocated */
2014-11-17 17:18:11 +00:00
pair_t tmp;
tmp = *x_cur;
2010-10-31 06:49:18 +00:00
/* call the callback function to manipulate the pair */
x_new = cbserter (rbt, x_cur, kptr, klen, ctx);
if (x_new == QSE_NULL)
2010-10-31 06:49:18 +00:00
{
/* error returned by the callback function */
return QSE_NULL;
}
if (x_new != x_cur)
2010-10-31 06:49:18 +00:00
{
/* the current pair has been reallocated, which implicitly
* means the previous contents were wiped out. so the contents
* backed up will be used for restoration/migration */
x_new->color = tmp.color;
x_new->left = tmp.left;
x_new->right = tmp.right;
x_new->parent = tmp.parent;
2010-10-31 06:49:18 +00:00
if (tmp.parent)
{
if (tmp.parent->left == x_cur)
2010-10-31 06:49:18 +00:00
{
tmp.parent->left = x_new;
2010-10-31 06:49:18 +00:00
}
else
2010-10-31 06:49:18 +00:00
{
QSE_ASSERT (tmp.parent->right == x_cur);
tmp.parent->right = x_new;
2010-10-31 06:49:18 +00:00
}
}
if (!IS_NIL(rbt,tmp.left)) tmp.left->parent = x_new;
if (!IS_NIL(rbt,tmp.right)) tmp.right->parent = x_new;
2010-10-31 06:49:18 +00:00
if (x_cur == rbt->root) rbt->root = x_new;
2010-10-31 06:49:18 +00:00
}
return x_new;
2010-10-31 06:49:18 +00:00
}
x_par = x_cur;
2010-10-31 06:49:18 +00:00
if (n > 0) x_cur = x_cur->right;
else /* if (n < 0) */ x_cur = x_cur->left;
2010-10-31 06:49:18 +00:00
}
x_new = cbserter (rbt, QSE_NULL, kptr, klen, ctx);
if (x_new == QSE_NULL) return QSE_NULL;
2010-10-31 06:49:18 +00:00
if (x_par == QSE_NULL)
2010-10-31 06:49:18 +00:00
{
/* the tree contains no pair */
QSE_ASSERT (rbt->root == &rbt->xnil);
rbt->root = x_new;
2010-10-31 06:49:18 +00:00
}
else
{
/* perform normal binary insert */
int n = rbt->style->comper (rbt, kptr, klen, KPTR(x_par), KLEN(x_par));
2010-10-31 06:49:18 +00:00
if (n > 0)
{
QSE_ASSERT (x_par->right == &rbt->xnil);
x_par->right = x_new;
2010-10-31 06:49:18 +00:00
}
else
{
QSE_ASSERT (x_par->left == &rbt->xnil);
x_par->left = x_new;
2010-10-31 06:49:18 +00:00
}
x_new->parent = x_par;
adjust (rbt, x_new);
2010-10-31 06:49:18 +00:00
}
rbt->root->color = QSE_RBT_BLACK;
2012-10-22 09:36:15 +00:00
rbt->size++;
return x_new;
2010-10-31 06:49:18 +00:00
}
2010-07-14 05:25:24 +00:00
static void adjust_for_delete (rbt_t* rbt, pair_t* pair, pair_t* par)
{
2010-07-14 05:18:30 +00:00
while (pair != rbt->root && pair->color == QSE_RBT_BLACK)
{
2010-07-14 05:25:24 +00:00
pair_t* tmp;
2010-07-14 05:18:30 +00:00
if (pair == par->left)
{
tmp = par->right;
if (tmp->color == QSE_RBT_RED)
{
tmp->color = QSE_RBT_BLACK;
par->color = QSE_RBT_RED;
rotate_left (rbt, par);
tmp = par->right;
}
if (tmp->left->color == QSE_RBT_BLACK &&
tmp->right->color == QSE_RBT_BLACK)
{
if (!IS_NIL(rbt,tmp)) tmp->color = QSE_RBT_RED;
2010-07-14 05:18:30 +00:00
pair = par;
par = pair->parent;
}
else
{
if (tmp->right->color == QSE_RBT_BLACK)
{
if (!IS_NIL(rbt,tmp->left))
tmp->left->color = QSE_RBT_BLACK;
tmp->color = QSE_RBT_RED;
rotate_right (rbt, tmp);
tmp = par->right;
}
tmp->color = par->color;
if (!IS_NIL(rbt,par)) par->color = QSE_RBT_BLACK;
if (tmp->right->color == QSE_RBT_RED)
tmp->right->color = QSE_RBT_BLACK;
rotate_left (rbt, par);
2010-07-14 05:18:30 +00:00
pair = rbt->root;
}
}
else
{
2010-07-14 05:18:30 +00:00
QSE_ASSERT (pair == par->right);
tmp = par->left;
if (tmp->color == QSE_RBT_RED)
{
tmp->color = QSE_RBT_BLACK;
par->color = QSE_RBT_RED;
rotate_right (rbt, par);
tmp = par->left;
}
if (tmp->left->color == QSE_RBT_BLACK &&
tmp->right->color == QSE_RBT_BLACK)
{
if (!IS_NIL(rbt,tmp)) tmp->color = QSE_RBT_RED;
2010-07-14 05:18:30 +00:00
pair = par;
par = pair->parent;
}
else
{
if (tmp->left->color == QSE_RBT_BLACK)
{
if (!IS_NIL(rbt,tmp->right))
tmp->right->color = QSE_RBT_BLACK;
tmp->color = QSE_RBT_RED;
rotate_left (rbt, tmp);
tmp = par->left;
}
tmp->color = par->color;
if (!IS_NIL(rbt,par)) par->color = QSE_RBT_BLACK;
if (tmp->left->color == QSE_RBT_RED)
tmp->left->color = QSE_RBT_BLACK;
rotate_right (rbt, par);
2010-07-14 05:18:30 +00:00
pair = rbt->root;
}
}
}
2010-07-14 05:18:30 +00:00
pair->color = QSE_RBT_BLACK;
}
2010-07-14 05:25:24 +00:00
static void delete_pair (rbt_t* rbt, pair_t* pair)
{
2010-07-14 05:25:24 +00:00
pair_t* x, * y, * par;
2010-07-14 05:18:30 +00:00
QSE_ASSERT (pair && !IS_NIL(rbt,pair));
2010-07-14 05:18:30 +00:00
if (IS_NIL(rbt,pair->left) || IS_NIL(rbt,pair->right))
{
2010-07-14 05:18:30 +00:00
y = pair;
}
else
{
/* find a successor with NIL as a child */
2010-07-14 05:18:30 +00:00
y = pair->right;
while (!IS_NIL(rbt,y->left)) y = y->left;
}
x = IS_NIL(rbt,y->left)? y->right: y->left;
par = y->parent;
if (!IS_NIL(rbt,x)) x->parent = par;
if (par)
{
if (y == par->left)
par->left = x;
else
par->right = x;
}
else
{
rbt->root = x;
}
2010-07-14 05:18:30 +00:00
if (y == pair)
{
if (y->color == QSE_RBT_BLACK && !IS_NIL(rbt,x))
adjust_for_delete (rbt, x, par);
qse_rbt_freepair (rbt, y);
}
else
{
if (y->color == QSE_RBT_BLACK && !IS_NIL(rbt,x))
adjust_for_delete (rbt, x, par);
#if 1
2010-07-14 05:18:30 +00:00
if (pair->parent)
{
2010-07-14 05:18:30 +00:00
if (pair->parent->left == pair) pair->parent->left = y;
if (pair->parent->right == pair) pair->parent->right = y;
}
else
{
rbt->root = y;
}
2010-07-14 05:18:30 +00:00
y->parent = pair->parent;
y->left = pair->left;
y->right = pair->right;
y->color = pair->color;
2010-07-14 05:18:30 +00:00
if (pair->left->parent == pair) pair->left->parent = y;
if (pair->right->parent == pair) pair->right->parent = y;
#else
2010-07-14 05:18:30 +00:00
*y = *pair;
if (y->parent)
{
2010-07-14 05:18:30 +00:00
if (y->parent->left == pair) y->parent->left = y;
if (y->parent->right == pair) y->parent->right = y;
}
else
{
rbt->root = y;
}
2010-07-14 05:18:30 +00:00
if (y->left->parent == pair) y->left->parent = y;
if (y->right->parent == pair) y->right->parent = y;
#endif
qse_rbt_freepair (rbt, pair);
}
2010-07-14 05:18:30 +00:00
rbt->size--;
}
2010-07-14 05:25:24 +00:00
int qse_rbt_delete (rbt_t* rbt, const void* kptr, size_t klen)
{
2010-07-14 05:25:24 +00:00
pair_t* pair;
2010-07-14 05:18:30 +00:00
pair = qse_rbt_search (rbt, kptr, klen);
if (pair == QSE_NULL) return -1;
2010-07-14 05:18:30 +00:00
delete_pair (rbt, pair);
return 0;
}
2010-07-14 05:18:30 +00:00
void qse_rbt_clear (rbt_t* rbt)
{
/* TODO: improve this */
2010-07-14 05:18:30 +00:00
while (!IS_NIL(rbt,rbt->root)) delete_pair (rbt, rbt->root);
}
2010-07-15 06:54:48 +00:00
#if 0
static QSE_INLINE qse_rbt_walk_t walk_recursively (
rbt_t* rbt, walker_t walker, void* ctx, qse_rbt_pair_t* pair)
{
if (!IS_NIL(rbt,pair->left))
{
if (walk_recursively (rbt, walker, ctx, pair->left) == QSE_RBT_WALK_STOP)
return QSE_RBT_WALK_STOP;
}
if (walker (rbt, pair, ctx) == QSE_RBT_WALK_STOP) return QSE_RBT_WALK_STOP;
if (!IS_NIL(rbt,pair->right))
{
if (walk_recursively (rbt, walker, ctx, pair->right) == QSE_RBT_WALK_STOP)
return QSE_RBT_WALK_STOP;
}
return QSE_RBT_WALK_FORWARD;
}
#endif
static QSE_INLINE void walk (
rbt_t* rbt, walker_t walker, void* ctx, int l, int r)
{
pair_t* x_cur = rbt->root;
2010-07-14 05:25:24 +00:00
pair_t* prev = rbt->root->parent;
2010-07-14 05:18:30 +00:00
while (x_cur && !IS_NIL(rbt,x_cur))
{
if (prev == x_cur->parent)
2010-07-14 05:18:30 +00:00
{
/* the previous node is the parent of the current node.
* it indicates that we're going down to the child[l] */
if (!IS_NIL(rbt,x_cur->child[l]))
2010-07-14 05:18:30 +00:00
{
/* go to the child[l] child */
prev = x_cur;
x_cur = x_cur->child[l];
2010-07-14 05:18:30 +00:00
}
else
{
if (walker (rbt, x_cur, ctx) == QSE_RBT_WALK_STOP) break;
if (!IS_NIL(rbt,x_cur->child[r]))
2010-07-14 05:18:30 +00:00
{
/* go down to the right node if exists */
prev = x_cur;
x_cur = x_cur->child[r];
2010-07-14 05:18:30 +00:00
}
else
{
/* otherwise, move up to the parent */
prev = x_cur;
x_cur = x_cur->parent;
2010-07-14 05:18:30 +00:00
}
}
}
else if (prev == x_cur->child[l])
2010-07-14 05:18:30 +00:00
{
/* the left child has been already traversed */
if (walker (rbt, x_cur, ctx) == QSE_RBT_WALK_STOP) break;
2010-07-14 05:18:30 +00:00
if (!IS_NIL(rbt,x_cur->child[r]))
2010-07-14 05:18:30 +00:00
{
/* go down to the right node if it exists */
prev = x_cur;
x_cur = x_cur->child[r];
2010-07-14 05:18:30 +00:00
}
else
{
/* otherwise, move up to the parent */
prev = x_cur;
x_cur = x_cur->parent;
2010-07-14 05:18:30 +00:00
}
}
else
{
2010-07-15 06:54:48 +00:00
/* both the left child and the right child have been traversed */
QSE_ASSERT (prev == x_cur->child[r]);
2010-07-14 05:18:30 +00:00
/* just move up to the parent */
prev = x_cur;
x_cur = x_cur->parent;
2010-07-14 05:18:30 +00:00
}
}
2010-07-14 05:18:30 +00:00
}
2010-07-14 05:18:30 +00:00
void qse_rbt_walk (rbt_t* rbt, walker_t walker, void* ctx)
{
walk (rbt, walker, ctx, LEFT, RIGHT);
}
2010-07-14 05:18:30 +00:00
void qse_rbt_rwalk (rbt_t* rbt, walker_t walker, void* ctx)
{
2010-07-14 05:18:30 +00:00
walk (rbt, walker, ctx, RIGHT, LEFT);
}
2010-07-15 06:54:48 +00:00
int qse_rbt_dflcomp (
const qse_rbt_t* rbt,
const void* kptr1, size_t klen1,
const void* kptr2, size_t klen2)
{
size_t min;
int n, nn;
if (klen1 < klen2)
{
min = klen1;
nn = -1;
}
else
{
min = klen2;
nn = (klen1 == klen2)? 0: 1;
}
n = QSE_MEMCMP (kptr1, kptr2, KTOB(rbt,min));
if (n == 0) n = nn;
return n;
}