summaryrefslogtreecommitdiffstats
path: root/src/lib/kStuff/include/k/kAvlTmpl
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/kStuff/include/k/kAvlTmpl')
-rw-r--r--src/lib/kStuff/include/k/kAvlTmpl/kAvlBase.h626
-rw-r--r--src/lib/kStuff/include/k/kAvlTmpl/kAvlDestroy.h129
-rw-r--r--src/lib/kStuff/include/k/kAvlTmpl/kAvlDoWithAll.h166
-rw-r--r--src/lib/kStuff/include/k/kAvlTmpl/kAvlEnum.h187
-rw-r--r--src/lib/kStuff/include/k/kAvlTmpl/kAvlGet.h89
-rw-r--r--src/lib/kStuff/include/k/kAvlTmpl/kAvlGetBestFit.h112
-rw-r--r--src/lib/kStuff/include/k/kAvlTmpl/kAvlGetWithParent.h65
-rw-r--r--src/lib/kStuff/include/k/kAvlTmpl/kAvlRemove2.h133
-rw-r--r--src/lib/kStuff/include/k/kAvlTmpl/kAvlRemoveBestFit.h70
-rw-r--r--src/lib/kStuff/include/k/kAvlTmpl/kAvlUndef.h79
10 files changed, 1656 insertions, 0 deletions
diff --git a/src/lib/kStuff/include/k/kAvlTmpl/kAvlBase.h b/src/lib/kStuff/include/k/kAvlTmpl/kAvlBase.h
new file mode 100644
index 0000000..90efdee
--- /dev/null
+++ b/src/lib/kStuff/include/k/kAvlTmpl/kAvlBase.h
@@ -0,0 +1,626 @@
+/* $Id: kAvlBase.h 36 2009-11-09 22:49:02Z bird $ */
+/** @file
+ * kAvlTmpl - Templated AVL Trees, The Mandatory Base Code.
+ */
+
+/*
+ * Copyright (c) 2001-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/** @page pg_kAvlTmpl Template Configuration.
+ *
+ * This is a templated implementation of AVL trees in C. The template
+ * parameters relates to the kind of key used and how duplicates are
+ * treated.
+ *
+ * \#define KAVL_EQUAL_ALLOWED
+ * Define this to tell us that equal keys are allowed.
+ * Then Equal keys will be put in a list pointed to by KAVLNODE::pList.
+ * This is by default not defined.
+ *
+ * \#define KAVL_CHECK_FOR_EQUAL_INSERT
+ * Define this to enable insert check for equal nodes.
+ * This is by default not defined.
+ *
+ * \#define KAVL_MAX_STACK
+ * Use this to specify the max number of stack entries the stack will use when
+ * inserting and removing nodes from the tree. The size should be something like
+ * log2(<max nodes>) + 3
+ * Must be defined.
+ *
+ * \#define KAVL_RANGE
+ * Define this to enable key ranges.
+ *
+ * \#define KAVL_OFFSET
+ * Define this to link the tree together using self relative offset
+ * instead of memory pointers, thus making the entire tree relocatable
+ * provided all the nodes - including the root node variable - are moved
+ * the exact same distance.
+ *
+ * \#define KAVL_LOOKTHRU
+ * Define this to employ a lookthru cache (direct) to speed up lookup for
+ * some usage patterns. The value should be the number of members of the
+ * array.
+ *
+ * \#define KAVL_LOOKTHRU_HASH(Key)
+ * Define this to specify a more efficient translation of the key into
+ * a lookthru array index. The default is key % size.
+ * For some key types this is required as the default will not compile.
+ *
+ * \#define KAVL_LOCKED
+ * Define this if you wish for the tree to be locked via the
+ * KAVL_WRITE_LOCK, KAVL_WRITE_UNLOCK, KAVL_READ_LOCK and
+ * KAVL_READ_UNLOCK macros. If not defined the tree will not be subject
+ * do any kind of locking and the problem of concurrency is left the user.
+ *
+ * \#define KAVL_WRITE_LOCK(pRoot)
+ * Lock the tree for writing.
+ *
+ * \#define KAVL_WRITE_UNLOCK(pRoot)
+ * Counteracts KAVL_WRITE_LOCK.
+ *
+ * \#define KAVL_READ_LOCK(pRoot)
+ * Lock the tree for reading.
+ *
+ * \#define KAVL_READ_UNLOCK(pRoot)
+ * Counteracts KAVL_READ_LOCK.
+ *
+ * \#define KAVLKEY
+ * Define this to the name of the AVL key type.
+ *
+ * \#define KAVL_STD_KEY_COMP
+ * Define this to use the standard key compare macros. If not set all the
+ * compare operations for KAVLKEY have to be defined: KAVL_G, KAVL_E, KAVL_NE,
+ * KAVL_R_IS_IDENTICAL, KAVL_R_IS_INTERSECTING and KAVL_R_IS_IN_RANGE. The
+ * latter three are only required when KAVL_RANGE is defined.
+ *
+ * \#define KAVLNODE
+ * Define this to the name (typedef) of the AVL node structure. This
+ * structure must have a mpLeft, mpRight, mKey and mHeight member.
+ * If KAVL_RANGE is defined a mKeyLast is also required.
+ * If KAVL_EQUAL_ALLOWED is defined a mpList member is required.
+ * It's possible to use other member names by redefining the names.
+ *
+ * \#define KAVLTREEPTR
+ * Define this to the name (typedef) of the tree pointer type. This is
+ * required when KAVL_OFFSET is defined. When not defined it defaults
+ * to KAVLNODE *.
+ *
+ * \#define KAVLROOT
+ * Define this to the name (typedef) of the AVL root structure. This
+ * is optional. However, if specified it must at least have a mpRoot
+ * member of KAVLTREEPTR type. If KAVL_LOOKTHRU is non-zero a
+ * maLookthru[KAVL_LOOKTHRU] member of the KAVLTREEPTR type is also
+ * required.
+ *
+ * \#define KAVL_FN
+ * Use this to alter the names of the AVL functions.
+ * Must be defined.
+ *
+ * \#define KAVL_TYPE(prefix, name)
+ * Use this to make external type names and unique. The prefix may be empty.
+ * Must be defined.
+ *
+ * \#define KAVL_INT(name)
+ * Use this to make internal type names and unique. The prefix may be empty.
+ * Must be defined.
+ *
+ * \#define KAVL_DECL(rettype)
+ * Function declaration macro that should be set according to the scope
+ * the instantiated template should have. For instance an inlined scope
+ * (private or public) should K_DECL_INLINE(rettype) here.
+ *
+ * This version of the kAVL tree offers the option of inlining the entire
+ * implementation. This depends on the compiler doing a decent job in both
+ * making use of the inlined code and to eliminate const variables.
+ */
+
+
+/*******************************************************************************
+* Internal Functions *
+*******************************************************************************/
+#include <k/kDefs.h>
+#include <k/kTypes.h>
+#include <k/kHlpAssert.h>
+
+
+/*******************************************************************************
+* Defined Constants And Macros *
+*******************************************************************************/
+#define KAVL_HEIGHTOF(pNode) ((KU8)((pNode) != NULL ? (pNode)->mHeight : 0))
+
+/** @def KAVL_GET_POINTER
+ * Reads a 'pointer' value.
+ *
+ * @returns The native pointer.
+ * @param pp Pointer to the pointer to read.
+ * @internal
+ */
+
+/** @def KAVL_GET_POINTER_NULL
+ * Reads a 'pointer' value which can be KAVL_NULL.
+ *
+ * @returns The native pointer.
+ * @returns NULL pointer if KAVL_NULL.
+ * @param pp Pointer to the pointer to read.
+ * @internal
+ */
+
+/** @def KAVL_SET_POINTER
+ * Writes a 'pointer' value.
+ * For offset-based schemes offset relative to pp is calculated and assigned to *pp.
+ *
+ * @returns stored pointer.
+ * @param pp Pointer to where to store the pointer.
+ * @param p Native pointer to assign to *pp.
+ * @internal
+ */
+
+/** @def KAVL_SET_POINTER_NULL
+ * Writes a 'pointer' value which can be KAVL_NULL.
+ *
+ * For offset-based schemes offset relative to pp is calculated and assigned to *pp,
+ * if p is not KAVL_NULL of course.
+ *
+ * @returns stored pointer.
+ * @param pp Pointer to where to store the pointer.
+ * @param pp2 Pointer to where to pointer to assign to pp. This can be KAVL_NULL
+ * @internal
+ */
+
+#ifndef KAVLTREEPTR
+# define KAVLTREEPTR KAVLNODE *
+#endif
+
+#ifndef KAVLROOT
+# define KAVLROOT KAVL_TYPE(,ROOT)
+# define KAVL_NEED_KAVLROOT
+#endif
+
+#ifdef KAVL_LOOKTHRU
+# ifndef KAVL_LOOKTHRU_HASH
+# define KAVL_LOOKTHRU_HASH(Key) ( (Key) % (KAVL_LOOKTHRU) )
+# endif
+#elif defined(KAVL_LOOKTHRU_HASH)
+# error "KAVL_LOOKTHRU_HASH without KAVL_LOOKTHRU!"
+#endif
+
+#ifdef KAVL_LOOKTHRU
+# define KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, Key) \
+ do { \
+ KAVLTREEPTR **ppEntry = &pRoot->maLookthru[KAVL_LOOKTHRU_HASH(Key)]; \
+ if ((pNode) == KAVL_GET_POINTER_NULL(ppEntry)) \
+ *ppEntry = KAVL_NULL; \
+ } while (0)
+#else
+# define KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, Key) do { } while (0)
+#endif
+
+#ifndef KAVL_LOCKED
+# define KAVL_WRITE_LOCK(pRoot) do { } while (0)
+# define KAVL_WRITE_UNLOCK(pRoot) do { } while (0)
+# define KAVL_READ_LOCK(pRoot) do { } while (0)
+# define KAVL_READ_UNLOCK(pRoot) do { } while (0)
+#endif
+
+#ifdef KAVL_OFFSET
+# define KAVL_GET_POINTER(pp) ( (KAVLNODE *)((KIPTR)(pp) + *(pp)) )
+# define KAVL_GET_POINTER_NULL(pp) ( *(pp) != KAVL_NULL ? KAVL_GET_POINTER(pp) : NULL )
+# define KAVL_SET_POINTER(pp, p) ( (*(pp)) = ((KIPTR)(p) - (KIPTR)(pp)) )
+# define KAVL_SET_POINTER_NULL(pp, pp2) ( (*(pp)) = *(pp2) != KAVL_NULL ? (KIPTR)KAVL_GET_POINTER(pp2) - (KIPTR)(pp) : KAVL_NULL )
+#else
+# define KAVL_GET_POINTER(pp) ( *(pp) )
+# define KAVL_GET_POINTER_NULL(pp) ( *(pp) )
+# define KAVL_SET_POINTER(pp, p) ( (*(pp)) = (p) )
+# define KAVL_SET_POINTER_NULL(pp, pp2) ( (*(pp)) = *(pp2) )
+#endif
+
+
+/** @def KAVL_NULL
+ * The NULL 'pointer' equivalent.
+ */
+#ifdef KAVL_OFFSET
+# define KAVL_NULL 0
+#else
+# define KAVL_NULL NULL
+#endif
+
+#ifdef KAVL_STD_KEY_COMP
+# define KAVL_G(key1, key2) ( (key1) > (key2) )
+# define KAVL_E(key1, key2) ( (key1) == (key2) )
+# define KAVL_NE(key1, key2) ( (key1) != (key2) )
+# ifdef KAVL_RANGE
+# define KAVL_R_IS_IDENTICAL(key1B, key2B, key1E, key2E) ( (key1B) == (key2B) && (key1E) == (key2E) )
+# define KAVL_R_IS_INTERSECTING(key1B, key2B, key1E, key2E) ( (key1B) <= (key2E) && (key1E) >= (key2B) )
+# define KAVL_R_IS_IN_RANGE(key1B, key1E, key2) KAVL_R_IS_INTERSECTING(key1B, key2, key1E, key2)
+# endif
+#endif
+
+#ifndef KAVL_RANGE
+# define KAVL_R_IS_INTERSECTING(key1B, key2B, key1E, key2E) KAVL_E(key1B, key2B)
+# define KAVL_R_IS_IDENTICAL(key1B, key2B, key1E, key2E) KAVL_E(key1B, key2B)
+#endif
+
+
+
+/*******************************************************************************
+* Structures and Typedefs *
+*******************************************************************************/
+/**
+ * Stack used to avoid recursive calls during insert and removal.
+ */
+typedef struct
+{
+ unsigned cEntries;
+ KAVLTREEPTR *aEntries[KAVL_MAX_STACK];
+} KAVL_INT(STACK);
+
+/**
+ * The callback used by the Destroy and DoWithAll functions.
+ */
+typedef int (* KAVL_TYPE(PFN,CALLBACK))(KAVLNODE *, void *);
+
+#ifdef KAVL_NEED_KAVLROOT
+/**
+ * The AVL root structure.
+ */
+typedef struct
+{
+ KAVLTREEPTR mpRoot;
+# ifdef KAVL_LOOKTHRU
+ KAVLTREEPTR maLookthru[KAVL_LOOKTHRU];
+# endif
+} KAVLROOT;
+#endif
+
+
+/**
+ * Rewinds a stack of node pointer pointers, rebalancing the tree.
+ *
+ * @param pStack Pointer to stack to rewind.
+ * @sketch LOOP thru all stack entries
+ * BEGIN
+ * Get pointer to pointer to node (and pointer to node) from the stack.
+ * IF 2 higher left subtree than in right subtree THEN
+ * BEGIN
+ * IF higher (or equal) left-sub-subtree than right-sub-subtree THEN
+ * * n+2|n+3
+ * / \ / \
+ * n+2 n ==> n+1 n+1|n+2
+ * / \ / \
+ * n+1 n|n+1 n|n+1 n
+ *
+ * Or with keys:
+ *
+ * 4 2
+ * / \ / \
+ * 2 5 ==> 1 4
+ * / \ / \
+ * 1 3 3 5
+ *
+ * ELSE
+ * * n+2
+ * / \ / \
+ * n+2 n n+1 n+1
+ * / \ ==> / \ / \
+ * n n+1 n L R n
+ * / \
+ * L R
+ *
+ * Or with keys:
+ * 6 4
+ * / \ / \
+ * 2 7 ==> 2 6
+ * / \ / \ / \
+ * 1 4 1 3 5 7
+ * / \
+ * 3 5
+ * END
+ * ELSE IF 2 higher in right subtree than in left subtree THEN
+ * BEGIN
+ * Same as above but left <==> right. (invert the picture)
+ * ELSE
+ * IF correct height THEN break
+ * ELSE correct height.
+ * END
+ */
+K_DECL_INLINE(void) KAVL_FN(Rebalance)(KAVL_INT(STACK) *pStack)
+{
+ while (pStack->cEntries > 0)
+ {
+ KAVLTREEPTR *ppNode = pStack->aEntries[--pStack->cEntries];
+ KAVLNODE *pNode = KAVL_GET_POINTER(ppNode);
+ KAVLNODE *pLeftNode = KAVL_GET_POINTER_NULL(&pNode->mpLeft);
+ KU8 uLeftHeight = KAVL_HEIGHTOF(pLeftNode);
+ KAVLNODE *pRightNode = KAVL_GET_POINTER_NULL(&pNode->mpRight);
+ KU8 uRightHeight = KAVL_HEIGHTOF(pRightNode);
+
+ if (uRightHeight + 1 < uLeftHeight)
+ {
+ KAVLNODE *pLeftLeftNode = KAVL_GET_POINTER_NULL(&pLeftNode->mpLeft);
+ KAVLNODE *pLeftRightNode = KAVL_GET_POINTER_NULL(&pLeftNode->mpRight);
+ KU8 uLeftRightHeight = KAVL_HEIGHTOF(pLeftRightNode);
+
+ if (KAVL_HEIGHTOF(pLeftLeftNode) >= uLeftRightHeight)
+ {
+ KAVL_SET_POINTER_NULL(&pNode->mpLeft, &pLeftNode->mpRight);
+ KAVL_SET_POINTER(&pLeftNode->mpRight, pNode);
+ pLeftNode->mHeight = (KU8)(1 + (pNode->mHeight = (KU8)(1 + uLeftRightHeight)));
+ KAVL_SET_POINTER(ppNode, pLeftNode);
+ }
+ else
+ {
+ KAVL_SET_POINTER_NULL(&pLeftNode->mpRight, &pLeftRightNode->mpLeft);
+ KAVL_SET_POINTER_NULL(&pNode->mpLeft, &pLeftRightNode->mpRight);
+ KAVL_SET_POINTER(&pLeftRightNode->mpLeft, pLeftNode);
+ KAVL_SET_POINTER(&pLeftRightNode->mpRight, pNode);
+ pLeftNode->mHeight = pNode->mHeight = uLeftRightHeight;
+ pLeftRightNode->mHeight = uLeftHeight;
+ KAVL_SET_POINTER(ppNode, pLeftRightNode);
+ }
+ }
+ else if (uLeftHeight + 1 < uRightHeight)
+ {
+ KAVLNODE *pRightLeftNode = KAVL_GET_POINTER_NULL(&pRightNode->mpLeft);
+ KU8 uRightLeftHeight = KAVL_HEIGHTOF(pRightLeftNode);
+ KAVLNODE *pRightRightNode = KAVL_GET_POINTER_NULL(&pRightNode->mpRight);
+
+ if (KAVL_HEIGHTOF(pRightRightNode) >= uRightLeftHeight)
+ {
+ KAVL_SET_POINTER_NULL(&pNode->mpRight, &pRightNode->mpLeft);
+ KAVL_SET_POINTER(&pRightNode->mpLeft, pNode);
+ pRightNode->mHeight = (KU8)(1 + (pNode->mHeight = (KU8)(1 + uRightLeftHeight)));
+ KAVL_SET_POINTER(ppNode, pRightNode);
+ }
+ else
+ {
+ KAVL_SET_POINTER_NULL(&pRightNode->mpLeft, &pRightLeftNode->mpRight);
+ KAVL_SET_POINTER_NULL(&pNode->mpRight, &pRightLeftNode->mpLeft);
+ KAVL_SET_POINTER(&pRightLeftNode->mpRight, pRightNode);
+ KAVL_SET_POINTER(&pRightLeftNode->mpLeft, pNode);
+ pRightNode->mHeight = pNode->mHeight = uRightLeftHeight;
+ pRightLeftNode->mHeight = uRightHeight;
+ KAVL_SET_POINTER(ppNode, pRightLeftNode);
+ }
+ }
+ else
+ {
+ KU8 uHeight = (KU8)(K_MAX(uLeftHeight, uRightHeight) + 1);
+ if (uHeight == pNode->mHeight)
+ break;
+ pNode->mHeight = uHeight;
+ }
+ }
+
+}
+
+
+/**
+ * Initializes the root of the AVL-tree.
+ *
+ * @param pTree Pointer to the root structure.
+ */
+KAVL_DECL(void) KAVL_FN(Init)(KAVLROOT *pRoot)
+{
+#ifdef KAVL_LOOKTHRU
+ unsigned i;
+#endif
+
+ pRoot->mpRoot = KAVL_NULL;
+#ifdef KAVL_LOOKTHRU
+ for (i = 0; i < (KAVL_LOOKTHRU); i++)
+ pRoot->maLookthru[i] = KAVL_NULL;
+#endif
+}
+
+
+/**
+ * Inserts a node into the AVL-tree.
+ * @returns K_TRUE if inserted.
+ * K_FALSE if node exists in tree.
+ * @param pRoot Pointer to the AVL-tree root structure.
+ * @param pNode Pointer to the node which is to be added.
+ * @sketch Find the location of the node (using binary tree algorithm.):
+ * LOOP until NULL leaf pointer
+ * BEGIN
+ * Add node pointer pointer to the AVL-stack.
+ * IF new-node-key < node key THEN
+ * left
+ * ELSE
+ * right
+ * END
+ * Fill in leaf node and insert it.
+ * Rebalance the tree.
+ */
+KAVL_DECL(KBOOL) KAVL_FN(Insert)(KAVLROOT *pRoot, KAVLNODE *pNode)
+{
+ KAVL_INT(STACK) AVLStack;
+ KAVLTREEPTR *ppCurNode = &pRoot->mpRoot;
+ register KAVLKEY Key = pNode->mKey;
+#ifdef KAVL_RANGE
+ register KAVLKEY KeyLast = pNode->mKeyLast;
+#endif
+
+#ifdef KAVL_RANGE
+ if (Key > KeyLast)
+ return K_FALSE;
+#endif
+
+ KAVL_WRITE_LOCK(pRoot);
+
+ AVLStack.cEntries = 0;
+ while (*ppCurNode != KAVL_NULL)
+ {
+ register KAVLNODE *pCurNode = KAVL_GET_POINTER(ppCurNode);
+
+ kHlpAssert(AVLStack.cEntries < KAVL_MAX_STACK);
+ AVLStack.aEntries[AVLStack.cEntries++] = ppCurNode;
+#ifdef KAVL_EQUAL_ALLOWED
+ if (KAVL_R_IS_IDENTICAL(pCurNode->mKey, Key, pCurNode->mKeyLast, KeyLast))
+ {
+ /*
+ * If equal then we'll use a list of equal nodes.
+ */
+ pNode->mpLeft = pNode->mpRight = KAVL_NULL;
+ pNode->mHeight = 0;
+ KAVL_SET_POINTER_NULL(&pNode->mpList, &pCurNode->mpList);
+ KAVL_SET_POINTER(&pCurNode->mpList, pNode);
+ KAVL_WRITE_UNLOCK(pRoot);
+ return K_TRUE;
+ }
+#endif
+#ifdef KAVL_CHECK_FOR_EQUAL_INSERT
+ if (KAVL_R_IS_INTERSECTING(pCurNode->mKey, Key, pCurNode->mKeyLast, KeyLast))
+ {
+ KAVL_WRITE_UNLOCK(pRoot);
+ return K_FALSE;
+ }
+#endif
+ if (KAVL_G(pCurNode->mKey, Key))
+ ppCurNode = &pCurNode->mpLeft;
+ else
+ ppCurNode = &pCurNode->mpRight;
+ }
+
+ pNode->mpLeft = pNode->mpRight = KAVL_NULL;
+#ifdef KAVL_EQUAL_ALLOWED
+ pNode->mpList = KAVL_NULL;
+#endif
+ pNode->mHeight = 1;
+ KAVL_SET_POINTER(ppCurNode, pNode);
+
+ KAVL_FN(Rebalance)(&AVLStack);
+
+ KAVL_WRITE_UNLOCK(pRoot);
+ return K_TRUE;
+}
+
+
+/**
+ * Removes a node from the AVL-tree.
+ * @returns Pointer to the node.
+ * @param pRoot Pointer to the AVL-tree root structure.
+ * @param Key Key value of the node which is to be removed.
+ * @sketch Find the node which is to be removed:
+ * LOOP until not found
+ * BEGIN
+ * Add node pointer pointer to the AVL-stack.
+ * IF the keys matches THEN break!
+ * IF remove key < node key THEN
+ * left
+ * ELSE
+ * right
+ * END
+ * IF found THEN
+ * BEGIN
+ * IF left node not empty THEN
+ * BEGIN
+ * Find the right most node in the left tree while adding the pointer to the pointer to it's parent to the stack:
+ * Start at left node.
+ * LOOP until right node is empty
+ * BEGIN
+ * Add to stack.
+ * go right.
+ * END
+ * Link out the found node.
+ * Replace the node which is to be removed with the found node.
+ * Correct the stack entry for the pointer to the left tree.
+ * END
+ * ELSE
+ * BEGIN
+ * Move up right node.
+ * Remove last stack entry.
+ * END
+ * Balance tree using stack.
+ * END
+ * return pointer to the removed node (if found).
+ */
+KAVL_DECL(KAVLNODE *) KAVL_FN(Remove)(KAVLROOT *pRoot, KAVLKEY Key)
+{
+ KAVL_INT(STACK) AVLStack;
+ KAVLTREEPTR *ppDeleteNode = &pRoot->mpRoot;
+ register KAVLNODE *pDeleteNode;
+
+ KAVL_WRITE_LOCK(pRoot);
+
+ AVLStack.cEntries = 0;
+ for (;;)
+ {
+ if (*ppDeleteNode == KAVL_NULL)
+ {
+ KAVL_WRITE_UNLOCK(pRoot);
+ return NULL;
+ }
+ pDeleteNode = KAVL_GET_POINTER(ppDeleteNode);
+
+ kHlpAssert(AVLStack.cEntries < KAVL_MAX_STACK);
+ AVLStack.aEntries[AVLStack.cEntries++] = ppDeleteNode;
+ if (KAVL_E(pDeleteNode->mKey, Key))
+ break;
+
+ if (KAVL_G(pDeleteNode->mKey, Key))
+ ppDeleteNode = &pDeleteNode->mpLeft;
+ else
+ ppDeleteNode = &pDeleteNode->mpRight;
+ }
+
+ if (pDeleteNode->mpLeft != KAVL_NULL)
+ {
+ /* find the rightmost node in the left tree. */
+ const unsigned iStackEntry = AVLStack.cEntries;
+ KAVLTREEPTR *ppLeftLeast = &pDeleteNode->mpLeft;
+ register KAVLNODE *pLeftLeast = KAVL_GET_POINTER(ppLeftLeast);
+
+ while (pLeftLeast->mpRight != KAVL_NULL)
+ {
+ kHlpAssert(AVLStack.cEntries < KAVL_MAX_STACK);
+ AVLStack.aEntries[AVLStack.cEntries++] = ppLeftLeast;
+ ppLeftLeast = &pLeftLeast->mpRight;
+ pLeftLeast = KAVL_GET_POINTER(ppLeftLeast);
+ }
+
+ /* link out pLeftLeast */
+ KAVL_SET_POINTER_NULL(ppLeftLeast, &pLeftLeast->mpLeft);
+
+ /* link it in place of the delete node. */
+ KAVL_SET_POINTER_NULL(&pLeftLeast->mpLeft, &pDeleteNode->mpLeft);
+ KAVL_SET_POINTER_NULL(&pLeftLeast->mpRight, &pDeleteNode->mpRight);
+ pLeftLeast->mHeight = pDeleteNode->mHeight;
+ KAVL_SET_POINTER(ppDeleteNode, pLeftLeast);
+ AVLStack.aEntries[iStackEntry] = &pLeftLeast->mpLeft;
+ }
+ else
+ {
+ KAVL_SET_POINTER_NULL(ppDeleteNode, &pDeleteNode->mpRight);
+ AVLStack.cEntries--;
+ }
+
+ KAVL_FN(Rebalance)(&AVLStack);
+
+ KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pDeleteNode, Key);
+
+ KAVL_WRITE_UNLOCK(pRoot);
+ return pDeleteNode;
+}
+
diff --git a/src/lib/kStuff/include/k/kAvlTmpl/kAvlDestroy.h b/src/lib/kStuff/include/k/kAvlTmpl/kAvlDestroy.h
new file mode 100644
index 0000000..17115f3
--- /dev/null
+++ b/src/lib/kStuff/include/k/kAvlTmpl/kAvlDestroy.h
@@ -0,0 +1,129 @@
+/* $Id: kAvlDestroy.h 29 2009-07-01 20:30:29Z bird $ */
+/** @file
+ * kAvlTmpl - Templated AVL Trees, Destroy the tree.
+ */
+
+/*
+ * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * Destroys the specified tree, starting with the root node and working our way down.
+ *
+ * @returns 0 on success.
+ * @returns Return value from callback on failure. On failure, the tree will be in
+ * an unbalanced condition and only further calls to the Destroy should be
+ * made on it. Note that the node we fail on will be considered dead and
+ * no action is taken to link it back into the tree.
+ * @param pRoot Pointer to the AVL-tree root structure.
+ * @param pfnCallBack Pointer to callback function.
+ * @param pvUser User parameter passed on to the callback function.
+ */
+KAVL_DECL(int) KAVL_FN(Destroy)(KAVLROOT *pRoot, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
+{
+#ifdef KAVL_LOOKTHRU
+ unsigned i;
+#endif
+ unsigned cEntries;
+ KAVLNODE *apEntries[KAVL_MAX_STACK];
+ int rc;
+
+ KAVL_WRITE_LOCK(pRoot);
+ if (pRoot->mpRoot == KAVL_NULL)
+ {
+ KAVL_WRITE_UNLOCK(pRoot);
+ return 0;
+ }
+
+#ifdef KAVL_LOOKTHRU
+ /*
+ * Kill the lookthru cache.
+ */
+ for (i = 0; i < (KAVL_LOOKTHRU); i++)
+ pRoot->maLookthru[i] = KAVL_NULL;
+#endif
+
+ cEntries = 1;
+ apEntries[0] = KAVL_GET_POINTER(&pRoot->mpRoot);
+ while (cEntries > 0)
+ {
+ /*
+ * Process the subtrees first.
+ */
+ KAVLNODE *pNode = apEntries[cEntries - 1];
+ if (pNode->mpLeft != KAVL_NULL)
+ apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
+ else if (pNode->mpRight != KAVL_NULL)
+ apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
+ else
+ {
+#ifdef KAVL_EQUAL_ALLOWED
+ /*
+ * Process nodes with the same key.
+ */
+ while (pNode->pList != KAVL_NULL)
+ {
+ KAVLNODE *pEqual = KAVL_GET_POINTER(&pNode->pList);
+ KAVL_SET_POINTER(&pNode->pList, KAVL_GET_POINTER_NULL(&pEqual->pList));
+ pEqual->pList = KAVL_NULL;
+
+ rc = pfnCallBack(pEqual, pvUser);
+ if (rc)
+ {
+ KAVL_WRITE_UNLOCK(pRoot);
+ return rc;
+ }
+ }
+#endif
+
+ /*
+ * Unlink the node.
+ */
+ if (--cEntries > 0)
+ {
+ KAVLNODE *pParent = apEntries[cEntries - 1];
+ if (KAVL_GET_POINTER(&pParent->mpLeft) == pNode)
+ pParent->mpLeft = KAVL_NULL;
+ else
+ pParent->mpRight = KAVL_NULL;
+ }
+ else
+ pRoot->mpRoot = KAVL_NULL;
+
+ kHlpAssert(pNode->mpLeft == KAVL_NULL);
+ kHlpAssert(pNode->mpRight == KAVL_NULL);
+ rc = pfnCallBack(pNode, pvUser);
+ if (rc)
+ {
+ KAVL_WRITE_UNLOCK(pRoot);
+ return rc;
+ }
+ }
+ } /* while */
+ kHlpAssert(pRoot->mpRoot == KAVL_NULL);
+
+ KAVL_WRITE_UNLOCK(pRoot);
+ return 0;
+}
+
diff --git a/src/lib/kStuff/include/k/kAvlTmpl/kAvlDoWithAll.h b/src/lib/kStuff/include/k/kAvlTmpl/kAvlDoWithAll.h
new file mode 100644
index 0000000..f2eaba1
--- /dev/null
+++ b/src/lib/kStuff/include/k/kAvlTmpl/kAvlDoWithAll.h
@@ -0,0 +1,166 @@
+/* $Id: kAvlDoWithAll.h 29 2009-07-01 20:30:29Z bird $ */
+/** @file
+ * kAvlTmpl - Templated AVL Trees, The Callback Iterator.
+ */
+
+/*
+ * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/*******************************************************************************
+* Structures and Typedefs *
+*******************************************************************************/
+/**
+ * Stack used by DoWithAll to avoid recusive function calls.
+ */
+typedef struct
+{
+ unsigned cEntries;
+ KAVLNODE *aEntries[KAVL_MAX_STACK];
+ char achFlags[KAVL_MAX_STACK];
+ KAVLROOT pRoot;
+} KAVL_INT(STACK2);
+
+
+/**
+ * Iterates thru all nodes in the given tree.
+ *
+ * @returns 0 on success. Return from callback on failure.
+ * @param pRoot Pointer to the AVL-tree root structure.
+ * @param fFromLeft K_TRUE: Left to right.
+ * K_FALSE: Right to left.
+ * @param pfnCallBack Pointer to callback function.
+ * @param pvUser User parameter passed on to the callback function.
+ */
+KAVL_DECL(int) KAVL_FN(DoWithAll)(KAVLROOT *pRoot, KBOOL fFromLeft, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
+{
+ KAVL_INT(STACK2) AVLStack;
+ KAVLNODE *pNode;
+#ifdef KAVL_EQUAL_ALLOWED
+ KAVLNODE *pEqual;
+#endif
+ int rc;
+
+ KAVL_READ_LOCK(pRoot);
+ if (pRoot->mpRoot == KAVL_NULL)
+ {
+ KAVL_READ_UNLOCK(pRoot);
+ return 0;
+ }
+
+ AVLStack.cEntries = 1;
+ AVLStack.achFlags[0] = 0;
+ AVLStack.aEntries[0] = KAVL_GET_POINTER(&pRoot->mpRoot);
+
+ if (fFromLeft)
+ { /* from left */
+ while (AVLStack.cEntries > 0)
+ {
+ pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
+
+ /* left */
+ if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
+ {
+ if (pNode->mpLeft != KAVL_NULL)
+ {
+ AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
+ AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
+ continue;
+ }
+ }
+
+ /* center */
+ rc = pfnCallBack(pNode, pvUser);
+ if (rc)
+ return rc;
+#ifdef KAVL_EQUAL_ALLOWED
+ if (pNode->mpList != KAVL_NULL)
+ for (pEqual = KAVL_GET_POINTER(&pNode->mpList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->mpList))
+ {
+ rc = pfnCallBack(pEqual, pvUser);
+ if (rc)
+ {
+ KAVL_READ_UNLOCK(pRoot);
+ return rc;
+ }
+ }
+#endif
+
+ /* right */
+ AVLStack.cEntries--;
+ if (pNode->mpRight != KAVL_NULL)
+ {
+ AVLStack.achFlags[AVLStack.cEntries] = 0;
+ AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
+ }
+ } /* while */
+ }
+ else
+ { /* from right */
+ while (AVLStack.cEntries > 0)
+ {
+ pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
+
+ /* right */
+ if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
+ {
+ if (pNode->mpRight != KAVL_NULL)
+ {
+ AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
+ AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
+ continue;
+ }
+ }
+
+ /* center */
+ rc = pfnCallBack(pNode, pvUser);
+ if (rc)
+ return rc;
+#ifdef KAVL_EQUAL_ALLOWED
+ if (pNode->mpList != KAVL_NULL)
+ for (pEqual = KAVL_GET_POINTER(&pNode->mpList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->pList))
+ {
+ rc = pfnCallBack(pEqual, pvUser);
+ if (rc)
+ {
+ KAVL_READ_UNLOCK(pRoot);
+ return rc;
+ }
+ }
+#endif
+
+ /* left */
+ AVLStack.cEntries--;
+ if (pNode->mpLeft != KAVL_NULL)
+ {
+ AVLStack.achFlags[AVLStack.cEntries] = 0;
+ AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
+ }
+ } /* while */
+ }
+
+ KAVL_READ_UNLOCK(pRoot);
+ return 0;
+}
+
diff --git a/src/lib/kStuff/include/k/kAvlTmpl/kAvlEnum.h b/src/lib/kStuff/include/k/kAvlTmpl/kAvlEnum.h
new file mode 100644
index 0000000..da151d1
--- /dev/null
+++ b/src/lib/kStuff/include/k/kAvlTmpl/kAvlEnum.h
@@ -0,0 +1,187 @@
+/* $Id: kAvlEnum.h 29 2009-07-01 20:30:29Z bird $ */
+/** @file
+ * kAvlTmpl - Templated AVL Trees, Node Enumeration.
+ */
+
+/*
+ * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/*******************************************************************************
+* Structures and Typedefs *
+*******************************************************************************/
+/**
+ * Enumeration control data.
+ *
+ * This is initialized by BeginEnum and used by GetNext to figure out what
+ * to do next.
+ */
+typedef struct KAVL_TYPE(,ENUMDATA)
+{
+ KBOOL fFromLeft;
+ KI8 cEntries;
+ KU8 achFlags[KAVL_MAX_STACK];
+ KAVLNODE * aEntries[KAVL_MAX_STACK];
+} KAVL_TYPE(,ENUMDATA), *KAVL_TYPE(P,ENUMDATA);
+
+
+/**
+ * Ends an enumeration.
+ *
+ * The purpose of this function is to unlock the tree should the
+ * AVL implementation include locking. It's good practice to call
+ * it anyway even if the tree doesn't do any locking.
+ *
+ * @param pEnumData Pointer to enumeration control data.
+ */
+KAVL_DECL(void) KAVL_FN(EndEnum)(KAVL_TYPE(,ENUMDATA) *pEnumData)
+{
+ KAVLROOT pRoot = pEnumData->pRoot;
+ pEnumData->pRoot = NULL;
+ if (pRoot)
+ KAVL_READ_UNLOCK(pEnumData->pRoot);
+}
+
+
+/**
+ * Get the next node in the tree enumeration.
+ *
+ * The current implementation of this function willl not walk the mpList
+ * chain like the DoWithAll function does. This may be changed later.
+ *
+ * @returns Pointer to the next node in the tree.
+ * NULL is returned when the end of the tree has been reached,
+ * it is not necessary to call EndEnum in this case.
+ * @param pEnumData Pointer to enumeration control data.
+ */
+KAVL_DECL(KAVLNODE *) KAVL_FN(GetNext)(KAVL_TYPE(,ENUMDATA) *pEnumData)
+{
+ if (pEnumData->fFromLeft)
+ { /* from left */
+ while (pEnumData->cEntries > 0)
+ {
+ KAVLNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1];
+
+ /* left */
+ if (pEnumData->achFlags[pEnumData->cEntries - 1] == 0)
+ {
+ pEnumData->achFlags[pEnumData->cEntries - 1]++;
+ if (pNode->mpLeft != KAVL_NULL)
+ {
+ pEnumData->achFlags[pEnumData->cEntries] = 0; /* 0 left, 1 center, 2 right */
+ pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
+ continue;
+ }
+ }
+
+ /* center */
+ if (pEnumData->achFlags[pEnumData->cEntries - 1] == 1)
+ {
+ pEnumData->achFlags[pEnumData->cEntries - 1]++;
+ return pNode;
+ }
+
+ /* right */
+ pEnumData->cEntries--;
+ if (pNode->mpRight != KAVL_NULL)
+ {
+ pEnumData->achFlags[pEnumData->cEntries] = 0;
+ pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
+ }
+ } /* while */
+ }
+ else
+ { /* from right */
+ while (pEnumData->cEntries > 0)
+ {
+ KAVLNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1];
+
+ /* right */
+ if (pEnumData->achFlags[pEnumData->cEntries - 1] == 0)
+ {
+ pEnumData->achFlags[pEnumData->cEntries - 1]++;
+ if (pNode->mpRight != KAVL_NULL)
+ {
+ pEnumData->achFlags[pEnumData->cEntries] = 0; /* 0 right, 1 center, 2 left */
+ pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
+ continue;
+ }
+ }
+
+ /* center */
+ if (pEnumData->achFlags[pEnumData->cEntries - 1] == 1)
+ {
+ pEnumData->achFlags[pEnumData->cEntries - 1]++;
+ return pNode;
+ }
+
+ /* left */
+ pEnumData->cEntries--;
+ if (pNode->mpLeft != KAVL_NULL)
+ {
+ pEnumData->achFlags[pEnumData->cEntries] = 0;
+ pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
+ }
+ } /* while */
+ }
+
+ /*
+ * Call EndEnum.
+ */
+ KAVL_FN(EndEnum)(pEnumData);
+ return NULL;
+}
+
+
+/**
+ * Starts an enumeration of all nodes in the given AVL tree.
+ *
+ * The current implementation of this function will not walk the mpList
+ * chain like the DoWithAll function does. This may be changed later.
+ *
+ * @returns Pointer to the first node in the enumeration.
+ * If NULL is returned the tree is empty calling EndEnum isn't
+ * strictly necessary (although it will do no harm).
+ * @param pRoot Pointer to the AVL-tree root structure.
+ * @param pEnumData Pointer to enumeration control data.
+ * @param fFromLeft K_TRUE: Left to right.
+ * K_FALSE: Right to left.
+ */
+KAVL_DECL(KAVLNODE *) KAVL_FN(BeginEnum)(KAVLROOT *pRoot, KAVL_TYPE(,ENUMDATA) *pEnumData, KBOOL fFromLeft)
+{
+ KAVL_READ_LOCK(pRoot);
+ pEnumData->pRoot = pRoot;
+ if (pRoot->mpRoot != KAVL_NULL)
+ {
+ pEnumData->fFromLeft = fFromLeft;
+ pEnumData->cEntries = 1;
+ pEnumData->aEntries[0] = KAVL_GET_POINTER(pRoot->mpRoot);
+ pEnumData->achFlags[0] = 0;
+ }
+ else
+ pEnumData->cEntries = 0;
+
+ return KAVL_FN(GetNext)(pEnumData);
+}
+
diff --git a/src/lib/kStuff/include/k/kAvlTmpl/kAvlGet.h b/src/lib/kStuff/include/k/kAvlTmpl/kAvlGet.h
new file mode 100644
index 0000000..a80eb49
--- /dev/null
+++ b/src/lib/kStuff/include/k/kAvlTmpl/kAvlGet.h
@@ -0,0 +1,89 @@
+/* $Id: kAvlGet.h 29 2009-07-01 20:30:29Z bird $ */
+/** @file
+ * kAvlTmpl - Templated AVL Trees, Get a Node.
+ */
+
+/*
+ * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * Gets a node from the tree (does not remove it!)
+ *
+ * @returns Pointer to the node holding the given key.
+ * @param pRoot Pointer to the AVL-tree root structure.
+ * @param Key Key value of the node which is to be found.
+ */
+KAVL_DECL(KAVLNODE *) KAVL_FN(Get)(KAVLROOT *pRoot, KAVLKEY Key)
+{
+ KAVLNODE *pNode;
+#ifdef KAVL_LOOKTHRU_CACHE
+ KAVLTREEPTR *ppEntry;
+#endif
+
+ KAVL_READ_LOCK(pRoot);
+ if (pRoot->mpRoot == KAVL_NULL)
+ {
+ KAVL_READ_UNLOCK(pRoot);
+ return NULL;
+ }
+
+#ifdef KAVL_LOOKTHRU_CACHE
+ ppEntry = &pRoot->maLookthru[KAVL_LOOKTHRU_HASH(Key)];
+ pNode = KAVL_GET_POINTER_NULL(ppEntry);
+ if (!pNode || KAVL_NE(pNode->mKey, Key))
+#endif
+ {
+ pNode = KAVL_GET_POINTER(&pRoot->mpRoot);
+ while (KAVL_NE(pNode->mKey, Key))
+ {
+ if (KAVL_G(pNode->mKey, Key))
+ {
+ if (pNode->mpLeft == KAVL_NULL)
+ {
+ KAVL_READ_UNLOCK(pRoot);
+ return NULL;
+ }
+ pNode = KAVL_GET_POINTER(&pNode->mpLeft);
+ }
+ else
+ {
+ if (pNode->mpRight == KAVL_NULL)
+ {
+ KAVL_READ_UNLOCK(pRoot);
+ return NULL;
+ }
+ pNode = KAVL_GET_POINTER(&pNode->mpRight);
+ }
+ }
+
+#ifdef KAVL_LOOKTHRU_CACHE
+ KAVL_SET_POINTER(ppEntry, pNode);
+#endif
+ }
+
+ KAVL_READ_UNLOCK(pRoot);
+ return pNode;
+}
+
diff --git a/src/lib/kStuff/include/k/kAvlTmpl/kAvlGetBestFit.h b/src/lib/kStuff/include/k/kAvlTmpl/kAvlGetBestFit.h
new file mode 100644
index 0000000..1d51709
--- /dev/null
+++ b/src/lib/kStuff/include/k/kAvlTmpl/kAvlGetBestFit.h
@@ -0,0 +1,112 @@
+/* $Id: kAvlGetBestFit.h 29 2009-07-01 20:30:29Z bird $ */
+/** @file
+ * kAvlTmpl - Templated AVL Trees, Get Best Fitting Node.
+ */
+
+/*
+ * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * Finds the best fitting node in the tree for the given Key value.
+ *
+ * @returns Pointer to the best fitting node found.
+ * @param pRoot Pointer to the AVL-tree root structure.
+ * @param Key The Key of which is to be found a best fitting match for..
+ * @param fAbove K_TRUE: Returned node is have the closest key to Key from above.
+ * K_FALSE: Returned node is have the closest key to Key from below.
+ * @sketch The best fitting node is always located in the searchpath above you.
+ * >= (above): The node where you last turned left.
+ * <= (below): the node where you last turned right.
+ */
+KAVL_DECL(KAVLNODE *) KAVL_FN(GetBestFit)(KAVLROOT *pRoot, KAVLKEY Key, KBOOL fAbove)
+{
+ register KAVLNODE *pNode;
+ KAVLNODE *pNodeLast;
+
+ KAVL_READ_LOCK(pLook);
+ if (pRoot->mpRoot == KAVL_NULL)
+ {
+ KAVL_READ_UNLOCK(pLook);
+ return NULL;
+ }
+
+ pNode = KAVL_GET_POINTER(&pRoot->mpRoot);
+ pNodeLast = NULL;
+ if (fAbove)
+ { /* pNode->mKey >= Key */
+ while (KAVL_NE(pNode->mKey, Key))
+ {
+ if (KAVL_G(pNode->mKey, Key))
+ {
+ if (pNode->mpLeft == KAVL_NULL)
+ {
+ KAVL_READ_UNLOCK(pLook);
+ return pNode;
+ }
+ pNodeLast = pNode;
+ pNode = KAVL_GET_POINTER(&pNode->mpLeft);
+ }
+ else
+ {
+ if (pNode->mpRight == KAVL_NULL)
+ {
+ KAVL_READ_UNLOCK(pLook);
+ return pNodeLast;
+ }
+ pNode = KAVL_GET_POINTER(&pNode->mpRight);
+ }
+ }
+ }
+ else
+ { /* pNode->mKey <= Key */
+ while (KAVL_NE(pNode->mKey, Key))
+ {
+ if (KAVL_G(pNode->mKey, Key))
+ {
+ if (pNode->mpLeft == KAVL_NULL)
+ {
+ KAVL_READ_UNLOCK(pLook);
+ return pNodeLast;
+ }
+ pNode = KAVL_GET_POINTER(&pNode->mpLeft);
+ }
+ else
+ {
+ if (pNode->mpRight == KAVL_NULL)
+ {
+ KAVL_READ_UNLOCK(pLook);
+ return pNode;
+ }
+ pNodeLast = pNode;
+ pNode = KAVL_GET_POINTER(&pNode->mpRight);
+ }
+ }
+ }
+
+ /* perfect match or nothing. */
+ KAVL_READ_UNLOCK(pLook);
+ return pNode;
+}
+
diff --git a/src/lib/kStuff/include/k/kAvlTmpl/kAvlGetWithParent.h b/src/lib/kStuff/include/k/kAvlTmpl/kAvlGetWithParent.h
new file mode 100644
index 0000000..997c394
--- /dev/null
+++ b/src/lib/kStuff/include/k/kAvlTmpl/kAvlGetWithParent.h
@@ -0,0 +1,65 @@
+/* $Id: kAvlGetWithParent.h 34 2009-11-08 19:38:40Z bird $ */
+/** @file
+ * kAvlTmpl - Templated AVL Trees, Get Node With Parent.
+ */
+
+/*
+ * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * Gets a node from the tree and its parent node (if any).
+ * The tree remains unchanged.
+ *
+ * @returns Pointer to the node holding the given key.
+ * @param pRoot Pointer to the AVL-tree root structure.
+ * @param ppParent Pointer to a variable which will hold the pointer to the partent node on
+ * return. When no node is found, this will hold the last searched node.
+ * @param Key Key value of the node which is to be found.
+ */
+KAVL_DECL(KAVLNODE *) KAVL_FN(GetWithParent)(KAVLROOT *pRoot, KAVLNODE **ppParent, KAVLKEY Key)
+{
+ register KAVLNODE *pNode;
+ register KAVLNODE *pParent;
+
+ KAVL_READ_LOCK(pRoot);
+
+ pParent = NULL;
+ pNode = KAVL_GET_POINTER_NULL(&pRoot->mpRoot);
+ while ( pNode != NULL
+ && KAVL_NE(pNode->mKey, Key))
+ {
+ pParent = pNode;
+ if (KAVL_G(pNode->mKey, Key))
+ pNode = KAVL_GET_POINTER_NULL(&pNode->mpLeft);
+ else
+ pNode = KAVL_GET_POINTER_NULL(&pNode->mpRight);
+ }
+
+ KAVL_READ_UNLOCK(pRoot);
+
+ *ppParent = pParent;
+ return pNode;
+}
+
diff --git a/src/lib/kStuff/include/k/kAvlTmpl/kAvlRemove2.h b/src/lib/kStuff/include/k/kAvlTmpl/kAvlRemove2.h
new file mode 100644
index 0000000..d027014
--- /dev/null
+++ b/src/lib/kStuff/include/k/kAvlTmpl/kAvlRemove2.h
@@ -0,0 +1,133 @@
+/* $Id: kAvlRemove2.h 34 2009-11-08 19:38:40Z bird $ */
+/** @file
+ * kAvlTmpl - Templated AVL Trees, Remove A Specific Node.
+ */
+
+/*
+ * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * Removes the specified node from the tree.
+ *
+ * @returns Pointer to the removed node (NULL if not in the tree)
+ * @param pRoot Pointer to the AVL-tree root structure.
+ * @param Key The Key of which is to be found a best fitting match for..
+ *
+ * @remark This implementation isn't the most efficient, but this short and
+ * easier to manage.
+ */
+KAVL_DECL(KAVLNODE *) KAVL_FN(Remove2)(KAVLROOT *pRoot, KAVLNODE *pNode)
+{
+#ifdef KAVL_EQUAL_ALLOWED
+ /*
+ * Find the right node by key and see if it's what we want.
+ */
+ KAVLNODE *pParent;
+ KAVLNODE *pCurNode = KAVL_FN(GetWithParent)(pRoot, pNode->mKey, &pParent);
+ if (!pCurNode)
+ return NULL;
+ KAVL_WRITE_LOCK(pRoot); /** @todo the locking here isn't 100% sane. The only way to archive that is by no calling worker functions. */
+ if (pCurNode != pNode)
+ {
+ /*
+ * It's not the one we want, but it could be in the duplicate list.
+ */
+ while (pCurNode->mpList != KAVL_NULL)
+ {
+ KAVLNODE *pNext = KAVL_GET_POINTER(&pCurNode->mpList);
+ if (pNext == pNode)
+ {
+ KAVL_SET_POINTER_NULL(&pCurNode->mpList, KAVL_GET_POINTER_NULL(&pNode->mpList));
+ pNode->mpList = KAVL_NULL;
+ KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
+ KAVL_WRITE_UNLOCK(pRoot);
+ return pNode;
+ }
+ pCurNode = pNext;
+ }
+ KAVL_WRITE_UNLOCK(pRoot);
+ return NULL;
+ }
+
+ /*
+ * Ok, it's the one we want alright.
+ *
+ * Simply remove it if it's the only one with they Key,
+ * if there are duplicates we'll have to unlink it and
+ * insert the first duplicate in our place.
+ */
+ if (pNode->mpList == KAVL_NULL)
+ {
+ KAVL_WRITE_UNLOCK(pRoot);
+ KAVL_FN(Remove)(pRoot, pNode->mKey);
+ }
+ else
+ {
+ KAVLNODE *pNewUs = KAVL_GET_POINTER(&pNode->mpList);
+
+ pNewUs->mHeight = pNode->mHeight;
+
+ if (pNode->mpLeft != KAVL_NULL)
+ KAVL_SET_POINTER(&pNewUs->mpLeft, KAVL_GET_POINTER(&pNode->mpLeft))
+ else
+ pNewUs->mpLeft = KAVL_NULL;
+
+ if (pNode->mpRight != KAVL_NULL)
+ KAVL_SET_POINTER(&pNewUs->mpRight, KAVL_GET_POINTER(&pNode->mpRight))
+ else
+ pNewUs->mpRight = KAVL_NULL;
+
+ if (pParent)
+ {
+ if (KAVL_GET_POINTER_NULL(&pParent->mpLeft) == pNode)
+ KAVL_SET_POINTER(&pParent->mpLeft, pNewUs);
+ else
+ KAVL_SET_POINTER(&pParent->mpRight, pNewUs);
+ }
+ else
+ KAVL_SET_POINTER(&pRoot->mpRoot, pNewUs);
+
+ KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
+ KAVL_WRITE_UNLOCK(pRoot);
+ }
+
+ return pNode;
+
+#else
+ /*
+ * Delete it, if we got the wrong one, reinsert it.
+ *
+ * This ASSUMS that the caller is NOT going to hand us a lot
+ * of wrong nodes but just uses this API for his convenience.
+ */
+ KAVLNODE *pRemovedNode = KAVL_FN(Remove)(pRoot, pNode->mKey);
+ if (pRemovedNode == pNode)
+ return pRemovedNode;
+
+ KAVL_FN(Insert)(pRoot, pRemovedNode);
+ return NULL;
+#endif
+}
+
diff --git a/src/lib/kStuff/include/k/kAvlTmpl/kAvlRemoveBestFit.h b/src/lib/kStuff/include/k/kAvlTmpl/kAvlRemoveBestFit.h
new file mode 100644
index 0000000..3c9ed98
--- /dev/null
+++ b/src/lib/kStuff/include/k/kAvlTmpl/kAvlRemoveBestFit.h
@@ -0,0 +1,70 @@
+/* $Id: kAvlRemoveBestFit.h 29 2009-07-01 20:30:29Z bird $ */
+/** @file
+ * kAvlTmpl - Templated AVL Trees, Remove Best Fitting Node.
+ */
+
+/*
+ * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * Finds the best fitting node in the tree for the given Key value and removes the node.
+ *
+ * @returns Pointer to the removed node.
+ * @param pRoot Pointer to the AVL-tree root structure.
+ * @param Key The Key of which is to be found a best fitting match for..
+ * @param fAbove K_TRUE: Returned node is have the closest key to Key from above.
+ * K_FALSE: Returned node is have the closest key to Key from below.
+ *
+ * @remark This implementation uses GetBestFit and then Remove and might therefore
+ * not be the most optimal kind of implementation, but it reduces the complexity
+ * code size, and the likelyhood for bugs.
+ */
+KAVL_DECL(KAVLNODE *) KAVL_FN(RemoveBestFit)(KAVLROOT *pRoot, KAVLKEY Key, KBOOL fAbove)
+{
+ /*
+ * If we find anything we'll have to remove the node and return it.
+ * Now, if duplicate keys are allowed we'll remove a duplicate before
+ * removing the in-tree node as this is way cheaper.
+ */
+ KAVLNODE *pNode = KAVL_FN(GetBestFit)(pRoot, Key, fAbove);
+ if (pNode != NULL)
+ {
+#ifdef KAVL_EQUAL_ALLOWED
+ KAVL_WRITE_LOCK(pRoot); /** @todo the locking isn't quite sane here. :-/ */
+ if (pNode->mpList != KAVL_NULL)
+ {
+ KAVLNODE *pRet = KAVL_GET_POINTER(&pNode->mpList);
+ KAVL_SET_POINTER_NULL(&pNode->mpList, &pRet->mpList);
+ KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
+ KAVL_WRITE_UNLOCK(pRoot);
+ return pRet;
+ }
+ KAVL_WRITE_UNLOCK(pRoot);
+#endif
+ pNode = KAVL_FN(Remove)(pRoot, pNode->mKey);
+ }
+ return pNode;
+}
+
diff --git a/src/lib/kStuff/include/k/kAvlTmpl/kAvlUndef.h b/src/lib/kStuff/include/k/kAvlTmpl/kAvlUndef.h
new file mode 100644
index 0000000..bd6957f
--- /dev/null
+++ b/src/lib/kStuff/include/k/kAvlTmpl/kAvlUndef.h
@@ -0,0 +1,79 @@
+/* $Id: kAvlUndef.h 29 2009-07-01 20:30:29Z bird $ */
+/** @file
+ * kAvlTmpl - Undefines All Macros (both config and temp).
+ */
+
+/*
+ * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/*
+ * The configuration.
+ */
+#undef KAVL_EQUAL_ALLOWED
+#undef KAVL_CHECK_FOR_EQUAL_INSERT
+#undef KAVL_MAX_STACK
+#undef KAVL_RANGE
+#undef KAVL_OFFSET
+#undef KAVL_STD_KEY_COMP
+#undef KAVL_LOOKTHRU
+#undef KAVL_LOOKTHRU_HASH
+#undef KAVL_LOCKED
+#undef KAVL_WRITE_LOCK
+#undef KAVL_WRITE_UNLOCK
+#undef KAVL_READ_LOCK
+#undef KAVL_READ_UNLOCK
+#undef KAVLKEY
+#undef KAVLNODE
+#undef KAVLTREEPTR
+#undef KAVLROOT
+#undef KAVL_FN
+#undef KAVL_TYPE
+#undef KAVL_INT
+#undef KAVL_DECL
+#undef mKey
+#undef mKeyLast
+#undef mHeight
+#undef mpLeft
+#undef mpRight
+#undef mpList
+#undef mpRoot
+#undef maLookthru
+
+/*
+ * The internal macros.
+ */
+#undef KAVL_HEIGHTOF
+#undef KAVL_GET_POINTER
+#undef KAVL_GET_POINTER_NULL
+#undef KAVL_SET_POINTER
+#undef KAVL_SET_POINTER_NULL
+#undef KAVL_NULL
+#undef KAVL_G
+#undef KAVL_E
+#undef KAVL_NE
+#undef KAVL_R_IS_IDENTICAL
+#undef KAVL_R_IS_INTERSECTING
+#undef KAVL_R_IS_IN_RANGE
+