Changeset 3562
- Timestamp:
- Aug 27, 2007, 12:31:38 AM (18 years ago)
- Location:
- trunk/kStuff/include/k
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kStuff/include/k/kAvlTmpl/kAvlBase.h
r3560 r3562 34 34 * \#define KAVL_EQUAL_ALLOWED 35 35 * Define this to tell us that equal keys are allowed. 36 * Then Equal keys will be put in a list pointed to by KAVLNODE CORE::pList.36 * Then Equal keys will be put in a list pointed to by KAVLNODE::pList. 37 37 * This is by default not defined. 38 38 * … … 65 65 * three are only required when KAVL_RANGE is defined. 66 66 * 67 * \#define KAVLNODECORE 68 * Define this to the name (typedef) of the AVL core node structure. This 69 * structure must have a pLeft, pRight, Key and uchHeight member. 70 * If KAVL_RANGE is defined a KeyLast is also required. 71 * If KAVL_EQUAL_ALLOWED is defined a pList member is required. 67 * \#define KAVLNODE 68 * Define this to the name (typedef) of the AVL node structure. This 69 * structure must have a mpLeft, mpRight, mKey and mHeight member. 70 * If KAVL_RANGE is defined a mKeyLast is also required. 71 * If KAVL_EQUAL_ALLOWED is defined a mpList member is required. 72 * It's possible to use other member names by redefining the names. 72 73 * 73 74 * \#define KAVLTREEPTR 74 75 * Define this to the name (typedef) of the tree pointer type. This is 75 76 * required when KAVL_OFFSET is defined. When not defined it defaults 76 * to KAVLNODE CORE*.77 * to KAVLNODE *. 77 78 * 78 79 * \#define KAVL_FN … … 92 93 * the instantiated template should have. For instance an inlined scope 93 94 * (private or public) should K_DECL_INLINE(rettype) here. 94 *95 * typedef KAVL_TYPE(PFN,CALLBACK)96 * kAVLDoWithAll requires KAVL_TYPE(PFN,CALLBACK) to be a function pointer97 * to a callback suitable for the DoWithAll method.98 *99 95 * 100 96 * This version of the kAVL tree offers the option of inlining the entire … … 115 111 * Defined Constants And Macros * 116 112 *******************************************************************************/ 117 #define KAVL_HEIGHTOF(pNode) (( unsigned char)((pNode) != NULL ? (pNode)->uchHeight : 0))113 #define KAVL_HEIGHTOF(pNode) ((KU8)((pNode) != NULL ? (pNode)->mHeight : 0)) 118 114 119 115 /** @def KAVL_GET_POINTER … … 157 153 158 154 #ifndef KAVLTREEPTR 159 # define KAVLTREEPTR KAVLNODE CORE*155 # define KAVLTREEPTR KAVLNODE * 160 156 #endif 161 157 162 158 #ifdef KAVL_OFFSET 163 # define KAVL_GET_POINTER(pp) ( (KAVLNODE CORE*)((KIPTR)(pp) + *(pp)) )159 # define KAVL_GET_POINTER(pp) ( (KAVLNODE *)((KIPTR)(pp) + *(pp)) ) 164 160 # define KAVL_GET_POINTER_NULL(pp) ( *(pp) != KAVL_NULL ? KAVL_GET_POINTER(pp) : NULL ) 165 161 # define KAVL_SET_POINTER(pp, p) ( (*(pp)) = ((KIPTR)(p) - (KIPTR)(pp)) ) … … 203 199 * Structures and Typedefs * 204 200 *******************************************************************************/ 205 /* 206 * Two stacks that's used to avoid recursive calls.201 /** 202 * Stack used to avoid recursive calls during insert and removal. 207 203 */ 208 204 typedef struct … … 211 207 KAVLTREEPTR *aEntries[KAVL_MAX_STACK]; 212 208 } KAVL_INT(STACK); 213 214 typedef struct215 {216 unsigned cEntries;217 KAVLNODECORE *aEntries[KAVL_MAX_STACK];218 char achFlags[KAVL_MAX_STACK];219 } KAVL_INT(STACK2);220 209 221 210 … … 275 264 { 276 265 KAVLTREEPTR *ppNode = pStack->aEntries[--pStack->cEntries]; 277 KAVLNODE CORE*pNode = KAVL_GET_POINTER(ppNode);278 KAVLNODE CORE *pLeftNode = KAVL_GET_POINTER_NULL(&pNode->pLeft);279 unsigned char uchLeftHeight = KAVL_HEIGHTOF(pLeftNode);280 KAVLNODE CORE *pRightNode = KAVL_GET_POINTER_NULL(&pNode->pRight);281 unsigned char uchRightHeight = KAVL_HEIGHTOF(pRightNode);282 283 if (u chRightHeight + 1 < uchLeftHeight)266 KAVLNODE *pNode = KAVL_GET_POINTER(ppNode); 267 KAVLNODE *pLeftNode = KAVL_GET_POINTER_NULL(&pNode->mpLeft); 268 KU8 uLeftHeight = KAVL_HEIGHTOF(pLeftNode); 269 KAVLNODE *pRightNode = KAVL_GET_POINTER_NULL(&pNode->mpRight); 270 KU8 uRightHeight = KAVL_HEIGHTOF(pRightNode); 271 272 if (uRightHeight + 1 < uLeftHeight) 284 273 { 285 KAVLNODE CORE *pLeftLeftNode = KAVL_GET_POINTER_NULL(&pLeftNode->pLeft);286 KAVLNODE CORE *pLeftRightNode = KAVL_GET_POINTER_NULL(&pLeftNode->pRight);287 unsigned char uchLeftRightHeight = KAVL_HEIGHTOF(pLeftRightNode);288 289 if (KAVL_HEIGHTOF(pLeftLeftNode) >= u chLeftRightHeight)274 KAVLNODE *pLeftLeftNode = KAVL_GET_POINTER_NULL(&pLeftNode->mpLeft); 275 KAVLNODE *pLeftRightNode = KAVL_GET_POINTER_NULL(&pLeftNode->mpRight); 276 KU8 uLeftRightHeight = KAVL_HEIGHTOF(pLeftRightNode); 277 278 if (KAVL_HEIGHTOF(pLeftLeftNode) >= uLeftRightHeight) 290 279 { 291 KAVL_SET_POINTER_NULL(&pNode-> pLeft, &pLeftNode->pRight);292 KAVL_SET_POINTER(&pLeftNode-> pRight, pNode);293 pLeftNode-> uchHeight = (unsigned char)(1 + (pNode->uchHeight = (unsigned char)(1 + uchLeftRightHeight)));280 KAVL_SET_POINTER_NULL(&pNode->mpLeft, &pLeftNode->mpRight); 281 KAVL_SET_POINTER(&pLeftNode->mpRight, pNode); 282 pLeftNode->mHeight = (KU8)(1 + (pNode->mHeight = (KU8)(1 + uLeftRightHeight))); 294 283 KAVL_SET_POINTER(ppNode, pLeftNode); 295 284 } 296 285 else 297 286 { 298 KAVL_SET_POINTER_NULL(&pLeftNode-> pRight, &pLeftRightNode->pLeft);299 KAVL_SET_POINTER_NULL(&pNode-> pLeft, &pLeftRightNode->pRight);300 KAVL_SET_POINTER(&pLeftRightNode-> pLeft, pLeftNode);301 KAVL_SET_POINTER(&pLeftRightNode-> pRight, pNode);302 pLeftNode-> uchHeight = pNode->uchHeight = uchLeftRightHeight;303 pLeftRightNode-> uchHeight = uchLeftHeight;287 KAVL_SET_POINTER_NULL(&pLeftNode->mpRight, &pLeftRightNode->mpLeft); 288 KAVL_SET_POINTER_NULL(&pNode->mpLeft, &pLeftRightNode->mpRight); 289 KAVL_SET_POINTER(&pLeftRightNode->mpLeft, pLeftNode); 290 KAVL_SET_POINTER(&pLeftRightNode->mpRight, pNode); 291 pLeftNode->mHeight = pNode->mHeight = uLeftRightHeight; 292 pLeftRightNode->mHeight = uLeftHeight; 304 293 KAVL_SET_POINTER(ppNode, pLeftRightNode); 305 294 } 306 295 } 307 else if (u chLeftHeight + 1 < uchRightHeight)296 else if (uLeftHeight + 1 < uRightHeight) 308 297 { 309 KAVLNODE CORE *pRightLeftNode = KAVL_GET_POINTER_NULL(&pRightNode->pLeft);310 unsigned char uchRightLeftHeight = KAVL_HEIGHTOF(pRightLeftNode);311 KAVLNODE CORE *pRightRightNode = KAVL_GET_POINTER_NULL(&pRightNode->pRight);312 313 if (KAVL_HEIGHTOF(pRightRightNode) >= u chRightLeftHeight)298 KAVLNODE *pRightLeftNode = KAVL_GET_POINTER_NULL(&pRightNode->mpLeft); 299 KU8 uRightLeftHeight = KAVL_HEIGHTOF(pRightLeftNode); 300 KAVLNODE *pRightRightNode = KAVL_GET_POINTER_NULL(&pRightNode->mpRight); 301 302 if (KAVL_HEIGHTOF(pRightRightNode) >= uRightLeftHeight) 314 303 { 315 KAVL_SET_POINTER_NULL(&pNode-> pRight, &pRightNode->pLeft);316 KAVL_SET_POINTER(&pRightNode-> pLeft, pNode);317 pRightNode-> uchHeight = (unsigned char)(1 + (pNode->uchHeight = (unsigned char)(1 + uchRightLeftHeight)));304 KAVL_SET_POINTER_NULL(&pNode->mpRight, &pRightNode->mpLeft); 305 KAVL_SET_POINTER(&pRightNode->mpLeft, pNode); 306 pRightNode->mHeight = (KU8)(1 + (pNode->mHeight = (KU8)(1 + uRightLeftHeight))); 318 307 KAVL_SET_POINTER(ppNode, pRightNode); 319 308 } 320 309 else 321 310 { 322 KAVL_SET_POINTER_NULL(&pRightNode-> pLeft, &pRightLeftNode->pRight);323 KAVL_SET_POINTER_NULL(&pNode-> pRight, &pRightLeftNode->pLeft);324 KAVL_SET_POINTER(&pRightLeftNode-> pRight, pRightNode);325 KAVL_SET_POINTER(&pRightLeftNode-> pLeft, pNode);326 pRightNode-> uchHeight = pNode->uchHeight = uchRightLeftHeight;327 pRightLeftNode-> uchHeight = uchRightHeight;311 KAVL_SET_POINTER_NULL(&pRightNode->mpLeft, &pRightLeftNode->mpRight); 312 KAVL_SET_POINTER_NULL(&pNode->mpRight, &pRightLeftNode->mpLeft); 313 KAVL_SET_POINTER(&pRightLeftNode->mpRight, pRightNode); 314 KAVL_SET_POINTER(&pRightLeftNode->mpLeft, pNode); 315 pRightNode->mHeight = pNode->mHeight = uRightLeftHeight; 316 pRightLeftNode->mHeight = uRightHeight; 328 317 KAVL_SET_POINTER(ppNode, pRightLeftNode); 329 318 } … … 331 320 else 332 321 { 333 register unsigned char uchHeight = (unsigned char)(K_MAX(uchLeftHeight, uchRightHeight) + 1);334 if (u chHeight == pNode->uchHeight)322 KU8 uHeight = (KU8)(K_MAX(uLeftHeight, uRightHeight) + 1); 323 if (uHeight == pNode->mHeight) 335 324 break; 336 pNode-> uchHeight = uchHeight;325 pNode->mHeight = uHeight; 337 326 } 338 327 } … … 359 348 * Rebalance the tree. 360 349 */ 361 KAVL_DECL(KBOOL) KAVL_FN(Insert)(KAVLTREEPTR *ppTree, KAVLNODE CORE*pNode)350 KAVL_DECL(KBOOL) KAVL_FN(Insert)(KAVLTREEPTR *ppTree, KAVLNODE *pNode) 362 351 { 363 KAVL_INT(STACK) 364 KAVLTREEPTR 365 register KAVLKEY Key = pNode->Key;352 KAVL_INT(STACK) AVLStack; 353 KAVLTREEPTR *ppCurNode = ppTree; 354 register KAVLKEY Key = pNode->mKey; 366 355 #ifdef KAVL_RANGE 367 register KAVLKEY KeyLast = pNode->KeyLast; 368 #endif 369 register KAVLNODECORE *pCurNode; 370 371 AVLStack.cEntries = 0; 356 register KAVLKEY KeyLast = pNode->mKeyLast; 357 #endif 372 358 373 359 #ifdef KAVL_RANGE 374 360 if (Key > KeyLast) 375 361 return false; 376 #else 377 K_NOREF(Key); 378 #endif 379 362 #endif 363 364 AVLStack.cEntries = 0; 380 365 while (*ppCurNode != KAVL_NULL) 381 366 { 382 pCurNode = KAVL_GET_POINTER(ppCurNode);367 register KAVLNODE *pCurNode = KAVL_GET_POINTER(ppCurNode); 383 368 384 369 kHlpAssert(AVLStack.cEntries < KAVL_MAX_STACK); 385 370 AVLStack.aEntries[AVLStack.cEntries++] = ppCurNode; 386 371 #ifdef KAVL_EQUAL_ALLOWED 387 if (KAVL_R_IS_IDENTICAL(pCurNode-> Key, Key, pCurNode->KeyLast, KeyLast))372 if (KAVL_R_IS_IDENTICAL(pCurNode->mKey, Key, pCurNode->mKeyLast, KeyLast)) 388 373 { 389 374 /* 390 375 * If equal then we'll use a list of equal nodes. 391 376 */ 392 pNode-> pLeft = pNode->pRight = KAVL_NULL;393 pNode-> uchHeight = 0;394 KAVL_SET_POINTER_NULL(&pNode-> pList, &pCurNode->pList);395 KAVL_SET_POINTER(&pCurNode-> pList, pNode);377 pNode->mpLeft = pNode->mpRight = KAVL_NULL; 378 pNode->mHeight = 0; 379 KAVL_SET_POINTER_NULL(&pNode->mpList, &pCurNode->mpList); 380 KAVL_SET_POINTER(&pCurNode->mpList, pNode); 396 381 return K_TRUE; 397 382 } 398 383 #endif 399 384 #ifdef KAVL_CHECK_FOR_EQUAL_INSERT 400 if (KAVL_R_IS_INTERSECTING(pCurNode-> Key, Key, pCurNode->KeyLast, KeyLast))385 if (KAVL_R_IS_INTERSECTING(pCurNode->mKey, Key, pCurNode->mKeyLast, KeyLast)) 401 386 return K_FALSE; 402 387 #endif 403 if (KAVL_G(pCurNode-> Key, Key))404 ppCurNode = &pCurNode-> pLeft;388 if (KAVL_G(pCurNode->mKey, Key)) 389 ppCurNode = &pCurNode->mpLeft; 405 390 else 406 ppCurNode = &pCurNode-> pRight;391 ppCurNode = &pCurNode->mpRight; 407 392 } 408 393 409 pNode-> pLeft = pNode->pRight = KAVL_NULL;394 pNode->mpLeft = pNode->mpRight = KAVL_NULL; 410 395 #ifdef KAVL_EQUAL_ALLOWED 411 pNode-> pList = KAVL_NULL;412 #endif 413 pNode-> uchHeight = 1;396 pNode->mpList = KAVL_NULL; 397 #endif 398 pNode->mHeight = 1; 414 399 KAVL_SET_POINTER(ppCurNode, pNode); 415 400 … … 458 443 * return pointer to the removed node (if found). 459 444 */ 460 KAVL_DECL(KAVLNODE CORE*) KAVL_FN(Remove)(KAVLTREEPTR *ppTree, KAVLKEY Key)445 KAVL_DECL(KAVLNODE *) KAVL_FN(Remove)(KAVLTREEPTR *ppTree, KAVLKEY Key) 461 446 { 462 KAVL_INT(STACK) 463 KAVLTREEPTR 464 register KAVLNODE CORE*pDeleteNode;447 KAVL_INT(STACK) AVLStack; 448 KAVLTREEPTR *ppDeleteNode = ppTree; 449 register KAVLNODE *pDeleteNode; 465 450 466 451 AVLStack.cEntries = 0; 467 468 452 for (;;) 469 453 { … … 474 458 kHlpAssert(AVLStack.cEntries < KAVL_MAX_STACK); 475 459 AVLStack.aEntries[AVLStack.cEntries++] = ppDeleteNode; 476 if (KAVL_E(pDeleteNode-> Key, Key))460 if (KAVL_E(pDeleteNode->mKey, Key)) 477 461 break; 478 462 479 if (KAVL_G(pDeleteNode-> Key, Key))480 ppDeleteNode = &pDeleteNode-> pLeft;463 if (KAVL_G(pDeleteNode->mKey, Key)) 464 ppDeleteNode = &pDeleteNode->mpLeft; 481 465 else 482 ppDeleteNode = &pDeleteNode-> pRight;466 ppDeleteNode = &pDeleteNode->mpRight; 483 467 } 484 468 485 if (pDeleteNode-> pLeft != KAVL_NULL)469 if (pDeleteNode->mpLeft != KAVL_NULL) 486 470 { 487 471 /* find the rightmost node in the left tree. */ 488 const unsigned 489 KAVLTREEPTR *ppLeftLeast = &pDeleteNode->pLeft;490 register KAVLNODE CORE*pLeftLeast = KAVL_GET_POINTER(ppLeftLeast);491 492 while (pLeftLeast-> pRight != KAVL_NULL)472 const unsigned iStackEntry = AVLStack.cEntries; 473 KAVLTREEPTR *ppLeftLeast = &pDeleteNode->mpLeft; 474 register KAVLNODE *pLeftLeast = KAVL_GET_POINTER(ppLeftLeast); 475 476 while (pLeftLeast->mpRight != KAVL_NULL) 493 477 { 494 478 kHlpAssert(AVLStack.cEntries < KAVL_MAX_STACK); 495 479 AVLStack.aEntries[AVLStack.cEntries++] = ppLeftLeast; 496 ppLeftLeast = &pLeftLeast-> pRight;480 ppLeftLeast = &pLeftLeast->mpRight; 497 481 pLeftLeast = KAVL_GET_POINTER(ppLeftLeast); 498 482 } 499 483 500 484 /* link out pLeftLeast */ 501 KAVL_SET_POINTER_NULL(ppLeftLeast, &pLeftLeast-> pLeft);485 KAVL_SET_POINTER_NULL(ppLeftLeast, &pLeftLeast->mpLeft); 502 486 503 487 /* link it in place of the delete node. */ 504 KAVL_SET_POINTER_NULL(&pLeftLeast-> pLeft, &pDeleteNode->pLeft);505 KAVL_SET_POINTER_NULL(&pLeftLeast-> pRight, &pDeleteNode->pRight);506 pLeftLeast-> uchHeight = pDeleteNode->uchHeight;488 KAVL_SET_POINTER_NULL(&pLeftLeast->mpLeft, &pDeleteNode->mpLeft); 489 KAVL_SET_POINTER_NULL(&pLeftLeast->mpRight, &pDeleteNode->mpRight); 490 pLeftLeast->mHeight = pDeleteNode->mHeight; 507 491 KAVL_SET_POINTER(ppDeleteNode, pLeftLeast); 508 AVLStack.aEntries[iStackEntry] = &pLeftLeast-> pLeft;492 AVLStack.aEntries[iStackEntry] = &pLeftLeast->mpLeft; 509 493 } 510 494 else 511 495 { 512 KAVL_SET_POINTER_NULL(ppDeleteNode, &pDeleteNode-> pRight);496 KAVL_SET_POINTER_NULL(ppDeleteNode, &pDeleteNode->mpRight); 513 497 AVLStack.cEntries--; 514 498 } -
trunk/kStuff/include/k/kAvlTmpl/kAvlDoWithAll.h
r3560 r3562 25 25 */ 26 26 27 /******************************************************************************* 28 * Structures and Typedefs * 29 *******************************************************************************/ 30 /** The callback typedef. */ 31 typedef int (* KAVL_TYPE(PFN,CALLBACK))(KAVLNODE *, void *); 32 33 /** 34 * Stack used by DoWithAll to avoid recusive function calls. 35 */ 36 typedef struct 37 { 38 unsigned cEntries; 39 KAVLNODE *aEntries[KAVL_MAX_STACK]; 40 char achFlags[KAVL_MAX_STACK]; 41 } KAVL_INT(STACK2); 42 27 43 28 44 /** … … 38 54 KAVL_DECL(int) KAVL_FN(DoWithAll)(KAVLTREEPTR *ppTree, KBOOL fFromLeft, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser) 39 55 { 40 KAVL_INT(STACK2) AVLStack;41 KAVLNODE CORE*pNode;42 unsignedrc;56 KAVL_INT(STACK2) AVLStack; 57 KAVLNODE *pNode; 58 int rc; 43 59 44 60 if (*ppTree == KAVL_NULL) … … 58 74 if (!AVLStack.achFlags[AVLStack.cEntries - 1]++) 59 75 { 60 if (pNode-> pLeft != KAVL_NULL)76 if (pNode->mpLeft != KAVL_NULL) 61 77 { 62 78 AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */ 63 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode-> pLeft);79 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft); 64 80 continue; 65 81 } … … 73 89 /* right */ 74 90 AVLStack.cEntries--; 75 if (pNode-> pRight != KAVL_NULL)91 if (pNode->mpRight != KAVL_NULL) 76 92 { 77 93 AVLStack.achFlags[AVLStack.cEntries] = 0; 78 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode-> pRight);94 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpRight); 79 95 } 80 96 } /* while */ … … 86 102 pNode = AVLStack.aEntries[AVLStack.cEntries - 1]; 87 103 88 89 104 /* right */ 90 105 if (!AVLStack.achFlags[AVLStack.cEntries - 1]++) 91 106 { 92 if (pNode-> pRight != KAVL_NULL)107 if (pNode->mpRight != KAVL_NULL) 93 108 { 94 109 AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */ 95 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode-> pRight);110 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpRight); 96 111 continue; 97 112 } … … 105 120 /* left */ 106 121 AVLStack.cEntries--; 107 if (pNode-> pLeft != KAVL_NULL)122 if (pNode->mpLeft != KAVL_NULL) 108 123 { 109 124 AVLStack.achFlags[AVLStack.cEntries] = 0; 110 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode-> pLeft);125 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft); 111 126 } 112 127 } /* while */ -
trunk/kStuff/include/k/kAvlTmpl/kAvlEnum.h
r3560 r3562 25 25 */ 26 26 27 /******************************************************************************* 28 * Structures and Typedefs * 29 *******************************************************************************/ 30 /** 31 * Enumeration control data. 32 * 33 * This is initialized by BeginEnum and used by GetNext to figure out what 34 * to do next. 35 */ 36 typedef struct KAVL_TYPE(,ENUMDATA) 37 { 38 KBOOL fFromLeft; 39 KI8 cEntries; 40 KU8 achFlags[KAVL_MAX_STACK]; 41 KAVLNODE * aEntries[KAVL_MAX_STACK]; 42 } KAVL_TYPE(,ENUMDATA), *KAVL_TYPE(P,ENUMDATA); 43 44 27 45 /** 28 46 * Get the next node in the tree enumeration. 29 * @returns Pointer to the first node in the tree. 30 * @param pEnumData Pointer to enumeration control data. 31 * @status completely implemented. 32 * @author knut st. osmundsen 47 * 48 * @returns Pointer to the first node in the tree. 49 * @param pEnumData Pointer to enumeration control data. 33 50 */ 34 KAVL_DECL(KAVLNODE CORE *) KAVL_FN(GetNextNode)(KAVL_TYPE(,ENUMDATA) *pEnumData)51 KAVL_DECL(KAVLNODE *) KAVL_FN(GetNext)(KAVL_TYPE(,ENUMDATA) *pEnumData) 35 52 { 36 53 if (pEnumData->fFromLeft) … … 38 55 while (pEnumData->cEntries > 0) 39 56 { 40 KAVLNODE CORE*pNode = pEnumData->aEntries[pEnumData->cEntries - 1];57 KAVLNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1]; 41 58 42 59 /* left */ … … 44 61 { 45 62 pEnumData->achFlags[pEnumData->cEntries - 1]++; 46 if (pNode-> pLeft != KAVL_NULL)63 if (pNode->mpLeft != KAVL_NULL) 47 64 { 48 65 pEnumData->achFlags[pEnumData->cEntries] = 0; /* 0 left, 1 center, 2 right */ 49 pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode-> pLeft);66 pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft); 50 67 continue; 51 68 } … … 61 78 /* right */ 62 79 pEnumData->cEntries--; 63 if (pNode-> pRight != KAVL_NULL)80 if (pNode->mpRight != KAVL_NULL) 64 81 { 65 82 pEnumData->achFlags[pEnumData->cEntries] = 0; 66 pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode-> pRight);83 pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpRight); 67 84 } 68 85 } /* while */ … … 72 89 while (pEnumData->cEntries > 0) 73 90 { 74 KAVLNODE CORE*pNode = pEnumData->aEntries[pEnumData->cEntries - 1];91 KAVLNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1]; 75 92 76 93 /* right */ … … 78 95 { 79 96 pEnumData->achFlags[pEnumData->cEntries - 1]++; 80 if (pNode-> pRight != KAVL_NULL)97 if (pNode->mpRight != KAVL_NULL) 81 98 { 82 99 pEnumData->achFlags[pEnumData->cEntries] = 0; /* 0 right, 1 center, 2 left */ 83 pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode-> pRight);100 pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpRight); 84 101 continue; 85 102 } … … 95 112 /* left */ 96 113 pEnumData->cEntries--; 97 if (pNode-> pLeft != KAVL_NULL)114 if (pNode->mpLeft != KAVL_NULL) 98 115 { 99 116 pEnumData->achFlags[pEnumData->cEntries] = 0; 100 pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode-> pLeft);117 pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft); 101 118 } 102 119 } /* while */ … … 116 133 * K_FALSE: Right to left. 117 134 */ 118 KAVL_DECL(KAVLNODE CORE *) KAVL_FN(BeginEnumTree)(KAVLTREEPTR *ppTree, KAVL_TYPE(,ENUMDATA) *pEnumData, KBOOL fFromLeft)135 KAVL_DECL(KAVLNODE *) KAVL_FN(BeginEnum)(KAVLTREEPTR *ppTree, KAVL_TYPE(,ENUMDATA) *pEnumData, KBOOL fFromLeft) 119 136 { 120 137 if (*ppTree != KAVL_NULL) … … 128 145 pEnumData->cEntries = 0; 129 146 130 return KAVL_FN(GetNext Node)(pEnumData);147 return KAVL_FN(GetNext)(pEnumData); 131 148 } 132 149 -
trunk/kStuff/include/k/kAvlTmpl/kAvlGet.h
r3560 r3562 33 33 * @param Key Key value of the node which is to be found. 34 34 */ 35 KAVL_DECL(KAVLNODE CORE*) KAVL_FN(Get)(KAVLTREEPTR *ppTree, KAVLKEY Key)35 KAVL_DECL(KAVLNODE *) KAVL_FN(Get)(KAVLTREEPTR *ppTree, KAVLKEY Key) 36 36 { 37 KAVLNODE CORE*pNode;37 KAVLNODE *pNode; 38 38 if (*ppTree == KAVL_NULL) 39 39 return NULL; 40 40 41 41 pNode = KAVL_GET_POINTER(ppTree); 42 while (KAVL_NE(pNode-> Key, Key))42 while (KAVL_NE(pNode->mKey, Key)) 43 43 { 44 if (KAVL_G(pNode-> Key, Key))44 if (KAVL_G(pNode->mKey, Key)) 45 45 { 46 if (pNode-> pLeft == KAVL_NULL)46 if (pNode->mpLeft == KAVL_NULL) 47 47 return NULL; 48 pNode = KAVL_GET_POINTER(&pNode-> pLeft);48 pNode = KAVL_GET_POINTER(&pNode->mpLeft); 49 49 } 50 50 else 51 51 { 52 if (pNode-> pRight == KAVL_NULL)52 if (pNode->mpRight == KAVL_NULL) 53 53 return NULL; 54 pNode = KAVL_GET_POINTER(&pNode-> pRight);54 pNode = KAVL_GET_POINTER(&pNode->mpRight); 55 55 } 56 56 } -
trunk/kStuff/include/k/kAvlTmpl/kAvlGetBestFit.h
r3560 r3562 38 38 * <= (below): the node where you last turned right. 39 39 */ 40 KAVL_DECL(KAVLNODE CORE*) KAVL_FN(GetBestFit)(KAVLTREEPTR *ppTree, KAVLKEY Key, KBOOL fAbove)40 KAVL_DECL(KAVLNODE *) KAVL_FN(GetBestFit)(KAVLTREEPTR *ppTree, KAVLKEY Key, KBOOL fAbove) 41 41 { 42 register KAVLNODE CORE*pNode;43 KAVLNODE CORE*pNodeLast;42 register KAVLNODE *pNode; 43 KAVLNODE *pNodeLast; 44 44 45 45 if (*ppTree == KAVL_NULL) … … 49 49 pNodeLast = NULL; 50 50 if (fAbove) 51 { /* pNode-> Key >= Key */52 while (KAVL_NE(pNode-> Key, Key))51 { /* pNode->mKey >= Key */ 52 while (KAVL_NE(pNode->mKey, Key)) 53 53 { 54 if (KAVL_G(pNode-> Key, Key))54 if (KAVL_G(pNode->mKey, Key)) 55 55 { 56 if (pNode-> pLeft == KAVL_NULL)56 if (pNode->mpLeft == KAVL_NULL) 57 57 return pNode; 58 58 pNodeLast = pNode; 59 pNode = KAVL_GET_POINTER(&pNode-> pLeft);59 pNode = KAVL_GET_POINTER(&pNode->mpLeft); 60 60 } 61 61 else 62 62 { 63 if (pNode-> pRight == KAVL_NULL)63 if (pNode->mpRight == KAVL_NULL) 64 64 return pNodeLast; 65 pNode = KAVL_GET_POINTER(&pNode-> pRight);65 pNode = KAVL_GET_POINTER(&pNode->mpRight); 66 66 } 67 67 } 68 68 } 69 69 else 70 { /* pNode-> Key <= Key */71 while (KAVL_NE(pNode-> Key, Key))70 { /* pNode->mKey <= Key */ 71 while (KAVL_NE(pNode->mKey, Key)) 72 72 { 73 if (KAVL_G(pNode-> Key, Key))73 if (KAVL_G(pNode->mKey, Key)) 74 74 { 75 if (pNode-> pLeft == KAVL_NULL)75 if (pNode->mpLeft == KAVL_NULL) 76 76 return pNodeLast; 77 pNode = KAVL_GET_POINTER(&pNode-> pLeft);77 pNode = KAVL_GET_POINTER(&pNode->mpLeft); 78 78 } 79 79 else 80 80 { 81 if (pNode-> pRight == KAVL_NULL)81 if (pNode->mpRight == KAVL_NULL) 82 82 return pNode; 83 83 pNodeLast = pNode; 84 pNode = KAVL_GET_POINTER(&pNode-> pRight);84 pNode = KAVL_GET_POINTER(&pNode->mpRight); 85 85 } 86 86 } -
trunk/kStuff/include/k/kAvlTmpl/kAvlGetWithParent.h
r3560 r3562 36 36 * @param Key Key value of the node which is to be found. 37 37 */ 38 KAVL_DECL(KAVLNODE CORE *) KAVL_FN(GetWithParent)(KAVLTREEPTR *ppTree, KAVLNODECORE **ppParent, KAVLKEY Key)38 KAVL_DECL(KAVLNODE *) KAVL_FN(GetWithParent)(KAVLTREEPTR *ppTree, KAVLNODE **ppParent, KAVLKEY Key) 39 39 { 40 register KAVLNODE CORE*pNode = KAVL_GET_POINTER_NULL(ppTree);41 register KAVLNODE CORE*pParent = NULL;40 register KAVLNODE *pNode = KAVL_GET_POINTER_NULL(ppTree); 41 register KAVLNODE *pParent = NULL; 42 42 43 43 while ( pNode != NULL 44 && KAVL_NE(pNode-> Key, Key))44 && KAVL_NE(pNode->mKey, Key)) 45 45 { 46 46 pParent = pNode; 47 if (KAVL_G(pNode-> Key, Key))48 pNode = KAVL_GET_POINTER_NULL(&pNode-> pLeft);47 if (KAVL_G(pNode->mKey, Key)) 48 pNode = KAVL_GET_POINTER_NULL(&pNode->mpLeft); 49 49 else 50 pNode = KAVL_GET_POINTER_NULL(&pNode-> pRight);50 pNode = KAVL_GET_POINTER_NULL(&pNode->mpRight); 51 51 } 52 52 -
trunk/kStuff/include/k/kAvlTmpl/kAvlRemove2.h
r3560 r3562 36 36 * easier to manage. 37 37 */ 38 KAVL_DECL(KAVLNODE CORE *) KAVL_FN(Remove2)(KAVLTREEPTR *ppTree, KAVLNODECORE *pNode)38 KAVL_DECL(KAVLNODE *) KAVL_FN(Remove2)(KAVLTREEPTR *ppTree, KAVLNODE *pNode) 39 39 { 40 40 #ifdef KAVL_EQUAL_ALLOWED … … 42 42 * Find the right node by key and see if it's what we want. 43 43 */ 44 KAVLNODE CORE*pParent;45 KAVLNODE CORE *pCurNode = KAVL_FN(GetWithParent)(ppTree, pNode->Key, &pParent);44 KAVLNODE *pParent; 45 KAVLNODE *pCurNode = KAVL_FN(GetWithParent)(ppTree, pNode->mKey, &pParent); 46 46 if (!pCurNode) 47 47 return NULL; … … 51 51 * It's not the one we want, but it could be in the duplicate list. 52 52 */ 53 while (pCurNode-> pList != KAVL_NULL)53 while (pCurNode->mpList != KAVL_NULL) 54 54 { 55 KAVLNODE CORE *pNext = KAVL_GET_POINTER(&pCurNode->pList);55 KAVLNODE *pNext = KAVL_GET_POINTER(&pCurNode->mpList); 56 56 if (pNext == pNode) 57 57 { 58 KAVL_SET_POINTER_NULL(&pCurNode-> pList, KAVL_GET_POINTER_NULL(&pNode->pList));59 pNode-> pList = KAVL_NULL;58 KAVL_SET_POINTER_NULL(&pCurNode->mpList, KAVL_GET_POINTER_NULL(&pNode->mpList)); 59 pNode->mpList = KAVL_NULL; 60 60 return pNode; 61 61 } … … 72 72 * insert the first duplicate in our place. 73 73 */ 74 if (pNode-> pList == KAVL_NODE)75 KAVL_FN(Remove)(ppTree, pNode-> Key);74 if (pNode->mpList == KAVL_NODE) 75 KAVL_FN(Remove)(ppTree, pNode->mKey); 76 76 else 77 77 { 78 KAVLNODE CORE *pNewUs = KAVL_GET_POINTER(&pNode->pList);78 KAVLNODE *pNewUs = KAVL_GET_POINTER(&pNode->mpList); 79 79 80 pNewUs-> uchHeight = pNode->uchHeight;80 pNewUs->mHeight = pNode->mHeight; 81 81 82 if (pNode-> pLeft != KAVL_NULL)83 KAVL_SET_POINTER(&pNewUs-> pLeft, KAVL_GET_POINTER(&pNode->pLeft))82 if (pNode->mpLeft != KAVL_NULL) 83 KAVL_SET_POINTER(&pNewUs->mpLeft, KAVL_GET_POINTER(&pNode->mpLeft)) 84 84 else 85 pNewUs-> pLeft = KAVL_NULL;85 pNewUs->mpLeft = KAVL_NULL; 86 86 87 if (pNode-> pRight != KAVL_NULL)88 KAVL_SET_POINTER(&pNewUs-> pRight, KAVL_GET_POINTER(&pNode->pRight))87 if (pNode->mpRight != KAVL_NULL) 88 KAVL_SET_POINTER(&pNewUs->mpRight, KAVL_GET_POINTER(&pNode->mpRight)) 89 89 else 90 pNewUs-> pRight = KAVL_NULL;90 pNewUs->mpRight = KAVL_NULL; 91 91 92 92 if (pParent) 93 93 { 94 if (KAVL_GET_POINTER_NULL(&pParent-> pLeft) == pNode)95 KAVL_SET_POINTER(&pParent-> pLeft, pNewUs);94 if (KAVL_GET_POINTER_NULL(&pParent->mpLeft) == pNode) 95 KAVL_SET_POINTER(&pParent->mpLeft, pNewUs); 96 96 else 97 KAVL_SET_POINTER(&pParent-> pRight, pNewUs);97 KAVL_SET_POINTER(&pParent->mpRight, pNewUs); 98 98 } 99 99 else … … 109 109 * of wrong nodes but just uses this API for his convenience. 110 110 */ 111 KAVLNODE CORE *pRemovedNode = KAVL_FN(Remove)(ppTree, pNode->Key);111 KAVLNODE *pRemovedNode = KAVL_FN(Remove)(ppTree, pNode->mKey); 112 112 if (pRemovedNode == pNode) 113 113 return pRemovedNode; -
trunk/kStuff/include/k/kAvlTmpl/kAvlRemoveBestFit.h
r3560 r3562 39 39 * code size, and the likelyhood for bugs. 40 40 */ 41 KAVL_DECL(KAVLNODE CORE*) KAVL_FN(RemoveBestFit)(KAVLTREEPTR *ppTree, KAVLKEY Key, KBOOL fAbove)41 KAVL_DECL(KAVLNODE *) KAVL_FN(RemoveBestFit)(KAVLTREEPTR *ppTree, KAVLKEY Key, KBOOL fAbove) 42 42 { 43 43 /* … … 46 46 * removing the in-tree node as this is way cheaper. 47 47 */ 48 KAVLNODE CORE*pNode = KAVL_FN(GetBestFit)(ppTree, Key, fAbove);48 KAVLNODE *pNode = KAVL_FN(GetBestFit)(ppTree, Key, fAbove); 49 49 if (pNode != NULL) 50 50 { 51 51 #ifdef KAVL_EQUAL_ALLOWED 52 if (pNode-> pList != KAVL_NULL)52 if (pNode->mpList != KAVL_NULL) 53 53 { 54 KAVL ANODECORE *pRet = KAVL_GET_POINTER(&pNode->pList);55 KAVL_SET_POINTER_NULL(&pNode-> pList, &pRet->pList);54 KAVLNODE *pRet = KAVL_GET_POINTER(&pNode->mpList); 55 KAVL_SET_POINTER_NULL(&pNode->mpList, &pRet->mpList); 56 56 return pRet; 57 57 } 58 58 #endif 59 pNode = KAVL_FN(Remove)(ppTree, pNode-> Key);59 pNode = KAVL_FN(Remove)(ppTree, pNode->mKey); 60 60 } 61 61 return pNode; -
trunk/kStuff/include/k/kAvlTmpl/kAvlUndef.h
r3560 r3562 35 35 #undef KAVL_STD_KEY_COMP 36 36 #undef KAVLKEY 37 #undef KAVLNODE CORE37 #undef KAVLNODE 38 38 #undef KAVLTREEPTR 39 39 #undef KAVL_FN … … 41 41 #undef KAVL_INT 42 42 #undef KAVL_DECL 43 #undef mKey 44 #undef mKeyLast 45 #undef mHeight 46 #undef mpLeft 47 #undef mpRight 48 #undef mpList 43 49 44 50 /* -
trunk/kStuff/include/k/kAvlU32.h
r3561 r3562 30 30 typedef struct KAVLU32 31 31 { 32 KU32 Key;33 KU8 uchHeight;34 struct KAVLU32 * pLeft;35 struct KAVLU32 * pRight;32 KU32 mKey; 33 KU8 mHeight; 34 struct KAVLU32 *mpLeft; 35 struct KAVLU32 *mpRight; 36 36 } KAVLU32, *PKAVLU32, **PPKAVLU32; 37 38 typedef struct39 {40 KBOOL fFromLeft;41 KI8 cEntries;42 KU8 achFlags[32];43 PKAVLU32 aEntries[32];44 } KAVLU32ENUMDATA, *PKAVLU32ENUMDATA;45 46 typedef int (* PFNKAVLU32CALLBACK)(KAVLU32 *, void *);47 37 48 38 /*#define KAVL_EQUAL_ALLOWED*/ … … 53 43 #define KAVL_STD_KEY_COMP 54 44 #define KAVLKEY KU32 55 #define KAVLNODE COREKAVLU3245 #define KAVLNODE KAVLU32 56 46 #define KAVL_FN(name) kAvlU32 ## name 57 47 #define KAVL_TYPE(prefix,name) prefix ## KAVLU32 ## name -
trunk/kStuff/include/k/kAvloU32.h
r3561 r3562 28 28 #define ___k_kAvloU32_h___ 29 29 30 typedef K U32 KAVLOU32PTR;30 typedef KI32 KAVLOU32PTR; 31 31 32 32 typedef struct KAVLOU32 33 33 { 34 KU32 Key;35 KU8 uchHeight;36 KAVLOU32PTR pLeft;37 KAVLOU32PTR pRight;34 KU32 u32; 35 KU8 cFloorsToGo; 36 KAVLOU32PTR offLeft; 37 KAVLOU32PTR offRight; 38 38 } KAVLOU32, *PKAVLOU32, **PPKAVLOU32; 39 39 40 typedef struct 41 { 42 KBOOL fFromLeft; 43 KI8 cEntries; 44 KU8 achFlags[32]; 45 PKAVLOU32 aEntries[32]; 46 } KAVLOU32ENUMDATA, *PKAVLOU32ENUMDATA; 47 48 typedef int (* PFNKAVLOU32CALLBACK)(KAVLOU32 *, void *); 40 #define mKey u32 41 #define mHeight cFloorsToGo 42 #define mpLeft offLeft 43 #define mpRight offRight 49 44 50 45 /*#define KAVL_EQUAL_ALLOWED*/ … … 56 51 #define KAVLKEY KU32 57 52 #define KAVLTREEPTR KAVLOU32PTR 58 #define KAVLNODE COREKAVLOU3253 #define KAVLNODE KAVLOU32 59 54 #define KAVL_FN(name) kAvloU32 ## name 60 55 #define KAVL_TYPE(prefix,name) prefix ## KAVLOU32 ## name -
trunk/kStuff/include/k/kAvlrU32.h
r3561 r3562 30 30 typedef struct KAVLRU32 31 31 { 32 KU32 Key;33 KU32 KeyLast;34 struct KAVLRU32 * pLeft;35 struct KAVLRU32 * pRight;36 KU8 uchHeight;32 KU32 u32Start; 33 KU32 u32Last; 34 struct KAVLRU32 *mpLeft; 35 struct KAVLRU32 *mpRight; 36 KU8 mHeight; 37 37 } KAVLRU32, *PKAVLRU32, **PPKAVLRU32; 38 38 39 typedef struct 40 { 41 KBOOL fFromLeft; 42 KI8 cEntries; 43 KU8 achFlags[32]; 44 PKAVLRU32 aEntries[32]; 45 } KAVLRU32ENUMDATA, *PKAVLRU32ENUMDATA; 46 47 typedef int (* PFNKAVLRU32CALLBACK)(KAVLRU32 *, void *); 39 #define mKey u32Start 40 #define mKeyLast u32Last 48 41 49 42 /*#define KAVL_EQUAL_ALLOWED*/ … … 54 47 #define KAVL_STD_KEY_COMP 55 48 #define KAVLKEY KU32 56 #define KAVLNODE COREKAVLRU3249 #define KAVLNODE KAVLRU32 57 50 #define KAVL_FN(name) kAvlrU32 ## name 58 51 #define KAVL_TYPE(prefix,name) prefix ## KAVLRU32 ## name
Note:
See TracChangeset
for help on using the changeset viewer.