1 | /* $Id: avl_RemoveBestFit.cpp.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * kAVLRemoveBestFit - Remove Best Fit routine for AVL trees.
|
---|
4 | * Intended specially on heaps. The tree should allow duplicate keys.
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox base platform packages, as
|
---|
12 | * available from https://www.virtualbox.org.
|
---|
13 | *
|
---|
14 | * This program is free software; you can redistribute it and/or
|
---|
15 | * modify it under the terms of the GNU General Public License
|
---|
16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
17 | * License.
|
---|
18 | *
|
---|
19 | * This program is distributed in the hope that it will be useful, but
|
---|
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | * General Public License for more details.
|
---|
23 | *
|
---|
24 | * You should have received a copy of the GNU General Public License
|
---|
25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
26 | *
|
---|
27 | * The contents of this file may alternatively be used under the terms
|
---|
28 | * of the Common Development and Distribution License Version 1.0
|
---|
29 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
30 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
31 | * CDDL are applicable instead of those of the GPL.
|
---|
32 | *
|
---|
33 | * You may elect to license modified versions of this file under the
|
---|
34 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
35 | *
|
---|
36 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
37 | */
|
---|
38 |
|
---|
39 | #ifndef _kAVLRemoveBestFit_h_
|
---|
40 | #define _kAVLRemoveBestFit_h_
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Finds the best fitting node in the tree for the given Key value.
|
---|
45 | * And removes it.
|
---|
46 | * @returns Pointer to the best fitting node found.
|
---|
47 | * @param ppTree Pointer to Pointer to the tree root node.
|
---|
48 | * @param Key The Key of which is to be found a best fitting match for..
|
---|
49 | * @param fAbove TRUE: Returned node is have the closest key to Key from above.
|
---|
50 | * FALSE: Returned node is have the closest key to Key from below.
|
---|
51 | * @sketch The best fitting node is always located in the searchpath above you.
|
---|
52 | * >= (above): The node where you last turned left.
|
---|
53 | * <= (below): the node where you last turned right.
|
---|
54 | * @remark This implementation should be speeded up slightly!
|
---|
55 | */
|
---|
56 | KAVL_DECL(PKAVLNODECORE) KAVL_FN(RemoveBestFit)(PPKAVLNODECORE ppTree, KAVLKEY Key, bool fAbove)
|
---|
57 | {
|
---|
58 | /*
|
---|
59 | * If we find anything we'll have to remove the node and return it.
|
---|
60 | * But, if duplicate keys are allowed we'll have to check for multiple
|
---|
61 | * nodes first and return one of them before doing an expensive remove+insert.
|
---|
62 | */
|
---|
63 | PKAVLNODECORE pNode = KAVL_FN(GetBestFit)(ppTree, Key, fAbove);
|
---|
64 | if (pNode != NULL)
|
---|
65 | {
|
---|
66 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
67 | if (pNode->pList != KAVL_NULL)
|
---|
68 | {
|
---|
69 | PKAVLNODECORE pRet = KAVL_GET_POINTER(&pNode->pList);
|
---|
70 | KAVL_SET_POINTER_NULL(&pNode->pList, &pRet->pList);
|
---|
71 | return pRet;
|
---|
72 | }
|
---|
73 | #endif
|
---|
74 | pNode = KAVL_FN(Remove)(ppTree, pNode->Key);
|
---|
75 | }
|
---|
76 | return pNode;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | #endif
|
---|