summaryrefslogtreecommitdiff
path: root/src/VBox/Runtime/common/table
diff options
context:
space:
mode:
authorFelix Geyer <debfx-pkg@fobos.de>2012-03-14 12:03:25 +0100
committerFelix Geyer <debfx-pkg@fobos.de>2012-03-14 12:03:25 +0100
commit6718f4631b859139b072de5a0406d781d60f9084 (patch)
tree4cb1b24186b5a02cbb0737a710a784e8ae2d04d2 /src/VBox/Runtime/common/table
parentfe1df57ef1efe45f85243a527a295b5fd2e4e778 (diff)
downloadvirtualbox-6718f4631b859139b072de5a0406d781d60f9084.tar.gz
Imported Upstream version 4.1.10-dfsgupstream/4.1.10-dfsg
Diffstat (limited to 'src/VBox/Runtime/common/table')
-rw-r--r--src/VBox/Runtime/common/table/avl_RemoveNode.cpp.h144
-rw-r--r--src/VBox/Runtime/common/table/avllu32.cpp1
2 files changed, 145 insertions, 0 deletions
diff --git a/src/VBox/Runtime/common/table/avl_RemoveNode.cpp.h b/src/VBox/Runtime/common/table/avl_RemoveNode.cpp.h
new file mode 100644
index 000000000..179e1359b
--- /dev/null
+++ b/src/VBox/Runtime/common/table/avl_RemoveNode.cpp.h
@@ -0,0 +1,144 @@
+/* $Id: avl_RemoveNode.cpp.h $ */
+/** @file
+ * kAVLRemove2 - Remove specific node (by pointer) from an AVL tree.
+ */
+
+/*
+ * Copyright (C) 2001-2002 knut st. osmundsen (bird-src-spam@anduin.net)
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * The contents of this file may alternatively be used under the terms
+ * of the Common Development and Distribution License Version 1.0
+ * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
+ * VirtualBox OSE distribution, in which case the provisions of the
+ * CDDL are applicable instead of those of the GPL.
+ *
+ * You may elect to license modified versions of this file under the
+ * terms and conditions of either the GPL or the CDDL or both.
+ */
+
+
+/**
+ * Removes the specified node from the tree.
+ *
+ * @returns Pointer to the removed node (NULL if not in the tree)
+ * @param ppTree Pointer to the AVL-tree root structure.
+ * @param pNode Pointer to the node to be removed.
+ *
+ * @remark This implementation isn't the most efficient, but it's relatively
+ * short and easier to manage.
+ */
+KAVL_DECL(PKAVLNODECORE) KAVL_FN(RemoveNode)(PPKAVLNODECORE ppTree, PKAVLNODECORE pNode)
+{
+#ifdef KAVL_EQUAL_ALLOWED
+ /*
+ * Find the right node by key together with the parent node.
+ */
+ KAVLKEY const Key = pNode->Key;
+ PKAVLNODECORE pParent = NULL;
+ PKAVLNODECORE pCurNode = KAVL_GET_POINTER_NULL(ppTree);
+ if (!pCurNode)
+ return NULL;
+ while (KAVL_NE(pCurNode->Key, Key))
+ {
+ pParent = pCurNode;
+ if (KAVL_G(pCurNode->Key, Key))
+ {
+ if (pCurNode->pLeft != KAVL_NULL)
+ pCurNode = KAVL_GET_POINTER(&pCurNode->pLeft);
+ else
+ return NULL;
+ }
+ else
+ {
+ if (pCurNode->pRight != KAVL_NULL)
+ pCurNode = KAVL_GET_POINTER(&pCurNode->pRight);
+ else
+ return NULL;
+ }
+ }
+
+ if (pCurNode != pNode)
+ {
+ /*
+ * It's not the one we want, but it could be in the duplicate list.
+ */
+ while (pCurNode->pList != KAVL_NULL)
+ {
+ PKAVLNODECORE pNext = KAVL_GET_POINTER(&pCurNode->pList);
+ if (pNext == pNode)
+ {
+ if (pNode->pList != KAVL_NULL)
+ KAVL_SET_POINTER(&pCurNode->pList, KAVL_GET_POINTER(&pNode->pList));
+ else
+ pCurNode->pList = KAVL_NULL;
+ pNode->pList = KAVL_NULL;
+ return pNode;
+ }
+ pCurNode = pNext;
+ }
+ 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->pList == KAVL_NULL)
+ KAVL_FN(Remove)(ppTree, pNode->Key);
+ else
+ {
+ PKAVLNODECORE pNewUs = KAVL_GET_POINTER(&pNode->pList);
+
+ pNewUs->uchHeight = pNode->uchHeight;
+
+ if (pNode->pLeft != KAVL_NULL)
+ KAVL_SET_POINTER(&pNewUs->pLeft, KAVL_GET_POINTER(&pNode->pLeft));
+ else
+ pNewUs->pLeft = KAVL_NULL;
+
+ if (pNode->pRight != KAVL_NULL)
+ KAVL_SET_POINTER(&pNewUs->pRight, KAVL_GET_POINTER(&pNode->pRight));
+ else
+ pNewUs->pRight = KAVL_NULL;
+
+ if (pParent)
+ {
+ if (KAVL_GET_POINTER_NULL(&pParent->pLeft) == pNode)
+ KAVL_SET_POINTER(&pParent->pLeft, pNewUs);
+ else
+ KAVL_SET_POINTER(&pParent->pRight, pNewUs);
+ }
+ else
+ KAVL_SET_POINTER(ppTree, pNewUs);
+ }
+
+ 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->Key);
+ if (pRemovedNode == pNode)
+ return pRemovedNode;
+
+ KAVL_FN(Insert)(pRoot, pRemovedNode);
+ return NULL;
+#endif
+}
+
+
diff --git a/src/VBox/Runtime/common/table/avllu32.cpp b/src/VBox/Runtime/common/table/avllu32.cpp
index 587a5feb3..45bc2a2f0 100644
--- a/src/VBox/Runtime/common/table/avllu32.cpp
+++ b/src/VBox/Runtime/common/table/avllu32.cpp
@@ -70,6 +70,7 @@ static const char szFileId[] = "Id: kAVLULInt.c,v 1.4 2003/02/13 02:02:38 bird E
#include "avl_Base.cpp.h"
#include "avl_Get.cpp.h"
#include "avl_GetBestFit.cpp.h"
+#include "avl_RemoveNode.cpp.h"
#include "avl_RemoveBestFit.cpp.h"
#include "avl_DoWithAll.cpp.h"
#include "avl_Destroy.cpp.h"