ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
avl.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "avl.h"
Include dependency graph for avl.c:

Go to the source code of this file.

Macros

#define HEIGHT(node)
 
#define BALANCE(node)
 
#define compute_height(node)
 
#define COMPARE(key, nodekey, compare)
 
#define STACK_SIZE   50
 

Functions

avl_treeavl_init_table (int *compar)
 
 avl_lookup (avl_tree *tree, char *key, char **value_p)
 
 avl_first (avl_tree *tree, char **key_p, char **value_p)
 
 avl_last (avl_tree *tree, char **key_p, char **value_p)
 
 avl_insert (avl_tree *tree, char *key, char *value)
 
 avl_find_or_add (avl_tree *tree, char *key, char ***slot_p)
 
 avl_delete (avl_tree *tree, char **key_p, char **value_p)
 
avl_generatoravl_init_gen (avl_tree *tree, int dir)
 
 avl_gen (avl_generator *gen, char **key_p, char **value_p)
 
void avl_free_gen (avl_generator *gen)
 
void avl_foreach (avl_tree *tree, void *func, int direction)
 
void avl_free_table (avl_tree *tree, void *key_free, void *value_free)
 
int avl_count (avl_tree *tree)
 
int avl_numcmp (char *x, char *y)
 
int avl_check_tree (avl_tree *tree)
 

Macro Definition Documentation

◆ BALANCE

#define BALANCE ( node)
Value:
(HEIGHT((node)->right) - HEIGHT((node)->left))
#define HEIGHT(node)
Definition avl.c:23

Definition at line 24 of file avl.c.

◆ COMPARE

#define COMPARE ( key,
nodekey,
compare )
Value:
((compare == avl_numcmp) ? \
(int) key - (int) nodekey : \
(*compare)(key, nodekey))
int avl_numcmp(char *x, char *y)
Definition avl.c:553
enum keys key
Definition main.h:25

Definition at line 31 of file avl.c.

31#define COMPARE(key, nodekey, compare) \
32 ((compare == avl_numcmp) ? \
33 (int) key - (int) nodekey : \
34 (*compare)(key, nodekey))

◆ compute_height

#define compute_height ( node)
Value:
{ \
int x=HEIGHT(node->left), y=HEIGHT(node->right); \
(node)->height = MAX(x,y) + 1; \
}
#define MAX(a, b)
Definition avl.h:23

Definition at line 26 of file avl.c.

26#define compute_height(node) { \
27 int x=HEIGHT(node->left), y=HEIGHT(node->right); \
28 (node)->height = MAX(x,y) + 1; \
29}

◆ HEIGHT

#define HEIGHT ( node)
Value:
(node == NIL(avl_node) ? -1 : (node)->height)
#define NIL(type)
Definition avl.h:25
struct avl_node_struct avl_node
Definition avl.h:36

Definition at line 23 of file avl.c.

◆ STACK_SIZE

#define STACK_SIZE   50

Definition at line 37 of file avl.c.

Function Documentation

◆ avl_check_tree()

int avl_check_tree ( avl_tree * tree)

Definition at line 560 of file avl.c.

562{
563 int error = 0;
564 (void) do_check_tree (tree->root, tree->compar, &error);
565 return error;
566}
avl_node * root
Definition avl.h:47
int(* compar)()
Definition avl.h:48

◆ avl_count()

int avl_count ( avl_tree * tree)

Definition at line 530 of file avl.c.

532{
533 return tree->num_entries;
534}
int num_entries
Definition avl.h:49
Here is the caller graph for this function:

◆ avl_delete()

avl_delete ( avl_tree * tree,
char ** key_p,
char ** value_p )

Definition at line 207 of file avl.c.

211{
212 register avl_node **node_p, *node, *rightmost;
213 register int stack_n = 0;
214 char *key = *key_p;
215 int (*compare) () = tree->compar, diff;
216 avl_node **stack_nodep[STACK_SIZE];
217
218 node_p = &tree->root;
219
220 /* Walk down the tree saving the path; return if not found */
221 while ((node = *node_p) != NIL (avl_node))
222 {
223 diff = COMPARE (key, node->key, compare);
224 if (diff == 0)
225 goto delete_item;
226 stack_nodep[stack_n++] = node_p;
227 node_p = (diff < 0) ? &node->left : &node->right;
228 }
229 return 0; /* not found */
230
231 /* prepare to delete node and replace it with rightmost of left tree */
232 delete_item:
233 *key_p = node->key;
234 if (value_p != 0)
235 *value_p = node->value;
236 if (node->left == NIL (avl_node))
237 {
238 *node_p = node->right;
239 }
240 else
241 {
242 rightmost = find_rightmost (&node->left);
243 rightmost->left = node->left;
244 rightmost->right = node->right;
245 rightmost->height = -2; /* mark bogus height for do_rebal */
246 *node_p = rightmost;
247 stack_nodep[stack_n++] = node_p;
248 }
249 FREE (node);
250
251 /* work our way back up, re-balancing the tree */
252 do_rebalance (stack_nodep, stack_n);
253 tree->num_entries--;
254 tree->modified = 1;
255 return 1;
256}
#define STACK_SIZE
Definition avl.c:37
#define COMPARE(key, nodekey, compare)
Definition avl.c:31
#define FREE(obj)
Definition avl.h:31
char * key
Definition avl.h:39
avl_node * left
Definition avl.h:38
int modified
Definition avl.h:50
Here is the caller graph for this function:

◆ avl_find_or_add()

avl_find_or_add ( avl_tree * tree,
char * key,
char *** slot_p )

Definition at line 170 of file avl.c.

174{
175 register avl_node **node_p, *node;
176 register int stack_n = 0;
177 register int (*compare) () = tree->compar;
178 avl_node **stack_nodep[STACK_SIZE];
179 int diff;
180
181 node_p = &tree->root;
182
183 /* walk down the tree (saving the path); stop at insertion point */
184 while ((node = *node_p) != NIL (avl_node))
185 {
186 stack_nodep[stack_n++] = node_p;
187 diff = COMPARE (key, node->key, compare);
188 if (diff == 0)
189 {
190 if (slot_p != 0)
191 *slot_p = &node->value;
192 return 1; /* found */
193 }
194 node_p = (diff < 0) ? &node->left : &node->right;
195 }
196
197 /* insert the item and re-balance the tree */
198 *node_p = new_node (key, NIL (char));
199 if (slot_p != 0)
200 *slot_p = &(*node_p)->value;
201 do_rebalance (stack_nodep, stack_n);
202 tree->num_entries++;
203 tree->modified = 1;
204 return 0; /* not already in tree */
205}
Here is the caller graph for this function:

◆ avl_first()

avl_first ( avl_tree * tree,
char ** key_p,
char ** value_p )

Definition at line 85 of file avl.c.

89{
90 register avl_node *node;
91
92 if (tree->root == 0)
93 {
94 return 0; /* no entries */
95 }
96 else
97 {
98 /* walk down the tree; stop at leftmost leaf */
99 for (node = tree->root; node->left != 0; node = node->left)
100 {
101 }
102 if (key_p != NIL (char *))
103 *key_p = node->key;
104 if (value_p != NIL (char *))
105 *value_p = node->value;
106 return 1;
107 }
108}
char * value
Definition avl.h:40
Here is the caller graph for this function:

◆ avl_foreach()

void avl_foreach ( avl_tree * tree,
void * func,
int direction )

Definition at line 483 of file avl.c.

487{
488 if (direction == AVL_FORWARD)
489 {
490 avl_walk_forward (tree->root, func);
491 }
492 else
493 {
494 avl_walk_backward (tree->root, func);
495 }
496}
#define AVL_FORWARD
Definition avl.h:62
Here is the caller graph for this function:

◆ avl_free_gen()

void avl_free_gen ( avl_generator * gen)

Definition at line 338 of file avl.c.

340{
341 FREE (gen->nodelist);
342 FREE (gen);
343}
avl_node ** nodelist
Definition avl.h:57
Here is the caller graph for this function:

◆ avl_free_table()

void avl_free_table ( avl_tree * tree,
void * key_free,
void * value_free )

Definition at line 519 of file avl.c.

523{
524 free_entry (tree->root, key_free, value_free);
525 FREE (tree);
526}
Here is the caller graph for this function:

◆ avl_gen()

avl_gen ( avl_generator * gen,
char ** key_p,
char ** value_p )

Definition at line 314 of file avl.c.

318{
319 avl_node *node;
320
321 if (gen->count == gen->tree->num_entries)
322 {
323 return 0;
324 }
325 else
326 {
327 node = gen->nodelist[gen->count++];
328 if (key_p != NIL (char *))
329 *key_p = node->key;
330 if (value_p != NIL (char *))
331 *value_p = node->value;
332 return 1;
333 }
334}
avl_tree * tree
Definition avl.h:56
Here is the caller graph for this function:

◆ avl_init_gen()

avl_generator * avl_init_gen ( avl_tree * tree,
int dir )

Definition at line 287 of file avl.c.

290{
291 avl_generator *gen;
292
293 /* what a hack */
294 gen = ALLOC (avl_generator, 1);
295 gen->tree = tree;
296 gen->nodelist = ALLOC (avl_node *, avl_count (tree));
297 gen->count = 0;
298 if (dir == AVL_FORWARD)
299 {
300 avl_record_gen_forward (tree->root, gen);
301 }
302 else
303 {
304 avl_record_gen_backward (tree->root, gen);
305 }
306 gen->count = 0;
307
308 /* catch any attempt to modify the tree while we generate */
309 tree->modified = 0;
310 return gen;
311}
int avl_count(avl_tree *tree)
Definition avl.c:530
struct avl_generator_struct avl_generator
Definition avl.h:54
#define ALLOC(type, num)
Definition avl.h:27
Here is the call graph for this function:
Here is the caller graph for this function:

◆ avl_init_table()

avl_tree * avl_init_table ( int * compar)

Definition at line 47 of file avl.c.

49{
50 avl_tree *tree;
51
52 tree = ALLOC (avl_tree, 1);
53 tree->root = NIL (avl_node);
54 tree->compar = compar;
55 tree->num_entries = 0;
56 return tree;
57}
struct avl_tree_struct avl_tree
Definition avl.h:45
Here is the caller graph for this function:

◆ avl_insert()

avl_insert ( avl_tree * tree,
char * key,
char * value )

Definition at line 136 of file avl.c.

140{
141 register avl_node **node_p, *node;
142 register int stack_n = 0;
143 register int (*compare) () = tree->compar;
144 avl_node **stack_nodep[STACK_SIZE];
145 int diff, status;
146
147 node_p = &tree->root;
148
149 /* walk down the tree (saving the path); stop at insertion point */
150 status = 0;
151 while ((node = *node_p) != NIL (avl_node))
152 {
153 stack_nodep[stack_n++] = node_p;
154 diff = COMPARE (key, node->key, compare);
155 if (diff == 0)
156 status = 1;
157 node_p = (diff < 0) ? &node->left : &node->right;
158 }
159
160 /* insert the item and re-balance the tree */
161 *node_p = new_node (key, value);
162 do_rebalance (stack_nodep, stack_n);
163 tree->num_entries++;
164 tree->modified = 1;
165 return status;
166}
ABC_NAMESPACE_IMPL_START typedef signed char value
Here is the caller graph for this function:

◆ avl_last()

avl_last ( avl_tree * tree,
char ** key_p,
char ** value_p )

Definition at line 111 of file avl.c.

115{
116 register avl_node *node;
117
118 if (tree->root == 0)
119 {
120 return 0; /* no entries */
121 }
122 else
123 {
124 /* walk down the tree; stop at rightmost leaf */
125 for (node = tree->root; node->right != 0; node = node->right)
126 {
127 }
128 if (key_p != NIL (char *))
129 *key_p = node->key;
130 if (value_p != NIL (char *))
131 *value_p = node->value;
132 return 1;
133 }
134}
avl_node * right
Definition avl.h:38
Here is the caller graph for this function:

◆ avl_lookup()

avl_lookup ( avl_tree * tree,
char * key,
char ** value_p )

Definition at line 61 of file avl.c.

65{
66 register avl_node *node;
67 register int (*compare) () = tree->compar, diff;
68
69 node = tree->root;
70 while (node != NIL (avl_node))
71 {
72 diff = COMPARE (key, node->key, compare);
73 if (diff == 0)
74 {
75 /* got a match */
76 if (value_p != NIL (char *))
77 *value_p = node->value;
78 return 1;
79 }
80 node = (diff < 0) ? node->left : node->right;
81 }
82 return 0;
83}
Here is the caller graph for this function:

◆ avl_numcmp()

int avl_numcmp ( char * x,
char * y )

Definition at line 553 of file avl.c.

555{
556 return (int) x - (int) y;
557}
Here is the caller graph for this function: