source: trunk/include/k/kRbTmpl/kRbDestroy.h@ 91

Last change on this file since 91 was 35, checked in by bird, 16 years ago

Templated red-black trees - kick off.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.2 KB
Line 
1/* $Id: kRbDestroy.h 35 2009-11-08 19:39:03Z bird $ */
2/** @file
3 * kRbTmpl - Templated Red-Black 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 Red-Back tree's root structure.
40 * @param pfnCallBack Pointer to callback function.
41 * @param pvUser User parameter passed on to the callback function.
42 */
43KRB_DECL(int) KRB_FN(Destroy)(KRBROOT *pRoot, KRB_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
44{
45#ifdef KRB_CACHE_SIZE
46 unsigned i;
47#endif
48 unsigned cEntries;
49 KRBNODE *apEntries[KRB_MAX_STACK];
50 int rc;
51
52 KRB_WRITE_LOCK(pRoot);
53 if (pRoot->mpRoot == KRB_NULL)
54 {
55 KRB_WRITE_UNLOCK(pRoot);
56 return 0;
57 }
58
59#ifdef KRB_CACHE_SIZE
60 /*
61 * Kill the lookthru cache.
62 */
63 for (i = 0; i < (KRB_CACHE_SIZE); i++)
64 pRoot->maLookthru[i] = KRB_NULL;
65#endif
66
67 cEntries = 1;
68 apEntries[0] = KRB_GET_POINTER(&pRoot->mpRoot);
69 while (cEntries > 0)
70 {
71 /*
72 * Process the subtrees first.
73 */
74 KRBNODE *pNode = apEntries[cEntries - 1];
75 if (pNode->mpLeft != KRB_NULL)
76 apEntries[cEntries++] = KRB_GET_POINTER(&pNode->mpLeft);
77 else if (pNode->mpRight != KRB_NULL)
78 apEntries[cEntries++] = KRB_GET_POINTER(&pNode->mpRight);
79 else
80 {
81#ifdef KRB_EQUAL_ALLOWED
82 /*
83 * Process nodes with the same key.
84 */
85 while (pNode->pList != KRB_NULL)
86 {
87 KRBNODE *pEqual = KRB_GET_POINTER(&pNode->pList);
88 KRB_SET_POINTER(&pNode->pList, KRB_GET_POINTER_NULL(&pEqual->pList));
89 pEqual->pList = KRB_NULL;
90
91 rc = pfnCallBack(pEqual, pvUser);
92 if (rc)
93 {
94 KRB_WRITE_UNLOCK(pRoot);
95 return rc;
96 }
97 }
98#endif
99
100 /*
101 * Unlink the node.
102 */
103 if (--cEntries > 0)
104 {
105 KRBNODE *pParent = apEntries[cEntries - 1];
106 if (KRB_GET_POINTER(&pParent->mpLeft) == pNode)
107 pParent->mpLeft = KRB_NULL;
108 else
109 pParent->mpRight = KRB_NULL;
110 }
111 else
112 pRoot->mpRoot = KRB_NULL;
113
114 kHlpAssert(pNode->mpLeft == KRB_NULL);
115 kHlpAssert(pNode->mpRight == KRB_NULL);
116 rc = pfnCallBack(pNode, pvUser);
117 if (rc)
118 {
119 KRB_WRITE_UNLOCK(pRoot);
120 return rc;
121 }
122 }
123 } /* while */
124 kHlpAssert(pRoot->mpRoot == KRB_NULL);
125
126 KRB_WRITE_UNLOCK(pRoot);
127 return 0;
128}
129
Note: See TracBrowser for help on using the repository browser.