source: trunk/include/k/kAvlTmpl/kAvlDestroy.h@ 10

Last change on this file since 10 was 7, checked in by bird, 18 years ago

kAVL: Implemented locking, root node and a direct cache.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.2 KB
Line 
1/* $Id: kAvlDestroy.h 7 2008-02-04 02:08:02Z bird $ */
2/** @file
3 * kAvlTmpl - Templated AVL Trees, Destroy the tree.
4 */
5
6/*
7 * Copyright (c) 1999-2007 knut st. osmundsen <bird-src-spam@anduin.net>
8 *
9 * This file is part of kStuff.
10 *
11 * kStuff is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * kStuff is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with kStuff; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 *
26 * As a special exception, since this is a source file and not a header
27 * file, you are granted permission to #include this file as you wish
28 * without this in itself causing the resulting program or whatever to be
29 * covered by the LGPL license. This exception does not however invalidate
30 * any other reasons why the resulting program/whatever should not be
31 * covered the LGPL or GPL.
32 */
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 pRoot Pointer to the AVL-tree root structure.
44 * @param pfnCallBack Pointer to callback function.
45 * @param pvUser User parameter passed on to the callback function.
46 */
47KAVL_DECL(int) KAVL_FN(Destroy)(KAVLROOT *pRoot, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
48{
49#ifdef KAVL_LOOKTHRU
50 unsigned i;
51#endif
52 unsigned cEntries;
53 KAVLNODE *apEntries[KAVL_MAX_STACK];
54 int rc;
55
56 KAVL_WRITE_LOCK(pRoot);
57 if (pRoot->mpRoot == KAVL_NULL)
58 {
59 KAVL_WRITE_UNLOCK(pRoot);
60 return 0;
61 }
62
63#ifdef KAVL_LOOKTHRU
64 /*
65 * Kill the lookthru cache.
66 */
67 for (i = 0; i < (KAVL_LOOKTHRU); i++)
68 pRoot->maLookthru[i] = KAVL_NULL;
69#endif
70
71 cEntries = 1;
72 apEntries[0] = KAVL_GET_POINTER(&pRoot->mpRoot);
73 while (cEntries > 0)
74 {
75 /*
76 * Process the subtrees first.
77 */
78 KAVLNODE *pNode = apEntries[cEntries - 1];
79 if (pNode->mpLeft != KAVL_NULL)
80 apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
81 else if (pNode->mpRight != KAVL_NULL)
82 apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
83 else
84 {
85#ifdef KAVL_EQUAL_ALLOWED
86 /*
87 * Process nodes with the same key.
88 */
89 while (pNode->pList != KAVL_NULL)
90 {
91 KAVLNODE *pEqual = KAVL_GET_POINTER(&pNode->pList);
92 KAVL_SET_POINTER(&pNode->pList, KAVL_GET_POINTER_NULL(&pEqual->pList));
93 pEqual->pList = KAVL_NULL;
94
95 rc = pfnCallBack(pEqual, pvUser);
96 if (rc)
97 {
98 KAVL_WRITE_UNLOCK(pRoot);
99 return rc;
100 }
101 }
102#endif
103
104 /*
105 * Unlink the node.
106 */
107 if (--cEntries > 0)
108 {
109 KAVLNODE *pParent = apEntries[cEntries - 1];
110 if (KAVL_GET_POINTER(&pParent->mpLeft) == pNode)
111 pParent->mpLeft = KAVL_NULL;
112 else
113 pParent->mpRight = KAVL_NULL;
114 }
115 else
116 pRoot->mpRoot = KAVL_NULL;
117
118 kHlpAssert(pNode->mpLeft == KAVL_NULL);
119 kHlpAssert(pNode->mpRight == KAVL_NULL);
120 rc = pfnCallBack(pNode, pvUser);
121 if (rc)
122 {
123 KAVL_WRITE_UNLOCK(pRoot);
124 return rc;
125 }
126 }
127 } /* while */
128 kHlpAssert(pRoot->mpRoot == KAVL_NULL);
129
130 KAVL_WRITE_UNLOCK(pRoot);
131 return 0;
132}
133
Note: See TracBrowser for help on using the repository browser.