VirtualBox

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

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

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1/* $Id: avl_GetBestFit.cpp.h 4071 2007-08-07 17:07:59Z 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) 1999-2002 knut st. osmundsen (bird-src-spam@anduin.net)
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License as published by the Free Software Foundation,
15 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
16 * distribution. VirtualBox OSE is distributed in the hope that it will
17 * be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#ifndef _kAVLGetBestFit_h_
21#define _kAVLGetBestFit_h_
22
23
24/**
25 * Finds the best fitting node in the tree for the given Key value.
26 * @returns Pointer to the best fitting node found.
27 * @param ppTree Pointer to Pointer to the tree root node.
28 * @param Key The Key of which is to be found a best fitting match for..
29 * @param fAbove TRUE: Returned node is have the closest key to Key from above.
30 * FALSE: Returned node is have the closest key to Key from below.
31 * @sketch The best fitting node is always located in the searchpath above you.
32 * >= (above): The node where you last turned left.
33 * <= (below): the node where you last turned right.
34 */
35RTDECL(PKAVLNODECORE) KAVL_FN(GetBestFit)(PPKAVLNODECORE ppTree, KAVLKEY Key, bool fAbove)
36{
37 register PKAVLNODECORE pNode = KAVL_GET_POINTER_NULL(ppTree);
38 if (pNode)
39 {
40 PKAVLNODECORE pNodeLast = NULL;
41 if (fAbove)
42 { /* pNode->Key >= Key */
43 while (KAVL_NE(pNode->Key, Key))
44 {
45 if (KAVL_G(pNode->Key, Key))
46 {
47 if (pNode->pLeft != KAVL_NULL)
48 {
49 pNodeLast = pNode;
50 pNode = KAVL_GET_POINTER(&pNode->pLeft);
51 }
52 else
53 return pNode;
54 }
55 else
56 {
57 if (pNode->pRight != KAVL_NULL)
58 pNode = KAVL_GET_POINTER(&pNode->pRight);
59 else
60 return pNodeLast;
61 }
62 }
63 }
64 else
65 { /* pNode->Key <= Key */
66 while (KAVL_NE(pNode->Key, Key))
67 {
68 if (KAVL_G(pNode->Key, Key))
69 {
70 if (pNode->pLeft != KAVL_NULL)
71 pNode = KAVL_GET_POINTER(&pNode->pLeft);
72 else
73 return pNodeLast;
74 }
75 else
76 {
77 if (pNode->pRight != KAVL_NULL)
78 {
79 pNodeLast = pNode;
80 pNode = KAVL_GET_POINTER(&pNode->pRight);
81 }
82 else
83 return pNode;
84 }
85 }
86 }
87 }
88
89 /* perfect match or nothing. */
90 return pNode;
91}
92
93
94#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