| 1 |
/* Orginally produced by texiweb from libavl.w. |
| 2 |
Hand-edited for Marpa by Jeffrey Kegler. |
| 3 |
*/ |
| 4 |
|
| 5 |
/* libavl - library for manipulation of binary trees. |
| 6 |
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software |
| 7 |
Foundation, Inc. |
| 8 |
|
| 9 |
This library is free software; you can redistribute it and/or |
| 10 |
modify it under the terms of the GNU Lesser General Public |
| 11 |
License as published by the Free Software Foundation; either |
| 12 |
version 3 of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
This library is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
Lesser General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU Lesser General Public |
| 20 |
License along with this library; if not, write to the Free Software |
| 21 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 22 |
02110-1301 USA. |
| 23 |
*/ |
| 24 |
|
| 25 |
/* Namespace not corrected for use as public Marpa header. The |
| 26 |
names are namespace safe in terms of the avl library, but for use |
| 27 |
as a Marpa public header they really ought to be changed into |
| 28 |
the 'marpa__' namespace. */ |
| 29 |
|
| 30 |
#ifndef TAVL_H |
| 31 |
#define TAVL_H 1 |
| 32 |
|
| 33 |
#include <stddef.h> |
| 34 |
|
| 35 |
/* Function types. */ |
| 36 |
typedef int tavl_comparison_func (const void *tavl_a, const void *tavl_b, |
| 37 |
void *tavl_param); |
| 38 |
typedef void tavl_item_func (void *tavl_item, void *tavl_param); |
| 39 |
typedef void *tavl_copy_func (void *tavl_item, void *tavl_param); |
| 40 |
|
| 41 |
#ifndef LIBAVL_ALLOCATOR |
| 42 |
#define LIBAVL_ALLOCATOR |
| 43 |
/* Memory allocator. */ |
| 44 |
struct libavl_allocator |
| 45 |
{ |
| 46 |
void *(*libavl_malloc) (struct libavl_allocator *, size_t libavl_size); |
| 47 |
void (*libavl_free) (struct libavl_allocator *, void *libavl_block); |
| 48 |
}; |
| 49 |
extern struct libavl_allocator* marpa__tavl_allocator_default; |
| 50 |
#endif |
| 51 |
|
| 52 |
/* Maximum TAVL height. */ |
| 53 |
#ifndef TAVL_MAX_HEIGHT |
| 54 |
#define TAVL_MAX_HEIGHT 32 |
| 55 |
#endif |
| 56 |
|
| 57 |
/* Tree data structure. */ |
| 58 |
struct tavl_table |
| 59 |
{ |
| 60 |
struct tavl_node *tavl_root; /* Tree's root. */ |
| 61 |
tavl_comparison_func *tavl_compare; /* Comparison function. */ |
| 62 |
void *tavl_param; /* Extra argument to |tavl_compare|. */ |
| 63 |
size_t tavl_count; /* Number of items in tree. */ |
| 64 |
unsigned int tavl_duplicate_found:1; |
| 65 |
}; |
| 66 |
|
| 67 |
/* Characterizes a link as a child pointer or a thread. */ |
| 68 |
enum tavl_tag |
| 69 |
{ |
| 70 |
TAVL_CHILD, /* Child pointer. */ |
| 71 |
TAVL_THREAD /* Thread. */ |
| 72 |
}; |
| 73 |
|
| 74 |
/* An TAVL tree node. */ |
| 75 |
struct tavl_node |
| 76 |
{ |
| 77 |
struct tavl_node *tavl_link[2]; /* Subtrees. */ |
| 78 |
void *tavl_data; /* Pointer to data. */ |
| 79 |
unsigned char tavl_tag[2]; /* Tag fields. */ |
| 80 |
signed char tavl_balance; /* Balance factor. */ |
| 81 |
}; |
| 82 |
|
| 83 |
/* TAVL traverser structure. */ |
| 84 |
struct tavl_traverser |
| 85 |
{ |
| 86 |
struct tavl_table *tavl_table; /* Tree being traversed. */ |
| 87 |
struct tavl_node *tavl_node; /* Current node in tree. */ |
| 88 |
}; |
| 89 |
|
| 90 |
/* Table functions. */ |
| 91 |
struct tavl_table *marpa__tavl_create (tavl_comparison_func *, void *); |
| 92 |
struct tavl_table *marpa__tavl_copy (const struct tavl_table *, tavl_copy_func *, |
| 93 |
tavl_item_func *); |
| 94 |
void marpa__tavl_destroy (struct tavl_table *, tavl_item_func *); |
| 95 |
void **marpa__tavl_probe (struct tavl_table *, void *); |
| 96 |
|
| 97 |
void *marpa__tavl_delete (struct tavl_table *, const void *); |
| 98 |
void *marpa__tavl_find (const struct tavl_table *, const void *); |
| 99 |
void marpa__tavl_assert_insert (struct tavl_table *, void *); |
| 100 |
void *marpa__tavl_assert_delete (struct tavl_table *, void *); |
| 101 |
|
| 102 |
#ifdef TESTING_TAVL |
| 103 |
|
| 104 |
/* For testing, we don't have the usual headers, so we |
| 105 |
* dummy up in-lining for GCC. For other compilers, we |
| 106 |
* just turn it off. |
| 107 |
*/ |
| 108 |
#if defined (__GNUC__) && defined (__STRICT_ANSI__) |
| 109 |
# undef inline |
| 110 |
# define inline __inline__ |
| 111 |
#endif |
| 112 |
|
| 113 |
/* Don't bother with inlining on this test if we are not |
| 114 |
* GCC. */ |
| 115 |
#ifndef __GNUC__ |
| 116 |
#undef inline |
| 117 |
#define inline |
| 118 |
#endif |
| 119 |
|
| 120 |
#endif |
| 121 |
|
| 122 |
/* Inserts |item| into |table|. |
| 123 |
Returns |NULL| if |item| was successfully inserted |
| 124 |
or if a memory allocation error occurred. |
| 125 |
Otherwise, returns the duplicate item. */ |
| 126 |
static inline |
| 127 |
void * |
| 128 |
marpa__tavl_insert (struct tavl_table *table, void *item) |
| 129 |
{ |
| 130 |
void **p = marpa__tavl_probe (table, item); |
| 131 |
return table->tavl_duplicate_found ? *p : NULL; |
| 132 |
} |
| 133 |
|
| 134 |
/* Inserts |item| into |table|, replacing any duplicate item. |
| 135 |
Returns |NULL| if |item| was inserted without replacing a duplicate, |
| 136 |
or if a memory allocation error occurred. |
| 137 |
Otherwise, returns the item that was replaced. */ |
| 138 |
static inline |
| 139 |
void * |
| 140 |
marpa__tavl_replace (struct tavl_table *table, void *item) |
| 141 |
{ |
| 142 |
void **p = marpa__tavl_probe (table, item); |
| 143 |
if (table->tavl_duplicate_found) { |
| 144 |
void *r = *p; |
| 145 |
*p = item; |
| 146 |
return r; |
| 147 |
} |
| 148 |
return NULL; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
#define tavl_count(table) ((size_t) (table)->tavl_count) |
| 153 |
|
| 154 |
/* Table traverser functions. */ |
| 155 |
void marpa__tavl_t_init (struct tavl_traverser *, struct tavl_table *); |
| 156 |
void *marpa__tavl_t_first (struct tavl_traverser *, struct tavl_table *); |
| 157 |
void *marpa__tavl_t_last (struct tavl_traverser *, struct tavl_table *); |
| 158 |
void *marpa__tavl_t_find (struct tavl_traverser *, struct tavl_table *, void *); |
| 159 |
void *marpa__tavl_t_insert (struct tavl_traverser *, struct tavl_table *, void *); |
| 160 |
void *marpa__tavl_t_copy (struct tavl_traverser *, const struct tavl_traverser *); |
| 161 |
void *marpa__tavl_t_next (struct tavl_traverser *); |
| 162 |
void *marpa__tavl_t_prev (struct tavl_traverser *); |
| 163 |
void *marpa__tavl_t_cur (struct tavl_traverser *); |
| 164 |
void *marpa__tavl_t_replace (struct tavl_traverser *, void *); |
| 165 |
|
| 166 |
/* For testing, we don't include the Marpa utility macros, so make |
| 167 |
* sure we have this one |
| 168 |
*/ |
| 169 |
#undef UNUSED |
| 170 |
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) |
| 171 |
#define UNUSED __attribute__((__unused__)) |
| 172 |
#else |
| 173 |
#define UNUSED |
| 174 |
#endif |
| 175 |
|
| 176 |
#endif /* tavl.h */ |