2015-11-01 09:47:27 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-11-19 09:40:26 +00:00
|
|
|
Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved.
|
2015-11-01 09:47:27 +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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-11-15 09:39:12 +00:00
|
|
|
#include <moo-rbt.h>
|
2017-01-09 09:54:49 +00:00
|
|
|
#include "moo-prv.h"
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#define copier_t moo_rbt_copier_t
|
|
|
|
#define freeer_t moo_rbt_freeer_t
|
|
|
|
#define comper_t moo_rbt_comper_t
|
|
|
|
#define keeper_t moo_rbt_keeper_t
|
|
|
|
#define walker_t moo_rbt_walker_t
|
|
|
|
#define cbserter_t moo_rbt_cbserter_t
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#define KPTR(p) MOO_RBT_KPTR(p)
|
|
|
|
#define KLEN(p) MOO_RBT_KLEN(p)
|
|
|
|
#define VPTR(p) MOO_RBT_VPTR(p)
|
|
|
|
#define VLEN(p) MOO_RBT_VLEN(p)
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#define KTOB(rbt,len) ((len)*(rbt)->scale[MOO_RBT_KEY])
|
|
|
|
#define VTOB(rbt,len) ((len)*(rbt)->scale[MOO_RBT_VAL])
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
#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);
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_INLINE moo_rbt_pair_t* moo_rbt_allocpair (
|
|
|
|
moo_rbt_t* rbt, void* kptr, moo_oow_t klen, void* vptr, moo_oow_t vlen)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-04-28 14:45:13 +00:00
|
|
|
moo_rbt_pair_t* pair;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
copier_t kcop = rbt->style->copier[MOO_RBT_KEY];
|
|
|
|
copier_t vcop = rbt->style->copier[MOO_RBT_VAL];
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t as = MOO_SIZEOF(moo_rbt_pair_t);
|
2017-04-28 14:45:13 +00:00
|
|
|
if (kcop == MOO_RBT_COPIER_INLINE) as += MOO_ALIGN_POW2(KTOB(rbt,klen), MOO_SIZEOF_VOID_P);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (vcop == MOO_RBT_COPIER_INLINE) as += VTOB(rbt,vlen);
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2019-06-21 07:21:58 +00:00
|
|
|
pair = (moo_rbt_pair_t*)moo_allocmem(rbt->moo, as);
|
|
|
|
if (!pair) return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-04-28 14:45:13 +00:00
|
|
|
pair->color = MOO_RBT_RED;
|
|
|
|
pair->parent = MOO_NULL;
|
|
|
|
pair->child[LEFT] = &rbt->xnil;
|
|
|
|
pair->child[RIGHT] = &rbt->xnil;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-04-28 14:45:13 +00:00
|
|
|
KLEN(pair) = klen;
|
2017-01-09 09:54:49 +00:00
|
|
|
if (kcop == MOO_RBT_COPIER_SIMPLE)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-04-28 14:45:13 +00:00
|
|
|
KPTR(pair) = kptr;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
2017-01-09 09:54:49 +00:00
|
|
|
else if (kcop == MOO_RBT_COPIER_INLINE)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-04-28 14:45:13 +00:00
|
|
|
KPTR(pair) = pair + 1;
|
|
|
|
if (kptr) MOO_MEMCPY (KPTR(pair), kptr, KTOB(rbt,klen));
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-21 07:21:58 +00:00
|
|
|
KPTR(pair) = kcop(rbt, kptr, klen);
|
2017-04-28 14:45:13 +00:00
|
|
|
if (KPTR(pair) == MOO_NULL)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2019-06-21 07:21:58 +00:00
|
|
|
moo_freemem (rbt->moo, pair);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 14:45:13 +00:00
|
|
|
VLEN(pair) = vlen;
|
2017-01-09 09:54:49 +00:00
|
|
|
if (vcop == MOO_RBT_COPIER_SIMPLE)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-04-28 14:45:13 +00:00
|
|
|
VPTR(pair) = vptr;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
2017-01-09 09:54:49 +00:00
|
|
|
else if (vcop == MOO_RBT_COPIER_INLINE)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-04-28 14:45:13 +00:00
|
|
|
VPTR(pair) = pair + 1;
|
2017-01-09 09:54:49 +00:00
|
|
|
if (kcop == MOO_RBT_COPIER_INLINE)
|
2017-04-28 14:45:13 +00:00
|
|
|
VPTR(pair) = (moo_oob_t*)VPTR(pair) + MOO_ALIGN_POW2(KTOB(rbt,klen), MOO_SIZEOF_VOID_P);
|
|
|
|
if (vptr) MOO_MEMCPY (VPTR(pair), vptr, VTOB(rbt,vlen));
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-28 14:45:13 +00:00
|
|
|
VPTR(pair) = vcop (rbt, vptr, vlen);
|
|
|
|
if (VPTR(pair) != MOO_NULL)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (rbt->style->freeer[MOO_RBT_KEY] != MOO_NULL)
|
2017-04-28 14:45:13 +00:00
|
|
|
rbt->style->freeer[MOO_RBT_KEY] (rbt, KPTR(pair), KLEN(pair));
|
2019-06-21 07:21:58 +00:00
|
|
|
moo_freemem (rbt->moo, pair);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 14:45:13 +00:00
|
|
|
return pair;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_INLINE void moo_rbt_freepair (moo_rbt_t* rbt, moo_rbt_pair_t* pair)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (rbt->style->freeer[MOO_RBT_KEY] != MOO_NULL)
|
|
|
|
rbt->style->freeer[MOO_RBT_KEY] (rbt, KPTR(pair), KLEN(pair));
|
|
|
|
if (rbt->style->freeer[MOO_RBT_VAL] != MOO_NULL)
|
|
|
|
rbt->style->freeer[MOO_RBT_VAL] (rbt, VPTR(pair), VLEN(pair));
|
2019-06-21 07:21:58 +00:00
|
|
|
moo_freemem (rbt->moo, pair);
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static moo_rbt_style_t style[] =
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_COPIER_DEFAULT,
|
|
|
|
MOO_RBT_COPIER_DEFAULT
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_FREEER_DEFAULT,
|
|
|
|
MOO_RBT_FREEER_DEFAULT
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_COMPER_DEFAULT,
|
|
|
|
MOO_RBT_KEEPER_DEFAULT
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_COPIER_INLINE,
|
|
|
|
MOO_RBT_COPIER_INLINE
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_FREEER_DEFAULT,
|
|
|
|
MOO_RBT_FREEER_DEFAULT
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_COMPER_DEFAULT,
|
|
|
|
MOO_RBT_KEEPER_DEFAULT
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_COPIER_INLINE,
|
|
|
|
MOO_RBT_COPIER_DEFAULT
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_FREEER_DEFAULT,
|
|
|
|
MOO_RBT_FREEER_DEFAULT
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_COMPER_DEFAULT,
|
|
|
|
MOO_RBT_KEEPER_DEFAULT
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_COPIER_DEFAULT,
|
|
|
|
MOO_RBT_COPIER_INLINE
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_FREEER_DEFAULT,
|
|
|
|
MOO_RBT_FREEER_DEFAULT
|
2015-11-01 09:47:27 +00:00
|
|
|
},
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_RBT_COMPER_DEFAULT,
|
|
|
|
MOO_RBT_KEEPER_DEFAULT
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-18 14:11:14 +00:00
|
|
|
const moo_rbt_style_t* moo_get_rbt_style (moo_rbt_style_kind_t kind)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
return &style[kind];
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_t* moo_rbt_open (moo_t* moo, moo_oow_t xtnsize, int kscale, int vscale)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_t* rbt;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2019-06-21 07:21:58 +00:00
|
|
|
rbt = (moo_rbt_t*)moo_allocmem(moo, MOO_SIZEOF(moo_rbt_t) + xtnsize);
|
|
|
|
if (!rbt) return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2019-06-21 07:21:58 +00:00
|
|
|
if (moo_rbt_init(rbt, moo, kscale, vscale) <= -1)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2019-06-21 07:21:58 +00:00
|
|
|
moo_freemem (moo, rbt);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_MEMSET (rbt + 1, 0, xtnsize);
|
2015-11-01 09:47:27 +00:00
|
|
|
return rbt;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
void moo_rbt_close (moo_rbt_t* rbt)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_fini (rbt);
|
2019-06-21 07:21:58 +00:00
|
|
|
moo_freemem (rbt->moo, rbt);
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_rbt_init (moo_rbt_t* rbt, moo_t* moo, int kscale, int vscale)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
/* do not zero out the extension */
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_MEMSET (rbt, 0, MOO_SIZEOF(*rbt));
|
|
|
|
rbt->moo = moo;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
rbt->scale[MOO_RBT_KEY] = (kscale < 1)? 1: kscale;
|
|
|
|
rbt->scale[MOO_RBT_VAL] = (vscale < 1)? 1: vscale;
|
2015-11-01 09:47:27 +00:00
|
|
|
rbt->size = 0;
|
|
|
|
|
|
|
|
rbt->style = &style[0];
|
|
|
|
|
|
|
|
/* self-initializing nil */
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_MEMSET(&rbt->xnil, 0, MOO_SIZEOF(rbt->xnil));
|
|
|
|
rbt->xnil.color = MOO_RBT_BLACK;
|
2015-11-01 09:47:27 +00:00
|
|
|
rbt->xnil.left = &rbt->xnil;
|
|
|
|
rbt->xnil.right = &rbt->xnil;
|
|
|
|
|
|
|
|
/* root is set to nil initially */
|
|
|
|
rbt->root = &rbt->xnil;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
void moo_rbt_fini (moo_rbt_t* rbt)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_clear (rbt);
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_rbt_style_t* moo_rbt_getstyle (const moo_rbt_t* rbt)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
return rbt->style;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
void moo_rbt_setstyle (moo_rbt_t* rbt, const moo_rbt_style_t* style)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, style != MOO_NULL);
|
2015-11-01 09:47:27 +00:00
|
|
|
rbt->style = style;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t moo_rbt_getsize (const moo_rbt_t* rbt)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
return rbt->size;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* moo_rbt_search (const moo_rbt_t* rbt, const void* kptr, moo_oow_t klen)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* pair = rbt->root;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
while (!IS_NIL(rbt,pair))
|
|
|
|
{
|
|
|
|
int n = rbt->style->comper (rbt, kptr, klen, KPTR(pair), KLEN(pair));
|
|
|
|
if (n == 0) return pair;
|
|
|
|
|
|
|
|
if (n > 0) pair = pair->right;
|
|
|
|
else /* if (n < 0) */ pair = pair->left;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static void rotate (moo_rbt_t* rbt, moo_rbt_pair_t* pivot, int leftwise)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* == 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
|
|
|
|
* 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,
|
|
|
|
*/
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* parent, * z, * c;
|
2015-11-01 09:47:27 +00:00
|
|
|
int cid1, cid2;
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, pivot != MOO_NULL);
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, parent->right == pivot);
|
2015-11-01 09:47:27 +00:00
|
|
|
parent->right = z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, rbt->root == pivot);
|
2015-11-01 09:47:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static void adjust (moo_rbt_t* rbt, moo_rbt_pair_t* pair)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
while (pair != rbt->root)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* tmp, * tmp2, * x_par;
|
2015-11-01 09:47:27 +00:00
|
|
|
int leftwise;
|
|
|
|
|
|
|
|
x_par = pair->parent;
|
2017-01-09 09:54:49 +00:00
|
|
|
if (x_par->color == MOO_RBT_BLACK) break;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, x_par->parent != MOO_NULL);
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
if (tmp->color == MOO_RBT_RED)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
x_par->color = MOO_RBT_BLACK;
|
|
|
|
tmp->color = MOO_RBT_BLACK;
|
|
|
|
x_par->parent->color = MOO_RBT_RED;
|
2015-11-01 09:47:27 +00:00
|
|
|
pair = x_par->parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pair == tmp2)
|
|
|
|
{
|
|
|
|
pair = x_par;
|
|
|
|
rotate (rbt, pair, leftwise);
|
|
|
|
x_par = pair->parent;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
x_par->color = MOO_RBT_BLACK;
|
|
|
|
x_par->parent->color = MOO_RBT_RED;
|
2015-11-01 09:47:27 +00:00
|
|
|
rotate (rbt, x_par->parent, !leftwise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static moo_rbt_pair_t* change_pair_val (
|
|
|
|
moo_rbt_t* rbt, moo_rbt_pair_t* pair, void* vptr, moo_oow_t vlen)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
if (VPTR(pair) == vptr && VLEN(pair) == vlen)
|
|
|
|
{
|
|
|
|
/* if the old value and the new value are the same,
|
|
|
|
* it just calls the handler for this condition.
|
|
|
|
* No value replacement occurs. */
|
2017-01-09 09:54:49 +00:00
|
|
|
if (rbt->style->keeper != MOO_NULL)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
rbt->style->keeper (rbt, vptr, vlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
copier_t vcop = rbt->style->copier[MOO_RBT_VAL];
|
2015-11-01 09:47:27 +00:00
|
|
|
void* ovptr = VPTR(pair);
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t ovlen = VLEN(pair);
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
/* place the new value according to the copier */
|
2017-01-09 09:54:49 +00:00
|
|
|
if (vcop == MOO_RBT_COPIER_SIMPLE)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
VPTR(pair) = vptr;
|
|
|
|
VLEN(pair) = vlen;
|
|
|
|
}
|
2017-01-09 09:54:49 +00:00
|
|
|
else if (vcop == MOO_RBT_COPIER_INLINE)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
if (ovlen == vlen)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (vptr) MOO_MEMCPY (VPTR(pair), vptr, VTOB(rbt,vlen));
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* need to reconstruct the pair */
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* p = moo_rbt_allocpair (rbt,
|
2015-11-01 09:47:27 +00:00
|
|
|
KPTR(pair), KLEN(pair),
|
|
|
|
vptr, vlen);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (p == MOO_NULL) return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, pair->parent->right == pair);
|
2015-11-01 09:47:27 +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;
|
|
|
|
|
|
|
|
if (pair == rbt->root) rbt->root = p;
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_freepair (rbt, pair);
|
2015-11-01 09:47:27 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
void* nvptr = vcop (rbt, vptr, vlen);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (nvptr == MOO_NULL) return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
VPTR(pair) = nvptr;
|
|
|
|
VLEN(pair) = vlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free up the old value */
|
2017-01-09 09:54:49 +00:00
|
|
|
if (rbt->style->freeer[MOO_RBT_VAL] != MOO_NULL)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
rbt->style->freeer[MOO_RBT_VAL] (rbt, ovptr, ovlen);
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pair;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static moo_rbt_pair_t* insert (
|
|
|
|
moo_rbt_t* rbt, void* kptr, moo_oow_t klen, void* vptr, moo_oow_t vlen, int opt)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* x_cur = rbt->root;
|
|
|
|
moo_rbt_pair_t* x_par = MOO_NULL;
|
|
|
|
moo_rbt_pair_t* x_new;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
while (!IS_NIL(rbt,x_cur))
|
|
|
|
{
|
|
|
|
int n = rbt->style->comper (rbt, kptr, klen, KPTR(x_cur), KLEN(x_cur));
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case UPSERT:
|
|
|
|
case UPDATE:
|
|
|
|
return change_pair_val (rbt, x_cur, vptr, vlen);
|
|
|
|
|
|
|
|
case ENSERT:
|
|
|
|
/* return existing pair */
|
|
|
|
return x_cur;
|
|
|
|
|
|
|
|
case INSERT:
|
|
|
|
/* return failure */
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x_par = x_cur;
|
|
|
|
|
|
|
|
if (n > 0) x_cur = x_cur->right;
|
|
|
|
else /* if (n < 0) */ x_cur = x_cur->left;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
if (opt == UPDATE) return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
x_new = moo_rbt_allocpair (rbt, kptr, klen, vptr, vlen);
|
|
|
|
if (x_new == MOO_NULL) return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
if (x_par == MOO_NULL)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
/* the tree contains no pair */
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, rbt->root == &rbt->xnil);
|
2015-11-01 09:47:27 +00:00
|
|
|
rbt->root = x_new;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* perform normal binary insert */
|
|
|
|
int n = rbt->style->comper (rbt, kptr, klen, KPTR(x_par), KLEN(x_par));
|
|
|
|
if (n > 0)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, x_par->right == &rbt->xnil);
|
2015-11-01 09:47:27 +00:00
|
|
|
x_par->right = x_new;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, x_par->left == &rbt->xnil);
|
2015-11-01 09:47:27 +00:00
|
|
|
x_par->left = x_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
x_new->parent = x_par;
|
|
|
|
adjust (rbt, x_new);
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
rbt->root->color = MOO_RBT_BLACK;
|
2015-11-01 09:47:27 +00:00
|
|
|
rbt->size++;
|
|
|
|
return x_new;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* moo_rbt_upsert (
|
|
|
|
moo_rbt_t* rbt, void* kptr, moo_oow_t klen, void* vptr, moo_oow_t vlen)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
return insert (rbt, kptr, klen, vptr, vlen, UPSERT);
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* moo_rbt_ensert (
|
|
|
|
moo_rbt_t* rbt, void* kptr, moo_oow_t klen, void* vptr, moo_oow_t vlen)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
return insert (rbt, kptr, klen, vptr, vlen, ENSERT);
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* moo_rbt_insert (
|
|
|
|
moo_rbt_t* rbt, void* kptr, moo_oow_t klen, void* vptr, moo_oow_t vlen)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
return insert (rbt, kptr, klen, vptr, vlen, INSERT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* moo_rbt_update (
|
|
|
|
moo_rbt_t* rbt, void* kptr, moo_oow_t klen, void* vptr, moo_oow_t vlen)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
return insert (rbt, kptr, klen, vptr, vlen, UPDATE);
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* moo_rbt_cbsert (
|
|
|
|
moo_rbt_t* rbt, void* kptr, moo_oow_t klen, cbserter_t cbserter, void* ctx)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* x_cur = rbt->root;
|
|
|
|
moo_rbt_pair_t* x_par = MOO_NULL;
|
|
|
|
moo_rbt_pair_t* x_new;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
while (!IS_NIL(rbt,x_cur))
|
|
|
|
{
|
|
|
|
int n = rbt->style->comper (rbt, kptr, klen, KPTR(x_cur), KLEN(x_cur));
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* back up the contents of the current pair
|
|
|
|
* in case it is reallocated */
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t tmp;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
tmp = *x_cur;
|
|
|
|
|
|
|
|
/* call the callback function to manipulate the pair */
|
|
|
|
x_new = cbserter (rbt, x_cur, kptr, klen, ctx);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (x_new == MOO_NULL)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
/* error returned by the callback function */
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (x_new != x_cur)
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
if (tmp.parent)
|
|
|
|
{
|
|
|
|
if (tmp.parent->left == x_cur)
|
|
|
|
{
|
|
|
|
tmp.parent->left = x_new;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, tmp.parent->right == x_cur);
|
2015-11-01 09:47:27 +00:00
|
|
|
tmp.parent->right = x_new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!IS_NIL(rbt,tmp.left)) tmp.left->parent = x_new;
|
|
|
|
if (!IS_NIL(rbt,tmp.right)) tmp.right->parent = x_new;
|
|
|
|
|
|
|
|
if (x_cur == rbt->root) rbt->root = x_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
return x_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
x_par = x_cur;
|
|
|
|
|
|
|
|
if (n > 0) x_cur = x_cur->right;
|
|
|
|
else /* if (n < 0) */ x_cur = x_cur->left;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
x_new = cbserter (rbt, MOO_NULL, kptr, klen, ctx);
|
|
|
|
if (x_new == MOO_NULL) return MOO_NULL;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
if (x_par == MOO_NULL)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
/* the tree contains no pair */
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, rbt->root == &rbt->xnil);
|
2015-11-01 09:47:27 +00:00
|
|
|
rbt->root = x_new;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* perform normal binary insert */
|
|
|
|
int n = rbt->style->comper (rbt, kptr, klen, KPTR(x_par), KLEN(x_par));
|
|
|
|
if (n > 0)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, x_par->right == &rbt->xnil);
|
2015-11-01 09:47:27 +00:00
|
|
|
x_par->right = x_new;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, x_par->left == &rbt->xnil);
|
2015-11-01 09:47:27 +00:00
|
|
|
x_par->left = x_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
x_new->parent = x_par;
|
|
|
|
adjust (rbt, x_new);
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
rbt->root->color = MOO_RBT_BLACK;
|
2015-11-01 09:47:27 +00:00
|
|
|
rbt->size++;
|
|
|
|
return x_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static void adjust_for_delete (moo_rbt_t* rbt, moo_rbt_pair_t* pair, moo_rbt_pair_t* par)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
while (pair != rbt->root && pair->color == MOO_RBT_BLACK)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* tmp;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
if (pair == par->left)
|
|
|
|
{
|
|
|
|
tmp = par->right;
|
2017-01-09 09:54:49 +00:00
|
|
|
if (tmp->color == MOO_RBT_RED)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
tmp->color = MOO_RBT_BLACK;
|
|
|
|
par->color = MOO_RBT_RED;
|
2015-11-01 09:47:27 +00:00
|
|
|
rotate_left (rbt, par);
|
|
|
|
tmp = par->right;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
if (tmp->left->color == MOO_RBT_BLACK &&
|
|
|
|
tmp->right->color == MOO_RBT_BLACK)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!IS_NIL(rbt,tmp)) tmp->color = MOO_RBT_RED;
|
2015-11-01 09:47:27 +00:00
|
|
|
pair = par;
|
|
|
|
par = pair->parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (tmp->right->color == MOO_RBT_BLACK)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
if (!IS_NIL(rbt,tmp->left))
|
2017-01-09 09:54:49 +00:00
|
|
|
tmp->left->color = MOO_RBT_BLACK;
|
|
|
|
tmp->color = MOO_RBT_RED;
|
2015-11-01 09:47:27 +00:00
|
|
|
rotate_right (rbt, tmp);
|
|
|
|
tmp = par->right;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp->color = par->color;
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!IS_NIL(rbt,par)) par->color = MOO_RBT_BLACK;
|
|
|
|
if (tmp->right->color == MOO_RBT_RED)
|
|
|
|
tmp->right->color = MOO_RBT_BLACK;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
rotate_left (rbt, par);
|
|
|
|
pair = rbt->root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, pair == par->right);
|
2015-11-01 09:47:27 +00:00
|
|
|
tmp = par->left;
|
2017-01-09 09:54:49 +00:00
|
|
|
if (tmp->color == MOO_RBT_RED)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
tmp->color = MOO_RBT_BLACK;
|
|
|
|
par->color = MOO_RBT_RED;
|
2015-11-01 09:47:27 +00:00
|
|
|
rotate_right (rbt, par);
|
|
|
|
tmp = par->left;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
if (tmp->left->color == MOO_RBT_BLACK &&
|
|
|
|
tmp->right->color == MOO_RBT_BLACK)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!IS_NIL(rbt,tmp)) tmp->color = MOO_RBT_RED;
|
2015-11-01 09:47:27 +00:00
|
|
|
pair = par;
|
|
|
|
par = pair->parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (tmp->left->color == MOO_RBT_BLACK)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
if (!IS_NIL(rbt,tmp->right))
|
2017-01-09 09:54:49 +00:00
|
|
|
tmp->right->color = MOO_RBT_BLACK;
|
|
|
|
tmp->color = MOO_RBT_RED;
|
2015-11-01 09:47:27 +00:00
|
|
|
rotate_left (rbt, tmp);
|
|
|
|
tmp = par->left;
|
|
|
|
}
|
|
|
|
tmp->color = par->color;
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!IS_NIL(rbt,par)) par->color = MOO_RBT_BLACK;
|
|
|
|
if (tmp->left->color == MOO_RBT_RED)
|
|
|
|
tmp->left->color = MOO_RBT_BLACK;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
rotate_right (rbt, par);
|
|
|
|
pair = rbt->root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
pair->color = MOO_RBT_BLACK;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static void delete_pair (moo_rbt_t* rbt, moo_rbt_pair_t* pair)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* x, * y, * par;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, pair && !IS_NIL(rbt,pair));
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
if (IS_NIL(rbt,pair->left) || IS_NIL(rbt,pair->right))
|
|
|
|
{
|
|
|
|
y = pair;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* find a successor with NIL as a child */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y == pair)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (y->color == MOO_RBT_BLACK && !IS_NIL(rbt,x))
|
2015-11-01 09:47:27 +00:00
|
|
|
adjust_for_delete (rbt, x, par);
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_freepair (rbt, y);
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (y->color == MOO_RBT_BLACK && !IS_NIL(rbt,x))
|
2015-11-01 09:47:27 +00:00
|
|
|
adjust_for_delete (rbt, x, par);
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
if (pair->parent)
|
|
|
|
{
|
|
|
|
if (pair->parent->left == pair) pair->parent->left = y;
|
|
|
|
if (pair->parent->right == pair) pair->parent->right = y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rbt->root = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
y->parent = pair->parent;
|
|
|
|
y->left = pair->left;
|
|
|
|
y->right = pair->right;
|
|
|
|
y->color = pair->color;
|
|
|
|
|
|
|
|
if (pair->left->parent == pair) pair->left->parent = y;
|
|
|
|
if (pair->right->parent == pair) pair->right->parent = y;
|
|
|
|
#else
|
|
|
|
*y = *pair;
|
|
|
|
if (y->parent)
|
|
|
|
{
|
|
|
|
if (y->parent->left == pair) y->parent->left = y;
|
|
|
|
if (y->parent->right == pair) y->parent->right = y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rbt->root = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y->left->parent == pair) y->left->parent = y;
|
|
|
|
if (y->right->parent == pair) y->right->parent = y;
|
|
|
|
#endif
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_freepair (rbt, pair);
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rbt->size--;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_rbt_delete (moo_rbt_t* rbt, const void* kptr, moo_oow_t klen)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* pair;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
pair = moo_rbt_search (rbt, kptr, klen);
|
|
|
|
if (pair == MOO_NULL) return -1;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
delete_pair (rbt, pair);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
void moo_rbt_clear (moo_rbt_t* rbt)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
/* TODO: improve this */
|
|
|
|
while (!IS_NIL(rbt,rbt->root)) delete_pair (rbt, rbt->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2017-01-09 09:54:49 +00:00
|
|
|
static MOO_INLINE moo_rbt_walk_t walk_recursively (
|
|
|
|
moo_rbt_t* rbt, walker_t walker, void* ctx, moo_rbt_pair_t* pair)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
if (!IS_NIL(rbt,pair->left))
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (walk_recursively (rbt, walker, ctx, pair->left) == MOO_RBT_WALK_STOP)
|
|
|
|
return MOO_RBT_WALK_STOP;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
if (walker (rbt, pair, ctx) == MOO_RBT_WALK_STOP) return MOO_RBT_WALK_STOP;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
if (!IS_NIL(rbt,pair->right))
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (walk_recursively (rbt, walker, ctx, pair->right) == MOO_RBT_WALK_STOP)
|
|
|
|
return MOO_RBT_WALK_STOP;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_RBT_WALK_FORWARD;
|
2015-11-01 09:47:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static MOO_INLINE void walk (moo_rbt_t* rbt, walker_t walker, void* ctx, int l, int r)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_rbt_pair_t* x_cur = rbt->root;
|
|
|
|
moo_rbt_pair_t* prev = rbt->root->parent;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
while (x_cur && !IS_NIL(rbt,x_cur))
|
|
|
|
{
|
|
|
|
if (prev == x_cur->parent)
|
|
|
|
{
|
|
|
|
/* 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]))
|
|
|
|
{
|
|
|
|
/* go to the child[l] child */
|
|
|
|
prev = x_cur;
|
|
|
|
x_cur = x_cur->child[l];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (walker (rbt, x_cur, ctx) == MOO_RBT_WALK_STOP) break;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
if (!IS_NIL(rbt,x_cur->child[r]))
|
|
|
|
{
|
|
|
|
/* go down to the right node if exists */
|
|
|
|
prev = x_cur;
|
|
|
|
x_cur = x_cur->child[r];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* otherwise, move up to the parent */
|
|
|
|
prev = x_cur;
|
|
|
|
x_cur = x_cur->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (prev == x_cur->child[l])
|
|
|
|
{
|
|
|
|
/* the left child has been already traversed */
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
if (walker (rbt, x_cur, ctx) == MOO_RBT_WALK_STOP) break;
|
2015-11-01 09:47:27 +00:00
|
|
|
|
|
|
|
if (!IS_NIL(rbt,x_cur->child[r]))
|
|
|
|
{
|
|
|
|
/* go down to the right node if it exists */
|
|
|
|
prev = x_cur;
|
|
|
|
x_cur = x_cur->child[r];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* otherwise, move up to the parent */
|
|
|
|
prev = x_cur;
|
|
|
|
x_cur = x_cur->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* both the left child and the right child have been traversed */
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (rbt->moo, prev == x_cur->child[r]);
|
2015-11-01 09:47:27 +00:00
|
|
|
/* just move up to the parent */
|
|
|
|
prev = x_cur;
|
|
|
|
x_cur = x_cur->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
void moo_rbt_walk (moo_rbt_t* rbt, walker_t walker, void* ctx)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
walk (rbt, walker, ctx, LEFT, RIGHT);
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
void moo_rbt_rwalk (moo_rbt_t* rbt, walker_t walker, void* ctx)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
|
|
|
walk (rbt, walker, ctx, RIGHT, LEFT);
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_rbt_dflcomp (const moo_rbt_t* rbt, const void* kptr1, moo_oow_t klen1, const void* kptr2, moo_oow_t klen2)
|
2015-11-01 09:47:27 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t min;
|
2015-11-01 09:47:27 +00:00
|
|
|
int n, nn;
|
|
|
|
|
|
|
|
if (klen1 < klen2)
|
|
|
|
{
|
|
|
|
min = klen1;
|
|
|
|
nn = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
min = klen2;
|
|
|
|
nn = (klen1 == klen2)? 0: 1;
|
|
|
|
}
|
|
|
|
|
2019-11-15 09:39:12 +00:00
|
|
|
n = MOO_MEMCMP(kptr1, kptr2, KTOB(rbt,min));
|
2015-11-01 09:47:27 +00:00
|
|
|
if (n == 0) n = nn;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|