VirtualBox

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

Last change on this file since 5422 was 4687, checked in by vboxsync, 17 years ago

Added RTAvllU32*. Applied enumeration fixes for non-unique tree. Applied Destroy fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1/* $Id: avl_Destroy.cpp.h 4687 2007-09-11 09:13:32Z vboxsync $ */
2/** @file
3 * kAVLDestroy - Walk the tree calling a callback to destroy all the nodes.
4 */
5
6/*
7 * Copyright (C) 1999-2007 knut st. osmundsen ([email protected])
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef _kAVLDestroy_h_
19#define _kAVLDestroy_h_
20
21
22/**
23 * Destroys the specified tree, starting with the root node and working our way down.
24 *
25 * @returns 0 on success.
26 * @returns Return value from callback on failure. On failure, the tree will be in
27 * an unbalanced condition and only further calls to the Destroy should be
28 * made on it. Note that the node we fail on will be considered dead and
29 * no action is taken to link it back into the tree.
30 * @param ppTree Pointer to the AVL-tree root node pointer.
31 * @param pfnCallBack Pointer to callback function.
32 * @param pvUser User parameter passed on to the callback function.
33 */
34RTDECL(int) KAVL_FN(Destroy)(PPKAVLNODECORE ppTree, PKAVLCALLBACK pfnCallBack, void *pvUser)
35{
36 unsigned cEntries;
37 PKAVLNODECORE apEntries[KAVL_MAX_STACK];
38 int rc;
39
40 if (*ppTree == KAVL_NULL)
41 return 0;
42
43 cEntries = 1;
44 apEntries[0] = KAVL_GET_POINTER(ppTree);
45 while (cEntries > 0)
46 {
47 /*
48 * Process the subtrees first.
49 */
50 PKAVLNODECORE pNode = apEntries[cEntries - 1];
51 if (pNode->pLeft != KAVL_NULL)
52 apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
53 else if (pNode->pRight != KAVL_NULL)
54 apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
55 else
56 {
57#ifdef KAVL_EQUAL_ALLOWED
58 /*
59 * Process nodes with the same key.
60 */
61 while (pNode->pList != KAVL_NULL)
62 {
63 PKAVLNODECORE pEqual = KAVL_GET_POINTER(&pNode->pList);
64 KAVL_SET_POINTER(&pNode->pList, KAVL_GET_POINTER_NULL(&pEqual->pList));
65 pEqual->pList = KAVL_NULL;
66
67 rc = pfnCallBack(pEqual, pvUser);
68 if (rc)
69 return rc;
70 }
71#endif
72
73 /*
74 * Unlink the node.
75 */
76 if (--cEntries > 0)
77 {
78 PKAVLNODECORE pParent = apEntries[cEntries - 1];
79 if (KAVL_GET_POINTER(&pParent->pLeft) == pNode)
80 pParent->pLeft = KAVL_NULL;
81 else
82 pParent->pRight = KAVL_NULL;
83 }
84 else
85 *ppTree = KAVL_NULL;
86
87 kASSERT(pNode->pLeft == KAVL_NULL);
88 kASSERT(pNode->pRight == KAVL_NULL);
89 rc = pfnCallBack(pNode, pvUser);
90 if (rc)
91 return rc;
92 }
93 } /* while */
94
95 kASSERT(*ppTree == KAVL_NULL);
96
97 return 0;
98}
99
100#endif
101
102
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