implied: fix a type bug
[smatch.git] / avl.c
blob437206ce89652133232222e93da3c862db4618cb
1 /*
2 * Copyright (C) 2010 Joseph Adams <joeyadams3.14159@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
23 #include <assert.h>
24 #include <stdlib.h>
26 #include "smatch.h"
27 #include "smatch_slist.h"
29 static AvlNode *mkNode(const struct sm_state *sm);
30 static void freeNode(AvlNode *node);
32 static AvlNode *lookup(const struct stree *avl, AvlNode *node, const struct sm_state *sm);
34 static bool insert_sm(struct stree *avl, AvlNode **p, const struct sm_state *sm);
35 static bool remove_sm(struct stree *avl, AvlNode **p, const struct sm_state *sm, AvlNode **ret);
36 static bool removeExtremum(AvlNode **p, int side, AvlNode **ret);
38 static int sway(AvlNode **p, int sway);
39 static void balance(AvlNode **p, int side);
41 static bool checkBalances(AvlNode *node, int *height);
42 static bool checkOrder(struct stree *avl);
43 static size_t countNode(AvlNode *node);
45 int unfree_stree;
48 * Utility macros for converting between
49 * "balance" values (-1 or 1) and "side" values (0 or 1).
51 * bal(0) == -1
52 * bal(1) == +1
53 * side(-1) == 0
54 * side(+1) == 1
56 #define bal(side) ((side) == 0 ? -1 : 1)
57 #define side(bal) ((bal) == 1 ? 1 : 0)
59 struct stree *avl_new(void)
61 struct stree *avl = malloc(sizeof(*avl));
63 unfree_stree++;
64 assert(avl != NULL);
66 avl->root = NULL;
67 avl->count = 0;
68 avl->stree_id = 0;
69 avl->references = 1;
70 return avl;
73 void free_stree(struct stree **avl)
75 if (!*avl)
76 return;
78 assert((*avl)->references > 0);
80 (*avl)->references--;
81 if ((*avl)->references != 0) {
82 *avl = NULL;
83 return;
86 unfree_stree--;
88 freeNode((*avl)->root);
89 free(*avl);
90 *avl = NULL;
93 struct sm_state *avl_lookup(const struct stree *avl, const struct sm_state *sm)
95 AvlNode *found;
97 if (!avl)
98 return NULL;
99 found = lookup(avl, avl->root, sm);
100 if (!found)
101 return NULL;
102 return (struct sm_state *)found->sm;
105 AvlNode *avl_lookup_node(const struct stree *avl, const struct sm_state *sm)
107 return lookup(avl, avl->root, sm);
110 size_t stree_count(const struct stree *avl)
112 if (!avl)
113 return 0;
114 return avl->count;
117 static struct stree *clone_stree_real(struct stree *orig)
119 struct stree *new = avl_new();
120 AvlIter i;
122 avl_foreach(i, orig)
123 avl_insert(&new, i.sm);
125 return new;
128 bool avl_insert(struct stree **avl, const struct sm_state *sm)
130 size_t old_count;
132 if (!*avl)
133 *avl = avl_new();
134 if ((*avl)->references > 1) {
135 (*avl)->references--;
136 *avl = clone_stree_real(*avl);
138 old_count = (*avl)->count;
139 insert_sm(*avl, &(*avl)->root, sm);
140 return (*avl)->count != old_count;
143 bool avl_remove(struct stree **avl, const struct sm_state *sm)
145 AvlNode *node = NULL;
147 if (!*avl)
148 return false;
149 /* it's fairly rare for smatch to call avl_remove */
150 if ((*avl)->references > 1) {
151 (*avl)->references--;
152 *avl = clone_stree_real(*avl);
155 remove_sm(*avl, &(*avl)->root, sm, &node);
157 if ((*avl)->count == 0)
158 free_stree(avl);
160 if (node == NULL) {
161 return false;
162 } else {
163 free(node);
164 return true;
168 static AvlNode *mkNode(const struct sm_state *sm)
170 AvlNode *node = malloc(sizeof(*node));
172 assert(node != NULL);
174 node->sm = sm;
175 node->lr[0] = NULL;
176 node->lr[1] = NULL;
177 node->balance = 0;
178 return node;
181 static void freeNode(AvlNode *node)
183 if (node) {
184 freeNode(node->lr[0]);
185 freeNode(node->lr[1]);
186 free(node);
190 static AvlNode *lookup(const struct stree *avl, AvlNode *node, const struct sm_state *sm)
192 int cmp;
194 if (node == NULL)
195 return NULL;
197 cmp = cmp_tracker(sm, node->sm);
199 if (cmp < 0)
200 return lookup(avl, node->lr[0], sm);
201 if (cmp > 0)
202 return lookup(avl, node->lr[1], sm);
203 return node;
207 * Insert an sm into a subtree, rebalancing if necessary.
209 * Return true if the subtree's height increased.
211 static bool insert_sm(struct stree *avl, AvlNode **p, const struct sm_state *sm)
213 if (*p == NULL) {
214 *p = mkNode(sm);
215 avl->count++;
216 return true;
217 } else {
218 AvlNode *node = *p;
219 int cmp = cmp_tracker(sm, node->sm);
221 if (cmp == 0) {
222 node->sm = sm;
223 return false;
226 if (!insert_sm(avl, &node->lr[side(cmp)], sm))
227 return false;
229 /* If tree's balance became -1 or 1, it means the tree's height grew due to insertion. */
230 return sway(p, cmp) != 0;
235 * Remove the node matching the given sm.
236 * If present, return the removed node through *ret .
237 * The returned node's lr and balance are meaningless.
239 * Return true if the subtree's height decreased.
241 static bool remove_sm(struct stree *avl, AvlNode **p, const struct sm_state *sm, AvlNode **ret)
243 if (p == NULL || *p == NULL) {
244 return false;
245 } else {
246 AvlNode *node = *p;
247 int cmp = cmp_tracker(sm, node->sm);
249 if (cmp == 0) {
250 *ret = node;
251 avl->count--;
253 if (node->lr[0] != NULL && node->lr[1] != NULL) {
254 AvlNode *replacement;
255 int side;
256 bool shrunk;
258 /* Pick a subtree to pull the replacement from such that
259 * this node doesn't have to be rebalanced. */
260 side = node->balance <= 0 ? 0 : 1;
262 shrunk = removeExtremum(&node->lr[side], 1 - side, &replacement);
264 replacement->lr[0] = node->lr[0];
265 replacement->lr[1] = node->lr[1];
266 replacement->balance = node->balance;
267 *p = replacement;
269 if (!shrunk)
270 return false;
272 replacement->balance -= bal(side);
274 /* If tree's balance became 0, it means the tree's height shrank due to removal. */
275 return replacement->balance == 0;
278 if (node->lr[0] != NULL)
279 *p = node->lr[0];
280 else
281 *p = node->lr[1];
283 return true;
285 } else {
286 if (!remove_sm(avl, &node->lr[side(cmp)], sm, ret))
287 return false;
289 /* If tree's balance became 0, it means the tree's height shrank due to removal. */
290 return sway(p, -cmp) == 0;
296 * Remove either the left-most (if side == 0) or right-most (if side == 1)
297 * node in a subtree, returning the removed node through *ret .
298 * The returned node's lr and balance are meaningless.
300 * The subtree must not be empty (i.e. *p must not be NULL).
302 * Return true if the subtree's height decreased.
304 static bool removeExtremum(AvlNode **p, int side, AvlNode **ret)
306 AvlNode *node = *p;
308 if (node->lr[side] == NULL) {
309 *ret = node;
310 *p = node->lr[1 - side];
311 return true;
314 if (!removeExtremum(&node->lr[side], side, ret))
315 return false;
317 /* If tree's balance became 0, it means the tree's height shrank due to removal. */
318 return sway(p, -bal(side)) == 0;
322 * Rebalance a node if necessary. Think of this function
323 * as a higher-level interface to balance().
325 * sway must be either -1 or 1, and indicates what was added to
326 * the balance of this node by a prior operation.
328 * Return the new balance of the subtree.
330 static int sway(AvlNode **p, int sway)
332 if ((*p)->balance != sway)
333 (*p)->balance += sway;
334 else
335 balance(p, side(sway));
337 return (*p)->balance;
341 * Perform tree rotations on an unbalanced node.
343 * side == 0 means the node's balance is -2 .
344 * side == 1 means the node's balance is +2 .
346 static void balance(AvlNode **p, int side)
348 AvlNode *node = *p,
349 *child = node->lr[side];
350 int opposite = 1 - side;
351 int bal = bal(side);
353 if (child->balance != -bal) {
354 /* Left-left (side == 0) or right-right (side == 1) */
355 node->lr[side] = child->lr[opposite];
356 child->lr[opposite] = node;
357 *p = child;
359 child->balance -= bal;
360 node->balance = -child->balance;
362 } else {
363 /* Left-right (side == 0) or right-left (side == 1) */
364 AvlNode *grandchild = child->lr[opposite];
366 node->lr[side] = grandchild->lr[opposite];
367 child->lr[opposite] = grandchild->lr[side];
368 grandchild->lr[side] = child;
369 grandchild->lr[opposite] = node;
370 *p = grandchild;
372 node->balance = 0;
373 child->balance = 0;
375 if (grandchild->balance == bal)
376 node->balance = -bal;
377 else if (grandchild->balance == -bal)
378 child->balance = bal;
380 grandchild->balance = 0;
385 /************************* avl_check_invariants() *************************/
387 bool avl_check_invariants(struct stree *avl)
389 int dummy;
391 return checkBalances(avl->root, &dummy)
392 && checkOrder(avl)
393 && countNode(avl->root) == avl->count;
396 static bool checkBalances(AvlNode *node, int *height)
398 if (node) {
399 int h0, h1;
401 if (!checkBalances(node->lr[0], &h0))
402 return false;
403 if (!checkBalances(node->lr[1], &h1))
404 return false;
406 if (node->balance != h1 - h0 || node->balance < -1 || node->balance > 1)
407 return false;
409 *height = (h0 > h1 ? h0 : h1) + 1;
410 return true;
411 } else {
412 *height = 0;
413 return true;
417 static bool checkOrder(struct stree *avl)
419 AvlIter i;
420 const struct sm_state *last = NULL;
421 bool last_set = false;
423 avl_foreach(i, avl) {
424 if (last_set && cmp_tracker(last, i.sm) >= 0)
425 return false;
426 last = i.sm;
427 last_set = true;
430 return true;
433 static size_t countNode(AvlNode *node)
435 if (node)
436 return 1 + countNode(node->lr[0]) + countNode(node->lr[1]);
437 else
438 return 0;
442 /************************* Traversal *************************/
444 void avl_iter_begin(AvlIter *iter, struct stree *avl, AvlDirection dir)
446 AvlNode *node;
448 iter->stack_index = 0;
449 iter->direction = dir;
451 if (!avl || !avl->root) {
452 iter->sm = NULL;
453 iter->node = NULL;
454 return;
456 node = avl->root;
458 while (node->lr[dir] != NULL) {
459 iter->stack[iter->stack_index++] = node;
460 node = node->lr[dir];
463 iter->sm = (struct sm_state *) node->sm;
464 iter->node = node;
467 void avl_iter_next(AvlIter *iter)
469 AvlNode *node = iter->node;
470 AvlDirection dir = iter->direction;
472 if (node == NULL)
473 return;
475 node = node->lr[1 - dir];
476 if (node != NULL) {
477 while (node->lr[dir] != NULL) {
478 iter->stack[iter->stack_index++] = node;
479 node = node->lr[dir];
481 } else if (iter->stack_index > 0) {
482 node = iter->stack[--iter->stack_index];
483 } else {
484 iter->sm = NULL;
485 iter->node = NULL;
486 return;
489 iter->node = node;
490 iter->sm = (struct sm_state *) node->sm;
493 struct stree *clone_stree(struct stree *orig)
495 if (!orig)
496 return NULL;
498 orig->references++;
499 return orig;
502 void set_stree_id(struct stree **stree, int stree_id)
504 if ((*stree)->stree_id != 0)
505 *stree = clone_stree_real(*stree);
507 (*stree)->stree_id = stree_id;
510 int get_stree_id(struct stree *stree)
512 if (!stree)
513 return 0;
514 return stree->stree_id;