1 | /* $Id: avl_DoWithAll.cpp.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * kAVLDoWithAll - Do with all nodes routine for AVL trees.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef _kAVLDoWithAll_h_
|
---|
38 | #define _kAVLDoWithAll_h_
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Iterates thru all nodes in the given tree.
|
---|
43 | * @returns 0 on success. Return from callback on failure.
|
---|
44 | * @param ppTree Pointer to the AVL-tree root node pointer.
|
---|
45 | * @param fFromLeft TRUE: Left to right.
|
---|
46 | * FALSE: Right to left.
|
---|
47 | * @param pfnCallBack Pointer to callback function.
|
---|
48 | * @param pvParam Userparameter passed on to the callback function.
|
---|
49 | */
|
---|
50 | KAVL_DECL(int) KAVL_FN(DoWithAll)(PPKAVLNODECORE ppTree, int fFromLeft, PKAVLCALLBACK pfnCallBack, void * pvParam)
|
---|
51 | {
|
---|
52 | KAVLSTACK2 AVLStack;
|
---|
53 | PKAVLNODECORE pNode;
|
---|
54 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
55 | PKAVLNODECORE pEqual;
|
---|
56 | #endif
|
---|
57 | int rc;
|
---|
58 |
|
---|
59 | if (*ppTree == KAVL_NULL)
|
---|
60 | return VINF_SUCCESS;
|
---|
61 |
|
---|
62 | AVLStack.cEntries = 1;
|
---|
63 | AVLStack.achFlags[0] = 0;
|
---|
64 | AVLStack.aEntries[0] = KAVL_GET_POINTER(ppTree);
|
---|
65 |
|
---|
66 | if (fFromLeft)
|
---|
67 | { /* from left */
|
---|
68 | while (AVLStack.cEntries > 0)
|
---|
69 | {
|
---|
70 | pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
|
---|
71 |
|
---|
72 | /* left */
|
---|
73 | if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
|
---|
74 | {
|
---|
75 | if (pNode->pLeft != KAVL_NULL)
|
---|
76 | {
|
---|
77 | AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
|
---|
78 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
79 | continue;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /* center */
|
---|
84 | Assert(pNode->uchHeight == RT_MAX(AVL_HEIGHTOF(KAVL_GET_POINTER_NULL(&pNode->pLeft)),
|
---|
85 | AVL_HEIGHTOF(KAVL_GET_POINTER_NULL(&pNode->pRight))) + 1);
|
---|
86 | rc = pfnCallBack(pNode, pvParam);
|
---|
87 | if (rc != VINF_SUCCESS)
|
---|
88 | return rc;
|
---|
89 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
90 | if (pNode->pList != KAVL_NULL)
|
---|
91 | for (pEqual = KAVL_GET_POINTER(&pNode->pList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->pList))
|
---|
92 | {
|
---|
93 | rc = pfnCallBack(pEqual, pvParam);
|
---|
94 | if (rc != VINF_SUCCESS)
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | /* right */
|
---|
100 | AVLStack.cEntries--;
|
---|
101 | if (pNode->pRight != KAVL_NULL)
|
---|
102 | {
|
---|
103 | AVLStack.achFlags[AVLStack.cEntries] = 0;
|
---|
104 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
|
---|
105 | }
|
---|
106 | } /* while */
|
---|
107 | }
|
---|
108 | else
|
---|
109 | { /* from right */
|
---|
110 | while (AVLStack.cEntries > 0)
|
---|
111 | {
|
---|
112 | pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
|
---|
113 |
|
---|
114 | /* right */
|
---|
115 | if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
|
---|
116 | {
|
---|
117 | if (pNode->pRight != KAVL_NULL)
|
---|
118 | {
|
---|
119 | AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
|
---|
120 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
|
---|
121 | continue;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* center */
|
---|
126 | Assert(pNode->uchHeight == RT_MAX(AVL_HEIGHTOF(KAVL_GET_POINTER_NULL(&pNode->pLeft)),
|
---|
127 | AVL_HEIGHTOF(KAVL_GET_POINTER_NULL(&pNode->pRight))) + 1);
|
---|
128 | rc = pfnCallBack(pNode, pvParam);
|
---|
129 | if (rc != VINF_SUCCESS)
|
---|
130 | return rc;
|
---|
131 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
132 | if (pNode->pList != KAVL_NULL)
|
---|
133 | for (pEqual = KAVL_GET_POINTER(&pNode->pList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->pList))
|
---|
134 | {
|
---|
135 | rc = pfnCallBack(pEqual, pvParam);
|
---|
136 | if (rc != VINF_SUCCESS)
|
---|
137 | return rc;
|
---|
138 | }
|
---|
139 | #endif
|
---|
140 |
|
---|
141 | /* left */
|
---|
142 | AVLStack.cEntries--;
|
---|
143 | if (pNode->pLeft != KAVL_NULL)
|
---|
144 | {
|
---|
145 | AVLStack.achFlags[AVLStack.cEntries] = 0;
|
---|
146 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
147 | }
|
---|
148 | } /* while */
|
---|
149 | }
|
---|
150 |
|
---|
151 | return VINF_SUCCESS;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | #endif
|
---|
156 |
|
---|