summaryrefslogtreecommitdiffstats
path: root/src/lib/kStuff/include/k/kRbTmpl/kRbRemove2.h
blob: deed81d8735baa1f24e617d5e620a9093ccb8bc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* $Id: kRbRemove2.h 35 2009-11-08 19:39:03Z bird $ */
/** @file
 * kRbTmpl - Templated Red-Black Trees, Remove A Specific Node.
 */

/*
 * Copyright (c) 1999-2009 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * Removes the specified node from the tree.
 *
 * @returns Pointer to the removed node (NULL if not in the tree)
 * @param   pRoot       Pointer to the Red-Back tree's root structure.
 * @param   Key         The Key of which is to be found a best fitting match for..
 *
 * @remark  This implementation isn't the most efficient, but this short and
 *          easier to manage.
 */
KRB_DECL(KRBNODE *) KRB_FN(Remove2)(KRBROOT *pRoot, KRBNODE *pNode)
{
#ifdef KRB_EQUAL_ALLOWED
    /*
     * Find the right node by key and see if it's what we want.
     */
    KRBNODE *pParent;
    KRBNODE *pCurNode = KRB_FN(GetWithParent)(pRoot, pNode->mKey, &pParent);
    if (!pCurNode)
        return NULL;
    KRB_WRITE_LOCK(pRoot); /** @todo the locking here isn't 100% sane. The only way to archive that is by no calling worker functions. */
    if (pCurNode != pNode)
    {
        /*
         * It's not the one we want, but it could be in the duplicate list.
         */
        while (pCurNode->mpList != KRB_NULL)
        {
            KRBNODE *pNext = KRB_GET_POINTER(&pCurNode->mpList);
            if (pNext == pNode)
            {
                KRB_SET_POINTER_NULL(&pCurNode->mpList, KRB_GET_POINTER_NULL(&pNode->mpList));
                pNode->mpList = KRB_NULL;
                KRB_CACHE_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
                KRB_WRITE_UNLOCK(pRoot);
                return pNode;
            }
            pCurNode = pNext;
        }
        KRB_WRITE_UNLOCK(pRoot);
        return NULL;
    }

    /*
     * Ok, it's the one we want alright.
     *
     * Simply remove it if it's the only one with they Key,
     * if there are duplicates we'll have to unlink it and
     * insert the first duplicate in our place.
     */
    if (pNode->mpList == KRB_NULL)
    {
        KRB_WRITE_UNLOCK(pRoot);
        KRB_FN(Remove)(pRoot, pNode->mKey);
    }
    else
    {
        KRBNODE *pNewUs = KRB_GET_POINTER(&pNode->mpList);

        pNewUs->mHeight = pNode->mHeight;

        if (pNode->mpLeft != KRB_NULL)
            KRB_SET_POINTER(&pNewUs->mpLeft, KRB_GET_POINTER(&pNode->mpLeft))
        else
            pNewUs->mpLeft = KRB_NULL;

        if (pNode->mpRight != KRB_NULL)
            KRB_SET_POINTER(&pNewUs->mpRight, KRB_GET_POINTER(&pNode->mpRight))
        else
            pNewUs->mpRight = KRB_NULL;

        if (pParent)
        {
            if (KRB_GET_POINTER_NULL(&pParent->mpLeft) == pNode)
                KRB_SET_POINTER(&pParent->mpLeft, pNewUs);
            else
                KRB_SET_POINTER(&pParent->mpRight, pNewUs);
        }
        else
            KRB_SET_POINTER(&pRoot->mpRoot, pNewUs);

        KRB_CACHE_INVALIDATE_NODE(pRoot, pNode, pNode->mKey);
        KRB_WRITE_UNLOCK(pRoot);
    }

    return pNode;

#else
    /*
     * Delete it, if we got the wrong one, reinsert it.
     *
     * This ASSUMS that the caller is NOT going to hand us a lot
     * of wrong nodes but just uses this API for his convenience.
     */
    KRBNODE *pRemovedNode = KRB_FN(Remove)(pRoot, pNode->mKey);
    if (pRemovedNode == pNode)
        return pRemovedNode;

    KRB_FN(Insert)(pRoot, pRemovedNode);
    return NULL;
#endif
}