VirtualBox

Changeset 7 in kStuff for trunk/include/k/kAvlTmpl/kAvlGet.h


Ignore:
Timestamp:
Feb 4, 2008 2:08:02 AM (17 years ago)
Author:
bird
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/k/kAvlTmpl/kAvlGet.h

    r2 r7  
    3636 * Gets a node from the tree (does not remove it!)
    3737 *
    38  * @returns   Pointer to the node holding the given key.
    39  * @param     ppTree  Pointer to the AVL-tree root node pointer.
    40  * @param     Key     Key value of the node which is to be found.
     38 * @returns Pointer to the node holding the given key.
     39 * @param   pRoot       Pointer to the AVL-tree root structure.
     40 * @param   Key         Key value of the node which is to be found.
    4141 */
    42 KAVL_DECL(KAVLNODE *) KAVL_FN(Get)(KAVLTREEPTR *ppTree, KAVLKEY Key)
     42KAVL_DECL(KAVLNODE *) KAVL_FN(Get)(KAVLROOT *pRoot, KAVLKEY Key)
    4343{
    4444    KAVLNODE *pNode;
    45     if (*ppTree == KAVL_NULL)
     45#ifdef KAVL_LOOKTHRU_CACHE
     46    KAVLTREEPTR *ppEntry;
     47#endif
     48
     49    KAVL_READ_LOCK(pRoot);
     50    if (pRoot->mpRoot == KAVL_NULL)
     51    {
     52        KAVL_READ_UNLOCK(pRoot);
    4653        return NULL;
     54    }
    4755
    48     pNode = KAVL_GET_POINTER(ppTree);
    49     while (KAVL_NE(pNode->mKey, Key))
     56#ifdef KAVL_LOOKTHRU_CACHE
     57    ppEntry = &pRoot->maLookthru[KAVL_LOOKTHRU_HASH(Key)];
     58    pNode = KAVL_GET_POINTER_NULL(ppEntry);
     59    if (!pNode || KAVL_NE(pNode->mKey, Key))
     60#endif
    5061    {
    51         if (KAVL_G(pNode->mKey, Key))
     62        pNode = KAVL_GET_POINTER(&pRoot->mpRoot);
     63        while (KAVL_NE(pNode->mKey, Key))
    5264        {
    53             if (pNode->mpLeft == KAVL_NULL)
    54                 return NULL;
    55             pNode = KAVL_GET_POINTER(&pNode->mpLeft);
     65            if (KAVL_G(pNode->mKey, Key))
     66            {
     67                if (pNode->mpLeft == KAVL_NULL)
     68                {
     69                    KAVL_READ_UNLOCK(pRoot);
     70                    return NULL;
     71                }
     72                pNode = KAVL_GET_POINTER(&pNode->mpLeft);
     73            }
     74            else
     75            {
     76                if (pNode->mpRight == KAVL_NULL)
     77                {
     78                    KAVL_READ_UNLOCK(pRoot);
     79                    return NULL;
     80                }
     81                pNode = KAVL_GET_POINTER(&pNode->mpRight);
     82            }
    5683        }
    57         else
    58         {
    59             if (pNode->mpRight == KAVL_NULL)
    60                 return NULL;
    61             pNode = KAVL_GET_POINTER(&pNode->mpRight);
    62         }
     84   
     85#ifdef KAVL_LOOKTHRU_CACHE
     86        KAVL_SET_POINTER(ppEntry, pNode);
     87#endif
    6388    }
     89
     90    KAVL_READ_UNLOCK(pRoot);
    6491    return pNode;
    6592}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette