Changeset 35


Ignore:
Timestamp:
Nov 8, 2009, 8:39:03 PM (16 years ago)
Author:
bird
Message:

Templated red-black trees - kick off.

Location:
trunk/include/k
Files:
2 copied
10 moved

Legend:

Unmodified
Added
Removed
  • trunk/include/k/kRbTmpl/kRbBase.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvlTmpl - Templated AVL Trees, The Mandatory Base Code.
     3 * kRbTmpl - Templated Red-Black Trees, The Mandatory Base Code.
    44 */
    55
     
    3131/** @page pg_kAvlTmpl   Template Configuration.
    3232 *
    33  *  This is a templated implementation of AVL trees in C. The template
    34  *  parameters relates to the kind of key used and how duplicates are
    35  *  treated.
    36  *
    37  *  \#define KAVL_EQUAL_ALLOWED
     33 *  This is a templated implementation of Red-Black trees in C.  The template
     34 *  parameters relates to the kind of key used and how duplicates are treated.
     35 *
     36 *  \#define KRB_EQUAL_ALLOWED
    3837 *  Define this to tell us that equal keys are allowed.
    39  *  Then Equal keys will be put in a list pointed to by KAVLNODE::pList.
     38 *  Then Equal keys will be put in a list pointed to by KRBNODE::pList.
    4039 *  This is by default not defined.
    4140 *
    42  *  \#define KAVL_CHECK_FOR_EQUAL_INSERT
     41 *  \#define KRB_CHECK_FOR_EQUAL_INSERT
    4342 *  Define this to enable insert check for equal nodes.
    4443 *  This is by default not defined.
    4544 *
    46  *  \#define KAVL_MAX_STACK
     45 *  \#define KRB_MAX_STACK
    4746 *  Use this to specify the max number of stack entries the stack will use when
    4847 *  inserting and removing nodes from the tree. The size should be something like
     
    5049 *  Must be defined.
    5150 *
    52  *  \#define KAVL_RANGE
     51 *  \#define KRB_RANGE
    5352 *  Define this to enable key ranges.
    5453 *
    55  *  \#define KAVL_OFFSET
     54 *  \#define KRB_OFFSET
    5655 *  Define this to link the tree together using self relative offset
    5756 *  instead of memory pointers, thus making the entire tree relocatable
     
    5958 *  the exact same distance.
    6059 *
    61  *  \#define KAVL_LOOKTHRU
     60 *  \#define KRB_CACHE_SIZE
    6261 *  Define this to employ a lookthru cache (direct) to speed up lookup for
    63  *  some usage patterns. The value should be the number of members of the
    64  *   array.
    65  *
    66  *  \#define KAVL_LOOKTHRU_HASH(Key)
     62 *  some usage patterns. The value should be the number of members of the array.
     63 *
     64 *  \#define KRB_CACHE_HASH(Key)
    6765 *  Define this to specify a more efficient translation of the key into
    6866 *  a lookthru array index. The default is key % size.
    6967 *  For some key types this is required as the default will not compile.
    7068 *
    71  *  \#define KAVL_LOCKED
     69 *  \#define KRB_LOCKED
    7270 *  Define this if you wish for the tree to be locked via the
    73  *  KAVL_WRITE_LOCK,  KAVL_WRITE_UNLOCK, KAVL_READ_LOCK and
    74  *  KAVL_READ_UNLOCK macros. If not defined the tree will not be subject
     71 *  KRB_WRITE_LOCK,  KRB_WRITE_UNLOCK, KRB_READ_LOCK and
     72 *  KRB_READ_UNLOCK macros. If not defined the tree will not be subject
    7573 *  do any kind of locking and the problem of concurrency is left the user.
    7674 *
    77  *  \#define KAVL_WRITE_LOCK(pRoot)
     75 *  \#define KRB_WRITE_LOCK(pRoot)
    7876 *  Lock the tree for writing.
    7977 *
    80  *  \#define KAVL_WRITE_UNLOCK(pRoot)
    81  *  Counteracts KAVL_WRITE_LOCK.
    82  *
    83  *  \#define KAVL_READ_LOCK(pRoot)
     78 *  \#define KRB_WRITE_UNLOCK(pRoot)
     79 *  Counteracts KRB_WRITE_LOCK.
     80 *
     81 *  \#define KRB_READ_LOCK(pRoot)
    8482 *  Lock the tree for reading.
    8583 *
    86  *  \#define KAVL_READ_UNLOCK(pRoot)
    87  *  Counteracts KAVL_READ_LOCK.
    88  *
    89  *  \#define KAVLKEY
     84 *  \#define KRB_READ_UNLOCK(pRoot)
     85 *  Counteracts KRB_READ_LOCK.
     86 *
     87 *  \#define KRBKEY
    9088 *  Define this to the name of the AVL key type.
    9189 *
    92  *  \#define KAVL_STD_KEY_COMP
     90 *  \#define KRB_STD_KEY_COMP
    9391 *  Define this to use the standard key compare macros. If not set all the
    94  *  compare operations for KAVLKEY have to be defined: KAVL_G, KAVL_E, KAVL_NE,
    95  *  KAVL_R_IS_IDENTICAL, KAVL_R_IS_INTERSECTING and KAVL_R_IS_IN_RANGE. The
    96  *  latter three are only required when KAVL_RANGE is defined.
    97  *
    98  *  \#define KAVLNODE
     92 *  compare operations for KRBKEY have to be defined: KRB_CMP_G, KRB_CMP_E, KRB_CMP_NE,
     93 *  KRB_R_IS_IDENTICAL, KRB_R_IS_INTERSECTING and KRB_R_IS_IN_RANGE. The
     94 *  latter three are only required when KRB_RANGE is defined.
     95 *
     96 *  \#define KRBNODE
    9997 *  Define this to the name (typedef) of the AVL node structure. This
    10098 *  structure must have a mpLeft, mpRight, mKey and mHeight member.
    101  *  If KAVL_RANGE is defined a mKeyLast is also required.
    102  *  If KAVL_EQUAL_ALLOWED is defined a mpList member is required.
     99 *  If KRB_RANGE is defined a mKeyLast is also required.
     100 *  If KRB_EQUAL_ALLOWED is defined a mpList member is required.
    103101 *  It's possible to use other member names by redefining the names.
    104102 *
    105  *  \#define KAVLTREEPTR
     103 *  \#define KRBTREEPTR
    106104 *  Define this to the name (typedef) of the tree pointer type. This is
    107  *  required when KAVL_OFFSET is defined. When not defined it defaults
    108  *  to KAVLNODE *.
    109  *
    110  *  \#define KAVLROOT
     105 *  required when KRB_OFFSET is defined. When not defined it defaults
     106 *  to KRBNODE *.
     107 *
     108 *  \#define KRBROOT
    111109 *  Define this to the name (typedef) of the AVL root structure. This
    112110 *  is optional. However, if specified it must at least have a mpRoot
    113  *  member of KAVLTREEPTR type. If KAVL_LOOKTHRU is non-zero a
    114  *  maLookthru[KAVL_LOOKTHRU] member of the KAVLTREEPTR type is also
     111 *  member of KRBTREEPTR type. If KRB_CACHE_SIZE is non-zero a
     112 *  maLookthru[KRB_CACHE_SIZE] member of the KRBTREEPTR type is also
    115113 *  required.
    116114 *
    117  *  \#define KAVL_FN
     115 *  \#define KRB_FN
    118116 *  Use this to alter the names of the AVL functions.
    119117 *  Must be defined.
    120118 *
    121  *  \#define KAVL_TYPE(prefix, name)
     119 *  \#define KRB_TYPE(prefix, name)
    122120 *  Use this to make external type names and unique. The prefix may be empty.
    123121 *  Must be defined.
    124122 *
    125  *  \#define KAVL_INT(name)
     123 *  \#define KRB_INT(name)
    126124 *  Use this to make internal type names and unique. The prefix may be empty.
    127125 *  Must be defined.
    128126 *
    129  *  \#define KAVL_DECL(rettype)
     127 *  \#define KRB_DECL(rettype)
    130128 *  Function declaration macro that should be set according to the scope
    131129 *  the instantiated template should have. For instance an inlined scope
     
    151149#define KAVL_HEIGHTOF(pNode) ((KU8)((pNode) != NULL ? (pNode)->mHeight : 0))
    152150
    153 /** @def KAVL_GET_POINTER
     151/** @def KRB_GET_POINTER
    154152 * Reads a 'pointer' value.
    155153 *
     
    159157 */
    160158
    161 /** @def KAVL_GET_POINTER_NULL
    162  * Reads a 'pointer' value which can be KAVL_NULL.
     159/** @def KRB_GET_POINTER_NULL
     160 * Reads a 'pointer' value which can be KRB_NULL.
    163161 *
    164162 * @returns The native pointer.
    165  * @returns NULL pointer if KAVL_NULL.
     163 * @returns NULL pointer if KRB_NULL.
    166164 * @param   pp      Pointer to the pointer to read.
    167165 * @internal
    168166 */
    169167
    170 /** @def KAVL_SET_POINTER
     168/** @def KRB_SET_POINTER
    171169 * Writes a 'pointer' value.
    172170 * For offset-based schemes offset relative to pp is calculated and assigned to *pp.
     
    178176 */
    179177
    180 /** @def KAVL_SET_POINTER_NULL
    181  * Writes a 'pointer' value which can be KAVL_NULL.
     178/** @def KRB_SET_POINTER_NULL
     179 * Writes a 'pointer' value which can be KRB_NULL.
    182180 *
    183181 * For offset-based schemes offset relative to pp is calculated and assigned to *pp,
    184  * if p is not KAVL_NULL of course.
     182 * if p is not KRB_NULL of course.
    185183 *
    186184 * @returns stored pointer.
    187185 * @param   pp      Pointer to where to store the pointer.
    188  * @param   pp2     Pointer to where to pointer to assign to pp. This can be KAVL_NULL
     186 * @param   pp2     Pointer to where to pointer to assign to pp. This can be KRB_NULL
    189187 * @internal
    190188 */
    191189
    192 #ifndef KAVLTREEPTR
    193 # define KAVLTREEPTR                        KAVLNODE *
    194 #endif
    195 
    196 #ifndef KAVLROOT
    197 # define KAVLROOT                           KAVL_TYPE(,ROOT)
    198 # define KAVL_NEED_KAVLROOT
    199 #endif
    200 
    201 #ifdef KAVL_LOOKTHRU
    202 # ifndef KAVL_LOOKTHRU_HASH
    203 #  define KAVL_LOOKTHRU_HASH(Key)           ( (Key) % (KAVL_LOOKTHRU) )
     190#ifndef KRBTREEPTR
     191# define KRBTREEPTR                         KRBNODE *
     192#endif
     193
     194#ifndef KRBROOT
     195# define KRBROOT                            KRB_TYPE(,ROOT)
     196# define KRB_NEED_KRBROOT
     197#endif
     198
     199#ifdef KRB_CACHE_SIZE
     200# ifndef KRB_CACHE_HASH
     201#  define KRB_CACHE_HASH(Key)               ( (Key) % (KRB_CACHE_SIZE) )
    204202# endif
    205 #elif defined(KAVL_LOOKTHRU_HASH)
    206 # error "KAVL_LOOKTHRU_HASH without KAVL_LOOKTHRU!"
    207 #endif
    208 
    209 #ifdef KAVL_LOOKTHRU
    210 # define KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, Key) \
     203#elif defined(KRB_CACHE_HASH)
     204# error "KRB_CACHE_HASH without KRB_CACHE_SIZE!"
     205#endif
     206
     207#ifdef KRB_CACHE_SIZE
     208# define KRB_CACHE_INVALIDATE_NODE(pRoot, pNode, Key) \
    211209    do { \
    212         KAVLTREEPTR **ppEntry = &pRoot->maLookthru[KAVL_LOOKTHRU_HASH(Key)]; \
    213         if ((pNode) == KAVL_GET_POINTER_NULL(ppEntry)) \
    214             *ppEntry = KAVL_NULL; \
     210        KRBTREEPTR **ppEntry = &pRoot->maLookthru[KRB_CACHE_HASH(Key)]; \
     211        if ((pNode) == KRB_GET_POINTER_NULL(ppEntry)) \
     212            *ppEntry = KRB_NULL; \
    215213    } while (0)
    216214#else
    217 # define KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, Key) do { } while (0)
    218 #endif
    219 
    220 #ifndef KAVL_LOCKED
    221 # define KAVL_WRITE_LOCK(pRoot)             do { } while (0)
    222 # define KAVL_WRITE_UNLOCK(pRoot)           do { } while (0)
    223 # define KAVL_READ_LOCK(pRoot)              do { } while (0)
    224 # define KAVL_READ_UNLOCK(pRoot)            do { } while (0)
    225 #endif
    226 
    227 #ifdef KAVL_OFFSET
    228 # define KAVL_GET_POINTER(pp)               ( (KAVLNODE *)((KIPTR)(pp) + *(pp)) )
    229 # define KAVL_GET_POINTER_NULL(pp)          ( *(pp) != KAVL_NULL ? KAVL_GET_POINTER(pp) : NULL )
    230 # define KAVL_SET_POINTER(pp, p)            ( (*(pp)) = ((KIPTR)(p) - (KIPTR)(pp)) )
    231 # define KAVL_SET_POINTER_NULL(pp, pp2)     ( (*(pp)) = *(pp2) != KAVL_NULL ? (KIPTR)KAVL_GET_POINTER(pp2) - (KIPTR)(pp) : KAVL_NULL )
     215# define KRB_CACHE_INVALIDATE_NODE(pRoot, pNode, Key) do { } while (0)
     216#endif
     217
     218#ifndef KRB_LOCKED
     219# define KRB_WRITE_LOCK(pRoot)              do { } while (0)
     220# define KRB_WRITE_UNLOCK(pRoot)            do { } while (0)
     221# define KRB_READ_LOCK(pRoot)               do { } while (0)
     222# define KRB_READ_UNLOCK(pRoot)             do { } while (0)
     223#endif
     224
     225#ifdef KRB_OFFSET
     226# define KRB_GET_POINTER(pp)                ( (KRBNODE *)((KIPTR)(pp) + *(pp)) )
     227# define KRB_GET_POINTER_NULL(pp)           ( *(pp) != KRB_NULL ? KRB_GET_POINTER(pp) : NULL )
     228# define KRB_SET_POINTER(pp, p)             ( (*(pp)) = ((KIPTR)(p) - (KIPTR)(pp)) )
     229# define KRB_SET_POINTER_NULL(pp, pp2)      ( (*(pp)) = *(pp2) != KRB_NULL ? (KIPTR)KRB_GET_POINTER(pp2) - (KIPTR)(pp) : KRB_NULL )
    232230#else
    233 # define KAVL_GET_POINTER(pp)               ( *(pp) )
    234 # define KAVL_GET_POINTER_NULL(pp)          ( *(pp) )
    235 # define KAVL_SET_POINTER(pp, p)            ( (*(pp)) = (p) )
    236 # define KAVL_SET_POINTER_NULL(pp, pp2)     ( (*(pp)) = *(pp2) )
    237 #endif
    238 
    239 
    240 /** @def KAVL_NULL
     231# define KRB_GET_POINTER(pp)                ( *(pp) )
     232# define KRB_GET_POINTER_NULL(pp)           ( *(pp) )
     233# define KRB_SET_POINTER(pp, p)             ( (*(pp)) = (p) )
     234# define KRB_SET_POINTER_NULL(pp, pp2)      ( (*(pp)) = *(pp2) )
     235#endif
     236
     237
     238/** @def KRB_NULL
    241239 * The NULL 'pointer' equivalent.
    242240 */
    243 #ifdef KAVL_OFFSET
    244 # define KAVL_NULL     0
     241#ifdef KRB_OFFSET
     242# define KRB_NULL     0
    245243#else
    246 # define KAVL_NULL     NULL
    247 #endif
    248 
    249 #ifdef KAVL_STD_KEY_COMP
    250 # define KAVL_G(key1, key2)                 ( (key1) >  (key2) )
    251 # define KAVL_E(key1, key2)                 ( (key1) == (key2) )
    252 # define KAVL_NE(key1, key2)                ( (key1) != (key2) )
    253 # ifdef KAVL_RANGE
    254 #  define KAVL_R_IS_IDENTICAL(key1B, key2B, key1E, key2E)       ( (key1B) == (key2B) && (key1E) == (key2E) )
    255 #  define KAVL_R_IS_INTERSECTING(key1B, key2B, key1E, key2E)    ( (key1B) <= (key2E) && (key1E) >= (key2B) )
    256 #  define KAVL_R_IS_IN_RANGE(key1B, key1E, key2)                KAVL_R_IS_INTERSECTING(key1B, key2, key1E, key2)
     244# define KRB_NULL     NULL
     245#endif
     246
     247#ifdef KRB_STD_KEY_COMP
     248# define KRB_CMP_G(key1, key2)              ( (key1) >  (key2) )
     249# define KRB_CMP_E(key1, key2)              ( (key1) == (key2) )
     250# define KRB_CMP_NE(key1, key2)             ( (key1) != (key2) )
     251# ifdef KRB_RANGE
     252#  define KRB_R_IS_IDENTICAL(key1B, key2B, key1E, key2E)       ( (key1B) == (key2B) && (key1E) == (key2E) )
     253#  define KRB_R_IS_INTERSECTING(key1B, key2B, key1E, key2E)    ( (key1B) <= (key2E) && (key1E) >= (key2B) )
     254#  define KRB_R_IS_IN_RANGE(key1B, key1E, key2)                KRB_R_IS_INTERSECTING(key1B, key2, key1E, key2)
    257255# endif
    258256#endif
    259257
    260 #ifndef KAVL_RANGE
    261 # define KAVL_R_IS_INTERSECTING(key1B, key2B, key1E, key2E)     KAVL_E(key1B, key2B)
    262 # define KAVL_R_IS_IDENTICAL(key1B, key2B, key1E, key2E)        KAVL_E(key1B, key2B)
     258#ifndef KRB_RANGE
     259# define KRB_R_IS_INTERSECTING(key1B, key2B, key1E, key2E)     KRB_CMP_E(key1B, key2B)
     260# define KRB_R_IS_IDENTICAL(key1B, key2B, key1E, key2E)        KRB_CMP_E(key1B, key2B)
    263261#endif
    264262
     
    274272{
    275273    unsigned        cEntries;
    276     KAVLTREEPTR    *aEntries[KAVL_MAX_STACK];
    277 } KAVL_INT(STACK);
     274    KRBTREEPTR    *aEntries[KRB_MAX_STACK];
     275} KRB_INT(STACK);
    278276
    279277/**
    280278 * The callback used by the Destroy and DoWithAll functions.
    281279 */
    282 typedef int (* KAVL_TYPE(PFN,CALLBACK))(KAVLNODE *, void *);
    283 
    284 #ifdef KAVL_NEED_KAVLROOT
     280typedef int (* KRB_TYPE(PFN,CALLBACK))(KRBNODE *, void *);
     281
     282#ifdef KRB_NEED_KRBROOT
    285283/**
    286  * The AVL root structure.
     284 * The Red-Black tree root structure.
    287285 */
    288286typedef struct
    289287{
    290     KAVLTREEPTR     mpRoot;
    291 # ifdef KAVL_LOOKTHRU
    292     KAVLTREEPTR     maLookthru[KAVL_LOOKTHRU];
     288    KRBTREEPTR     mpRoot;
     289# ifdef KRB_CACHE_SIZE
     290    KRBTREEPTR     maLookthru[KRB_CACHE_SIZE];
    293291# endif
    294 } KAVLROOT;
    295 #endif
     292} KRBROOT;
     293#endif
     294
    296295
    297296
    298297/**
    299  * Rewinds a stack of node pointer pointers, rebalancing the tree.
    300  *
    301  * @param     pStack  Pointer to stack to rewind.
    302  * @sketch    LOOP thru all stack entries
    303  *            BEGIN
    304  *                Get pointer to pointer to node (and pointer to node) from the stack.
    305  *                IF 2 higher left subtree than in right subtree THEN
    306  *                BEGIN
    307  *                    IF higher (or equal) left-sub-subtree than right-sub-subtree THEN
    308  *                                *                       n+2|n+3
    309  *                              /   \                     /     \
    310  *                            n+2    n       ==>         n+1   n+1|n+2
    311  *                           /   \                             /     \
    312  *                         n+1 n|n+1                          n|n+1  n
    313  *
    314  *                         Or with keys:
    315  *
    316  *                               4                           2
    317  *                             /   \                       /   \
    318  *                            2     5        ==>          1     4
    319  *                           / \                               / \
    320  *                          1   3                             3   5
    321  *
    322  *                    ELSE
    323  *                                *                         n+2
    324  *                              /   \                      /   \
    325  *                            n+2    n                   n+1   n+1
    326  *                           /   \           ==>        /  \   /  \
    327  *                          n    n+1                    n  L   R   n
    328  *                               / \
    329  *                              L   R
    330  *
    331  *                         Or with keys:
    332  *                               6                           4
    333  *                             /   \                       /   \
    334  *                            2     7        ==>          2     6
    335  *                          /   \                       /  \  /  \
    336  *                          1    4                      1  3  5  7
    337  *                              / \
    338  *                             3   5
    339  *                END
    340  *                ELSE IF 2 higher in right subtree than in left subtree THEN
    341  *                BEGIN
    342  *                    Same as above but left <==> right. (invert the picture)
    343  *                ELSE
    344  *                    IF correct height THEN break
    345  *                    ELSE correct height.
    346  *            END
    347  */
    348 K_DECL_INLINE(void) KAVL_FN(Rebalance)(KAVL_INT(STACK) *pStack)
     298 * Initializes the root of the Red-Black tree.
     299 *
     300 * @param     pTree     Pointer to the root structure.
     301 */
     302KRB_DECL(void) KRB_FN(Init)(KRBROOT *pRoot)
    349303{
    350     while (pStack->cEntries > 0)
    351     {
    352         KAVLTREEPTR     *ppNode = pStack->aEntries[--pStack->cEntries];
    353         KAVLNODE        *pNode = KAVL_GET_POINTER(ppNode);
    354         KAVLNODE        *pLeftNode = KAVL_GET_POINTER_NULL(&pNode->mpLeft);
    355         KU8              uLeftHeight = KAVL_HEIGHTOF(pLeftNode);
    356         KAVLNODE        *pRightNode = KAVL_GET_POINTER_NULL(&pNode->mpRight);
    357         KU8              uRightHeight = KAVL_HEIGHTOF(pRightNode);
    358 
    359         if (uRightHeight + 1 < uLeftHeight)
    360         {
    361             KAVLNODE    *pLeftLeftNode = KAVL_GET_POINTER_NULL(&pLeftNode->mpLeft);
    362             KAVLNODE    *pLeftRightNode = KAVL_GET_POINTER_NULL(&pLeftNode->mpRight);
    363             KU8          uLeftRightHeight = KAVL_HEIGHTOF(pLeftRightNode);
    364 
    365             if (KAVL_HEIGHTOF(pLeftLeftNode) >= uLeftRightHeight)
    366             {
    367                 KAVL_SET_POINTER_NULL(&pNode->mpLeft, &pLeftNode->mpRight);
    368                 KAVL_SET_POINTER(&pLeftNode->mpRight, pNode);
    369                 pLeftNode->mHeight = (KU8)(1 + (pNode->mHeight = (KU8)(1 + uLeftRightHeight)));
    370                 KAVL_SET_POINTER(ppNode, pLeftNode);
    371             }
    372             else
    373             {
    374                 KAVL_SET_POINTER_NULL(&pLeftNode->mpRight, &pLeftRightNode->mpLeft);
    375                 KAVL_SET_POINTER_NULL(&pNode->mpLeft, &pLeftRightNode->mpRight);
    376                 KAVL_SET_POINTER(&pLeftRightNode->mpLeft, pLeftNode);
    377                 KAVL_SET_POINTER(&pLeftRightNode->mpRight, pNode);
    378                 pLeftNode->mHeight = pNode->mHeight = uLeftRightHeight;
    379                 pLeftRightNode->mHeight = uLeftHeight;
    380                 KAVL_SET_POINTER(ppNode, pLeftRightNode);
    381             }
    382         }
    383         else if (uLeftHeight + 1 < uRightHeight)
    384         {
    385             KAVLNODE    *pRightLeftNode = KAVL_GET_POINTER_NULL(&pRightNode->mpLeft);
    386             KU8          uRightLeftHeight = KAVL_HEIGHTOF(pRightLeftNode);
    387             KAVLNODE    *pRightRightNode = KAVL_GET_POINTER_NULL(&pRightNode->mpRight);
    388 
    389             if (KAVL_HEIGHTOF(pRightRightNode) >= uRightLeftHeight)
    390             {
    391                 KAVL_SET_POINTER_NULL(&pNode->mpRight, &pRightNode->mpLeft);
    392                 KAVL_SET_POINTER(&pRightNode->mpLeft, pNode);
    393                 pRightNode->mHeight = (KU8)(1 + (pNode->mHeight = (KU8)(1 + uRightLeftHeight)));
    394                 KAVL_SET_POINTER(ppNode, pRightNode);
    395             }
    396             else
    397             {
    398                 KAVL_SET_POINTER_NULL(&pRightNode->mpLeft, &pRightLeftNode->mpRight);
    399                 KAVL_SET_POINTER_NULL(&pNode->mpRight, &pRightLeftNode->mpLeft);
    400                 KAVL_SET_POINTER(&pRightLeftNode->mpRight, pRightNode);
    401                 KAVL_SET_POINTER(&pRightLeftNode->mpLeft, pNode);
    402                 pRightNode->mHeight = pNode->mHeight = uRightLeftHeight;
    403                 pRightLeftNode->mHeight = uRightHeight;
    404                 KAVL_SET_POINTER(ppNode, pRightLeftNode);
    405             }
    406         }
    407         else
    408         {
    409             KU8 uHeight = (KU8)(K_MAX(uLeftHeight, uRightHeight) + 1);
    410             if (uHeight == pNode->mHeight)
    411                 break;
    412             pNode->mHeight = uHeight;
    413         }
    414     }
    415 
     304#ifdef KRB_CACHE_SIZE
     305    unsigned i;
     306#endif
     307
     308    pRoot->mpRoot = KRB_NULL;
     309#ifdef KRB_CACHE_SIZE
     310    for (i = 0; i < (KRB_CACHE_SIZE); i++)
     311        pRoot->maLookthru[i] = KRB_NULL;
     312#endif
    416313}
    417314
    418315
    419316/**
    420  * Initializes the root of the AVL-tree.
    421  *
    422  * @param     pTree     Pointer to the root structure.
    423  */
    424 KAVL_DECL(void) KAVL_FN(Init)(KAVLROOT *pRoot)
    425 {
    426 #ifdef KAVL_LOOKTHRU
    427     unsigned i;
    428 #endif
    429 
    430     pRoot->mpRoot = KAVL_NULL;
    431 #ifdef KAVL_LOOKTHRU
    432     for (i = 0; i < (KAVL_LOOKTHRU); i++)
    433         pRoot->maLookthru[i] = KAVL_NULL;
    434 #endif
    435 }
    436 
    437 
    438 /**
    439  * Inserts a node into the AVL-tree.
     317 * Inserts a node into the Red-Black tree.
    440318 * @returns   K_TRUE if inserted.
    441319 *            K_FALSE if node exists in tree.
    442  * @param     pRoot     Pointer to the AVL-tree root structure.
     320 * @param     pRoot     Pointer to the Red-Back tree's root structure.
    443321 * @param     pNode     Pointer to the node which is to be added.
    444322 * @sketch    Find the location of the node (using binary tree algorithm.):
     
    454332 *            Rebalance the tree.
    455333 */
    456 KAVL_DECL(KBOOL) KAVL_FN(Insert)(KAVLROOT *pRoot, KAVLNODE *pNode)
     334KRB_DECL(KBOOL) KRB_FN(Insert)(KRBROOT *pRoot, KRBNODE *pNode)
    457335{
    458     KAVL_INT(STACK)     AVLStack;
    459     KAVLTREEPTR        *ppCurNode = &pRoot->mpRoot;
    460     register KAVLKEY    Key = pNode->mKey;
    461 #ifdef KAVL_RANGE
    462     register KAVLKEY    KeyLast = pNode->mKeyLast;
    463 #endif
    464 
    465 #ifdef KAVL_RANGE
     336    KRB_INT(STACK)     Stack;
     337    KRBTREEPTR        *ppCurNode = &pRoot->mpRoot;
     338    register KRBKEY    Key = pNode->mKey;
     339#ifdef KRB_RANGE
     340    register KRBKEY    KeyLast = pNode->mKeyLast;
     341#endif
     342
     343#ifdef KRB_RANGE
    466344    if (Key > KeyLast)
    467345        return false;
    468346#endif
    469347
    470     KAVL_WRITE_LOCK(pRoot);
    471 
    472     AVLStack.cEntries = 0;
    473     while (*ppCurNode != KAVL_NULL)
     348    KRB_WRITE_LOCK(pRoot);
     349
     350    Stack.cEntries = 0;
     351    while (*ppCurNode != KRB_NULL)
    474352    {
    475         register KAVLNODE *pCurNode = KAVL_GET_POINTER(ppCurNode);
    476 
    477         kHlpAssert(AVLStack.cEntries < KAVL_MAX_STACK);
    478         AVLStack.aEntries[AVLStack.cEntries++] = ppCurNode;
    479 #ifdef KAVL_EQUAL_ALLOWED
    480         if (KAVL_R_IS_IDENTICAL(pCurNode->mKey, Key, pCurNode->mKeyLast, KeyLast))
     353        register KRBNODE *pCurNode = KRB_GET_POINTER(ppCurNode);
     354
     355        kHlpAssert(Stack.cEntries < KRB_MAX_STACK);
     356        Stack.aEntries[Stack.cEntries++] = ppCurNode;
     357#ifdef KRB_EQUAL_ALLOWED
     358        if (KRB_R_IS_IDENTICAL(pCurNode->mKey, Key, pCurNode->mKeyLast, KeyLast))
    481359        {
    482360            /*
    483361             * If equal then we'll use a list of equal nodes.
    484362             */
    485             pNode->mpLeft = pNode->mpRight = KAVL_NULL;
     363            pNode->mpLeft = pNode->mpRight = KRB_NULL;
    486364            pNode->mHeight = 0;
    487             KAVL_SET_POINTER_NULL(&pNode->mpList, &pCurNode->mpList);
    488             KAVL_SET_POINTER(&pCurNode->mpList, pNode);
    489             KAVL_WRITE_UNLOCK(pRoot);
     365            KRB_SET_POINTER_NULL(&pNode->mpList, &pCurNode->mpList);
     366            KRB_SET_POINTER(&pCurNode->mpList, pNode);
     367            KRB_WRITE_UNLOCK(pRoot);
    490368            return K_TRUE;
    491369        }
    492370#endif
    493 #ifdef KAVL_CHECK_FOR_EQUAL_INSERT
    494         if (KAVL_R_IS_INTERSECTING(pCurNode->mKey, Key, pCurNode->mKeyLast, KeyLast))
     371#ifdef KRB_CHECK_FOR_EQUAL_INSERT
     372        if (KRB_R_IS_INTERSECTING(pCurNode->mKey, Key, pCurNode->mKeyLast, KeyLast))
    495373        {
    496             KAVL_WRITE_UNLOCK(pRoot);
     374            KRB_WRITE_UNLOCK(pRoot);
    497375            return K_FALSE;
    498376        }
    499377#endif
    500         if (KAVL_G(pCurNode->mKey, Key))
     378        if (KRB_CMP_G(pCurNode->mKey, Key))
    501379            ppCurNode = &pCurNode->mpLeft;
    502380        else
     
    504382    }
    505383
    506     pNode->mpLeft = pNode->mpRight = KAVL_NULL;
    507 #ifdef KAVL_EQUAL_ALLOWED
    508     pNode->mpList = KAVL_NULL;
     384    pNode->mpLeft = pNode->mpRight = KRB_NULL;
     385#ifdef KRB_EQUAL_ALLOWED
     386    pNode->mpList = KRB_NULL;
    509387#endif
    510388    pNode->mHeight = 1;
    511     KAVL_SET_POINTER(ppCurNode, pNode);
    512 
    513     KAVL_FN(Rebalance)(&AVLStack);
    514 
    515     KAVL_WRITE_UNLOCK(pRoot);
     389    KRB_SET_POINTER(ppCurNode, pNode);
     390
     391    KRB_FN(Rebalance)(&Stack);
     392
     393    KRB_WRITE_UNLOCK(pRoot);
    516394    return K_TRUE;
    517395}
     
    519397
    520398/**
    521  * Removes a node from the AVL-tree.
     399 * Removes a node from the Red-Black tree.
    522400 * @returns   Pointer to the node.
    523  * @param     pRoot     Pointer to the AVL-tree root structure.
     401 * @param     pRoot     Pointer to the Red-Back tree's root structure.
    524402 * @param     Key       Key value of the node which is to be removed.
    525403 * @sketch    Find the node which is to be removed:
     
    557435 *            return pointer to the removed node (if found).
    558436 */
    559 KAVL_DECL(KAVLNODE *) KAVL_FN(Remove)(KAVLROOT *pRoot, KAVLKEY Key)
     437KRB_DECL(KRBNODE *) KRB_FN(Remove)(KRBROOT *pRoot, KRBKEY Key)
    560438{
    561     KAVL_INT(STACK)     AVLStack;
    562     KAVLTREEPTR        *ppDeleteNode = &pRoot->mpRoot;
    563     register KAVLNODE  *pDeleteNode;
    564 
    565     KAVL_WRITE_LOCK(pRoot);
    566 
    567     AVLStack.cEntries = 0;
     439    KRB_INT(STACK)     Stack;
     440    KRBTREEPTR        *ppDeleteNode = &pRoot->mpRoot;
     441    register KRBNODE  *pDeleteNode;
     442
     443    KRB_WRITE_LOCK(pRoot);
     444
     445    Stack.cEntries = 0;
    568446    for (;;)
    569447    {
    570         if (*ppDeleteNode == KAVL_NULL)
     448        if (*ppDeleteNode == KRB_NULL)
    571449        {
    572             KAVL_WRITE_UNLOCK(pRoot);
     450            KRB_WRITE_UNLOCK(pRoot);
    573451            return NULL;
    574452        }
    575         pDeleteNode = KAVL_GET_POINTER(ppDeleteNode);
    576 
    577         kHlpAssert(AVLStack.cEntries < KAVL_MAX_STACK);
    578         AVLStack.aEntries[AVLStack.cEntries++] = ppDeleteNode;
    579         if (KAVL_E(pDeleteNode->mKey, Key))
     453        pDeleteNode = KRB_GET_POINTER(ppDeleteNode);
     454
     455        kHlpAssert(Stack.cEntries < KRB_MAX_STACK);
     456        Stack.aEntries[Stack.cEntries++] = ppDeleteNode;
     457        if (KRB_CMP_E(pDeleteNode->mKey, Key))
    580458            break;
    581459
    582         if (KAVL_G(pDeleteNode->mKey, Key))
     460        if (KRB_CMP_G(pDeleteNode->mKey, Key))
    583461            ppDeleteNode = &pDeleteNode->mpLeft;
    584462        else
     
    586464    }
    587465
    588     if (pDeleteNode->mpLeft != KAVL_NULL)
     466    if (pDeleteNode->mpLeft != KRB_NULL)
    589467    {
    590468        /* find the rightmost node in the left tree. */
    591         const unsigned      iStackEntry = AVLStack.cEntries;
    592         KAVLTREEPTR        *ppLeftLeast = &pDeleteNode->mpLeft;
    593         register KAVLNODE  *pLeftLeast = KAVL_GET_POINTER(ppLeftLeast);
    594 
    595         while (pLeftLeast->mpRight != KAVL_NULL)
     469        const unsigned      iStackEntry = Stack.cEntries;
     470        KRBTREEPTR        *ppLeftLeast = &pDeleteNode->mpLeft;
     471        register KRBNODE  *pLeftLeast = KRB_GET_POINTER(ppLeftLeast);
     472
     473        while (pLeftLeast->mpRight != KRB_NULL)
    596474        {
    597             kHlpAssert(AVLStack.cEntries < KAVL_MAX_STACK);
    598             AVLStack.aEntries[AVLStack.cEntries++] = ppLeftLeast;
     475            kHlpAssert(Stack.cEntries < KRB_MAX_STACK);
     476            Stack.aEntries[Stack.cEntries++] = ppLeftLeast;
    599477            ppLeftLeast = &pLeftLeast->mpRight;
    600             pLeftLeast  = KAVL_GET_POINTER(ppLeftLeast);
     478            pLeftLeast  = KRB_GET_POINTER(ppLeftLeast);
    601479        }
    602480
    603481        /* link out pLeftLeast */
    604         KAVL_SET_POINTER_NULL(ppLeftLeast, &pLeftLeast->mpLeft);
     482        KRB_SET_POINTER_NULL(ppLeftLeast, &pLeftLeast->mpLeft);
    605483
    606484        /* link it in place of the delete node. */
    607         KAVL_SET_POINTER_NULL(&pLeftLeast->mpLeft, &pDeleteNode->mpLeft);
    608         KAVL_SET_POINTER_NULL(&pLeftLeast->mpRight, &pDeleteNode->mpRight);
     485        KRB_SET_POINTER_NULL(&pLeftLeast->mpLeft, &pDeleteNode->mpLeft);
     486        KRB_SET_POINTER_NULL(&pLeftLeast->mpRight, &pDeleteNode->mpRight);
    609487        pLeftLeast->mHeight = pDeleteNode->mHeight;
    610         KAVL_SET_POINTER(ppDeleteNode, pLeftLeast);
    611         AVLStack.aEntries[iStackEntry] = &pLeftLeast->mpLeft;
     488        KRB_SET_POINTER(ppDeleteNode, pLeftLeast);
     489        Stack.aEntries[iStackEntry] = &pLeftLeast->mpLeft;
    612490    }
    613491    else
    614492    {
    615         KAVL_SET_POINTER_NULL(ppDeleteNode, &pDeleteNode->mpRight);
    616         AVLStack.cEntries--;
     493        KRB_SET_POINTER_NULL(ppDeleteNode, &pDeleteNode->mpRight);
     494        Stack.cEntries--;
    617495    }
    618496
    619     KAVL_FN(Rebalance)(&AVLStack);
    620 
    621     KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pDeleteNode, Key);
    622 
    623     KAVL_WRITE_UNLOCK(pRoot);
     497    KRB_FN(Rebalance)(&Stack);
     498
     499    KRB_CACHE_INVALIDATE_NODE(pRoot, pDeleteNode, Key);
     500
     501    KRB_WRITE_UNLOCK(pRoot);
    624502    return pDeleteNode;
    625503}
  • trunk/include/k/kRbTmpl/kRbDestroy.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvlTmpl - Templated AVL Trees, Destroy the tree.
     3 * kRbTmpl - Templated Red-Black Trees, Destroy the tree.
    44 */
    55
     
    3737 *          made on it. Note that the node we fail on will be considered dead and
    3838 *          no action is taken to link it back into the tree.
    39  * @param   pRoot           Pointer to the AVL-tree root structure.
     39 * @param   pRoot           Pointer to the Red-Back tree's root structure.
    4040 * @param   pfnCallBack     Pointer to callback function.
    4141 * @param   pvUser          User parameter passed on to the callback function.
    4242 */
    43 KAVL_DECL(int) KAVL_FN(Destroy)(KAVLROOT *pRoot, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
     43KRB_DECL(int) KRB_FN(Destroy)(KRBROOT *pRoot, KRB_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
    4444{
    45 #ifdef KAVL_LOOKTHRU
     45#ifdef KRB_CACHE_SIZE
    4646    unsigned    i;
    4747#endif
    4848    unsigned    cEntries;
    49     KAVLNODE   *apEntries[KAVL_MAX_STACK];
     49    KRBNODE    *apEntries[KRB_MAX_STACK];
    5050    int         rc;
    5151
    52     KAVL_WRITE_LOCK(pRoot);
    53     if (pRoot->mpRoot == KAVL_NULL)
     52    KRB_WRITE_LOCK(pRoot);
     53    if (pRoot->mpRoot == KRB_NULL)
    5454    {
    55         KAVL_WRITE_UNLOCK(pRoot);
     55        KRB_WRITE_UNLOCK(pRoot);
    5656        return 0;
    5757    }
    5858
    59 #ifdef KAVL_LOOKTHRU
     59#ifdef KRB_CACHE_SIZE
    6060    /*
    6161     * Kill the lookthru cache.
    6262     */
    63     for (i = 0; i < (KAVL_LOOKTHRU); i++)
    64         pRoot->maLookthru[i] = KAVL_NULL;
     63    for (i = 0; i < (KRB_CACHE_SIZE); i++)
     64        pRoot->maLookthru[i] = KRB_NULL;
    6565#endif
    6666
    6767    cEntries = 1;
    68     apEntries[0] = KAVL_GET_POINTER(&pRoot->mpRoot);
     68    apEntries[0] = KRB_GET_POINTER(&pRoot->mpRoot);
    6969    while (cEntries > 0)
    7070    {
     
    7272         * Process the subtrees first.
    7373         */
    74         KAVLNODE *pNode = apEntries[cEntries - 1];
    75         if (pNode->mpLeft != KAVL_NULL)
    76             apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
    77         else if (pNode->mpRight != KAVL_NULL)
    78             apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
     74        KRBNODE *pNode = apEntries[cEntries - 1];
     75        if (pNode->mpLeft != KRB_NULL)
     76            apEntries[cEntries++] = KRB_GET_POINTER(&pNode->mpLeft);
     77        else if (pNode->mpRight != KRB_NULL)
     78            apEntries[cEntries++] = KRB_GET_POINTER(&pNode->mpRight);
    7979        else
    8080        {
    81 #ifdef KAVL_EQUAL_ALLOWED
     81#ifdef KRB_EQUAL_ALLOWED
    8282            /*
    8383             * Process nodes with the same key.
    8484             */
    85             while (pNode->pList != KAVL_NULL)
     85            while (pNode->pList != KRB_NULL)
    8686            {
    87                 KAVLNODE *pEqual = KAVL_GET_POINTER(&pNode->pList);
    88                 KAVL_SET_POINTER(&pNode->pList, KAVL_GET_POINTER_NULL(&pEqual->pList));
    89                 pEqual->pList = KAVL_NULL;
     87                KRBNODE *pEqual = KRB_GET_POINTER(&pNode->pList);
     88                KRB_SET_POINTER(&pNode->pList, KRB_GET_POINTER_NULL(&pEqual->pList));
     89                pEqual->pList = KRB_NULL;
    9090
    9191                rc = pfnCallBack(pEqual, pvUser);
    9292                if (rc)
    9393                {
    94                     KAVL_WRITE_UNLOCK(pRoot);
     94                    KRB_WRITE_UNLOCK(pRoot);
    9595                    return rc;
    9696                }
     
    103103            if (--cEntries > 0)
    104104            {
    105                 KAVLNODE *pParent = apEntries[cEntries - 1];
    106                 if (KAVL_GET_POINTER(&pParent->mpLeft) == pNode)
    107                     pParent->mpLeft = KAVL_NULL;
     105                KRBNODE *pParent = apEntries[cEntries - 1];
     106                if (KRB_GET_POINTER(&pParent->mpLeft) == pNode)
     107                    pParent->mpLeft = KRB_NULL;
    108108                else
    109                     pParent->mpRight = KAVL_NULL;
     109                    pParent->mpRight = KRB_NULL;
    110110            }
    111111            else
    112                 pRoot->mpRoot = KAVL_NULL;
     112                pRoot->mpRoot = KRB_NULL;
    113113
    114             kHlpAssert(pNode->mpLeft == KAVL_NULL);
    115             kHlpAssert(pNode->mpRight == KAVL_NULL);
     114            kHlpAssert(pNode->mpLeft == KRB_NULL);
     115            kHlpAssert(pNode->mpRight == KRB_NULL);
    116116            rc = pfnCallBack(pNode, pvUser);
    117117            if (rc)
    118118            {
    119                 KAVL_WRITE_UNLOCK(pRoot);
     119                KRB_WRITE_UNLOCK(pRoot);
    120120                return rc;
    121121            }
    122122        }
    123123    } /* while */
    124     kHlpAssert(pRoot->mpRoot == KAVL_NULL);
     124    kHlpAssert(pRoot->mpRoot == KRB_NULL);
    125125
    126     KAVL_WRITE_UNLOCK(pRoot);
     126    KRB_WRITE_UNLOCK(pRoot);
    127127    return 0;
    128128}
  • trunk/include/k/kRbTmpl/kRbDoWithAll.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvlTmpl - Templated AVL Trees, The Callback Iterator.
     3 * kRbTmpl - Templated Red-Black Trees, The Callback Iterator.
    44 */
    55
     
    3838{
    3939    unsigned        cEntries;
    40     KAVLNODE       *aEntries[KAVL_MAX_STACK];
    41     char            achFlags[KAVL_MAX_STACK];
    42     KAVLROOT        pRoot;
    43 } KAVL_INT(STACK2);
     40    KRBNODE        *aEntries[KRB_MAX_STACK];
     41    char            achFlags[KRB_MAX_STACK];
     42    KRBROOT        pRoot;
     43} KRB_INT(STACK2);
    4444
    4545
     
    4848 *
    4949 * @returns   0 on success. Return from callback on failure.
    50  * @param     pRoot        Pointer to the AVL-tree root structure.
     50 * @param     pRoot        Pointer to the Red-Back tree's root structure.
    5151 * @param     fFromLeft    K_TRUE:  Left to right.
    5252 *                         K_FALSE: Right to left.
     
    5454 * @param     pvUser       User parameter passed on to the callback function.
    5555 */
    56 KAVL_DECL(int) KAVL_FN(DoWithAll)(KAVLROOT *pRoot, KBOOL fFromLeft, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
     56KRB_DECL(int) KRB_FN(DoWithAll)(KRBROOT *pRoot, KBOOL fFromLeft, KRB_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
    5757{
    58     KAVL_INT(STACK2)    AVLStack;
    59     KAVLNODE           *pNode;
    60 #ifdef KAVL_EQUAL_ALLOWED
    61     KAVLNODE           *pEqual;
     58    KRB_INT(STACK2)     Stack;
     59    KRBNODE            *pNode;
     60#ifdef KRB_EQUAL_ALLOWED
     61    KRBNODE            *pEqual;
    6262#endif
    6363    int                 rc;
    6464
    65     KAVL_READ_LOCK(pRoot);
    66     if (pRoot->mpRoot == KAVL_NULL)
     65    KRB_READ_LOCK(pRoot);
     66    if (pRoot->mpRoot == KRB_NULL)
    6767    {
    68         KAVL_READ_UNLOCK(pRoot);
     68        KRB_READ_UNLOCK(pRoot);
    6969        return 0;
    7070    }
    7171
    72     AVLStack.cEntries = 1;
    73     AVLStack.achFlags[0] = 0;
    74     AVLStack.aEntries[0] = KAVL_GET_POINTER(&pRoot->mpRoot);
     72    Stack.cEntries = 1;
     73    Stack.achFlags[0] = 0;
     74    Stack.aEntries[0] = KRB_GET_POINTER(&pRoot->mpRoot);
    7575
    7676    if (fFromLeft)
    7777    {   /* from left */
    78         while (AVLStack.cEntries > 0)
     78        while (Stack.cEntries > 0)
    7979        {
    80             pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
     80            pNode = Stack.aEntries[Stack.cEntries - 1];
    8181
    8282            /* left */
    83             if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
     83            if (!Stack.achFlags[Stack.cEntries - 1]++)
    8484            {
    85                 if (pNode->mpLeft != KAVL_NULL)
     85                if (pNode->mpLeft != KRB_NULL)
    8686                {
    87                     AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
    88                     AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
     87                    Stack.achFlags[Stack.cEntries] = 0; /* 0 first, 1 last */
     88                    Stack.aEntries[Stack.cEntries++] = KRB_GET_POINTER(&pNode->mpLeft);
    8989                    continue;
    9090                }
     
    9595            if (rc)
    9696                return rc;
    97 #ifdef KAVL_EQUAL_ALLOWED
    98             if (pNode->mpList != KAVL_NULL)
    99                 for (pEqual = KAVL_GET_POINTER(&pNode->mpList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->mpList))
     97#ifdef KRB_EQUAL_ALLOWED
     98            if (pNode->mpList != KRB_NULL)
     99                for (pEqual = KRB_GET_POINTER(&pNode->mpList); pEqual; pEqual = KRB_GET_POINTER_NULL(&pEqual->mpList))
    100100                {
    101101                    rc = pfnCallBack(pEqual, pvUser);
    102102                    if (rc)
    103103                    {
    104                         KAVL_READ_UNLOCK(pRoot);
     104                        KRB_READ_UNLOCK(pRoot);
    105105                        return rc;
    106106                    }
     
    109109
    110110            /* right */
    111             AVLStack.cEntries--;
    112             if (pNode->mpRight != KAVL_NULL)
     111            Stack.cEntries--;
     112            if (pNode->mpRight != KRB_NULL)
    113113            {
    114                 AVLStack.achFlags[AVLStack.cEntries] = 0;
    115                 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
     114                Stack.achFlags[Stack.cEntries] = 0;
     115                Stack.aEntries[Stack.cEntries++] = KRB_GET_POINTER(&pNode->mpRight);
    116116            }
    117117        } /* while */
     
    119119    else
    120120    {   /* from right */
    121         while (AVLStack.cEntries > 0)
     121        while (Stack.cEntries > 0)
    122122        {
    123             pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
     123            pNode = Stack.aEntries[Stack.cEntries - 1];
    124124
    125125            /* right */
    126             if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
     126            if (!Stack.achFlags[Stack.cEntries - 1]++)
    127127            {
    128                 if (pNode->mpRight != KAVL_NULL)
     128                if (pNode->mpRight != KRB_NULL)
    129129                {
    130                     AVLStack.achFlags[AVLStack.cEntries] = 0;  /* 0 first, 1 last */
    131                     AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
     130                    Stack.achFlags[Stack.cEntries] = 0;  /* 0 first, 1 last */
     131                    Stack.aEntries[Stack.cEntries++] = KRB_GET_POINTER(&pNode->mpRight);
    132132                    continue;
    133133                }
     
    138138            if (rc)
    139139                return rc;
    140 #ifdef KAVL_EQUAL_ALLOWED
    141             if (pNode->mpList != KAVL_NULL)
    142                 for (pEqual = KAVL_GET_POINTER(&pNode->mpList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->pList))
     140#ifdef KRB_EQUAL_ALLOWED
     141            if (pNode->mpList != KRB_NULL)
     142                for (pEqual = KRB_GET_POINTER(&pNode->mpList); pEqual; pEqual = KRB_GET_POINTER_NULL(&pEqual->pList))
    143143                {
    144144                    rc = pfnCallBack(pEqual, pvUser);
    145145                    if (rc)
    146146                    {
    147                         KAVL_READ_UNLOCK(pRoot);
     147                        KRB_READ_UNLOCK(pRoot);
    148148                        return rc;
    149149                    }
     
    152152
    153153            /* left */
    154             AVLStack.cEntries--;
    155             if (pNode->mpLeft != KAVL_NULL)
     154            Stack.cEntries--;
     155            if (pNode->mpLeft != KRB_NULL)
    156156            {
    157                 AVLStack.achFlags[AVLStack.cEntries] = 0;
    158                 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
     157                Stack.achFlags[Stack.cEntries] = 0;
     158                Stack.aEntries[Stack.cEntries++] = KRB_GET_POINTER(&pNode->mpLeft);
    159159            }
    160160        } /* while */
    161161    }
    162162
    163     KAVL_READ_UNLOCK(pRoot);
     163    KRB_READ_UNLOCK(pRoot);
    164164    return 0;
    165165}
  • trunk/include/k/kRbTmpl/kRbEnum.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvlTmpl - Templated AVL Trees, Node Enumeration.
     3 * kRbTmpl - Templated Red-Black Trees, Node Enumeration.
    44 */
    55
     
    3838 * to do next.
    3939 */
    40 typedef struct KAVL_TYPE(,ENUMDATA)
     40typedef struct KRB_TYPE(,ENUMDATA)
    4141{
    4242    KBOOL               fFromLeft;
    4343    KI8                 cEntries;
    44     KU8                 achFlags[KAVL_MAX_STACK];
    45     KAVLNODE *          aEntries[KAVL_MAX_STACK];
    46 } KAVL_TYPE(,ENUMDATA), *KAVL_TYPE(P,ENUMDATA);
     44    KU8                 achFlags[KRB_MAX_STACK];
     45    KRBNODE *           aEntries[KRB_MAX_STACK];
     46} KRB_TYPE(,ENUMDATA), *KRB_TYPE(P,ENUMDATA);
    4747
    4848
     
    5050 * Ends an enumeration.
    5151 *
    52  * The purpose of this function is to unlock the tree should the
    53  * AVL implementation include locking. It's good practice to call
    54  * it anyway even if the tree doesn't do any locking.
     52 * The purpose of this function is to unlock the tree should the Red-Black tree
     53 * implementation include locking.  It's good practice to call it anyway even if
     54 * the tree doesn't do any locking.
    5555 *
    5656 * @param   pEnumData   Pointer to enumeration control data.
    5757 */
    58 KAVL_DECL(void) KAVL_FN(EndEnum)(KAVL_TYPE(,ENUMDATA) *pEnumData)
     58KRB_DECL(void) KRB_FN(EndEnum)(KRB_TYPE(,ENUMDATA) *pEnumData)
    5959{
    60     KAVLROOT pRoot = pEnumData->pRoot;
     60    KRBROOT pRoot = pEnumData->pRoot;
    6161    pEnumData->pRoot = NULL;
    6262    if (pRoot)
    63         KAVL_READ_UNLOCK(pEnumData->pRoot);
     63        KRB_READ_UNLOCK(pEnumData->pRoot);
    6464}
    6565
     
    7676 * @param   pEnumData   Pointer to enumeration control data.
    7777 */
    78 KAVL_DECL(KAVLNODE *) KAVL_FN(GetNext)(KAVL_TYPE(,ENUMDATA) *pEnumData)
     78KRB_DECL(KRBNODE *) KRB_FN(GetNext)(KRB_TYPE(,ENUMDATA) *pEnumData)
    7979{
    8080    if (pEnumData->fFromLeft)
     
    8282        while (pEnumData->cEntries > 0)
    8383        {
    84             KAVLNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1];
     84            KRBNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1];
    8585
    8686            /* left */
     
    8888            {
    8989                pEnumData->achFlags[pEnumData->cEntries - 1]++;
    90                 if (pNode->mpLeft != KAVL_NULL)
     90                if (pNode->mpLeft != KRB_NULL)
    9191                {
    9292                    pEnumData->achFlags[pEnumData->cEntries] = 0; /* 0 left, 1 center, 2 right */
    93                     pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
     93                    pEnumData->aEntries[pEnumData->cEntries++] = KRB_GET_POINTER(&pNode->mpLeft);
    9494                    continue;
    9595                }
     
    105105            /* right */
    106106            pEnumData->cEntries--;
    107             if (pNode->mpRight != KAVL_NULL)
     107            if (pNode->mpRight != KRB_NULL)
    108108            {
    109109                pEnumData->achFlags[pEnumData->cEntries] = 0;
    110                 pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
     110                pEnumData->aEntries[pEnumData->cEntries++] = KRB_GET_POINTER(&pNode->mpRight);
    111111            }
    112112        } /* while */
     
    116116        while (pEnumData->cEntries > 0)
    117117        {
    118             KAVLNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1];
     118            KRBNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1];
    119119
    120120            /* right */
     
    122122            {
    123123                pEnumData->achFlags[pEnumData->cEntries - 1]++;
    124                 if (pNode->mpRight != KAVL_NULL)
     124                if (pNode->mpRight != KRB_NULL)
    125125                {
    126126                    pEnumData->achFlags[pEnumData->cEntries] = 0;  /* 0 right, 1 center, 2 left */
    127                     pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
     127                    pEnumData->aEntries[pEnumData->cEntries++] = KRB_GET_POINTER(&pNode->mpRight);
    128128                    continue;
    129129                }
     
    139139            /* left */
    140140            pEnumData->cEntries--;
    141             if (pNode->mpLeft != KAVL_NULL)
     141            if (pNode->mpLeft != KRB_NULL)
    142142            {
    143143                pEnumData->achFlags[pEnumData->cEntries] = 0;
    144                 pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
     144                pEnumData->aEntries[pEnumData->cEntries++] = KRB_GET_POINTER(&pNode->mpLeft);
    145145            }
    146146        } /* while */
     
    150150     * Call EndEnum.
    151151     */
    152     KAVL_FN(EndEnum)(pEnumData);
     152    KRB_FN(EndEnum)(pEnumData);
    153153    return NULL;
    154154}
     
    156156
    157157/**
    158  * Starts an enumeration of all nodes in the given AVL tree.
     158 * Starts an enumeration of all nodes in the given tree.
    159159 *
    160160 * The current implementation of this function will not walk the mpList
     
    164164 *          If NULL is returned the tree is empty calling EndEnum isn't
    165165 *          strictly necessary (although it will do no harm).
    166  * @param   pRoot           Pointer to the AVL-tree root structure.
     166 * @param   pRoot           Pointer to the Red-Back tree's root structure.
    167167 * @param   pEnumData       Pointer to enumeration control data.
    168168 * @param   fFromLeft       K_TRUE:  Left to right.
    169169 *                          K_FALSE: Right to left.
    170170 */
    171 KAVL_DECL(KAVLNODE *) KAVL_FN(BeginEnum)(KAVLROOT *pRoot, KAVL_TYPE(,ENUMDATA) *pEnumData, KBOOL fFromLeft)
     171KRB_DECL(KRBNODE *) KRB_FN(BeginEnum)(KRBROOT *pRoot, KRB_TYPE(,ENUMDATA) *pEnumData, KBOOL fFromLeft)
    172172{
    173     KAVL_READ_LOCK(pRoot);
     173    KRB_READ_LOCK(pRoot);
    174174    pEnumData->pRoot = pRoot;
    175     if (pRoot->mpRoot != KAVL_NULL)
     175    if (pRoot->mpRoot != KRB_NULL)
    176176    {
    177177        pEnumData->fFromLeft = fFromLeft;
    178178        pEnumData->cEntries = 1;
    179         pEnumData->aEntries[0] = KAVL_GET_POINTER(pRoot->mpRoot);
     179        pEnumData->aEntries[0] = KRB_GET_POINTER(pRoot->mpRoot);
    180180        pEnumData->achFlags[0] = 0;
    181181    }
     
    183183        pEnumData->cEntries = 0;
    184184
    185     return KAVL_FN(GetNext)(pEnumData);
     185    return KRB_FN(GetNext)(pEnumData);
    186186}
    187187
  • trunk/include/k/kRbTmpl/kRbGet.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvlTmpl - Templated AVL Trees, Get a Node.
     3 * kRbTmpl - Templated Red-Black Trees, Get a Node.
    44 */
    55
     
    3333 *
    3434 * @returns Pointer to the node holding the given key.
    35  * @param   pRoot       Pointer to the AVL-tree root structure.
     35 * @param   pRoot       Pointer to the Red-Back tree's root structure.
    3636 * @param   Key         Key value of the node which is to be found.
    3737 */
    38 KAVL_DECL(KAVLNODE *) KAVL_FN(Get)(KAVLROOT *pRoot, KAVLKEY Key)
     38KRB_DECL(KRBNODE *) KRB_FN(Get)(KRBROOT *pRoot, KRBKEY Key)
    3939{
    40     KAVLNODE *pNode;
    41 #ifdef KAVL_LOOKTHRU_CACHE
    42     KAVLTREEPTR *ppEntry;
     40    KRBNODE *pNode;
     41#ifdef KRB_CACHE_SIZE
     42    KRBTREEPTR *ppEntry;
    4343#endif
    4444
    45     KAVL_READ_LOCK(pRoot);
    46     if (pRoot->mpRoot == KAVL_NULL)
     45    KRB_READ_LOCK(pRoot);
     46    if (pRoot->mpRoot == KRB_NULL)
    4747    {
    48         KAVL_READ_UNLOCK(pRoot);
     48        KRB_READ_UNLOCK(pRoot);
    4949        return NULL;
    5050    }
    5151
    52 #ifdef KAVL_LOOKTHRU_CACHE
    53     ppEntry = &pRoot->maLookthru[KAVL_LOOKTHRU_HASH(Key)];
    54     pNode = KAVL_GET_POINTER_NULL(ppEntry);
    55     if (!pNode || KAVL_NE(pNode->mKey, Key))
     52#ifdef KRB_CACHE_SIZE
     53    ppEntry = &pRoot->maLookthru[KRB_CACHE_HASH(Key)];
     54    pNode = KRB_GET_POINTER_NULL(ppEntry);
     55    if (!pNode || KRB_CMP_NE(pNode->mKey, Key))
    5656#endif
    5757    {
    58         pNode = KAVL_GET_POINTER(&pRoot->mpRoot);
    59         while (KAVL_NE(pNode->mKey, Key))
     58        pNode = KRB_GET_POINTER(&pRoot->mpRoot);
     59        while (KRB_CMP_NE(pNode->mKey, Key))
    6060        {
    61             if (KAVL_G(pNode->mKey, Key))
     61            if (KRB_CMP_G(pNode->mKey, Key))
    6262            {
    63                 if (pNode->mpLeft == KAVL_NULL)
     63                if (pNode->mpLeft == KRB_NULL)
    6464                {
    65                     KAVL_READ_UNLOCK(pRoot);
     65                    KRB_READ_UNLOCK(pRoot);
    6666                    return NULL;
    6767                }
    68                 pNode = KAVL_GET_POINTER(&pNode->mpLeft);
     68                pNode = KRB_GET_POINTER(&pNode->mpLeft);
    6969            }
    7070            else
    7171            {
    72                 if (pNode->mpRight == KAVL_NULL)
     72                if (pNode->mpRight == KRB_NULL)
    7373                {
    74                     KAVL_READ_UNLOCK(pRoot);
     74                    KRB_READ_UNLOCK(pRoot);
    7575                    return NULL;
    7676                }
    77                 pNode = KAVL_GET_POINTER(&pNode->mpRight);
     77                pNode = KRB_GET_POINTER(&pNode->mpRight);
    7878            }
    7979        }
    8080
    81 #ifdef KAVL_LOOKTHRU_CACHE
    82         KAVL_SET_POINTER(ppEntry, pNode);
     81#ifdef KRB_CACHE_SIZE
     82        KRB_SET_POINTER(ppEntry, pNode);
    8383#endif
    8484    }
    8585
    86     KAVL_READ_UNLOCK(pRoot);
     86    KRB_READ_UNLOCK(pRoot);
    8787    return pNode;
    8888}
  • trunk/include/k/kRbTmpl/kRbGetBestFit.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvlTmpl - Templated AVL Trees, Get Best Fitting Node.
     3 * kRbTmpl - Templated Red-Black Trees, Get Best Fitting Node.
    44 */
    55
     
    3333 *
    3434 * @returns Pointer to the best fitting node found.
    35  * @param   pRoot           Pointer to the AVL-tree root structure.
     35 * @param   pRoot           Pointer to the Red-Back tree's root structure.
    3636 * @param   Key             The Key of which is to be found a best fitting match for..
    3737 * @param   fAbove          K_TRUE:  Returned node is have the closest key to Key from above.
     
    4141 *          <= (below): the node where you last turned right.
    4242 */
    43 KAVL_DECL(KAVLNODE *) KAVL_FN(GetBestFit)(KAVLROOT *pRoot, KAVLKEY Key, KBOOL fAbove)
     43KRB_DECL(KRBNODE *) KRB_FN(GetBestFit)(KRBROOT *pRoot, KRBKEY Key, KBOOL fAbove)
    4444{
    45     register KAVLNODE  *pNode;
    46     KAVLNODE           *pNodeLast;
     45    register KRBNODE  *pNode;
     46    KRBNODE           *pNodeLast;
    4747
    48     KAVL_READ_LOCK(pLook);
    49     if (pRoot->mpRoot == KAVL_NULL)
     48    KRB_READ_LOCK(pLook);
     49    if (pRoot->mpRoot == KRB_NULL)
    5050    {
    51         KAVL_READ_UNLOCK(pLook);
     51        KRB_READ_UNLOCK(pLook);
    5252        return NULL;
    5353    }
    5454
    55     pNode = KAVL_GET_POINTER(&pRoot->mpRoot);
     55    pNode = KRB_GET_POINTER(&pRoot->mpRoot);
    5656    pNodeLast = NULL;
    5757    if (fAbove)
    5858    {   /* pNode->mKey >= Key */
    59         while (KAVL_NE(pNode->mKey, Key))
     59        while (KRB_CMP_NE(pNode->mKey, Key))
    6060        {
    61             if (KAVL_G(pNode->mKey, Key))
     61            if (KRB_CMP_G(pNode->mKey, Key))
    6262            {
    63                 if (pNode->mpLeft == KAVL_NULL)
     63                if (pNode->mpLeft == KRB_NULL)
    6464                {
    65                     KAVL_READ_UNLOCK(pLook);
     65                    KRB_READ_UNLOCK(pLook);
    6666                    return pNode;
    6767                }
    6868                pNodeLast = pNode;
    69                 pNode = KAVL_GET_POINTER(&pNode->mpLeft);
     69                pNode = KRB_GET_POINTER(&pNode->mpLeft);
    7070            }
    7171            else
    7272            {
    73                 if (pNode->mpRight == KAVL_NULL)
     73                if (pNode->mpRight == KRB_NULL)
    7474                {
    75                     KAVL_READ_UNLOCK(pLook);
     75                    KRB_READ_UNLOCK(pLook);
    7676                    return pNodeLast;
    7777                }
    78                 pNode = KAVL_GET_POINTER(&pNode->mpRight);
     78                pNode = KRB_GET_POINTER(&pNode->mpRight);
    7979            }
    8080        }
     
    8282    else
    8383    {   /* pNode->mKey <= Key */
    84         while (KAVL_NE(pNode->mKey, Key))
     84        while (KRB_CMP_NE(pNode->mKey, Key))
    8585        {
    86             if (KAVL_G(pNode->mKey, Key))
     86            if (KRB_CMP_G(pNode->mKey, Key))
    8787            {
    88                 if (pNode->mpLeft == KAVL_NULL)
     88                if (pNode->mpLeft == KRB_NULL)
    8989                {
    90                     KAVL_READ_UNLOCK(pLook);
     90                    KRB_READ_UNLOCK(pLook);
    9191                    return pNodeLast;
    9292                }
    93                 pNode = KAVL_GET_POINTER(&pNode->mpLeft);
     93                pNode = KRB_GET_POINTER(&pNode->mpLeft);
    9494            }
    9595            else
    9696            {
    97                 if (pNode->mpRight == KAVL_NULL)
     97                if (pNode->mpRight == KRB_NULL)
    9898                {
    99                     KAVL_READ_UNLOCK(pLook);
     99                    KRB_READ_UNLOCK(pLook);
    100100                    return pNode;
    101101                }
    102102                pNodeLast = pNode;
    103                 pNode = KAVL_GET_POINTER(&pNode->mpRight);
     103                pNode = KRB_GET_POINTER(&pNode->mpRight);
    104104            }
    105105        }
     
    107107
    108108    /* perfect match or nothing. */
    109     KAVL_READ_UNLOCK(pLook);
     109    KRB_READ_UNLOCK(pLook);
    110110    return pNode;
    111111}
  • trunk/include/k/kRbTmpl/kRbGetWithParent.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvlTmpl - Templated AVL Trees, Get Node With Parent.
     3 * kRbTmpl - Templated Red-Black Trees, Get Node With Parent.
    44 */
    55
     
    3434 *
    3535 * @returns Pointer to the node holding the given key.
    36  * @param   pRoot       Pointer to the AVL-tree root structure.
     36 * @param   pRoot       Pointer to the Red-Back tree's root structure.
    3737 * @param   ppParent    Pointer to a variable which will hold the pointer to the partent node on
    3838 *                      return. When no node is found, this will hold the last searched node.
    3939 * @param   Key         Key value of the node which is to be found.
    4040 */
    41 KAVL_DECL(KAVLNODE *) KAVL_FN(GetWithParent)(KAVLROOT *pRoot, KAVLNODE **ppParent, KAVLKEY Key)
     41KRB_DECL(KRBNODE *) KRB_FN(GetWithParent)(KRBROOT *pRoot, KRBNODE **ppParent, KRBKEY Key)
    4242{
    43     register KAVLNODE *pNode;
    44     register KAVLNODE *pParent;
     43    register KRBNODE *pNode;
     44    register KRBNODE *pParent;
    4545
    46     KAVL_READ_LOCK(pRoot);
     46    KRB_READ_LOCK(pRoot);
    4747
    4848    pParent = NULL;
    49     pNode = KAVL_GET_POINTER_NULL(&pRoot->mpRoot);
     49    pNode = KRB_GET_POINTER_NULL(&pRoot->mpRoot);
    5050    while (     pNode != NULL
    51            &&   KAVL_NE(pNode->mKey, Key))
     51           &&   KRB_CMP_NE(pNode->mKey, Key))
    5252    {
    5353        pParent = pNode;
    54         if (KAVL_G(pNode->mKey, Key))
    55             pNode = KAVL_GET_POINTER_NULL(&pNode->mpLeft);
     54        if (KRB_CMP_G(pNode->mKey, Key))
     55            pNode = KRB_GET_POINTER_NULL(&pNode->mpLeft);
    5656        else
    57             pNode = KAVL_GET_POINTER_NULL(&pNode->mpRight);
     57            pNode = KRB_GET_POINTER_NULL(&pNode->mpRight);
    5858    }
    5959
    60     KAVL_UNLOCK(pRoot);
     60    KRB_READ_UNLOCK(pRoot);
    6161
    6262    *ppParent = pParent;
  • trunk/include/k/kRbTmpl/kRbRemove2.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvlTmpl - Templated AVL Trees, Remove A Specific Node.
     3 * kRbTmpl - Templated Red-Black Trees, Remove A Specific Node.
    44 */
    55
     
    3333 *
    3434 * @returns Pointer to the removed node (NULL if not in the tree)
    35  * @param   pRoot       Pointer to the AVL-tree root structure.
     35 * @param   pRoot       Pointer to the Red-Back tree's root structure.
    3636 * @param   Key         The Key of which is to be found a best fitting match for..
    3737 *
     
    3939 *          easier to manage.
    4040 */
    41 KAVL_DECL(KAVLNODE *) KAVL_FN(Remove2)(KAVLTROOT *pRoot, KAVLNODE *pNode)
     41KRB_DECL(KRBNODE *) KRB_FN(Remove2)(KRBROOT *pRoot, KRBNODE *pNode)
    4242{
    43 #ifdef KAVL_EQUAL_ALLOWED
     43#ifdef KRB_EQUAL_ALLOWED
    4444    /*
    4545     * Find the right node by key and see if it's what we want.
    4646     */
    47     KAVLNODE *pParent;
    48     KAVLNODE *pCurNode = KAVL_FN(GetWithParent)(pRoot, pNode->mKey, &pParent);
     47    KRBNODE *pParent;
     48    KRBNODE *pCurNode = KRB_FN(GetWithParent)(pRoot, pNode->mKey, &pParent);
    4949    if (!pCurNode)
    5050        return NULL;
    51     KAVL_WRITE_LOCK(pRoot); /** @todo the locking here isn't 100% sane. The only way to archive that is by no calling worker functions. */
     51    KRB_WRITE_LOCK(pRoot); /** @todo the locking here isn't 100% sane. The only way to archive that is by no calling worker functions. */
    5252    if (pCurNode != pNode)
    5353    {
     
    5555         * It's not the one we want, but it could be in the duplicate list.
    5656         */
    57         while (pCurNode->mpList != KAVL_NULL)
     57        while (pCurNode->mpList != KRB_NULL)
    5858        {
    59             KAVLNODE *pNext = KAVL_GET_POINTER(&pCurNode->mpList);
     59            KRBNODE *pNext = KRB_GET_POINTER(&pCurNode->mpList);
    6060            if (pNext == pNode)
    6161            {
    62                 KAVL_SET_POINTER_NULL(&pCurNode->mpList, KAVL_GET_POINTER_NULL(&pNode->mpList));
    63                 pNode->mpList = KAVL_NULL;
    64                 KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
    65                 KAVL_WRITE_UNLOCK(pRoot);
     62                KRB_SET_POINTER_NULL(&pCurNode->mpList, KRB_GET_POINTER_NULL(&pNode->mpList));
     63                pNode->mpList = KRB_NULL;
     64                KRB_CACHE_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
     65                KRB_WRITE_UNLOCK(pRoot);
    6666                return pNode;
    6767            }
    6868            pCurNode = pNext;
    6969        }
    70         KAVL_WRITE_UNLOCK(pRoot);
     70        KRB_WRITE_UNLOCK(pRoot);
    7171        return NULL;
    7272    }
     
    7979     * insert the first duplicate in our place.
    8080     */
    81     if (pNode->mpList == KAVL_NODE)
     81    if (pNode->mpList == KRB_NULL)
    8282    {
    83         KAVL_WRITE_UNLOCK(pRoot);
    84         KAVL_FN(Remove)(pRoot, pNode->mKey);
     83        KRB_WRITE_UNLOCK(pRoot);
     84        KRB_FN(Remove)(pRoot, pNode->mKey);
    8585    }
    8686    else
    8787    {
    88         KAVLNODE *pNewUs = KAVL_GET_POINTER(&pNode->mpList);
     88        KRBNODE *pNewUs = KRB_GET_POINTER(&pNode->mpList);
    8989
    9090        pNewUs->mHeight = pNode->mHeight;
    9191
    92         if (pNode->mpLeft != KAVL_NULL)
    93             KAVL_SET_POINTER(&pNewUs->mpLeft, KAVL_GET_POINTER(&pNode->mpLeft))
     92        if (pNode->mpLeft != KRB_NULL)
     93            KRB_SET_POINTER(&pNewUs->mpLeft, KRB_GET_POINTER(&pNode->mpLeft))
    9494        else
    95             pNewUs->mpLeft = KAVL_NULL;
     95            pNewUs->mpLeft = KRB_NULL;
    9696
    97         if (pNode->mpRight != KAVL_NULL)
    98             KAVL_SET_POINTER(&pNewUs->mpRight, KAVL_GET_POINTER(&pNode->mpRight))
     97        if (pNode->mpRight != KRB_NULL)
     98            KRB_SET_POINTER(&pNewUs->mpRight, KRB_GET_POINTER(&pNode->mpRight))
    9999        else
    100             pNewUs->mpRight = KAVL_NULL;
     100            pNewUs->mpRight = KRB_NULL;
    101101
    102102        if (pParent)
    103103        {
    104             if (KAVL_GET_POINTER_NULL(&pParent->mpLeft) == pNode)
    105                 KAVL_SET_POINTER(&pParent->mpLeft, pNewUs);
     104            if (KRB_GET_POINTER_NULL(&pParent->mpLeft) == pNode)
     105                KRB_SET_POINTER(&pParent->mpLeft, pNewUs);
    106106            else
    107                 KAVL_SET_POINTER(&pParent->mpRight, pNewUs);
     107                KRB_SET_POINTER(&pParent->mpRight, pNewUs);
    108108        }
    109109        else
    110             KAVL_SET_POINTER(&pRoot->mpRoot, pNewUs);
     110            KRB_SET_POINTER(&pRoot->mpRoot, pNewUs);
    111111
    112         KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
    113         KAVL_WRITE_UNLOCK(pRoot);
     112        KRB_CACHE_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
     113        KRB_WRITE_UNLOCK(pRoot);
    114114    }
    115115
     
    123123     * of wrong nodes but just uses this API for his convenience.
    124124     */
    125     KAVLNODE *pRemovedNode = KAVL_FN(Remove)(pRoot, pNode->mKey);
     125    KRBNODE *pRemovedNode = KRB_FN(Remove)(pRoot, pNode->mKey);
    126126    if (pRemovedNode == pNode)
    127127        return pRemovedNode;
    128128
    129     KAVL_FN(Insert)(pRoot, pRemovedNode);
     129    KRB_FN(Insert)(pRoot, pRemovedNode);
    130130    return NULL;
    131131#endif
  • trunk/include/k/kRbTmpl/kRbRemoveBestFit.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvlTmpl - Templated AVL Trees, Remove Best Fitting Node.
     3 * kRbTmpl - Templated Red-Black Trees, Remove Best Fitting Node.
    44 */
    55
     
    3333 *
    3434 * @returns Pointer to the removed node.
    35  * @param   pRoot       Pointer to the AVL-tree root structure.
     35 * @param   pRoot       Pointer to the Red-Back tree's root structure.
    3636 * @param   Key         The Key of which is to be found a best fitting match for..
    3737 * @param   fAbove      K_TRUE:  Returned node is have the closest key to Key from above.
     
    4242 *          code size, and the likelyhood for bugs.
    4343 */
    44 KAVL_DECL(KAVLNODE *) KAVL_FN(RemoveBestFit)(KAVLROOT *pRoot, KAVLKEY Key, KBOOL fAbove)
     44KRB_DECL(KRBNODE *) KRB_FN(RemoveBestFit)(KRBROOT *pRoot, KRBKEY Key, KBOOL fAbove)
    4545{
    4646    /*
     
    4949     * removing the in-tree node as this is way cheaper.
    5050     */
    51     KAVLNODE *pNode = KAVL_FN(GetBestFit)(pRoot, Key, fAbove);
     51    KRBNODE *pNode = KRB_FN(GetBestFit)(pRoot, Key, fAbove);
    5252    if (pNode != NULL)
    5353    {
    54 #ifdef KAVL_EQUAL_ALLOWED
    55         KAVL_WRITE_LOCK(pRoot); /** @todo the locking isn't quite sane here. :-/ */
    56         if (pNode->mpList != KAVL_NULL)
     54#ifdef KRB_EQUAL_ALLOWED
     55        KRB_WRITE_LOCK(pRoot); /** @todo the locking isn't quite sane here. :-/ */
     56        if (pNode->mpList != KRB_NULL)
    5757        {
    58             KAVLNODE *pRet = KAVL_GET_POINTER(&pNode->mpList);
    59             KAVL_SET_POINTER_NULL(&pNode->mpList, &pRet->mpList);
    60             KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
    61             KAVL_WRITE_UNLOCK(pRoot);
     58            KRBNODE *pRet = KRB_GET_POINTER(&pNode->mpList);
     59            KRB_SET_POINTER_NULL(&pNode->mpList, &pRet->mpList);
     60            KRB_CACHE_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
     61            KRB_WRITE_UNLOCK(pRoot);
    6262            return pRet;
    6363        }
    64         KAVL_WRITE_UNLOCK(pRoot);
     64        KRB_WRITE_UNLOCK(pRoot);
    6565#endif
    66         pNode = KAVL_FN(Remove)(pRoot, pNode->mKey);
     66        pNode = KRB_FN(Remove)(pRoot, pNode->mKey);
    6767    }
    6868    return pNode;
  • trunk/include/k/kRbTmpl/kRbUndef.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvlTmpl - Undefines All Macros (both config and temp).
     3 * kRbTmpl - Undefines All Macros (both config and temp).
    44 */
    55
    66/*
    7  * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
     7 * Copyright (c) 2006-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
    88 *
    99 * Permission is hereby granted, free of charge, to any person
     
    3232 * The configuration.
    3333 */
    34 #undef KAVL_EQUAL_ALLOWED
    35 #undef KAVL_CHECK_FOR_EQUAL_INSERT
    36 #undef KAVL_MAX_STACK
    37 #undef KAVL_RANGE
    38 #undef KAVL_OFFSET
    39 #undef KAVL_STD_KEY_COMP
    40 #undef KAVL_LOOKTHRU
    41 #undef KAVL_LOOKTHRU_HASH
    42 #undef KAVL_LOCKED
    43 #undef KAVL_WRITE_LOCK
    44 #undef KAVL_WRITE_UNLOCK
    45 #undef KAVL_READ_LOCK
    46 #undef KAVL_READ_UNLOCK
    47 #undef KAVLKEY
    48 #undef KAVLNODE
    49 #undef KAVLTREEPTR
    50 #undef KAVLROOT
    51 #undef KAVL_FN
    52 #undef KAVL_TYPE
    53 #undef KAVL_INT
    54 #undef KAVL_DECL
     34#undef KRB_EQUAL_ALLOWED
     35#undef KRB_CHECK_FOR_EQUAL_INSERT
     36#undef KRB_MAX_STACK
     37#undef KRB_RANGE
     38#undef KRB_OFFSET
     39#undef KRB_STD_KEY_COMP
     40#undef KRB_CACHE_SIZE
     41#undef KRB_CACHE_HASH
     42#undef KRB_LOCKED
     43#undef KRB_WRITE_LOCK
     44#undef KRB_WRITE_UNLOCK
     45#undef KRB_READ_LOCK
     46#undef KRB_READ_UNLOCK
     47#undef KRBKEY
     48#undef KRBNODE
     49#undef KRBTREEPTR
     50#undef KRBROOT
     51#undef KRB_FN
     52#undef KRB_TYPE
     53#undef KRB_INT
     54#undef KRB_DECL
    5555#undef mKey
    5656#undef mKeyLast
    57 #undef mHeight
     57#undef mfIsRed
    5858#undef mpLeft
    5959#undef mpRight
     
    6161#undef mpRoot
    6262#undef maLookthru
     63#undef KRB_CMP_G
     64#undef KRB_CMP_E
     65#undef KRB_CMP_NE
     66#undef KRB_R_IS_IDENTICAL
     67#undef KRB_R_IS_INTERSECTING
     68#undef KRB_R_IS_IN_RANGE
    6369
    6470/*
    65  * The internal macros.
     71 * Internal ones.
    6672 */
    67 #undef KAVL_HEIGHTOF
    68 #undef KAVL_GET_POINTER
    69 #undef KAVL_GET_POINTER_NULL
    70 #undef KAVL_SET_POINTER
    71 #undef KAVL_SET_POINTER_NULL
    72 #undef KAVL_NULL
    73 #undef KAVL_G
    74 #undef KAVL_E
    75 #undef KAVL_NE
    76 #undef KAVL_R_IS_IDENTICAL
    77 #undef KAVL_R_IS_INTERSECTING
    78 #undef KAVL_R_IS_IN_RANGE
     73#undef KRB_IS_RED
     74#undef KRB_NULL
     75#undef KRB_GET_POINTER
     76#undef KRB_GET_POINTER_NULL
     77#undef KRB_SET_POINTER
     78#undef KRB_SET_POINTER_NULL
    7979
  • trunk/include/k/kRbU32.h

    r33 r35  
    11/* $Id$ */
    22/** @file
    3  * kAvl - AVL Tree Implementation, KU32 keys.
     3 * kRb - Red-Black Tree Implementation, KU32 keys.
    44 */
    55
    66/*
    7  * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
     7 * Copyright (c) 2006-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
    88 *
    99 * Permission is hereby granted, free of charge, to any person
     
    2929 */
    3030
    31 #ifndef ___k_kAvlU32_h___
    32 #define ___k_kAvlU32_h___
     31#ifndef ___k_kRbU32_h___
     32#define ___k_kRbU32_h___
    3333
    34 typedef struct KAVLU32
     34typedef struct KRBU32
    3535{
    3636    KU32                mKey;
    37     KU8                 mHeight;
    38     struct KAVLU32     *mpLeft;
    39     struct KAVLU32     *mpRight;
    40 } KAVLU32, *PKAVLU32, **PPKAVLU32;
     37    KBOOL               mfRed;
     38    struct KRBU32      *mpLeft;
     39    struct KRBU32      *mpRight;
     40} KRBU32;
     41typedef KRBU32  *PRBU32;
     42typedef KRBU32 **PPRBU32;
    4143
    42 /*#define KAVL_EQUAL_ALLOWED*/
    43 #define KAVL_CHECK_FOR_EQUAL_INSERT
    44 #define KAVL_MAX_STACK          32
    45 /*#define KAVL_RANGE */
    46 /*#define KAVL_OFFSET */
    47 #define KAVL_STD_KEY_COMP
    48 #define KAVLKEY                 KU32
    49 #define KAVLNODE            KAVLU32
    50 #define KAVL_FN(name)           kAvlU32 ## name
    51 #define KAVL_TYPE(prefix,name)  prefix ## KAVLU32 ## name
    52 #define KAVL_INT(name)          KAVLU32INT ## name
    53 #define KAVL_DECL(rettype)      K_DECL_INLINE(rettype)
     44/*#define KRB_EQUAL_ALLOWED*/
     45#define KRB_CHECK_FOR_EQUAL_INSERT
     46/*#define KRB_RANGE */
     47/*#define KRB_OFFSET */
     48#define KRB_MAX_STACK           48
     49#define KRB_STD_KEY_COMP
     50#define KRBKEY                  KU32
     51#define KRBNODE                 KRBU32
     52#define KRB_FN(name)            kRbU32 ## name
     53#define KRB_TYPE(prefix,name)   prefix ## KRBU32 ## name
     54#define KRB_INT(name)           KRBU32INT ## name
     55#define KRB_DECL(rettype)       K_DECL_INLINE(rettype)
    5456
    55 #include <k/kAvlTmpl/kAvlBase.h>
    56 #include <k/kAvlTmpl/kAvlDoWithAll.h>
    57 #include <k/kAvlTmpl/kAvlEnum.h>
    58 #include <k/kAvlTmpl/kAvlGet.h>
    59 #include <k/kAvlTmpl/kAvlGetBestFit.h>
    60 #include <k/kAvlTmpl/kAvlGetWithParent.h>
    61 #include <k/kAvlTmpl/kAvlRemove2.h>
    62 #include <k/kAvlTmpl/kAvlRemoveBestFit.h>
    63 #include <k/kAvlTmpl/kAvlUndef.h>
     57#include <k/kRbTmpl/kRbBase.h>
     58#include <k/kRbTmpl/kRbDoWithAll.h>
     59#include <k/kRbTmpl/kRbEnum.h>
     60#include <k/kRbTmpl/kRbGet.h>
     61#include <k/kRbTmpl/kRbGetBestFit.h>
     62#include <k/kRbTmpl/kRbGetWithParent.h>
     63#include <k/kRbTmpl/kRbRemove2.h>
     64#include <k/kRbTmpl/kRbRemoveBestFit.h>
     65#include <k/kRbTmpl/kRbUndef.h>
    6466
    6567#endif
Note: See TracChangeset for help on using the changeset viewer.