summaryrefslogtreecommitdiff
path: root/nasmlib/rbtree.c
blob: 773338bb4d283cb45cd2075662f9c07c4d5f5d0f (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 1996-2020 The NASM Authors - All Rights Reserved
 *   See the file AUTHORS included with the NASM distribution for
 *   the specific copyright holders.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following
 *   conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *
 *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ----------------------------------------------------------------------- */

/*
 * rbtree.c
 *
 * Simple implementation of a "left-leaning threaded red-black tree"
 * with 64-bit integer keys.  The search operation will return the
 * highest node <= the key; only search and insert are supported, but
 * additional standard llrbtree operations can be coded up at will.
 *
 * See http://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf for
 * information about left-leaning red-black trees.
 *
 * The "threaded" part means that left and right pointers that would
 * otherwise be NULL are pointers to the in-order predecessor or
 * successor node. The only pointers that are NULL are the very left-
 * and rightmost, for which no corresponding side node exists.
 *
 * This, among other things, allows for efficient predecessor and
 * successor operations without requiring dedicated space for a parent
 * pointer.
 *
 * This implementation is robust for identical key values; such keys
 * will not have their insertion order preserved, and after insertion
 * of unrelated keys a lookup may return a different node for the
 * duplicated key, but the prev/next operations will always enumerate
 * all entries.
 *
 * The NULL pointers at the end are considered predecessor/successor
 * pointers, so if the corresponding flags are clear it is always safe
 * to access the pointed-to object without an explicit NULL pointer
 * check.
 */

#include "rbtree.h"
#include "nasmlib.h"

struct rbtree *rb_search(const struct rbtree *tree, uint64_t key)
{
    const struct rbtree *best = NULL;

    if (tree) {
        while (true) {
            if (tree->key > key) {
                if (tree->m.flags & RBTREE_NODE_PRED)
                    break;
                tree = tree->m.left;
            } else {
                best = tree;
                if (tree->key == key || (tree->m.flags & RBTREE_NODE_SUCC))
                    break;
                tree = tree->m.right;
            }
	}
    }
    return (struct rbtree *)best;
}

struct rbtree *rb_search_exact(const struct rbtree *tree, uint64_t key)
{
    struct rbtree *rv;

    rv = rb_search(tree, key);
    return (rv && rv->key == key) ? rv : NULL;
}

/* Reds two left in a row? */
static inline bool is_red_left_left(struct rbtree *h)
{
    return !(h->m.flags & RBTREE_NODE_PRED) &&
        !(h->m.left->m.flags & (RBTREE_NODE_BLACK|RBTREE_NODE_PRED)) &&
        !(h->m.left->m.left->m.flags & RBTREE_NODE_BLACK);
}

/* Node to the right is red? */
static inline bool is_red_right(struct rbtree *h)
{
    return !(h->m.flags & RBTREE_NODE_SUCC) &&
           !(h->m.right->m.flags & RBTREE_NODE_BLACK);
}

/* Both the left and right hand nodes are red? */
static inline bool is_red_both(struct rbtree *h)
{
    return !(h->m.flags & (RBTREE_NODE_PRED|RBTREE_NODE_SUCC))
        && !(h->m.left->m.flags & h->m.right->m.flags & RBTREE_NODE_BLACK);
}

static inline struct rbtree *rotate_left(struct rbtree *h)
{
    struct rbtree *x = h->m.right;
    enum rbtree_node_flags hf = h->m.flags;
    enum rbtree_node_flags xf = x->m.flags;

    if (xf & RBTREE_NODE_PRED) {
        h->m.right = x;
        h->m.flags = (hf & RBTREE_NODE_PRED)  | RBTREE_NODE_SUCC;
    } else {
        h->m.right = x->m.left;
        h->m.flags = hf & RBTREE_NODE_PRED;
    }
    x->m.flags = (hf & RBTREE_NODE_BLACK) | (xf & RBTREE_NODE_SUCC);
    x->m.left = h;

    return x;
}

static inline struct rbtree *rotate_right(struct rbtree *h)
{
    struct rbtree *x = h->m.left;
    enum rbtree_node_flags hf = h->m.flags;
    enum rbtree_node_flags xf = x->m.flags;

    if (xf & RBTREE_NODE_SUCC) {
        h->m.left = x;
        h->m.flags = (hf & RBTREE_NODE_SUCC)  | RBTREE_NODE_PRED;
    } else {
        h->m.left = x->m.right;
        h->m.flags = hf & RBTREE_NODE_SUCC;
    }
    x->m.flags = (hf & RBTREE_NODE_BLACK) | (xf & RBTREE_NODE_PRED);
    x->m.right = h;

    return x;
}

static inline void color_flip(struct rbtree *h)
{
    h->m.flags          ^= RBTREE_NODE_BLACK;
    h->m.left->m.flags  ^= RBTREE_NODE_BLACK;
    h->m.right->m.flags ^= RBTREE_NODE_BLACK;
}

static struct rbtree *
_rb_insert(struct rbtree *tree, struct rbtree *node);

struct rbtree *rb_insert(struct rbtree *tree, struct rbtree *node)
{
    /* Initialize node as if it was the sole member of the tree */

    nasm_zero(node->m);
    node->m.flags = RBTREE_NODE_PRED|RBTREE_NODE_SUCC;

    if (unlikely(!tree))
        return node;

    return _rb_insert(tree, node);
}

static struct rbtree *
_rb_insert(struct rbtree *tree, struct rbtree *node)
{
    /* Recursive part of the algorithm */

    /* Red on both sides? */
    if (is_red_both(tree))
	color_flip(tree);

    if (node->key < tree->key) {
        node->m.right  = tree;  /* Potential successor */
        if (tree->m.flags & RBTREE_NODE_PRED) {
            node->m.left   = tree->m.left;
            tree->m.flags &= ~RBTREE_NODE_PRED;
            tree->m.left   = node;
        } else {
            tree->m.left   = _rb_insert(tree->m.left, node);
        }
    } else {
        node->m.left   = tree;  /* Potential predecessor */
        if (tree->m.flags & RBTREE_NODE_SUCC) {
            node->m.right  = tree->m.right;
            tree->m.flags &= ~RBTREE_NODE_SUCC;
            tree->m.right  = node;
        } else {
            tree->m.right  = _rb_insert(tree->m.right, node);
        }
    }

    if (is_red_right(tree))
	tree = rotate_left(tree);

    if (is_red_left_left(tree))
	tree = rotate_right(tree);

    return tree;
}

struct rbtree *rb_first(const struct rbtree *tree)
{
    if (unlikely(!tree))
        return NULL;

    while (!(tree->m.flags & RBTREE_NODE_PRED))
        tree = tree->m.left;

    return (struct rbtree *)tree;
}

struct rbtree *rb_last(const struct rbtree *tree)
{
    if (unlikely(!tree))
        return NULL;

    while (!(tree->m.flags & RBTREE_NODE_SUCC))
        tree = tree->m.right;

    return (struct rbtree *)tree;
}

struct rbtree *rb_prev(const struct rbtree *node)
{
    struct rbtree *np = node->m.left;

    if (node->m.flags & RBTREE_NODE_PRED)
        return np;
    else
        return rb_last(np);
}

struct rbtree *rb_next(const struct rbtree *node)
{
    struct rbtree *np = node->m.right;

    if (node->m.flags & RBTREE_NODE_SUCC)
        return np;
    else
        return rb_first(np);
}