VirtualBox

source: kStuff/trunk/include/k/kAvlTmpl/kAvlDoWithAll.h@ 75

Last change on this file since 75 was 29, checked in by bird, 15 years ago

Finally got around execute the switch to the MIT license.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.5 KB
Line 
1/* $Id: kAvlDoWithAll.h 29 2009-07-01 20:30:29Z bird $ */
2/** @file
3 * kAvlTmpl - Templated AVL Trees, The Callback Iterator.
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* Structures and Typedefs *
33*******************************************************************************/
34/**
35 * Stack used by DoWithAll to avoid recusive function calls.
36 */
37typedef struct
38{
39 unsigned cEntries;
40 KAVLNODE *aEntries[KAVL_MAX_STACK];
41 char achFlags[KAVL_MAX_STACK];
42 KAVLROOT pRoot;
43} KAVL_INT(STACK2);
44
45
46/**
47 * Iterates thru all nodes in the given tree.
48 *
49 * @returns 0 on success. Return from callback on failure.
50 * @param pRoot Pointer to the AVL-tree root structure.
51 * @param fFromLeft K_TRUE: Left to right.
52 * K_FALSE: Right to left.
53 * @param pfnCallBack Pointer to callback function.
54 * @param pvUser User parameter passed on to the callback function.
55 */
56KAVL_DECL(int) KAVL_FN(DoWithAll)(KAVLROOT *pRoot, KBOOL fFromLeft, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
57{
58 KAVL_INT(STACK2) AVLStack;
59 KAVLNODE *pNode;
60#ifdef KAVL_EQUAL_ALLOWED
61 KAVLNODE *pEqual;
62#endif
63 int rc;
64
65 KAVL_READ_LOCK(pRoot);
66 if (pRoot->mpRoot == KAVL_NULL)
67 {
68 KAVL_READ_UNLOCK(pRoot);
69 return 0;
70 }
71
72 AVLStack.cEntries = 1;
73 AVLStack.achFlags[0] = 0;
74 AVLStack.aEntries[0] = KAVL_GET_POINTER(&pRoot->mpRoot);
75
76 if (fFromLeft)
77 { /* from left */
78 while (AVLStack.cEntries > 0)
79 {
80 pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
81
82 /* left */
83 if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
84 {
85 if (pNode->mpLeft != KAVL_NULL)
86 {
87 AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
88 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
89 continue;
90 }
91 }
92
93 /* center */
94 rc = pfnCallBack(pNode, pvUser);
95 if (rc)
96 return rc;
97#ifdef KAVL_EQUAL_ALLOWED
98 if (pNode->mpList != KAVL_NULL)
99 for (pEqual = KAVL_GET_POINTER(&pNode->mpList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->mpList))
100 {
101 rc = pfnCallBack(pEqual, pvUser);
102 if (rc)
103 {
104 KAVL_READ_UNLOCK(pRoot);
105 return rc;
106 }
107 }
108#endif
109
110 /* right */
111 AVLStack.cEntries--;
112 if (pNode->mpRight != KAVL_NULL)
113 {
114 AVLStack.achFlags[AVLStack.cEntries] = 0;
115 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
116 }
117 } /* while */
118 }
119 else
120 { /* from right */
121 while (AVLStack.cEntries > 0)
122 {
123 pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
124
125 /* right */
126 if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
127 {
128 if (pNode->mpRight != KAVL_NULL)
129 {
130 AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
131 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
132 continue;
133 }
134 }
135
136 /* center */
137 rc = pfnCallBack(pNode, pvUser);
138 if (rc)
139 return rc;
140#ifdef KAVL_EQUAL_ALLOWED
141 if (pNode->mpList != KAVL_NULL)
142 for (pEqual = KAVL_GET_POINTER(&pNode->mpList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->pList))
143 {
144 rc = pfnCallBack(pEqual, pvUser);
145 if (rc)
146 {
147 KAVL_READ_UNLOCK(pRoot);
148 return rc;
149 }
150 }
151#endif
152
153 /* left */
154 AVLStack.cEntries--;
155 if (pNode->mpLeft != KAVL_NULL)
156 {
157 AVLStack.achFlags[AVLStack.cEntries] = 0;
158 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
159 }
160 } /* while */
161 }
162
163 KAVL_READ_UNLOCK(pRoot);
164 return 0;
165}
166
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette