Changeset 35
- Timestamp:
- Nov 8, 2009, 8:39:03 PM (16 years ago)
- Location:
- trunk/include/k
- Files:
-
- 2 copied
- 10 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/k/kRbTmpl/kRbBase.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k AvlTmpl - Templated AVLTrees, The Mandatory Base Code.3 * kRbTmpl - Templated Red-Black Trees, The Mandatory Base Code. 4 4 */ 5 5 … … 31 31 /** @page pg_kAvlTmpl Template Configuration. 32 32 * 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 38 37 * Define this to tell us that equal keys are allowed. 39 * Then Equal keys will be put in a list pointed to by K AVLNODE::pList.38 * Then Equal keys will be put in a list pointed to by KRBNODE::pList. 40 39 * This is by default not defined. 41 40 * 42 * \#define K AVL_CHECK_FOR_EQUAL_INSERT41 * \#define KRB_CHECK_FOR_EQUAL_INSERT 43 42 * Define this to enable insert check for equal nodes. 44 43 * This is by default not defined. 45 44 * 46 * \#define K AVL_MAX_STACK45 * \#define KRB_MAX_STACK 47 46 * Use this to specify the max number of stack entries the stack will use when 48 47 * inserting and removing nodes from the tree. The size should be something like … … 50 49 * Must be defined. 51 50 * 52 * \#define K AVL_RANGE51 * \#define KRB_RANGE 53 52 * Define this to enable key ranges. 54 53 * 55 * \#define K AVL_OFFSET54 * \#define KRB_OFFSET 56 55 * Define this to link the tree together using self relative offset 57 56 * instead of memory pointers, thus making the entire tree relocatable … … 59 58 * the exact same distance. 60 59 * 61 * \#define K AVL_LOOKTHRU60 * \#define KRB_CACHE_SIZE 62 61 * 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) 67 65 * Define this to specify a more efficient translation of the key into 68 66 * a lookthru array index. The default is key % size. 69 67 * For some key types this is required as the default will not compile. 70 68 * 71 * \#define K AVL_LOCKED69 * \#define KRB_LOCKED 72 70 * Define this if you wish for the tree to be locked via the 73 * K AVL_WRITE_LOCK, KAVL_WRITE_UNLOCK, KAVL_READ_LOCK and74 * K AVL_READ_UNLOCK macros. If not defined the tree will not be subject71 * KRB_WRITE_LOCK, KRB_WRITE_UNLOCK, KRB_READ_LOCK and 72 * KRB_READ_UNLOCK macros. If not defined the tree will not be subject 75 73 * do any kind of locking and the problem of concurrency is left the user. 76 74 * 77 * \#define K AVL_WRITE_LOCK(pRoot)75 * \#define KRB_WRITE_LOCK(pRoot) 78 76 * Lock the tree for writing. 79 77 * 80 * \#define K AVL_WRITE_UNLOCK(pRoot)81 * Counteracts K AVL_WRITE_LOCK.82 * 83 * \#define K AVL_READ_LOCK(pRoot)78 * \#define KRB_WRITE_UNLOCK(pRoot) 79 * Counteracts KRB_WRITE_LOCK. 80 * 81 * \#define KRB_READ_LOCK(pRoot) 84 82 * Lock the tree for reading. 85 83 * 86 * \#define K AVL_READ_UNLOCK(pRoot)87 * Counteracts K AVL_READ_LOCK.88 * 89 * \#define K AVLKEY84 * \#define KRB_READ_UNLOCK(pRoot) 85 * Counteracts KRB_READ_LOCK. 86 * 87 * \#define KRBKEY 90 88 * Define this to the name of the AVL key type. 91 89 * 92 * \#define K AVL_STD_KEY_COMP90 * \#define KRB_STD_KEY_COMP 93 91 * Define this to use the standard key compare macros. If not set all the 94 * compare operations for K AVLKEY have to be defined: KAVL_G, KAVL_E, KAVL_NE,95 * K AVL_R_IS_IDENTICAL, KAVL_R_IS_INTERSECTING and KAVL_R_IS_IN_RANGE. The96 * latter three are only required when K AVL_RANGE is defined.97 * 98 * \#define K AVLNODE92 * 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 99 97 * Define this to the name (typedef) of the AVL node structure. This 100 98 * structure must have a mpLeft, mpRight, mKey and mHeight member. 101 * If K AVL_RANGE is defined a mKeyLast is also required.102 * If K AVL_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. 103 101 * It's possible to use other member names by redefining the names. 104 102 * 105 * \#define K AVLTREEPTR103 * \#define KRBTREEPTR 106 104 * Define this to the name (typedef) of the tree pointer type. This is 107 * required when K AVL_OFFSET is defined. When not defined it defaults108 * to K AVLNODE *.109 * 110 * \#define K AVLROOT105 * required when KRB_OFFSET is defined. When not defined it defaults 106 * to KRBNODE *. 107 * 108 * \#define KRBROOT 111 109 * Define this to the name (typedef) of the AVL root structure. This 112 110 * is optional. However, if specified it must at least have a mpRoot 113 * member of K AVLTREEPTR type. If KAVL_LOOKTHRUis non-zero a114 * maLookthru[K AVL_LOOKTHRU] member of the KAVLTREEPTR type is also111 * member of KRBTREEPTR type. If KRB_CACHE_SIZE is non-zero a 112 * maLookthru[KRB_CACHE_SIZE] member of the KRBTREEPTR type is also 115 113 * required. 116 114 * 117 * \#define K AVL_FN115 * \#define KRB_FN 118 116 * Use this to alter the names of the AVL functions. 119 117 * Must be defined. 120 118 * 121 * \#define K AVL_TYPE(prefix, name)119 * \#define KRB_TYPE(prefix, name) 122 120 * Use this to make external type names and unique. The prefix may be empty. 123 121 * Must be defined. 124 122 * 125 * \#define K AVL_INT(name)123 * \#define KRB_INT(name) 126 124 * Use this to make internal type names and unique. The prefix may be empty. 127 125 * Must be defined. 128 126 * 129 * \#define K AVL_DECL(rettype)127 * \#define KRB_DECL(rettype) 130 128 * Function declaration macro that should be set according to the scope 131 129 * the instantiated template should have. For instance an inlined scope … … 151 149 #define KAVL_HEIGHTOF(pNode) ((KU8)((pNode) != NULL ? (pNode)->mHeight : 0)) 152 150 153 /** @def K AVL_GET_POINTER151 /** @def KRB_GET_POINTER 154 152 * Reads a 'pointer' value. 155 153 * … … 159 157 */ 160 158 161 /** @def K AVL_GET_POINTER_NULL162 * Reads a 'pointer' value which can be K AVL_NULL.159 /** @def KRB_GET_POINTER_NULL 160 * Reads a 'pointer' value which can be KRB_NULL. 163 161 * 164 162 * @returns The native pointer. 165 * @returns NULL pointer if K AVL_NULL.163 * @returns NULL pointer if KRB_NULL. 166 164 * @param pp Pointer to the pointer to read. 167 165 * @internal 168 166 */ 169 167 170 /** @def K AVL_SET_POINTER168 /** @def KRB_SET_POINTER 171 169 * Writes a 'pointer' value. 172 170 * For offset-based schemes offset relative to pp is calculated and assigned to *pp. … … 178 176 */ 179 177 180 /** @def K AVL_SET_POINTER_NULL181 * Writes a 'pointer' value which can be K AVL_NULL.178 /** @def KRB_SET_POINTER_NULL 179 * Writes a 'pointer' value which can be KRB_NULL. 182 180 * 183 181 * For offset-based schemes offset relative to pp is calculated and assigned to *pp, 184 * if p is not K AVL_NULL of course.182 * if p is not KRB_NULL of course. 185 183 * 186 184 * @returns stored pointer. 187 185 * @param pp Pointer to where to store the pointer. 188 * @param pp2 Pointer to where to pointer to assign to pp. This can be K AVL_NULL186 * @param pp2 Pointer to where to pointer to assign to pp. This can be KRB_NULL 189 187 * @internal 190 188 */ 191 189 192 #ifndef K AVLTREEPTR193 # define K AVLTREEPTR KAVLNODE *194 #endif 195 196 #ifndef K AVLROOT197 # define K AVLROOT KAVL_TYPE(,ROOT)198 # define K AVL_NEED_KAVLROOT199 #endif 200 201 #ifdef K AVL_LOOKTHRU202 # ifndef K AVL_LOOKTHRU_HASH203 # define K AVL_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) ) 204 202 # endif 205 #elif defined(K AVL_LOOKTHRU_HASH)206 # error "K AVL_LOOKTHRU_HASH without KAVL_LOOKTHRU!"207 #endif 208 209 #ifdef K AVL_LOOKTHRU210 # define K AVL_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) \ 211 209 do { \ 212 K AVLTREEPTR **ppEntry = &pRoot->maLookthru[KAVL_LOOKTHRU_HASH(Key)]; \213 if ((pNode) == K AVL_GET_POINTER_NULL(ppEntry)) \214 *ppEntry = K AVL_NULL; \210 KRBTREEPTR **ppEntry = &pRoot->maLookthru[KRB_CACHE_HASH(Key)]; \ 211 if ((pNode) == KRB_GET_POINTER_NULL(ppEntry)) \ 212 *ppEntry = KRB_NULL; \ 215 213 } while (0) 216 214 #else 217 # define K AVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, Key) do { } while (0)218 #endif 219 220 #ifndef K AVL_LOCKED221 # define K AVL_WRITE_LOCK(pRoot)do { } while (0)222 # define K AVL_WRITE_UNLOCK(pRoot)do { } while (0)223 # define K AVL_READ_LOCK(pRoot)do { } while (0)224 # define K AVL_READ_UNLOCK(pRoot)do { } while (0)225 #endif 226 227 #ifdef K AVL_OFFSET228 # define K AVL_GET_POINTER(pp) ( (KAVLNODE *)((KIPTR)(pp) + *(pp)) )229 # define K AVL_GET_POINTER_NULL(pp) ( *(pp) != KAVL_NULL ? KAVL_GET_POINTER(pp) : NULL )230 # define K AVL_SET_POINTER(pp, p)( (*(pp)) = ((KIPTR)(p) - (KIPTR)(pp)) )231 # define K AVL_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 ) 232 230 #else 233 # define K AVL_GET_POINTER(pp)( *(pp) )234 # define K AVL_GET_POINTER_NULL(pp)( *(pp) )235 # define K AVL_SET_POINTER(pp, p)( (*(pp)) = (p) )236 # define K AVL_SET_POINTER_NULL(pp, pp2)( (*(pp)) = *(pp2) )237 #endif 238 239 240 /** @def K AVL_NULL231 # 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 241 239 * The NULL 'pointer' equivalent. 242 240 */ 243 #ifdef K AVL_OFFSET244 # define K AVL_NULL 0241 #ifdef KRB_OFFSET 242 # define KRB_NULL 0 245 243 #else 246 # define K AVL_NULL NULL247 #endif 248 249 #ifdef K AVL_STD_KEY_COMP250 # define K AVL_G(key1, key2)( (key1) > (key2) )251 # define K AVL_E(key1, key2)( (key1) == (key2) )252 # define K AVL_NE(key1, key2)( (key1) != (key2) )253 # ifdef K AVL_RANGE254 # define K AVL_R_IS_IDENTICAL(key1B, key2B, key1E, key2E) ( (key1B) == (key2B) && (key1E) == (key2E) )255 # define K AVL_R_IS_INTERSECTING(key1B, key2B, key1E, key2E) ( (key1B) <= (key2E) && (key1E) >= (key2B) )256 # define K AVL_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) 257 255 # endif 258 256 #endif 259 257 260 #ifndef K AVL_RANGE261 # define K AVL_R_IS_INTERSECTING(key1B, key2B, key1E, key2E) KAVL_E(key1B, key2B)262 # define K AVL_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) 263 261 #endif 264 262 … … 274 272 { 275 273 unsigned cEntries; 276 K AVLTREEPTR *aEntries[KAVL_MAX_STACK];277 } K AVL_INT(STACK);274 KRBTREEPTR *aEntries[KRB_MAX_STACK]; 275 } KRB_INT(STACK); 278 276 279 277 /** 280 278 * The callback used by the Destroy and DoWithAll functions. 281 279 */ 282 typedef int (* K AVL_TYPE(PFN,CALLBACK))(KAVLNODE *, void *);283 284 #ifdef K AVL_NEED_KAVLROOT280 typedef int (* KRB_TYPE(PFN,CALLBACK))(KRBNODE *, void *); 281 282 #ifdef KRB_NEED_KRBROOT 285 283 /** 286 * The AVLroot structure.284 * The Red-Black tree root structure. 287 285 */ 288 286 typedef struct 289 287 { 290 K AVLTREEPTR mpRoot;291 # ifdef K AVL_LOOKTHRU292 K AVLTREEPTR maLookthru[KAVL_LOOKTHRU];288 KRBTREEPTR mpRoot; 289 # ifdef KRB_CACHE_SIZE 290 KRBTREEPTR maLookthru[KRB_CACHE_SIZE]; 293 291 # endif 294 } KAVLROOT; 295 #endif 292 } KRBROOT; 293 #endif 294 296 295 297 296 298 297 /** 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 */ 302 KRB_DECL(void) KRB_FN(Init)(KRBROOT *pRoot) 349 303 { 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 416 313 } 417 314 418 315 419 316 /** 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. 440 318 * @returns K_TRUE if inserted. 441 319 * K_FALSE if node exists in tree. 442 * @param pRoot Pointer to the AVL-treeroot structure.320 * @param pRoot Pointer to the Red-Back tree's root structure. 443 321 * @param pNode Pointer to the node which is to be added. 444 322 * @sketch Find the location of the node (using binary tree algorithm.): … … 454 332 * Rebalance the tree. 455 333 */ 456 K AVL_DECL(KBOOL) KAVL_FN(Insert)(KAVLROOT *pRoot, KAVLNODE *pNode)334 KRB_DECL(KBOOL) KRB_FN(Insert)(KRBROOT *pRoot, KRBNODE *pNode) 457 335 { 458 K AVL_INT(STACK) AVLStack;459 K AVLTREEPTR *ppCurNode = &pRoot->mpRoot;460 register K AVLKEY Key = pNode->mKey;461 #ifdef K AVL_RANGE462 register K AVLKEY KeyLast = pNode->mKeyLast;463 #endif 464 465 #ifdef K AVL_RANGE336 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 466 344 if (Key > KeyLast) 467 345 return false; 468 346 #endif 469 347 470 K AVL_WRITE_LOCK(pRoot);471 472 AVLStack.cEntries = 0;473 while (*ppCurNode != K AVL_NULL)348 KRB_WRITE_LOCK(pRoot); 349 350 Stack.cEntries = 0; 351 while (*ppCurNode != KRB_NULL) 474 352 { 475 register K AVLNODE *pCurNode = KAVL_GET_POINTER(ppCurNode);476 477 kHlpAssert( AVLStack.cEntries < KAVL_MAX_STACK);478 AVLStack.aEntries[AVLStack.cEntries++] = ppCurNode;479 #ifdef K AVL_EQUAL_ALLOWED480 if (K AVL_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)) 481 359 { 482 360 /* 483 361 * If equal then we'll use a list of equal nodes. 484 362 */ 485 pNode->mpLeft = pNode->mpRight = K AVL_NULL;363 pNode->mpLeft = pNode->mpRight = KRB_NULL; 486 364 pNode->mHeight = 0; 487 K AVL_SET_POINTER_NULL(&pNode->mpList, &pCurNode->mpList);488 K AVL_SET_POINTER(&pCurNode->mpList, pNode);489 K AVL_WRITE_UNLOCK(pRoot);365 KRB_SET_POINTER_NULL(&pNode->mpList, &pCurNode->mpList); 366 KRB_SET_POINTER(&pCurNode->mpList, pNode); 367 KRB_WRITE_UNLOCK(pRoot); 490 368 return K_TRUE; 491 369 } 492 370 #endif 493 #ifdef K AVL_CHECK_FOR_EQUAL_INSERT494 if (K AVL_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)) 495 373 { 496 K AVL_WRITE_UNLOCK(pRoot);374 KRB_WRITE_UNLOCK(pRoot); 497 375 return K_FALSE; 498 376 } 499 377 #endif 500 if (K AVL_G(pCurNode->mKey, Key))378 if (KRB_CMP_G(pCurNode->mKey, Key)) 501 379 ppCurNode = &pCurNode->mpLeft; 502 380 else … … 504 382 } 505 383 506 pNode->mpLeft = pNode->mpRight = K AVL_NULL;507 #ifdef K AVL_EQUAL_ALLOWED508 pNode->mpList = K AVL_NULL;384 pNode->mpLeft = pNode->mpRight = KRB_NULL; 385 #ifdef KRB_EQUAL_ALLOWED 386 pNode->mpList = KRB_NULL; 509 387 #endif 510 388 pNode->mHeight = 1; 511 K AVL_SET_POINTER(ppCurNode, pNode);512 513 K AVL_FN(Rebalance)(&AVLStack);514 515 K AVL_WRITE_UNLOCK(pRoot);389 KRB_SET_POINTER(ppCurNode, pNode); 390 391 KRB_FN(Rebalance)(&Stack); 392 393 KRB_WRITE_UNLOCK(pRoot); 516 394 return K_TRUE; 517 395 } … … 519 397 520 398 /** 521 * Removes a node from the AVL-tree.399 * Removes a node from the Red-Black tree. 522 400 * @returns Pointer to the node. 523 * @param pRoot Pointer to the AVL-treeroot structure.401 * @param pRoot Pointer to the Red-Back tree's root structure. 524 402 * @param Key Key value of the node which is to be removed. 525 403 * @sketch Find the node which is to be removed: … … 557 435 * return pointer to the removed node (if found). 558 436 */ 559 K AVL_DECL(KAVLNODE *) KAVL_FN(Remove)(KAVLROOT *pRoot, KAVLKEY Key)437 KRB_DECL(KRBNODE *) KRB_FN(Remove)(KRBROOT *pRoot, KRBKEY Key) 560 438 { 561 K AVL_INT(STACK) AVLStack;562 K AVLTREEPTR *ppDeleteNode = &pRoot->mpRoot;563 register K AVLNODE *pDeleteNode;564 565 K AVL_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; 568 446 for (;;) 569 447 { 570 if (*ppDeleteNode == K AVL_NULL)448 if (*ppDeleteNode == KRB_NULL) 571 449 { 572 K AVL_WRITE_UNLOCK(pRoot);450 KRB_WRITE_UNLOCK(pRoot); 573 451 return NULL; 574 452 } 575 pDeleteNode = K AVL_GET_POINTER(ppDeleteNode);576 577 kHlpAssert( AVLStack.cEntries < KAVL_MAX_STACK);578 AVLStack.aEntries[AVLStack.cEntries++] = ppDeleteNode;579 if (K AVL_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)) 580 458 break; 581 459 582 if (K AVL_G(pDeleteNode->mKey, Key))460 if (KRB_CMP_G(pDeleteNode->mKey, Key)) 583 461 ppDeleteNode = &pDeleteNode->mpLeft; 584 462 else … … 586 464 } 587 465 588 if (pDeleteNode->mpLeft != K AVL_NULL)466 if (pDeleteNode->mpLeft != KRB_NULL) 589 467 { 590 468 /* find the rightmost node in the left tree. */ 591 const unsigned iStackEntry = AVLStack.cEntries;592 K AVLTREEPTR *ppLeftLeast = &pDeleteNode->mpLeft;593 register K AVLNODE *pLeftLeast = KAVL_GET_POINTER(ppLeftLeast);594 595 while (pLeftLeast->mpRight != K AVL_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) 596 474 { 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; 599 477 ppLeftLeast = &pLeftLeast->mpRight; 600 pLeftLeast = K AVL_GET_POINTER(ppLeftLeast);478 pLeftLeast = KRB_GET_POINTER(ppLeftLeast); 601 479 } 602 480 603 481 /* link out pLeftLeast */ 604 K AVL_SET_POINTER_NULL(ppLeftLeast, &pLeftLeast->mpLeft);482 KRB_SET_POINTER_NULL(ppLeftLeast, &pLeftLeast->mpLeft); 605 483 606 484 /* link it in place of the delete node. */ 607 K AVL_SET_POINTER_NULL(&pLeftLeast->mpLeft, &pDeleteNode->mpLeft);608 K AVL_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); 609 487 pLeftLeast->mHeight = pDeleteNode->mHeight; 610 K AVL_SET_POINTER(ppDeleteNode, pLeftLeast);611 AVLStack.aEntries[iStackEntry] = &pLeftLeast->mpLeft;488 KRB_SET_POINTER(ppDeleteNode, pLeftLeast); 489 Stack.aEntries[iStackEntry] = &pLeftLeast->mpLeft; 612 490 } 613 491 else 614 492 { 615 K AVL_SET_POINTER_NULL(ppDeleteNode, &pDeleteNode->mpRight);616 AVLStack.cEntries--;493 KRB_SET_POINTER_NULL(ppDeleteNode, &pDeleteNode->mpRight); 494 Stack.cEntries--; 617 495 } 618 496 619 K AVL_FN(Rebalance)(&AVLStack);620 621 K AVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pDeleteNode, Key);622 623 K AVL_WRITE_UNLOCK(pRoot);497 KRB_FN(Rebalance)(&Stack); 498 499 KRB_CACHE_INVALIDATE_NODE(pRoot, pDeleteNode, Key); 500 501 KRB_WRITE_UNLOCK(pRoot); 624 502 return pDeleteNode; 625 503 } -
trunk/include/k/kRbTmpl/kRbDestroy.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k AvlTmpl - Templated AVLTrees, Destroy the tree.3 * kRbTmpl - Templated Red-Black Trees, Destroy the tree. 4 4 */ 5 5 … … 37 37 * made on it. Note that the node we fail on will be considered dead and 38 38 * no action is taken to link it back into the tree. 39 * @param pRoot Pointer to the AVL-treeroot structure.39 * @param pRoot Pointer to the Red-Back tree's root structure. 40 40 * @param pfnCallBack Pointer to callback function. 41 41 * @param pvUser User parameter passed on to the callback function. 42 42 */ 43 K AVL_DECL(int) KAVL_FN(Destroy)(KAVLROOT *pRoot, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)43 KRB_DECL(int) KRB_FN(Destroy)(KRBROOT *pRoot, KRB_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser) 44 44 { 45 #ifdef K AVL_LOOKTHRU45 #ifdef KRB_CACHE_SIZE 46 46 unsigned i; 47 47 #endif 48 48 unsigned cEntries; 49 K AVLNODE *apEntries[KAVL_MAX_STACK];49 KRBNODE *apEntries[KRB_MAX_STACK]; 50 50 int rc; 51 51 52 K AVL_WRITE_LOCK(pRoot);53 if (pRoot->mpRoot == K AVL_NULL)52 KRB_WRITE_LOCK(pRoot); 53 if (pRoot->mpRoot == KRB_NULL) 54 54 { 55 K AVL_WRITE_UNLOCK(pRoot);55 KRB_WRITE_UNLOCK(pRoot); 56 56 return 0; 57 57 } 58 58 59 #ifdef K AVL_LOOKTHRU59 #ifdef KRB_CACHE_SIZE 60 60 /* 61 61 * Kill the lookthru cache. 62 62 */ 63 for (i = 0; i < (K AVL_LOOKTHRU); i++)64 pRoot->maLookthru[i] = K AVL_NULL;63 for (i = 0; i < (KRB_CACHE_SIZE); i++) 64 pRoot->maLookthru[i] = KRB_NULL; 65 65 #endif 66 66 67 67 cEntries = 1; 68 apEntries[0] = K AVL_GET_POINTER(&pRoot->mpRoot);68 apEntries[0] = KRB_GET_POINTER(&pRoot->mpRoot); 69 69 while (cEntries > 0) 70 70 { … … 72 72 * Process the subtrees first. 73 73 */ 74 K AVLNODE *pNode = apEntries[cEntries - 1];75 if (pNode->mpLeft != K AVL_NULL)76 apEntries[cEntries++] = K AVL_GET_POINTER(&pNode->mpLeft);77 else if (pNode->mpRight != K AVL_NULL)78 apEntries[cEntries++] = K AVL_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); 79 79 else 80 80 { 81 #ifdef K AVL_EQUAL_ALLOWED81 #ifdef KRB_EQUAL_ALLOWED 82 82 /* 83 83 * Process nodes with the same key. 84 84 */ 85 while (pNode->pList != K AVL_NULL)85 while (pNode->pList != KRB_NULL) 86 86 { 87 K AVLNODE *pEqual = KAVL_GET_POINTER(&pNode->pList);88 K AVL_SET_POINTER(&pNode->pList, KAVL_GET_POINTER_NULL(&pEqual->pList));89 pEqual->pList = K AVL_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; 90 90 91 91 rc = pfnCallBack(pEqual, pvUser); 92 92 if (rc) 93 93 { 94 K AVL_WRITE_UNLOCK(pRoot);94 KRB_WRITE_UNLOCK(pRoot); 95 95 return rc; 96 96 } … … 103 103 if (--cEntries > 0) 104 104 { 105 K AVLNODE *pParent = apEntries[cEntries - 1];106 if (K AVL_GET_POINTER(&pParent->mpLeft) == pNode)107 pParent->mpLeft = K AVL_NULL;105 KRBNODE *pParent = apEntries[cEntries - 1]; 106 if (KRB_GET_POINTER(&pParent->mpLeft) == pNode) 107 pParent->mpLeft = KRB_NULL; 108 108 else 109 pParent->mpRight = K AVL_NULL;109 pParent->mpRight = KRB_NULL; 110 110 } 111 111 else 112 pRoot->mpRoot = K AVL_NULL;112 pRoot->mpRoot = KRB_NULL; 113 113 114 kHlpAssert(pNode->mpLeft == K AVL_NULL);115 kHlpAssert(pNode->mpRight == K AVL_NULL);114 kHlpAssert(pNode->mpLeft == KRB_NULL); 115 kHlpAssert(pNode->mpRight == KRB_NULL); 116 116 rc = pfnCallBack(pNode, pvUser); 117 117 if (rc) 118 118 { 119 K AVL_WRITE_UNLOCK(pRoot);119 KRB_WRITE_UNLOCK(pRoot); 120 120 return rc; 121 121 } 122 122 } 123 123 } /* while */ 124 kHlpAssert(pRoot->mpRoot == K AVL_NULL);124 kHlpAssert(pRoot->mpRoot == KRB_NULL); 125 125 126 K AVL_WRITE_UNLOCK(pRoot);126 KRB_WRITE_UNLOCK(pRoot); 127 127 return 0; 128 128 } -
trunk/include/k/kRbTmpl/kRbDoWithAll.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k AvlTmpl - Templated AVLTrees, The Callback Iterator.3 * kRbTmpl - Templated Red-Black Trees, The Callback Iterator. 4 4 */ 5 5 … … 38 38 { 39 39 unsigned cEntries; 40 K AVLNODE *aEntries[KAVL_MAX_STACK];41 char achFlags[K AVL_MAX_STACK];42 K AVLROOT pRoot;43 } K AVL_INT(STACK2);40 KRBNODE *aEntries[KRB_MAX_STACK]; 41 char achFlags[KRB_MAX_STACK]; 42 KRBROOT pRoot; 43 } KRB_INT(STACK2); 44 44 45 45 … … 48 48 * 49 49 * @returns 0 on success. Return from callback on failure. 50 * @param pRoot Pointer to the AVL-treeroot structure.50 * @param pRoot Pointer to the Red-Back tree's root structure. 51 51 * @param fFromLeft K_TRUE: Left to right. 52 52 * K_FALSE: Right to left. … … 54 54 * @param pvUser User parameter passed on to the callback function. 55 55 */ 56 K AVL_DECL(int) KAVL_FN(DoWithAll)(KAVLROOT *pRoot, KBOOL fFromLeft, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)56 KRB_DECL(int) KRB_FN(DoWithAll)(KRBROOT *pRoot, KBOOL fFromLeft, KRB_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser) 57 57 { 58 K AVL_INT(STACK2) AVLStack;59 K AVLNODE*pNode;60 #ifdef K AVL_EQUAL_ALLOWED61 K AVLNODE*pEqual;58 KRB_INT(STACK2) Stack; 59 KRBNODE *pNode; 60 #ifdef KRB_EQUAL_ALLOWED 61 KRBNODE *pEqual; 62 62 #endif 63 63 int rc; 64 64 65 K AVL_READ_LOCK(pRoot);66 if (pRoot->mpRoot == K AVL_NULL)65 KRB_READ_LOCK(pRoot); 66 if (pRoot->mpRoot == KRB_NULL) 67 67 { 68 K AVL_READ_UNLOCK(pRoot);68 KRB_READ_UNLOCK(pRoot); 69 69 return 0; 70 70 } 71 71 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); 75 75 76 76 if (fFromLeft) 77 77 { /* from left */ 78 while ( AVLStack.cEntries > 0)78 while (Stack.cEntries > 0) 79 79 { 80 pNode = AVLStack.aEntries[AVLStack.cEntries - 1];80 pNode = Stack.aEntries[Stack.cEntries - 1]; 81 81 82 82 /* left */ 83 if (! AVLStack.achFlags[AVLStack.cEntries - 1]++)83 if (!Stack.achFlags[Stack.cEntries - 1]++) 84 84 { 85 if (pNode->mpLeft != K AVL_NULL)85 if (pNode->mpLeft != KRB_NULL) 86 86 { 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); 89 89 continue; 90 90 } … … 95 95 if (rc) 96 96 return rc; 97 #ifdef K AVL_EQUAL_ALLOWED98 if (pNode->mpList != K AVL_NULL)99 for (pEqual = K AVL_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)) 100 100 { 101 101 rc = pfnCallBack(pEqual, pvUser); 102 102 if (rc) 103 103 { 104 K AVL_READ_UNLOCK(pRoot);104 KRB_READ_UNLOCK(pRoot); 105 105 return rc; 106 106 } … … 109 109 110 110 /* right */ 111 AVLStack.cEntries--;112 if (pNode->mpRight != K AVL_NULL)111 Stack.cEntries--; 112 if (pNode->mpRight != KRB_NULL) 113 113 { 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); 116 116 } 117 117 } /* while */ … … 119 119 else 120 120 { /* from right */ 121 while ( AVLStack.cEntries > 0)121 while (Stack.cEntries > 0) 122 122 { 123 pNode = AVLStack.aEntries[AVLStack.cEntries - 1];123 pNode = Stack.aEntries[Stack.cEntries - 1]; 124 124 125 125 /* right */ 126 if (! AVLStack.achFlags[AVLStack.cEntries - 1]++)126 if (!Stack.achFlags[Stack.cEntries - 1]++) 127 127 { 128 if (pNode->mpRight != K AVL_NULL)128 if (pNode->mpRight != KRB_NULL) 129 129 { 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); 132 132 continue; 133 133 } … … 138 138 if (rc) 139 139 return rc; 140 #ifdef K AVL_EQUAL_ALLOWED141 if (pNode->mpList != K AVL_NULL)142 for (pEqual = K AVL_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)) 143 143 { 144 144 rc = pfnCallBack(pEqual, pvUser); 145 145 if (rc) 146 146 { 147 K AVL_READ_UNLOCK(pRoot);147 KRB_READ_UNLOCK(pRoot); 148 148 return rc; 149 149 } … … 152 152 153 153 /* left */ 154 AVLStack.cEntries--;155 if (pNode->mpLeft != K AVL_NULL)154 Stack.cEntries--; 155 if (pNode->mpLeft != KRB_NULL) 156 156 { 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); 159 159 } 160 160 } /* while */ 161 161 } 162 162 163 K AVL_READ_UNLOCK(pRoot);163 KRB_READ_UNLOCK(pRoot); 164 164 return 0; 165 165 } -
trunk/include/k/kRbTmpl/kRbEnum.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k AvlTmpl - Templated AVLTrees, Node Enumeration.3 * kRbTmpl - Templated Red-Black Trees, Node Enumeration. 4 4 */ 5 5 … … 38 38 * to do next. 39 39 */ 40 typedef struct K AVL_TYPE(,ENUMDATA)40 typedef struct KRB_TYPE(,ENUMDATA) 41 41 { 42 42 KBOOL fFromLeft; 43 43 KI8 cEntries; 44 KU8 achFlags[K AVL_MAX_STACK];45 K AVLNODE * aEntries[KAVL_MAX_STACK];46 } K AVL_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); 47 47 48 48 … … 50 50 * Ends an enumeration. 51 51 * 52 * The purpose of this function is to unlock the tree should the 53 * AVL implementation include locking. It's good practice to call54 * it anyway even ifthe 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. 55 55 * 56 56 * @param pEnumData Pointer to enumeration control data. 57 57 */ 58 K AVL_DECL(void) KAVL_FN(EndEnum)(KAVL_TYPE(,ENUMDATA) *pEnumData)58 KRB_DECL(void) KRB_FN(EndEnum)(KRB_TYPE(,ENUMDATA) *pEnumData) 59 59 { 60 K AVLROOT pRoot = pEnumData->pRoot;60 KRBROOT pRoot = pEnumData->pRoot; 61 61 pEnumData->pRoot = NULL; 62 62 if (pRoot) 63 K AVL_READ_UNLOCK(pEnumData->pRoot);63 KRB_READ_UNLOCK(pEnumData->pRoot); 64 64 } 65 65 … … 76 76 * @param pEnumData Pointer to enumeration control data. 77 77 */ 78 K AVL_DECL(KAVLNODE *) KAVL_FN(GetNext)(KAVL_TYPE(,ENUMDATA) *pEnumData)78 KRB_DECL(KRBNODE *) KRB_FN(GetNext)(KRB_TYPE(,ENUMDATA) *pEnumData) 79 79 { 80 80 if (pEnumData->fFromLeft) … … 82 82 while (pEnumData->cEntries > 0) 83 83 { 84 K AVLNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1];84 KRBNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1]; 85 85 86 86 /* left */ … … 88 88 { 89 89 pEnumData->achFlags[pEnumData->cEntries - 1]++; 90 if (pNode->mpLeft != K AVL_NULL)90 if (pNode->mpLeft != KRB_NULL) 91 91 { 92 92 pEnumData->achFlags[pEnumData->cEntries] = 0; /* 0 left, 1 center, 2 right */ 93 pEnumData->aEntries[pEnumData->cEntries++] = K AVL_GET_POINTER(&pNode->mpLeft);93 pEnumData->aEntries[pEnumData->cEntries++] = KRB_GET_POINTER(&pNode->mpLeft); 94 94 continue; 95 95 } … … 105 105 /* right */ 106 106 pEnumData->cEntries--; 107 if (pNode->mpRight != K AVL_NULL)107 if (pNode->mpRight != KRB_NULL) 108 108 { 109 109 pEnumData->achFlags[pEnumData->cEntries] = 0; 110 pEnumData->aEntries[pEnumData->cEntries++] = K AVL_GET_POINTER(&pNode->mpRight);110 pEnumData->aEntries[pEnumData->cEntries++] = KRB_GET_POINTER(&pNode->mpRight); 111 111 } 112 112 } /* while */ … … 116 116 while (pEnumData->cEntries > 0) 117 117 { 118 K AVLNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1];118 KRBNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1]; 119 119 120 120 /* right */ … … 122 122 { 123 123 pEnumData->achFlags[pEnumData->cEntries - 1]++; 124 if (pNode->mpRight != K AVL_NULL)124 if (pNode->mpRight != KRB_NULL) 125 125 { 126 126 pEnumData->achFlags[pEnumData->cEntries] = 0; /* 0 right, 1 center, 2 left */ 127 pEnumData->aEntries[pEnumData->cEntries++] = K AVL_GET_POINTER(&pNode->mpRight);127 pEnumData->aEntries[pEnumData->cEntries++] = KRB_GET_POINTER(&pNode->mpRight); 128 128 continue; 129 129 } … … 139 139 /* left */ 140 140 pEnumData->cEntries--; 141 if (pNode->mpLeft != K AVL_NULL)141 if (pNode->mpLeft != KRB_NULL) 142 142 { 143 143 pEnumData->achFlags[pEnumData->cEntries] = 0; 144 pEnumData->aEntries[pEnumData->cEntries++] = K AVL_GET_POINTER(&pNode->mpLeft);144 pEnumData->aEntries[pEnumData->cEntries++] = KRB_GET_POINTER(&pNode->mpLeft); 145 145 } 146 146 } /* while */ … … 150 150 * Call EndEnum. 151 151 */ 152 K AVL_FN(EndEnum)(pEnumData);152 KRB_FN(EndEnum)(pEnumData); 153 153 return NULL; 154 154 } … … 156 156 157 157 /** 158 * Starts an enumeration of all nodes in the given AVLtree.158 * Starts an enumeration of all nodes in the given tree. 159 159 * 160 160 * The current implementation of this function will not walk the mpList … … 164 164 * If NULL is returned the tree is empty calling EndEnum isn't 165 165 * strictly necessary (although it will do no harm). 166 * @param pRoot Pointer to the AVL-treeroot structure.166 * @param pRoot Pointer to the Red-Back tree's root structure. 167 167 * @param pEnumData Pointer to enumeration control data. 168 168 * @param fFromLeft K_TRUE: Left to right. 169 169 * K_FALSE: Right to left. 170 170 */ 171 K AVL_DECL(KAVLNODE *) KAVL_FN(BeginEnum)(KAVLROOT *pRoot, KAVL_TYPE(,ENUMDATA) *pEnumData, KBOOL fFromLeft)171 KRB_DECL(KRBNODE *) KRB_FN(BeginEnum)(KRBROOT *pRoot, KRB_TYPE(,ENUMDATA) *pEnumData, KBOOL fFromLeft) 172 172 { 173 K AVL_READ_LOCK(pRoot);173 KRB_READ_LOCK(pRoot); 174 174 pEnumData->pRoot = pRoot; 175 if (pRoot->mpRoot != K AVL_NULL)175 if (pRoot->mpRoot != KRB_NULL) 176 176 { 177 177 pEnumData->fFromLeft = fFromLeft; 178 178 pEnumData->cEntries = 1; 179 pEnumData->aEntries[0] = K AVL_GET_POINTER(pRoot->mpRoot);179 pEnumData->aEntries[0] = KRB_GET_POINTER(pRoot->mpRoot); 180 180 pEnumData->achFlags[0] = 0; 181 181 } … … 183 183 pEnumData->cEntries = 0; 184 184 185 return K AVL_FN(GetNext)(pEnumData);185 return KRB_FN(GetNext)(pEnumData); 186 186 } 187 187 -
trunk/include/k/kRbTmpl/kRbGet.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k AvlTmpl - Templated AVLTrees, Get a Node.3 * kRbTmpl - Templated Red-Black Trees, Get a Node. 4 4 */ 5 5 … … 33 33 * 34 34 * @returns Pointer to the node holding the given key. 35 * @param pRoot Pointer to the AVL-treeroot structure.35 * @param pRoot Pointer to the Red-Back tree's root structure. 36 36 * @param Key Key value of the node which is to be found. 37 37 */ 38 K AVL_DECL(KAVLNODE *) KAVL_FN(Get)(KAVLROOT *pRoot, KAVLKEY Key)38 KRB_DECL(KRBNODE *) KRB_FN(Get)(KRBROOT *pRoot, KRBKEY Key) 39 39 { 40 K AVLNODE *pNode;41 #ifdef K AVL_LOOKTHRU_CACHE42 K AVLTREEPTR *ppEntry;40 KRBNODE *pNode; 41 #ifdef KRB_CACHE_SIZE 42 KRBTREEPTR *ppEntry; 43 43 #endif 44 44 45 K AVL_READ_LOCK(pRoot);46 if (pRoot->mpRoot == K AVL_NULL)45 KRB_READ_LOCK(pRoot); 46 if (pRoot->mpRoot == KRB_NULL) 47 47 { 48 K AVL_READ_UNLOCK(pRoot);48 KRB_READ_UNLOCK(pRoot); 49 49 return NULL; 50 50 } 51 51 52 #ifdef K AVL_LOOKTHRU_CACHE53 ppEntry = &pRoot->maLookthru[K AVL_LOOKTHRU_HASH(Key)];54 pNode = K AVL_GET_POINTER_NULL(ppEntry);55 if (!pNode || K AVL_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)) 56 56 #endif 57 57 { 58 pNode = K AVL_GET_POINTER(&pRoot->mpRoot);59 while (K AVL_NE(pNode->mKey, Key))58 pNode = KRB_GET_POINTER(&pRoot->mpRoot); 59 while (KRB_CMP_NE(pNode->mKey, Key)) 60 60 { 61 if (K AVL_G(pNode->mKey, Key))61 if (KRB_CMP_G(pNode->mKey, Key)) 62 62 { 63 if (pNode->mpLeft == K AVL_NULL)63 if (pNode->mpLeft == KRB_NULL) 64 64 { 65 K AVL_READ_UNLOCK(pRoot);65 KRB_READ_UNLOCK(pRoot); 66 66 return NULL; 67 67 } 68 pNode = K AVL_GET_POINTER(&pNode->mpLeft);68 pNode = KRB_GET_POINTER(&pNode->mpLeft); 69 69 } 70 70 else 71 71 { 72 if (pNode->mpRight == K AVL_NULL)72 if (pNode->mpRight == KRB_NULL) 73 73 { 74 K AVL_READ_UNLOCK(pRoot);74 KRB_READ_UNLOCK(pRoot); 75 75 return NULL; 76 76 } 77 pNode = K AVL_GET_POINTER(&pNode->mpRight);77 pNode = KRB_GET_POINTER(&pNode->mpRight); 78 78 } 79 79 } 80 80 81 #ifdef K AVL_LOOKTHRU_CACHE82 K AVL_SET_POINTER(ppEntry, pNode);81 #ifdef KRB_CACHE_SIZE 82 KRB_SET_POINTER(ppEntry, pNode); 83 83 #endif 84 84 } 85 85 86 K AVL_READ_UNLOCK(pRoot);86 KRB_READ_UNLOCK(pRoot); 87 87 return pNode; 88 88 } -
trunk/include/k/kRbTmpl/kRbGetBestFit.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k AvlTmpl - Templated AVLTrees, Get Best Fitting Node.3 * kRbTmpl - Templated Red-Black Trees, Get Best Fitting Node. 4 4 */ 5 5 … … 33 33 * 34 34 * @returns Pointer to the best fitting node found. 35 * @param pRoot Pointer to the AVL-treeroot structure.35 * @param pRoot Pointer to the Red-Back tree's root structure. 36 36 * @param Key The Key of which is to be found a best fitting match for.. 37 37 * @param fAbove K_TRUE: Returned node is have the closest key to Key from above. … … 41 41 * <= (below): the node where you last turned right. 42 42 */ 43 K AVL_DECL(KAVLNODE *) KAVL_FN(GetBestFit)(KAVLROOT *pRoot, KAVLKEY Key, KBOOL fAbove)43 KRB_DECL(KRBNODE *) KRB_FN(GetBestFit)(KRBROOT *pRoot, KRBKEY Key, KBOOL fAbove) 44 44 { 45 register K AVLNODE *pNode;46 K AVLNODE *pNodeLast;45 register KRBNODE *pNode; 46 KRBNODE *pNodeLast; 47 47 48 K AVL_READ_LOCK(pLook);49 if (pRoot->mpRoot == K AVL_NULL)48 KRB_READ_LOCK(pLook); 49 if (pRoot->mpRoot == KRB_NULL) 50 50 { 51 K AVL_READ_UNLOCK(pLook);51 KRB_READ_UNLOCK(pLook); 52 52 return NULL; 53 53 } 54 54 55 pNode = K AVL_GET_POINTER(&pRoot->mpRoot);55 pNode = KRB_GET_POINTER(&pRoot->mpRoot); 56 56 pNodeLast = NULL; 57 57 if (fAbove) 58 58 { /* pNode->mKey >= Key */ 59 while (K AVL_NE(pNode->mKey, Key))59 while (KRB_CMP_NE(pNode->mKey, Key)) 60 60 { 61 if (K AVL_G(pNode->mKey, Key))61 if (KRB_CMP_G(pNode->mKey, Key)) 62 62 { 63 if (pNode->mpLeft == K AVL_NULL)63 if (pNode->mpLeft == KRB_NULL) 64 64 { 65 K AVL_READ_UNLOCK(pLook);65 KRB_READ_UNLOCK(pLook); 66 66 return pNode; 67 67 } 68 68 pNodeLast = pNode; 69 pNode = K AVL_GET_POINTER(&pNode->mpLeft);69 pNode = KRB_GET_POINTER(&pNode->mpLeft); 70 70 } 71 71 else 72 72 { 73 if (pNode->mpRight == K AVL_NULL)73 if (pNode->mpRight == KRB_NULL) 74 74 { 75 K AVL_READ_UNLOCK(pLook);75 KRB_READ_UNLOCK(pLook); 76 76 return pNodeLast; 77 77 } 78 pNode = K AVL_GET_POINTER(&pNode->mpRight);78 pNode = KRB_GET_POINTER(&pNode->mpRight); 79 79 } 80 80 } … … 82 82 else 83 83 { /* pNode->mKey <= Key */ 84 while (K AVL_NE(pNode->mKey, Key))84 while (KRB_CMP_NE(pNode->mKey, Key)) 85 85 { 86 if (K AVL_G(pNode->mKey, Key))86 if (KRB_CMP_G(pNode->mKey, Key)) 87 87 { 88 if (pNode->mpLeft == K AVL_NULL)88 if (pNode->mpLeft == KRB_NULL) 89 89 { 90 K AVL_READ_UNLOCK(pLook);90 KRB_READ_UNLOCK(pLook); 91 91 return pNodeLast; 92 92 } 93 pNode = K AVL_GET_POINTER(&pNode->mpLeft);93 pNode = KRB_GET_POINTER(&pNode->mpLeft); 94 94 } 95 95 else 96 96 { 97 if (pNode->mpRight == K AVL_NULL)97 if (pNode->mpRight == KRB_NULL) 98 98 { 99 K AVL_READ_UNLOCK(pLook);99 KRB_READ_UNLOCK(pLook); 100 100 return pNode; 101 101 } 102 102 pNodeLast = pNode; 103 pNode = K AVL_GET_POINTER(&pNode->mpRight);103 pNode = KRB_GET_POINTER(&pNode->mpRight); 104 104 } 105 105 } … … 107 107 108 108 /* perfect match or nothing. */ 109 K AVL_READ_UNLOCK(pLook);109 KRB_READ_UNLOCK(pLook); 110 110 return pNode; 111 111 } -
trunk/include/k/kRbTmpl/kRbGetWithParent.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k AvlTmpl - Templated AVLTrees, Get Node With Parent.3 * kRbTmpl - Templated Red-Black Trees, Get Node With Parent. 4 4 */ 5 5 … … 34 34 * 35 35 * @returns Pointer to the node holding the given key. 36 * @param pRoot Pointer to the AVL-treeroot structure.36 * @param pRoot Pointer to the Red-Back tree's root structure. 37 37 * @param ppParent Pointer to a variable which will hold the pointer to the partent node on 38 38 * return. When no node is found, this will hold the last searched node. 39 39 * @param Key Key value of the node which is to be found. 40 40 */ 41 K AVL_DECL(KAVLNODE *) KAVL_FN(GetWithParent)(KAVLROOT *pRoot, KAVLNODE **ppParent, KAVLKEY Key)41 KRB_DECL(KRBNODE *) KRB_FN(GetWithParent)(KRBROOT *pRoot, KRBNODE **ppParent, KRBKEY Key) 42 42 { 43 register K AVLNODE *pNode;44 register K AVLNODE *pParent;43 register KRBNODE *pNode; 44 register KRBNODE *pParent; 45 45 46 K AVL_READ_LOCK(pRoot);46 KRB_READ_LOCK(pRoot); 47 47 48 48 pParent = NULL; 49 pNode = K AVL_GET_POINTER_NULL(&pRoot->mpRoot);49 pNode = KRB_GET_POINTER_NULL(&pRoot->mpRoot); 50 50 while ( pNode != NULL 51 && K AVL_NE(pNode->mKey, Key))51 && KRB_CMP_NE(pNode->mKey, Key)) 52 52 { 53 53 pParent = pNode; 54 if (K AVL_G(pNode->mKey, Key))55 pNode = K AVL_GET_POINTER_NULL(&pNode->mpLeft);54 if (KRB_CMP_G(pNode->mKey, Key)) 55 pNode = KRB_GET_POINTER_NULL(&pNode->mpLeft); 56 56 else 57 pNode = K AVL_GET_POINTER_NULL(&pNode->mpRight);57 pNode = KRB_GET_POINTER_NULL(&pNode->mpRight); 58 58 } 59 59 60 K AVL_UNLOCK(pRoot);60 KRB_READ_UNLOCK(pRoot); 61 61 62 62 *ppParent = pParent; -
trunk/include/k/kRbTmpl/kRbRemove2.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k AvlTmpl - Templated AVLTrees, Remove A Specific Node.3 * kRbTmpl - Templated Red-Black Trees, Remove A Specific Node. 4 4 */ 5 5 … … 33 33 * 34 34 * @returns Pointer to the removed node (NULL if not in the tree) 35 * @param pRoot Pointer to the AVL-treeroot structure.35 * @param pRoot Pointer to the Red-Back tree's root structure. 36 36 * @param Key The Key of which is to be found a best fitting match for.. 37 37 * … … 39 39 * easier to manage. 40 40 */ 41 K AVL_DECL(KAVLNODE *) KAVL_FN(Remove2)(KAVLTROOT *pRoot, KAVLNODE *pNode)41 KRB_DECL(KRBNODE *) KRB_FN(Remove2)(KRBROOT *pRoot, KRBNODE *pNode) 42 42 { 43 #ifdef K AVL_EQUAL_ALLOWED43 #ifdef KRB_EQUAL_ALLOWED 44 44 /* 45 45 * Find the right node by key and see if it's what we want. 46 46 */ 47 K AVLNODE *pParent;48 K AVLNODE *pCurNode = KAVL_FN(GetWithParent)(pRoot, pNode->mKey, &pParent);47 KRBNODE *pParent; 48 KRBNODE *pCurNode = KRB_FN(GetWithParent)(pRoot, pNode->mKey, &pParent); 49 49 if (!pCurNode) 50 50 return NULL; 51 K AVL_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. */ 52 52 if (pCurNode != pNode) 53 53 { … … 55 55 * It's not the one we want, but it could be in the duplicate list. 56 56 */ 57 while (pCurNode->mpList != K AVL_NULL)57 while (pCurNode->mpList != KRB_NULL) 58 58 { 59 K AVLNODE *pNext = KAVL_GET_POINTER(&pCurNode->mpList);59 KRBNODE *pNext = KRB_GET_POINTER(&pCurNode->mpList); 60 60 if (pNext == pNode) 61 61 { 62 K AVL_SET_POINTER_NULL(&pCurNode->mpList, KAVL_GET_POINTER_NULL(&pNode->mpList));63 pNode->mpList = K AVL_NULL;64 K AVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);65 K AVL_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); 66 66 return pNode; 67 67 } 68 68 pCurNode = pNext; 69 69 } 70 K AVL_WRITE_UNLOCK(pRoot);70 KRB_WRITE_UNLOCK(pRoot); 71 71 return NULL; 72 72 } … … 79 79 * insert the first duplicate in our place. 80 80 */ 81 if (pNode->mpList == K AVL_NODE)81 if (pNode->mpList == KRB_NULL) 82 82 { 83 K AVL_WRITE_UNLOCK(pRoot);84 K AVL_FN(Remove)(pRoot, pNode->mKey);83 KRB_WRITE_UNLOCK(pRoot); 84 KRB_FN(Remove)(pRoot, pNode->mKey); 85 85 } 86 86 else 87 87 { 88 K AVLNODE *pNewUs = KAVL_GET_POINTER(&pNode->mpList);88 KRBNODE *pNewUs = KRB_GET_POINTER(&pNode->mpList); 89 89 90 90 pNewUs->mHeight = pNode->mHeight; 91 91 92 if (pNode->mpLeft != K AVL_NULL)93 K AVL_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)) 94 94 else 95 pNewUs->mpLeft = K AVL_NULL;95 pNewUs->mpLeft = KRB_NULL; 96 96 97 if (pNode->mpRight != K AVL_NULL)98 K AVL_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)) 99 99 else 100 pNewUs->mpRight = K AVL_NULL;100 pNewUs->mpRight = KRB_NULL; 101 101 102 102 if (pParent) 103 103 { 104 if (K AVL_GET_POINTER_NULL(&pParent->mpLeft) == pNode)105 K AVL_SET_POINTER(&pParent->mpLeft, pNewUs);104 if (KRB_GET_POINTER_NULL(&pParent->mpLeft) == pNode) 105 KRB_SET_POINTER(&pParent->mpLeft, pNewUs); 106 106 else 107 K AVL_SET_POINTER(&pParent->mpRight, pNewUs);107 KRB_SET_POINTER(&pParent->mpRight, pNewUs); 108 108 } 109 109 else 110 K AVL_SET_POINTER(&pRoot->mpRoot, pNewUs);110 KRB_SET_POINTER(&pRoot->mpRoot, pNewUs); 111 111 112 K AVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);113 K AVL_WRITE_UNLOCK(pRoot);112 KRB_CACHE_INVALIDATE_NODE(pRoot, pNode, pNode->mKey); 113 KRB_WRITE_UNLOCK(pRoot); 114 114 } 115 115 … … 123 123 * of wrong nodes but just uses this API for his convenience. 124 124 */ 125 K AVLNODE *pRemovedNode = KAVL_FN(Remove)(pRoot, pNode->mKey);125 KRBNODE *pRemovedNode = KRB_FN(Remove)(pRoot, pNode->mKey); 126 126 if (pRemovedNode == pNode) 127 127 return pRemovedNode; 128 128 129 K AVL_FN(Insert)(pRoot, pRemovedNode);129 KRB_FN(Insert)(pRoot, pRemovedNode); 130 130 return NULL; 131 131 #endif -
trunk/include/k/kRbTmpl/kRbRemoveBestFit.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k AvlTmpl - Templated AVLTrees, Remove Best Fitting Node.3 * kRbTmpl - Templated Red-Black Trees, Remove Best Fitting Node. 4 4 */ 5 5 … … 33 33 * 34 34 * @returns Pointer to the removed node. 35 * @param pRoot Pointer to the AVL-treeroot structure.35 * @param pRoot Pointer to the Red-Back tree's root structure. 36 36 * @param Key The Key of which is to be found a best fitting match for.. 37 37 * @param fAbove K_TRUE: Returned node is have the closest key to Key from above. … … 42 42 * code size, and the likelyhood for bugs. 43 43 */ 44 K AVL_DECL(KAVLNODE *) KAVL_FN(RemoveBestFit)(KAVLROOT *pRoot, KAVLKEY Key, KBOOL fAbove)44 KRB_DECL(KRBNODE *) KRB_FN(RemoveBestFit)(KRBROOT *pRoot, KRBKEY Key, KBOOL fAbove) 45 45 { 46 46 /* … … 49 49 * removing the in-tree node as this is way cheaper. 50 50 */ 51 K AVLNODE *pNode = KAVL_FN(GetBestFit)(pRoot, Key, fAbove);51 KRBNODE *pNode = KRB_FN(GetBestFit)(pRoot, Key, fAbove); 52 52 if (pNode != NULL) 53 53 { 54 #ifdef K AVL_EQUAL_ALLOWED55 K AVL_WRITE_LOCK(pRoot); /** @todo the locking isn't quite sane here. :-/ */56 if (pNode->mpList != K AVL_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) 57 57 { 58 K AVLNODE *pRet = KAVL_GET_POINTER(&pNode->mpList);59 K AVL_SET_POINTER_NULL(&pNode->mpList, &pRet->mpList);60 K AVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);61 K AVL_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); 62 62 return pRet; 63 63 } 64 K AVL_WRITE_UNLOCK(pRoot);64 KRB_WRITE_UNLOCK(pRoot); 65 65 #endif 66 pNode = K AVL_FN(Remove)(pRoot, pNode->mKey);66 pNode = KRB_FN(Remove)(pRoot, pNode->mKey); 67 67 } 68 68 return pNode; -
trunk/include/k/kRbTmpl/kRbUndef.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k AvlTmpl - Undefines All Macros (both config and temp).3 * kRbTmpl - Undefines All Macros (both config and temp). 4 4 */ 5 5 6 6 /* 7 * Copyright (c) 2006-200 7Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>7 * Copyright (c) 2006-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net> 8 8 * 9 9 * Permission is hereby granted, free of charge, to any person … … 32 32 * The configuration. 33 33 */ 34 #undef K AVL_EQUAL_ALLOWED35 #undef K AVL_CHECK_FOR_EQUAL_INSERT36 #undef K AVL_MAX_STACK37 #undef K AVL_RANGE38 #undef K AVL_OFFSET39 #undef K AVL_STD_KEY_COMP40 #undef K AVL_LOOKTHRU41 #undef K AVL_LOOKTHRU_HASH42 #undef K AVL_LOCKED43 #undef K AVL_WRITE_LOCK44 #undef K AVL_WRITE_UNLOCK45 #undef K AVL_READ_LOCK46 #undef K AVL_READ_UNLOCK47 #undef K AVLKEY48 #undef K AVLNODE49 #undef K AVLTREEPTR50 #undef K AVLROOT51 #undef K AVL_FN52 #undef K AVL_TYPE53 #undef K AVL_INT54 #undef K AVL_DECL34 #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 55 55 #undef mKey 56 56 #undef mKeyLast 57 #undef m Height57 #undef mfIsRed 58 58 #undef mpLeft 59 59 #undef mpRight … … 61 61 #undef mpRoot 62 62 #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 63 69 64 70 /* 65 * The internal macros.71 * Internal ones. 66 72 */ 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 79 79 -
trunk/include/k/kRbU32.h
r33 r35 1 1 /* $Id$ */ 2 2 /** @file 3 * k Avl - AVLTree Implementation, KU32 keys.3 * kRb - Red-Black Tree Implementation, KU32 keys. 4 4 */ 5 5 6 6 /* 7 * Copyright (c) 2006-200 7Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>7 * Copyright (c) 2006-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net> 8 8 * 9 9 * Permission is hereby granted, free of charge, to any person … … 29 29 */ 30 30 31 #ifndef ___k_k AvlU32_h___32 #define ___k_k AvlU32_h___31 #ifndef ___k_kRbU32_h___ 32 #define ___k_kRbU32_h___ 33 33 34 typedef struct K AVLU3234 typedef struct KRBU32 35 35 { 36 36 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; 41 typedef KRBU32 *PRBU32; 42 typedef KRBU32 **PPRBU32; 41 43 42 /*#define K AVL_EQUAL_ALLOWED*/43 #define K AVL_CHECK_FOR_EQUAL_INSERT44 #define KAVL_MAX_STACK 32 45 /*#define K AVL_RANGE*/46 /*#define KAVL_OFFSET */ 47 #define K AVL_STD_KEY_COMP48 #define K AVLKEYKU3249 #define K AVLNODE KAVLU3250 #define K AVL_FN(name) kAvlU32 ## name51 #define K AVL_TYPE(prefix,name) prefix ## KAVLU32 ## name52 #define K AVL_INT(name) KAVLU32INT ## name53 #define K AVL_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) 54 56 55 #include <k/k AvlTmpl/kAvlBase.h>56 #include <k/k AvlTmpl/kAvlDoWithAll.h>57 #include <k/k AvlTmpl/kAvlEnum.h>58 #include <k/k AvlTmpl/kAvlGet.h>59 #include <k/k AvlTmpl/kAvlGetBestFit.h>60 #include <k/k AvlTmpl/kAvlGetWithParent.h>61 #include <k/k AvlTmpl/kAvlRemove2.h>62 #include <k/k AvlTmpl/kAvlRemoveBestFit.h>63 #include <k/k AvlTmpl/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> 64 66 65 67 #endif
Note:
See TracChangeset
for help on using the changeset viewer.