1 | /* $Id: kAvlDestroy.h 29 2009-07-01 20:30:29Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kAvlTmpl - Templated AVL Trees, Destroy the tree.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
|
---|
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 | /**
|
---|
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 pRoot Pointer to the AVL-tree root structure.
|
---|
40 | * @param pfnCallBack Pointer to callback function.
|
---|
41 | * @param pvUser User parameter passed on to the callback function.
|
---|
42 | */
|
---|
43 | KAVL_DECL(int) KAVL_FN(Destroy)(KAVLROOT *pRoot, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
|
---|
44 | {
|
---|
45 | #ifdef KAVL_LOOKTHRU
|
---|
46 | unsigned i;
|
---|
47 | #endif
|
---|
48 | unsigned cEntries;
|
---|
49 | KAVLNODE *apEntries[KAVL_MAX_STACK];
|
---|
50 | int rc;
|
---|
51 |
|
---|
52 | KAVL_WRITE_LOCK(pRoot);
|
---|
53 | if (pRoot->mpRoot == KAVL_NULL)
|
---|
54 | {
|
---|
55 | KAVL_WRITE_UNLOCK(pRoot);
|
---|
56 | return 0;
|
---|
57 | }
|
---|
58 |
|
---|
59 | #ifdef KAVL_LOOKTHRU
|
---|
60 | /*
|
---|
61 | * Kill the lookthru cache.
|
---|
62 | */
|
---|
63 | for (i = 0; i < (KAVL_LOOKTHRU); i++)
|
---|
64 | pRoot->maLookthru[i] = KAVL_NULL;
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | cEntries = 1;
|
---|
68 | apEntries[0] = KAVL_GET_POINTER(&pRoot->mpRoot);
|
---|
69 | while (cEntries > 0)
|
---|
70 | {
|
---|
71 | /*
|
---|
72 | * Process the subtrees first.
|
---|
73 | */
|
---|
74 | KAVLNODE *pNode = apEntries[cEntries - 1];
|
---|
75 | if (pNode->mpLeft != KAVL_NULL)
|
---|
76 | apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
|
---|
77 | else if (pNode->mpRight != KAVL_NULL)
|
---|
78 | apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
|
---|
79 | else
|
---|
80 | {
|
---|
81 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
82 | /*
|
---|
83 | * Process nodes with the same key.
|
---|
84 | */
|
---|
85 | while (pNode->pList != KAVL_NULL)
|
---|
86 | {
|
---|
87 | KAVLNODE *pEqual = KAVL_GET_POINTER(&pNode->pList);
|
---|
88 | KAVL_SET_POINTER(&pNode->pList, KAVL_GET_POINTER_NULL(&pEqual->pList));
|
---|
89 | pEqual->pList = KAVL_NULL;
|
---|
90 |
|
---|
91 | rc = pfnCallBack(pEqual, pvUser);
|
---|
92 | if (rc)
|
---|
93 | {
|
---|
94 | KAVL_WRITE_UNLOCK(pRoot);
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Unlink the node.
|
---|
102 | */
|
---|
103 | if (--cEntries > 0)
|
---|
104 | {
|
---|
105 | KAVLNODE *pParent = apEntries[cEntries - 1];
|
---|
106 | if (KAVL_GET_POINTER(&pParent->mpLeft) == pNode)
|
---|
107 | pParent->mpLeft = KAVL_NULL;
|
---|
108 | else
|
---|
109 | pParent->mpRight = KAVL_NULL;
|
---|
110 | }
|
---|
111 | else
|
---|
112 | pRoot->mpRoot = KAVL_NULL;
|
---|
113 |
|
---|
114 | kHlpAssert(pNode->mpLeft == KAVL_NULL);
|
---|
115 | kHlpAssert(pNode->mpRight == KAVL_NULL);
|
---|
116 | rc = pfnCallBack(pNode, pvUser);
|
---|
117 | if (rc)
|
---|
118 | {
|
---|
119 | KAVL_WRITE_UNLOCK(pRoot);
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | } /* while */
|
---|
124 | kHlpAssert(pRoot->mpRoot == KAVL_NULL);
|
---|
125 |
|
---|
126 | KAVL_WRITE_UNLOCK(pRoot);
|
---|
127 | return 0;
|
---|
128 | }
|
---|
129 |
|
---|