1 | /* $Id: avl_DoWithAll.cpp.h 65208 2017-01-09 14:39:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * kAVLDoWithAll - Do with all nodes routine for AVL trees.
|
---|
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 _kAVLDoWithAll_h_
|
---|
32 | #define _kAVLDoWithAll_h_
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Iterates thru all nodes in the given tree.
|
---|
37 | * @returns 0 on success. Return from callback on failure.
|
---|
38 | * @param ppTree Pointer to the AVL-tree root node pointer.
|
---|
39 | * @param fFromLeft TRUE: Left to right.
|
---|
40 | * FALSE: Right to left.
|
---|
41 | * @param pfnCallBack Pointer to callback function.
|
---|
42 | * @param pvParam Userparameter passed on to the callback function.
|
---|
43 | */
|
---|
44 | KAVL_DECL(int) KAVL_FN(DoWithAll)(PPKAVLNODECORE ppTree, int fFromLeft, PKAVLCALLBACK pfnCallBack, void * pvParam)
|
---|
45 | {
|
---|
46 | KAVLSTACK2 AVLStack;
|
---|
47 | PKAVLNODECORE pNode;
|
---|
48 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
49 | PKAVLNODECORE pEqual;
|
---|
50 | #endif
|
---|
51 | int rc;
|
---|
52 |
|
---|
53 | if (*ppTree == KAVL_NULL)
|
---|
54 | return VINF_SUCCESS;
|
---|
55 |
|
---|
56 | AVLStack.cEntries = 1;
|
---|
57 | AVLStack.achFlags[0] = 0;
|
---|
58 | AVLStack.aEntries[0] = KAVL_GET_POINTER(ppTree);
|
---|
59 |
|
---|
60 | if (fFromLeft)
|
---|
61 | { /* from left */
|
---|
62 | while (AVLStack.cEntries > 0)
|
---|
63 | {
|
---|
64 | pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
|
---|
65 |
|
---|
66 | /* left */
|
---|
67 | if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
|
---|
68 | {
|
---|
69 | if (pNode->pLeft != KAVL_NULL)
|
---|
70 | {
|
---|
71 | AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
|
---|
72 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
73 | continue;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | /* center */
|
---|
78 | rc = pfnCallBack(pNode, pvParam);
|
---|
79 | if (rc != VINF_SUCCESS)
|
---|
80 | return rc;
|
---|
81 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
82 | if (pNode->pList != KAVL_NULL)
|
---|
83 | for (pEqual = KAVL_GET_POINTER(&pNode->pList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->pList))
|
---|
84 | {
|
---|
85 | rc = pfnCallBack(pEqual, pvParam);
|
---|
86 | if (rc != VINF_SUCCESS)
|
---|
87 | return rc;
|
---|
88 | }
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | /* right */
|
---|
92 | AVLStack.cEntries--;
|
---|
93 | if (pNode->pRight != KAVL_NULL)
|
---|
94 | {
|
---|
95 | AVLStack.achFlags[AVLStack.cEntries] = 0;
|
---|
96 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
|
---|
97 | }
|
---|
98 | } /* while */
|
---|
99 | }
|
---|
100 | else
|
---|
101 | { /* from right */
|
---|
102 | while (AVLStack.cEntries > 0)
|
---|
103 | {
|
---|
104 | pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
|
---|
105 |
|
---|
106 | /* right */
|
---|
107 | if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
|
---|
108 | {
|
---|
109 | if (pNode->pRight != KAVL_NULL)
|
---|
110 | {
|
---|
111 | AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
|
---|
112 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
|
---|
113 | continue;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* center */
|
---|
118 | rc = pfnCallBack(pNode, pvParam);
|
---|
119 | if (rc != VINF_SUCCESS)
|
---|
120 | return rc;
|
---|
121 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
122 | if (pNode->pList != KAVL_NULL)
|
---|
123 | for (pEqual = KAVL_GET_POINTER(&pNode->pList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->pList))
|
---|
124 | {
|
---|
125 | rc = pfnCallBack(pEqual, pvParam);
|
---|
126 | if (rc != VINF_SUCCESS)
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | /* left */
|
---|
132 | AVLStack.cEntries--;
|
---|
133 | if (pNode->pLeft != KAVL_NULL)
|
---|
134 | {
|
---|
135 | AVLStack.achFlags[AVLStack.cEntries] = 0;
|
---|
136 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
137 | }
|
---|
138 | } /* while */
|
---|
139 | }
|
---|
140 |
|
---|
141 | return VINF_SUCCESS;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | #endif
|
---|
146 |
|
---|