1 | /* $Id: avl_Destroy.cpp.h 402 2007-01-28 08:44:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * kAVLDestroy - Walk the tree calling a callback to destroy all the nodes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 1999-2004 knut st. osmundsen (bird-src-spam@anduin.net)
|
---|
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 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef _kAVLDestroy_h_
|
---|
23 | #define _kAVLDestroy_h_
|
---|
24 |
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Iterates thru all nodes in the given tree so the caller can free resources
|
---|
28 | * associated with each node.
|
---|
29 | *
|
---|
30 | * @returns 0 on success.
|
---|
31 | * @returns Return code from the callback on failure. The tree might be half
|
---|
32 | * destroyed at this point and will not behave correctly when any
|
---|
33 | * insert or remove operation is attempted.
|
---|
34 | *
|
---|
35 | * @param ppTree Pointer to the AVL-tree root node pointer.
|
---|
36 | * @param pfnCallBack Pointer to callback function.
|
---|
37 | * @param pvParam User parameter passed on to the callback function.
|
---|
38 | */
|
---|
39 | RTDECL(int) KAVL_FN(Destroy)(PPKAVLNODECORE ppTree, PKAVLCALLBACK pfnCallBack, void *pvParam)
|
---|
40 | {
|
---|
41 | KAVLSTACK2 AVLStack;
|
---|
42 | if (*ppTree == KAVL_NULL)
|
---|
43 | return 0;
|
---|
44 |
|
---|
45 | AVLStack.cEntries = 1;
|
---|
46 | AVLStack.achFlags[0] = 0;
|
---|
47 | AVLStack.aEntries[0] = KAVL_GET_POINTER(ppTree);
|
---|
48 | while (AVLStack.cEntries > 0)
|
---|
49 | {
|
---|
50 | int rc;
|
---|
51 | PKAVLNODECORE pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
|
---|
52 |
|
---|
53 | if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
|
---|
54 | {
|
---|
55 | /* push left and recurse */
|
---|
56 | if (pNode->pLeft != KAVL_NULL)
|
---|
57 | {
|
---|
58 | AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
|
---|
59 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
60 | continue;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /* pop pNode */
|
---|
65 | AVLStack.cEntries--;
|
---|
66 |
|
---|
67 | /* push right */
|
---|
68 | if (pNode->pRight != KAVL_NULL)
|
---|
69 | {
|
---|
70 | AVLStack.achFlags[AVLStack.cEntries] = 0;
|
---|
71 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /* call destructor */
|
---|
75 | pNode->pRight = pNode->pLeft = KAVL_NULL;
|
---|
76 | rc = pfnCallBack(pNode, pvParam);
|
---|
77 | if (rc)
|
---|
78 | return rc;
|
---|
79 |
|
---|
80 | } /* while */
|
---|
81 |
|
---|
82 | *ppTree = KAVL_NULL;
|
---|
83 | return 0;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | #endif
|
---|
88 |
|
---|
89 |
|
---|