rework langinfo code for ABI compat and for use by time code
[musl] / src / search / tsearch_avl.c
1 #include <stdlib.h>
2 #include <search.h>
3
4 /*
5 avl tree implementation using recursive functions
6 the height of an n node tree is less than 1.44*log2(n+2)-1
7 (so the max recursion depth in case of a tree with 2^32 nodes is 45)
8 */
9
10 struct node {
11         const void *key;
12         struct node *left;
13         struct node *right;
14         int height;
15 };
16
17 static int delta(struct node *n) {
18         return (n->left ? n->left->height:0) - (n->right ? n->right->height:0);
19 }
20
21 static void updateheight(struct node *n) {
22         n->height = 0;
23         if (n->left && n->left->height > n->height)
24                 n->height = n->left->height;
25         if (n->right && n->right->height > n->height)
26                 n->height = n->right->height;
27         n->height++;
28 }
29
30 static struct node *rotl(struct node *n) {
31         struct node *r = n->right;
32         n->right = r->left;
33         r->left = n;
34         updateheight(n);
35         updateheight(r);
36         return r;
37 }
38
39 static struct node *rotr(struct node *n) {
40         struct node *l = n->left;
41         n->left = l->right;
42         l->right = n;
43         updateheight(n);
44         updateheight(l);
45         return l;
46 }
47
48 static struct node *balance(struct node *n) {
49         int d = delta(n);
50
51         if (d < -1) {
52                 if (delta(n->right) > 0)
53                         n->right = rotr(n->right);
54                 return rotl(n);
55         } else if (d > 1) {
56                 if (delta(n->left) < 0)
57                         n->left = rotl(n->left);
58                 return rotr(n);
59         }
60         updateheight(n);
61         return n;
62 }
63
64 static struct node *find(struct node *n, const void *k,
65         int (*cmp)(const void *, const void *))
66 {
67         int c;
68
69         if (!n)
70                 return 0;
71         c = cmp(k, n->key);
72         if (c == 0)
73                 return n;
74         if (c < 0)
75                 return find(n->left, k, cmp);
76         else
77                 return find(n->right, k, cmp);
78 }
79
80 static struct node *insert(struct node **n, const void *k,
81         int (*cmp)(const void *, const void *), int *new)
82 {
83         struct node *r = *n;
84         int c;
85
86         if (!r) {
87                 *n = r = malloc(sizeof **n);
88                 if (r) {
89                         r->key = k;
90                         r->left = r->right = 0;
91                         r->height = 1;
92                 }
93                 *new = 1;
94                 return r;
95         }
96         c = cmp(k, r->key);
97         if (c == 0)
98                 return r;
99         if (c < 0)
100                 r = insert(&r->left, k, cmp, new);
101         else
102                 r = insert(&r->right, k, cmp, new);
103         if (*new)
104                 *n = balance(*n);
105         return r;
106 }
107
108 static struct node *movr(struct node *n, struct node *r) {
109         if (!n)
110                 return r;
111         n->right = movr(n->right, r);
112         return balance(n);
113 }
114
115 static struct node *remove(struct node **n, const void *k,
116         int (*cmp)(const void *, const void *), struct node *parent)
117 {
118         int c;
119
120         if (!*n)
121                 return 0;
122         c = cmp(k, (*n)->key);
123         if (c == 0) {
124                 struct node *r = *n;
125                 *n = movr(r->left, r->right);
126                 free(r);
127                 return parent;
128         }
129         if (c < 0)
130                 parent = remove(&(*n)->left, k, cmp, *n);
131         else
132                 parent = remove(&(*n)->right, k, cmp, *n);
133         if (parent)
134                 *n = balance(*n);
135         return parent;
136 }
137
138 void *tdelete(const void *restrict key, void **restrict rootp,
139         int(*compar)(const void *, const void *))
140 {
141         /* last argument is arbitrary non-null pointer
142            which is returned when the root node is deleted */
143         return remove((void*)rootp, key, compar, *rootp);
144 }
145
146 void *tfind(const void *key, void *const *rootp,
147         int(*compar)(const void *, const void *))
148 {
149         return find(*rootp, key, compar);
150 }
151
152 void *tsearch(const void *key, void **rootp,
153         int (*compar)(const void *, const void *))
154 {
155         int new = 0;
156         return insert((void*)rootp, key, compar, &new);
157 }
158
159 static void walk(const struct node *r, void (*action)(const void *, VISIT, int), int d)
160 {
161         if (r == 0)
162                 return;
163         if (r->left == 0 && r->right == 0)
164                 action(r, leaf, d);
165         else {
166                 action(r, preorder, d);
167                 walk(r->left, action, d+1);
168                 action(r, postorder, d);
169                 walk(r->right, action, d+1);
170                 action(r, endorder, d);
171         }
172 }
173
174 void twalk(const void *root, void (*action)(const void *, VISIT, int))
175 {
176         walk(root, action, 0);
177 }