VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/table/avl_Destroy.cpp.h

Last change on this file was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.0 KB
Line 
1/* $Id: avl_Destroy.cpp.h 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * kAVLDestroy - Walk the tree calling a callback to destroy all the nodes.
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 _kAVLDestroy_h_
38#define _kAVLDestroy_h_
39
40
41/**
42 * Destroys the specified tree, starting with the root node and working our way down.
43 *
44 * @returns 0 on success.
45 * @returns Return value from callback on failure. On failure, the tree will be in
46 * an unbalanced condition and only further calls to the Destroy should be
47 * made on it. Note that the node we fail on will be considered dead and
48 * no action is taken to link it back into the tree.
49 * @param ppTree Pointer to the AVL-tree root node pointer.
50 * @param pfnCallBack Pointer to callback function.
51 * @param pvUser User parameter passed on to the callback function.
52 */
53KAVL_DECL(int) KAVL_FN(Destroy)(PPKAVLNODECORE ppTree, PKAVLCALLBACK pfnCallBack, void *pvUser)
54{
55 unsigned cEntries;
56 PKAVLNODECORE apEntries[KAVL_MAX_STACK];
57 int rc;
58
59 if (*ppTree == KAVL_NULL)
60 return VINF_SUCCESS;
61
62 cEntries = 1;
63 apEntries[0] = KAVL_GET_POINTER(ppTree);
64 while (cEntries > 0)
65 {
66 /*
67 * Process the subtrees first.
68 */
69 PKAVLNODECORE pNode = apEntries[cEntries - 1];
70 if (pNode->pLeft != KAVL_NULL)
71 apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
72 else if (pNode->pRight != KAVL_NULL)
73 apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
74 else
75 {
76#ifdef KAVL_EQUAL_ALLOWED
77 /*
78 * Process nodes with the same key.
79 */
80 while (pNode->pList != KAVL_NULL)
81 {
82 PKAVLNODECORE pEqual = KAVL_GET_POINTER(&pNode->pList);
83 KAVL_SET_POINTER(&pNode->pList, KAVL_GET_POINTER_NULL(&pEqual->pList));
84 pEqual->pList = KAVL_NULL;
85
86 rc = pfnCallBack(pEqual, pvUser);
87 if (rc != VINF_SUCCESS)
88 return rc;
89 }
90#endif
91
92 /*
93 * Unlink the node.
94 */
95 if (--cEntries > 0)
96 {
97 PKAVLNODECORE pParent = apEntries[cEntries - 1];
98 if (KAVL_GET_POINTER(&pParent->pLeft) == pNode)
99 pParent->pLeft = KAVL_NULL;
100 else
101 pParent->pRight = KAVL_NULL;
102 }
103 else
104 *ppTree = KAVL_NULL;
105
106 kASSERT(pNode->pLeft == KAVL_NULL);
107 kASSERT(pNode->pRight == KAVL_NULL);
108 rc = pfnCallBack(pNode, pvUser);
109 if (rc != VINF_SUCCESS)
110 return rc;
111 }
112 } /* while */
113
114 kASSERT(*ppTree == KAVL_NULL);
115
116 return VINF_SUCCESS;
117}
118
119#endif
120
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