1 | /* $Id: avl_Destroy.cpp.h 65208 2017-01-09 14:39:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * kAVLDestroy - Walk the tree calling a callback to destroy all the nodes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 1999-2011 knut st. osmundsen ([email protected])
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifndef _kAVLDestroy_h_
|
---|
32 | #define _kAVLDestroy_h_
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Destroys the specified tree, starting with the root node and working our way down.
|
---|
37 | *
|
---|
38 | * @returns 0 on success.
|
---|
39 | * @returns Return value from callback on failure. On failure, the tree will be in
|
---|
40 | * an unbalanced condition and only further calls to the Destroy should be
|
---|
41 | * made on it. Note that the node we fail on will be considered dead and
|
---|
42 | * no action is taken to link it back into the tree.
|
---|
43 | * @param ppTree Pointer to the AVL-tree root node pointer.
|
---|
44 | * @param pfnCallBack Pointer to callback function.
|
---|
45 | * @param pvUser User parameter passed on to the callback function.
|
---|
46 | */
|
---|
47 | KAVL_DECL(int) KAVL_FN(Destroy)(PPKAVLNODECORE ppTree, PKAVLCALLBACK pfnCallBack, void *pvUser)
|
---|
48 | {
|
---|
49 | unsigned cEntries;
|
---|
50 | PKAVLNODECORE apEntries[KAVL_MAX_STACK];
|
---|
51 | int rc;
|
---|
52 |
|
---|
53 | if (*ppTree == KAVL_NULL)
|
---|
54 | return VINF_SUCCESS;
|
---|
55 |
|
---|
56 | cEntries = 1;
|
---|
57 | apEntries[0] = KAVL_GET_POINTER(ppTree);
|
---|
58 | while (cEntries > 0)
|
---|
59 | {
|
---|
60 | /*
|
---|
61 | * Process the subtrees first.
|
---|
62 | */
|
---|
63 | PKAVLNODECORE pNode = apEntries[cEntries - 1];
|
---|
64 | if (pNode->pLeft != KAVL_NULL)
|
---|
65 | apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
66 | else if (pNode->pRight != KAVL_NULL)
|
---|
67 | apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
|
---|
68 | else
|
---|
69 | {
|
---|
70 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
71 | /*
|
---|
72 | * Process nodes with the same key.
|
---|
73 | */
|
---|
74 | while (pNode->pList != KAVL_NULL)
|
---|
75 | {
|
---|
76 | PKAVLNODECORE pEqual = KAVL_GET_POINTER(&pNode->pList);
|
---|
77 | KAVL_SET_POINTER(&pNode->pList, KAVL_GET_POINTER_NULL(&pEqual->pList));
|
---|
78 | pEqual->pList = KAVL_NULL;
|
---|
79 |
|
---|
80 | rc = pfnCallBack(pEqual, pvUser);
|
---|
81 | if (rc != VINF_SUCCESS)
|
---|
82 | return rc;
|
---|
83 | }
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Unlink the node.
|
---|
88 | */
|
---|
89 | if (--cEntries > 0)
|
---|
90 | {
|
---|
91 | PKAVLNODECORE pParent = apEntries[cEntries - 1];
|
---|
92 | if (KAVL_GET_POINTER(&pParent->pLeft) == pNode)
|
---|
93 | pParent->pLeft = KAVL_NULL;
|
---|
94 | else
|
---|
95 | pParent->pRight = KAVL_NULL;
|
---|
96 | }
|
---|
97 | else
|
---|
98 | *ppTree = KAVL_NULL;
|
---|
99 |
|
---|
100 | kASSERT(pNode->pLeft == KAVL_NULL);
|
---|
101 | kASSERT(pNode->pRight == KAVL_NULL);
|
---|
102 | rc = pfnCallBack(pNode, pvUser);
|
---|
103 | if (rc != VINF_SUCCESS)
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 | } /* while */
|
---|
107 |
|
---|
108 | kASSERT(*ppTree == KAVL_NULL);
|
---|
109 |
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 | #endif
|
---|
114 |
|
---|