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
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
);
48 * Utility macros for converting between
49 * "balance" values (-1 or 1) and "side" values (0 or 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
));
73 void free_stree(struct stree
**avl
)
78 assert((*avl
)->references
> 0);
81 if ((*avl
)->references
!= 0) {
88 freeNode((*avl
)->root
);
93 struct sm_state
*avl_lookup(const struct stree
*avl
, const struct sm_state
*sm
)
99 found
= lookup(avl
, avl
->root
, sm
);
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
)
117 static struct stree
*clone_stree_real(struct stree
*orig
)
119 struct stree
*new = avl_new();
123 avl_insert(&new, i
.sm
);
128 bool avl_insert(struct stree
**avl
, const struct sm_state
*sm
)
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
;
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)
168 static AvlNode
*mkNode(const struct sm_state
*sm
)
170 AvlNode
*node
= malloc(sizeof(*node
));
172 assert(node
!= NULL
);
181 static void freeNode(AvlNode
*node
)
184 freeNode(node
->lr
[0]);
185 freeNode(node
->lr
[1]);
190 static AvlNode
*lookup(const struct stree
*avl
, AvlNode
*node
, const struct sm_state
*sm
)
197 cmp
= cmp_tracker(sm
, node
->sm
);
200 return lookup(avl
, node
->lr
[0], sm
);
202 return lookup(avl
, node
->lr
[1], sm
);
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
)
219 int cmp
= cmp_tracker(sm
, node
->sm
);
226 if (!insert_sm(avl
, &node
->lr
[side(cmp
)], sm
))
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
) {
247 int cmp
= cmp_tracker(sm
, node
->sm
);
253 if (node
->lr
[0] != NULL
&& node
->lr
[1] != NULL
) {
254 AvlNode
*replacement
;
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
;
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
)
286 if (!remove_sm(avl
, &node
->lr
[side(cmp
)], sm
, ret
))
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
)
308 if (node
->lr
[side
] == NULL
) {
310 *p
= node
->lr
[1 - side
];
314 if (!removeExtremum(&node
->lr
[side
], side
, ret
))
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
;
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
)
349 *child
= node
->lr
[side
];
350 int opposite
= 1 - 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
;
359 child
->balance
-= bal
;
360 node
->balance
= -child
->balance
;
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
;
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
)
391 return checkBalances(avl
->root
, &dummy
)
393 && countNode(avl
->root
) == avl
->count
;
396 static bool checkBalances(AvlNode
*node
, int *height
)
401 if (!checkBalances(node
->lr
[0], &h0
))
403 if (!checkBalances(node
->lr
[1], &h1
))
406 if (node
->balance
!= h1
- h0
|| node
->balance
< -1 || node
->balance
> 1)
409 *height
= (h0
> h1
? h0
: h1
) + 1;
417 static bool checkOrder(struct stree
*avl
)
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)
433 static size_t countNode(AvlNode
*node
)
436 return 1 + countNode(node
->lr
[0]) + countNode(node
->lr
[1]);
442 /************************* Traversal *************************/
444 void avl_iter_begin(AvlIter
*iter
, struct stree
*avl
, AvlDirection dir
)
448 iter
->stack_index
= 0;
449 iter
->direction
= dir
;
451 if (!avl
|| !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
;
467 void avl_iter_next(AvlIter
*iter
)
469 AvlNode
*node
= iter
->node
;
470 AvlDirection dir
= iter
->direction
;
475 node
= node
->lr
[1 - dir
];
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
];
490 iter
->sm
= (struct sm_state
*) node
->sm
;
493 struct stree
*clone_stree(struct stree
*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
)
514 return stree
->stree_id
;