avl: rename avl_count() to stree_count()
[smatch.git] / avl.h
blobbbed674eae0386ed3d350b4d277b7fe811a3ed40
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 #ifndef CCAN_AVL_H
24 #define CCAN_AVL_H
26 #include <stdbool.h>
27 #include <stddef.h>
29 struct sm_state;
31 typedef struct AvlNode AvlNode;
32 typedef struct AvlIter AvlIter;
34 struct stree {
35 AvlNode *root;
36 size_t count;
39 void avl_free(struct stree **avl);
40 /* Free an stree tree. */
42 struct sm_state *avl_lookup(const struct stree *avl, const struct sm_state *sm);
43 /* O(log n). Lookup a sm. Return NULL if the sm is not present. */
45 #define avl_member(avl, sm) (!!avl_lookup_node(avl, sm))
46 /* O(log n). See if a sm is present. */
48 size_t stree_count(const struct stree *avl);
49 /* O(1). Return the number of elements in the tree. */
51 bool avl_insert(struct stree **avl, const struct sm_state *sm);
53 * O(log n). Insert an sm or replace it if already present.
55 * Return false if the insertion replaced an existing sm.
58 bool avl_remove(struct stree **avl, const struct sm_state *sm);
60 * O(log n). Remove an sm (if present).
62 * Return true if it was removed.
65 bool avl_check_invariants(struct stree *avl);
66 /* For testing purposes. This function will always return true :-) */
69 /************************* Traversal *************************/
71 #define avl_foreach(iter, avl) avl_traverse(iter, avl, FORWARD)
73 * O(n). Traverse an stree tree in order.
75 * Example:
77 * AvlIter i;
79 * avl_foreach(i, avl)
80 * printf("%s -> %s\n", i.sm->name, i.sm->state->name);
83 #define FOR_EACH_SM(avl, _sm) { \
84 AvlIter _i; \
85 avl_foreach(_i, avl) { \
86 _sm = _i.sm;
88 #define END_FOR_EACH_SM(_sm) }}
90 #define avl_foreach_reverse(iter, avl) avl_traverse(iter, avl, BACKWARD)
91 /* O(n). Traverse an stree tree in reverse order. */
93 typedef enum AvlDirection {FORWARD = 0, BACKWARD = 1} AvlDirection;
95 struct AvlIter {
96 struct sm_state *sm;
97 AvlNode *node;
99 /* private */
100 AvlNode *stack[100];
101 int stack_index;
102 AvlDirection direction;
105 void avl_iter_begin(AvlIter *iter, struct stree *avl, AvlDirection dir);
106 void avl_iter_next(AvlIter *iter);
107 #define avl_traverse(iter, avl, direction) \
108 for (avl_iter_begin(&(iter), avl, direction); \
109 (iter).node != NULL; \
110 avl_iter_next(&iter))
113 /***************** Internal data structures ******************/
115 struct AvlNode {
116 const struct sm_state *sm;
118 AvlNode *lr[2];
119 int balance; /* -1, 0, or 1 */
122 AvlNode *avl_lookup_node(const struct stree *avl, const struct sm_state *sm);
123 /* O(log n). Lookup an stree node by sm. Return NULL if not present. */
125 struct stree *clone_stree(struct stree *orig);
127 #endif