aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/tree.c
blob: c715fa86cbe3585464b2e10b563fc152b7b7e269 (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

/*
 * AVL tree map for hashed items.
 *
 * Copyright © 2021-2025 Samuel Lidén Borell <samuel@kodafritt.se>
 *
 * SPDX-License-Identifier: EUPL-1.2+
 */
#include <assert.h>
#include <string.h>
#include "compiler.h"

#define RDZERO  0
#define RDMINUS 1
#define RDPLUS  2

int treenode_equals(const struct TreeNode *n, const char *s, size_t len)
{
    if (n->length != len) return 0;
    return !memcmp(n->name, s, len);
}

/**
 * Compares the node a with the sought value b
 * \return -1 if b is larger, 0 if equal, 1 if a is larger
 */
static int ncmp(const struct TreeNode *a,
                HashCode bhash, size_t blen, const char *bname)
{
    size_t alen;
    /* Sort by hash first */
    if (a->hashcode < bhash) return -1;
    if (a->hashcode > bhash) return 1;
    /* Sort by length second */
    alen = a->length;
    if (alen < blen) return -1;
    if (alen > blen) return 1;
    /* Sort by contents as a last resort */
    return memcmp(a->name, bname, alen);
}

struct TreeNode *tree_search(const struct TreeNode *root,
                             HashCode h, size_t len, const char *name)
{
    const struct TreeNode *n = root;
    while (n) {
        int cmp = ncmp(n, h, len, name);
        if (cmp == 0) return (struct TreeNode *)n;
        else if (cmp > 0) n = n->lower;
        else n = n->higher;
    }
    return NULL;
}

struct TreeNode *tree_search_node(const struct TreeNode *root,
                                  const struct TreeNode *n)
{
    return tree_search(root, n->hashcode, n->length, n->name);
}

static struct TreeNode *rotate_left(struct TreeNode *n)
{
    struct TreeNode *higher = n->higher;
    n->higher = higher->lower;
    higher->lower = n;
    n->rankdelta = higher->rankdelta = RDZERO;
    return higher;
}

static struct TreeNode *rotate_right(struct TreeNode *n)
{
    struct TreeNode *lower = n->lower;
    n->lower = lower->higher;
    lower->higher = n;
    n->rankdelta = lower->rankdelta = RDZERO;
    return lower;
}

static struct TreeNode *rotate_leftright(struct TreeNode *base)
{
    struct TreeNode *l = base->lower;    /*       ___________b_   */
    struct TreeNode *lh = l->higher;     /*    __l____         h  */
    base->lower = lh->higher;            /*  ll       _lh_        */
    l->higher = lh->lower;               /*        lhl    lhh     */
    lh->higher = base;
    lh->lower = l;                       /*       becomes         */
    if (lh->rankdelta == RDMINUS) {
        base->rankdelta = RDPLUS;        /*       _____lh____     */
        l->rankdelta = RDZERO;           /*    __l__        _b_   */
    } else {                             /*  ll     lhl  lhh   h  */
        base->rankdelta = RDZERO;
        l->rankdelta = RDMINUS;
    }
    lh->rankdelta = RDZERO;
    return lh;
}

/** Performs a right-left rotation and returns the new subtree root */
static struct TreeNode *rotate_rightleft(struct TreeNode *base)
{
    struct TreeNode *h = base->higher;   /*   _b___________       */
    struct TreeNode *hl = h->lower;      /*  l         ____h__    */
    base->higher = hl->lower;            /*         _hl_      hh  */
    h->lower = hl->higher;               /*      hll    hlh       */
    hl->lower = base;
    hl->higher = h;                      /*       becomes         */
    if (hl->rankdelta == RDPLUS) {
        base->rankdelta = RDMINUS;       /*     ____hl_____       */
        h->rankdelta = RDZERO;           /*   _b_        __h__    */
    } else {                             /*  l   hll  hlh    hhh  */
        base->rankdelta = RDZERO;
        h->rankdelta = RDPLUS;
    }
    hl->rankdelta = RDZERO;
    return hl;
}

static struct TreeNode *create_node(HashCode h, size_t len, const char *name,
                                    struct TreeNode *newnode, size_t newsize)
{
    if (!newnode) {
        newnode = malloc(newsize);
        NO_NULL(newnode);
    }
    newnode->lower = NULL;
    newnode->higher = NULL;
    newnode->hashcode = h;
    newnode->rankdelta = RDZERO;
    assert(len <= TREENODE_LEN_MAX);
    newnode->length = (TREENODE_LEN_TYPE)len;
    newnode->is_new = 1;
    newnode->is_defined = 0;
    newnode->name = memzdup(name, len);
    return newnode;
}

struct TreeNode *tree_insert_str(struct TreeNode **root,
                                 const char *name, size_t len,
                                 struct TreeNode *newnode, size_t newsize)
{
    return tree_insert(root, hash_str(name, len), name, len, newnode, newsize);
}

struct TreeNode *tree_insert(struct TreeNode **root,
                             HashCode h, const char *name, size_t len,
                             struct TreeNode *newnode, size_t newsize)
{
    struct TreeNode *path[TREE_MAXDEPTH];
    struct TreeNode **lastp;
    struct TreeNode *n, *rotroot;
    struct TreeNode **inspoint;
    size_t length;
    short depth;

    assert(len <= TREENODE_LEN_MAX);
    if (!*root) {
        n = create_node(h, len, name, newnode, newsize);
        *root = n;
        return n;
    }
    length = len;
    lastp = &path[0];
    depth = 0;
    n = *root;
    /* Insert */
    for (;;) {
        int cmp;
        if (++depth >= TREE_MAXDEPTH) goto too_deep;
        *lastp = n;
        cmp = ncmp(n, h, length, name);
        if (cmp == 0) {
            n->is_new = 0;
            return n;
        } else if (cmp > 0) {
            inspoint = &n->lower;
            n = n->lower;
        } else {
            inspoint = &n->higher;
            n = n->higher;
        }
        if (!n) break;
        lastp++;
    }
    n = create_node(h, len, name, newnode, newsize);
    *inspoint = n;
    newnode = n;
    /* Rebalance if necessary.
       This is done by backtracking through the path. */
    for (;;) { /* while depth > 0 */
        struct TreeNode *p = *lastp;
        assert(n->lower  || n->rankdelta != RDMINUS);
        assert(n->higher || n->rankdelta != RDPLUS);
        if (p->lower == n) { /* new node is in left/lower subtree */
            if (p->rankdelta == RDMINUS) {
                if (n->rankdelta == RDPLUS) {
                    rotroot = rotate_leftright(p);
                } else {
                    rotroot = rotate_right(p);
                }
            } else if (p->rankdelta == RDPLUS) {
                p->rankdelta = RDZERO;
                break;
            } else {
                p->rankdelta = RDMINUS;
                goto continue_up;
            }
        } else { /* new node is in right/higher subtree */
            assert(p->higher == n);
            if (p->rankdelta == RDPLUS) {
                if (n->rankdelta == RDMINUS) {
                    rotroot = rotate_rightleft(p);
                } else {
                    rotroot = rotate_left(p);
                }
            } else if (p->rankdelta == RDMINUS) {
                p->rankdelta = RDZERO;
                break;
            } else {
                p->rankdelta = RDPLUS;
                goto continue_up;
            }
        }
        if (depth <= 1) *root = rotroot;
        else if (lastp[-1]->lower == p) {
            lastp[-1]->lower = rotroot;
        } else {
            assert(lastp[-1]->higher == p);
            lastp[-1]->higher = rotroot;
        }
        break;
      continue_up:
        if (!--depth) break;
        lastp--;
        n = p;
    }
    return newnode;
  too_deep:
    error("Tree structure too deep.");
    return NULL;
}