#ifndef TAVL_H
#define TAVL_H 1
#include <stddef.h>
typedef
int
tavl_comparison_func (
const
void
*tavl_a,
const
void
*tavl_b,
void
*tavl_param);
typedef
void
tavl_item_func (
void
*tavl_item,
void
*tavl_param);
typedef
void
*tavl_copy_func (
void
*tavl_item,
void
*tavl_param);
#ifndef LIBAVL_ALLOCATOR
#define LIBAVL_ALLOCATOR
struct
libavl_allocator
{
void
*(*libavl_malloc) (
struct
libavl_allocator *,
size_t
libavl_size);
void
(*libavl_free) (
struct
libavl_allocator *,
void
*libavl_block);
};
extern
struct
libavl_allocator* marpa__tavl_allocator_default;
#endif
#ifndef TAVL_MAX_HEIGHT
#define TAVL_MAX_HEIGHT 32
#endif
struct
tavl_table
{
struct
tavl_node *tavl_root;
tavl_comparison_func *tavl_compare;
void
*tavl_param;
size_t
tavl_count;
unsigned
int
tavl_duplicate_found:1;
};
enum
tavl_tag
{
TAVL_CHILD,
TAVL_THREAD
};
struct
tavl_node
{
struct
tavl_node *tavl_link[2];
void
*tavl_data;
unsigned
char
tavl_tag[2];
signed
char
tavl_balance;
};
struct
tavl_traverser
{
struct
tavl_table *tavl_table;
struct
tavl_node *tavl_node;
};
struct
tavl_table *marpa__tavl_create (tavl_comparison_func *,
void
*);
struct
tavl_table *marpa__tavl_copy (
const
struct
tavl_table *, tavl_copy_func *,
tavl_item_func *);
void
marpa__tavl_destroy (
struct
tavl_table *, tavl_item_func *);
void
**marpa__tavl_probe (
struct
tavl_table *,
void
*);
void
*marpa__tavl_delete (
struct
tavl_table *,
const
void
*);
void
*marpa__tavl_find (
const
struct
tavl_table *,
const
void
*);
void
marpa__tavl_assert_insert (
struct
tavl_table *,
void
*);
void
*marpa__tavl_assert_delete (
struct
tavl_table *,
void
*);
#ifdef TESTING_TAVL
#if defined (__GNUC__) && defined (__STRICT_ANSI__)
# undef inline
# define inline __inline__
#endif
#ifndef __GNUC__
#undef inline
#define inline
#endif
#endif
static
inline
void
*
marpa__tavl_insert (
struct
tavl_table *table,
void
*item)
{
void
**p = marpa__tavl_probe (table, item);
return
table->tavl_duplicate_found ? *p : NULL;
}
static
inline
void
*
marpa__tavl_replace (
struct
tavl_table *table,
void
*item)
{
void
**p = marpa__tavl_probe (table, item);
if
(table->tavl_duplicate_found) {
void
*r = *p;
*p = item;
return
r;
}
return
NULL;
}
#define tavl_count(table) ((size_t) (table)->tavl_count)
void
marpa__tavl_t_init (
struct
tavl_traverser *,
struct
tavl_table *);
void
*marpa__tavl_t_first (
struct
tavl_traverser *,
struct
tavl_table *);
void
*marpa__tavl_t_last (
struct
tavl_traverser *,
struct
tavl_table *);
void
*marpa__tavl_t_find (
struct
tavl_traverser *,
struct
tavl_table *,
void
*);
void
*marpa__tavl_t_insert (
struct
tavl_traverser *,
struct
tavl_table *,
void
*);
void
*marpa__tavl_t_copy (
struct
tavl_traverser *,
const
struct
tavl_traverser *);
void
*marpa__tavl_t_next (
struct
tavl_traverser *);
void
*marpa__tavl_t_prev (
struct
tavl_traverser *);
void
*marpa__tavl_t_cur (
struct
tavl_traverser *);
void
*marpa__tavl_t_replace (
struct
tavl_traverser *,
void
*);
#undef UNUSED
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
#define UNUSED __attribute__((__unused__))
#else
#define UNUSED
#endif
#endif /* tavl.h */