1 | /* $Id: avl_Destroy.cpp.h 5999 2007-12-07 15:05:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * kAVLDestroy - Walk the tree calling a callback to destroy all the nodes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 1999-2007 knut st. osmundsen ([email protected])
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef _kAVLDestroy_h_
|
---|
28 | #define _kAVLDestroy_h_
|
---|
29 |
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Destroys the specified tree, starting with the root node and working our way down.
|
---|
33 | *
|
---|
34 | * @returns 0 on success.
|
---|
35 | * @returns Return value from callback on failure. On failure, the tree will be in
|
---|
36 | * an unbalanced condition and only further calls to the Destroy should be
|
---|
37 | * made on it. Note that the node we fail on will be considered dead and
|
---|
38 | * no action is taken to link it back into the tree.
|
---|
39 | * @param ppTree Pointer to the AVL-tree root node pointer.
|
---|
40 | * @param pfnCallBack Pointer to callback function.
|
---|
41 | * @param pvUser User parameter passed on to the callback function.
|
---|
42 | */
|
---|
43 | RTDECL(int) KAVL_FN(Destroy)(PPKAVLNODECORE ppTree, PKAVLCALLBACK pfnCallBack, void *pvUser)
|
---|
44 | {
|
---|
45 | unsigned cEntries;
|
---|
46 | PKAVLNODECORE apEntries[KAVL_MAX_STACK];
|
---|
47 | int rc;
|
---|
48 |
|
---|
49 | if (*ppTree == KAVL_NULL)
|
---|
50 | return 0;
|
---|
51 |
|
---|
52 | cEntries = 1;
|
---|
53 | apEntries[0] = KAVL_GET_POINTER(ppTree);
|
---|
54 | while (cEntries > 0)
|
---|
55 | {
|
---|
56 | /*
|
---|
57 | * Process the subtrees first.
|
---|
58 | */
|
---|
59 | PKAVLNODECORE pNode = apEntries[cEntries - 1];
|
---|
60 | if (pNode->pLeft != KAVL_NULL)
|
---|
61 | apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
62 | else if (pNode->pRight != KAVL_NULL)
|
---|
63 | apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
|
---|
64 | else
|
---|
65 | {
|
---|
66 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
67 | /*
|
---|
68 | * Process nodes with the same key.
|
---|
69 | */
|
---|
70 | while (pNode->pList != KAVL_NULL)
|
---|
71 | {
|
---|
72 | PKAVLNODECORE pEqual = KAVL_GET_POINTER(&pNode->pList);
|
---|
73 | KAVL_SET_POINTER(&pNode->pList, KAVL_GET_POINTER_NULL(&pEqual->pList));
|
---|
74 | pEqual->pList = KAVL_NULL;
|
---|
75 |
|
---|
76 | rc = pfnCallBack(pEqual, pvUser);
|
---|
77 | if (rc)
|
---|
78 | return rc;
|
---|
79 | }
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Unlink the node.
|
---|
84 | */
|
---|
85 | if (--cEntries > 0)
|
---|
86 | {
|
---|
87 | PKAVLNODECORE pParent = apEntries[cEntries - 1];
|
---|
88 | if (KAVL_GET_POINTER(&pParent->pLeft) == pNode)
|
---|
89 | pParent->pLeft = KAVL_NULL;
|
---|
90 | else
|
---|
91 | pParent->pRight = KAVL_NULL;
|
---|
92 | }
|
---|
93 | else
|
---|
94 | *ppTree = KAVL_NULL;
|
---|
95 |
|
---|
96 | kASSERT(pNode->pLeft == KAVL_NULL);
|
---|
97 | kASSERT(pNode->pRight == KAVL_NULL);
|
---|
98 | rc = pfnCallBack(pNode, pvUser);
|
---|
99 | if (rc)
|
---|
100 | return rc;
|
---|
101 | }
|
---|
102 | } /* while */
|
---|
103 |
|
---|
104 | kASSERT(*ppTree == KAVL_NULL);
|
---|
105 |
|
---|
106 | return 0;
|
---|
107 | }
|
---|
108 |
|
---|
109 | #endif
|
---|
110 |
|
---|
111 |
|
---|