1 | /* $Id: avl_Destroy.cpp.h 4071 2007-08-07 17:07:59Z 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 ([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 | * Iterates thru all nodes in the given tree so the caller can free resources
|
---|
24 | * associated with each node.
|
---|
25 | *
|
---|
26 | * @returns 0 on success.
|
---|
27 | * @returns Return code from the callback on failure. The tree might be half
|
---|
28 | * destroyed at this point and will not behave correctly when any
|
---|
29 | * insert or remove operation is attempted.
|
---|
30 | *
|
---|
31 | * @param ppTree Pointer to the AVL-tree root node pointer.
|
---|
32 | * @param pfnCallBack Pointer to callback function.
|
---|
33 | * @param pvParam User parameter passed on to the callback function.
|
---|
34 | */
|
---|
35 | RTDECL(int) KAVL_FN(Destroy)(PPKAVLNODECORE ppTree, PKAVLCALLBACK pfnCallBack, void *pvParam)
|
---|
36 | {
|
---|
37 | KAVLSTACK2 AVLStack;
|
---|
38 | if (*ppTree == KAVL_NULL)
|
---|
39 | return 0;
|
---|
40 |
|
---|
41 | AVLStack.cEntries = 1;
|
---|
42 | AVLStack.achFlags[0] = 0;
|
---|
43 | AVLStack.aEntries[0] = KAVL_GET_POINTER(ppTree);
|
---|
44 | while (AVLStack.cEntries > 0)
|
---|
45 | {
|
---|
46 | int rc;
|
---|
47 | PKAVLNODECORE pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
|
---|
48 |
|
---|
49 | if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
|
---|
50 | {
|
---|
51 | /* push left and recurse */
|
---|
52 | if (pNode->pLeft != KAVL_NULL)
|
---|
53 | {
|
---|
54 | AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
|
---|
55 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
56 | continue;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /* pop pNode */
|
---|
61 | AVLStack.cEntries--;
|
---|
62 |
|
---|
63 | /* push right */
|
---|
64 | if (pNode->pRight != KAVL_NULL)
|
---|
65 | {
|
---|
66 | AVLStack.achFlags[AVLStack.cEntries] = 0;
|
---|
67 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /* call destructor */
|
---|
71 | pNode->pRight = pNode->pLeft = KAVL_NULL;
|
---|
72 | rc = pfnCallBack(pNode, pvParam);
|
---|
73 | if (rc)
|
---|
74 | return rc;
|
---|
75 |
|
---|
76 | } /* while */
|
---|
77 |
|
---|
78 | *ppTree = KAVL_NULL;
|
---|
79 | return 0;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | #endif
|
---|
84 |
|
---|
85 |
|
---|