Ignore:
Timestamp:
Aug 26, 2007, 12:43:27 PM (18 years ago)
Author:
bird
Message:

Merging in kAvl enhancements, doing some config simplications and made them inlineable.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kStuff/include/k/kAVLTmpl/kAVLGetBestFit.h

    r3553 r3555  
    11/* $Id$ */
    22/** @file
    3  *
    4  * kAVLGetBestFit - Get Best Fit routine for AVL trees.
    5  *                  Intended specially on heaps. The tree should allow duplicate keys.
    6  *
    7  * Copyright (c) 1999-2002 knut st. osmundsen (bird@anduin.net)
    8  *
    9  * GPL
     3 * kAVLTmpl - Templated AVL Trees, Get Best Fitting Node.
    104 */
    115
    12 #ifndef _kAVLGetBestFit_h_
    13 #define _kAVLGetBestFit_h_
     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 */
    1426
    1527
    1628/**
    1729 * Finds the best fitting node in the tree for the given Key value.
    18  * @returns   Pointer to the best fitting node found.
    19  * @param     ppTree  Pointer to Pointer to the tree root node.
    20  * @param     Key     The Key of which is to be found a best fitting match for..
    21  * @param     fAbove  TRUE:  Returned node is have the closest key to Key from above.
    22  *                    FALSE: Returned node is have the closest key to Key from below.
    23  * @status    completely implemented.
    24  * @sketch    The best fitting node is always located in the searchpath above you.
    25  *            >= (above): The node where you last turned left.
    26  *            <= (below): the node where you last turned right.
    27  * @author    knut st. osmundsen
     30 *
     31 * @returns Pointer to the best fitting node found.
     32 * @param   ppTree      Pointer to Pointer to the tree root node.
     33 * @param   Key         The Key of which is to be found a best fitting match for..
     34 * @param   fAbove      K_TRUE:  Returned node is have the closest key to Key from above.
     35 *                      K_FALSE: Returned node is have the closest key to Key from below.
     36 * @sketch  The best fitting node is always located in the searchpath above you.
     37 *          >= (above): The node where you last turned left.
     38 *          <= (below): the node where you last turned right.
    2839 */
    29 PKAVLNODECORE KLIBCALL KAVL_FN(GetBestFit)(PPKAVLNODECORE ppTree, KAVLKEY Key, KBOOL fAbove)
     40KAVL_DECL(KAVLNODECORE *) KAVL_FN(GetBestFit)(KAVLTREEPTR *ppTree, KAVLKEY Key, KBOOL fAbove)
    3041{
    31     KLOGENTRY3("PKAVLNODECORE","PPKAVLNODECORE ppTree, KAVLKEY Key, KBOOL fAbove", ppTree, Key, fAbove);
    32     #ifdef AVL_CMP
    33     register int            iDiff;
    34     #endif
    35     register PKAVLNODECORE   pNode = *ppTree;
    36     PKAVLNODECORE            pNodeLast = NULL;
     42    register KAVLNODECORE *pNode;
     43    KAVLNODECORE *pNodeLast;
    3744
     45    if (*ppTree == KAVL_NULL)
     46        return NULL;
     47
     48    pNode = KAVL_GET_POINTER(ppTree);
     49    pNodeLast = NULL;
    3850    if (fAbove)
    3951    {   /* pNode->Key >= Key */
    40         #ifndef AVL_CMP
    41         while (pNode != NULL && AVL_NE(pNode->Key, Key))
    42         #else
    43         while (pNode != NULL && (iDiff = AVL_CMP(pNode->Key, Key)) != 0)
    44         #endif
     52        while (KAVL_NE(pNode->Key, Key))
    4553        {
    46             #ifndef AVL_CMP
    47             if (AVL_G(pNode->Key, Key))
    48             #else
    49             if (iDiff > 0)
    50             #endif
     54            if (KAVL_G(pNode->Key, Key))
    5155            {
     56                if (pNode->pLeft == KAVL_NULL)
     57                    return pNode;
    5258                pNodeLast = pNode;
    53                 pNode = pNode->pLeft;
     59                pNode = KAVL_GET_POINTER(&pNode->pLeft);
    5460            }
    5561            else
    56                 pNode = pNode->pRight;
     62            {
     63                if (pNode->pRight == KAVL_NULL)
     64                    return pNodeLast;
     65                pNode = KAVL_GET_POINTER(&pNode->pRight);
     66            }
    5767        }
    5868    }
    5969    else
    6070    {   /* pNode->Key <= Key */
    61         #ifndef AVL_CMP
    62         while (pNode != NULL && AVL_NE(pNode->Key, Key))
    63         #else
    64         while (pNode != NULL && (iDiff = AVL_CMP(pNode->Key, Key)) != 0)
    65         #endif
     71        while (KAVL_NE(pNode->Key, Key))
    6672        {
    67             #ifndef AVL_CMP
    68             if (AVL_L(pNode->Key, Key))
    69             #else
    70             if (iDiff < 0)
    71             #endif
     73            if (KAVL_G(pNode->Key, Key))
    7274            {
    73                 pNodeLast = pNode;
    74                 pNode = pNode->pRight;
     75                if (pNode->pLeft == KAVL_NULL)
     76                    return pNodeLast;
     77                pNode = KAVL_GET_POINTER(&pNode->pLeft);
    7578            }
    7679            else
    77                 pNode = pNode->pLeft;
     80            {
     81                if (pNode->pRight == KAVL_NULL)
     82                    return pNode;
     83                pNodeLast = pNode;
     84                pNode = KAVL_GET_POINTER(&pNode->pRight);
     85            }
    7886        }
    7987    }
    8088
    81     KLOGEXIT(pNode == NULL ? pNodeLast /* best fit */ : pNode /* perfect match */);
    82     return pNode == NULL ? pNodeLast /* best fit */ : pNode /* perfect match */;
     89    /* perfect match or nothing. */
     90    return pNode;
    8391}
    8492
    85 
    86 #endif
Note: See TracChangeset for help on using the changeset viewer.