VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/table/avl_GetBestFit.cpp.h@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 3.8 KB
Line 
1/* $Id: avl_GetBestFit.cpp.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * kAVLGetBestFit - Get 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 _kAVLGetBestFit_h_
40#define _kAVLGetBestFit_h_
41
42
43/**
44 * Finds the best fitting node in the tree for the given Key value.
45 * @returns Pointer to the best fitting node found.
46 * @param ppTree Pointer to Pointer to the tree root node.
47 * @param Key The Key of which is to be found a best fitting match for..
48 * @param fAbove TRUE: Returned node is have the closest key to Key from above.
49 * FALSE: Returned node is have the closest key to Key from below.
50 * @sketch The best fitting node is always located in the searchpath above you.
51 * >= (above): The node where you last turned left.
52 * <= (below): the node where you last turned right.
53 */
54KAVL_DECL(PKAVLNODECORE) KAVL_FN(GetBestFit)(PPKAVLNODECORE ppTree, KAVLKEY Key, bool fAbove)
55{
56 PKAVLNODECORE pNode = KAVL_GET_POINTER_NULL(ppTree);
57 if (pNode)
58 {
59 PKAVLNODECORE pNodeLast = NULL;
60 if (fAbove)
61 { /* pNode->Key >= Key */
62 while (KAVL_NE(pNode->Key, Key))
63 {
64 if (KAVL_G(pNode->Key, Key))
65 {
66 if (pNode->pLeft != KAVL_NULL)
67 {
68 pNodeLast = pNode;
69 pNode = KAVL_GET_POINTER(&pNode->pLeft);
70 }
71 else
72 return pNode;
73 }
74 else
75 {
76 if (pNode->pRight != KAVL_NULL)
77 pNode = KAVL_GET_POINTER(&pNode->pRight);
78 else
79 return pNodeLast;
80 }
81 }
82 }
83 else
84 { /* pNode->Key <= Key */
85 while (KAVL_NE(pNode->Key, Key))
86 {
87 if (KAVL_G(pNode->Key, Key))
88 {
89 if (pNode->pLeft != KAVL_NULL)
90 pNode = KAVL_GET_POINTER(&pNode->pLeft);
91 else
92 return pNodeLast;
93 }
94 else
95 {
96 if (pNode->pRight != KAVL_NULL)
97 {
98 pNodeLast = pNode;
99 pNode = KAVL_GET_POINTER(&pNode->pRight);
100 }
101 else
102 return pNode;
103 }
104 }
105 }
106 }
107
108 /* perfect match or nothing. */
109 return pNode;
110}
111
112
113#endif
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