source: trunk/include/k/kAvlTmpl/kAvlRemoveBestFit.h@ 8

Last change on this file since 8 was 7, checked in by bird, 18 years ago

kAVL: Implemented locking, root node and a direct cache.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 2.9 KB
Line 
1/* $Id: kAvlRemoveBestFit.h 7 2008-02-04 02:08:02Z bird $ */
2/** @file
3 * kAvlTmpl - Templated AVL Trees, Remove Best Fitting Node.
4 */
5
6/*
7 * Copyright (c) 1999-2007 knut st. osmundsen <bird-src-spam@anduin.net>
8 *
9 * This file is part of kStuff.
10 *
11 * kStuff is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * kStuff is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with kStuff; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 *
26 * As a special exception, since this is a source file and not a header
27 * file, you are granted permission to #include this file as you wish
28 * without this in itself causing the resulting program or whatever to be
29 * covered by the LGPL license. This exception does not however invalidate
30 * any other reasons why the resulting program/whatever should not be
31 * covered the LGPL or GPL.
32 */
33
34
35/**
36 * Finds the best fitting node in the tree for the given Key value and removes the node.
37 *
38 * @returns Pointer to the removed node.
39 * @param pRoot Pointer to the AVL-tree root structure.
40 * @param Key The Key of which is to be found a best fitting match for..
41 * @param fAbove K_TRUE: Returned node is have the closest key to Key from above.
42 * K_FALSE: Returned node is have the closest key to Key from below.
43 *
44 * @remark This implementation uses GetBestFit and then Remove and might therefore
45 * not be the most optimal kind of implementation, but it reduces the complexity
46 * code size, and the likelyhood for bugs.
47 */
48KAVL_DECL(KAVLNODE *) KAVL_FN(RemoveBestFit)(KAVLROOT *pRoot, KAVLKEY Key, KBOOL fAbove)
49{
50 /*
51 * If we find anything we'll have to remove the node and return it.
52 * Now, if duplicate keys are allowed we'll remove a duplicate before
53 * removing the in-tree node as this is way cheaper.
54 */
55 KAVLNODE *pNode = KAVL_FN(GetBestFit)(pRoot, Key, fAbove);
56 if (pNode != NULL)
57 {
58#ifdef KAVL_EQUAL_ALLOWED
59 KAVL_WRITE_LOCK(pRoot); /** @todo the locking isn't quite sane here. :-/ */
60 if (pNode->mpList != KAVL_NULL)
61 {
62 KAVLNODE *pRet = KAVL_GET_POINTER(&pNode->mpList);
63 KAVL_SET_POINTER_NULL(&pNode->mpList, &pRet->mpList);
64 KAVL_LOOKTHRU_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
65 KAVL_WRITE_UNLOCK(pRoot);
66 return pRet;
67 }
68 KAVL_WRITE_UNLOCK(pRoot);
69#endif
70 pNode = KAVL_FN(Remove)(pRoot, pNode->mKey);
71 }
72 return pNode;
73}
74
Note: See TracBrowser for help on using the repository browser.