VirtualBox

source: kStuff/trunk/include/k/kAvlTmpl/kAvlRemoveBestFit.h@ 75

Last change on this file since 75 was 29, checked in by bird, 15 years ago

Finally got around execute the switch to the MIT license.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 2.8 KB
Line 
1/* $Id: kAvlRemoveBestFit.h 29 2009-07-01 20:30:29Z bird $ */
2/** @file
3 * kAvlTmpl - Templated AVL Trees, Remove Best Fitting Node.
4 */
5
6/*
7 * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31/**
32 * Finds the best fitting node in the tree for the given Key value and removes the node.
33 *
34 * @returns Pointer to the removed node.
35 * @param pRoot Pointer to the AVL-tree root structure.
36 * @param Key The Key of which is to be found a best fitting match for..
37 * @param fAbove K_TRUE: Returned node is have the closest key to Key from above.
38 * K_FALSE: Returned node is have the closest key to Key from below.
39 *
40 * @remark This implementation uses GetBestFit and then Remove and might therefore
41 * not be the most optimal kind of implementation, but it reduces the complexity
42 * code size, and the likelyhood for bugs.
43 */
44KAVL_DECL(KAVLNODE *) KAVL_FN(RemoveBestFit)(KAVLROOT *pRoot, KAVLKEY Key, KBOOL fAbove)
45{
46 /*
47 * If we find anything we'll have to remove the node and return it.
48 * Now, if duplicate keys are allowed we'll remove a duplicate before
49 * removing the in-tree node as this is way cheaper.
50 */
51 KAVLNODE *pNode = KAVL_FN(GetBestFit)(pRoot, Key, fAbove);
52 if (pNode != NULL)
53 {
54#ifdef KAVL_EQUAL_ALLOWED
55 KAVL_WRITE_LOCK(pRoot); /** @todo the locking isn't quite sane here. :-/ */
56 if (pNode->mpList != KAVL_NULL)
57 {
58 KAVLNODE *pRet = KAVL_GET_POINTER(&pNode->mpList);
59 KAVL_SET_POINTER_NULL(&pNode->mpList, &pRet->mpList);
60 KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
61 KAVL_WRITE_UNLOCK(pRoot);
62 return pRet;
63 }
64 KAVL_WRITE_UNLOCK(pRoot);
65#endif
66 pNode = KAVL_FN(Remove)(pRoot, pNode->mKey);
67 }
68 return pNode;
69}
70
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