2001-09-09 22:24:39 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995 by Sam Rushing <rushing@nightmare.com>
|
|
|
|
*/
|
|
|
|
|
2003-07-06 21:10:14 -04:00
|
|
|
/* $Id: avl.h,v 1.7 2003/07/07 01:10:14 brendan Exp $ */
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifndef __AVL_H
|
|
|
|
#define __AVL_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2013-01-16 07:02:14 -05:00
|
|
|
#define AVL_KEY_PRINTER_BUFLEN (256)
|
|
|
|
|
2003-03-05 21:35:12 -05:00
|
|
|
#ifndef NO_THREAD
|
2007-12-15 12:51:33 -05:00
|
|
|
#include "thread/thread.h"
|
2003-03-05 20:55:20 -05:00
|
|
|
#else
|
2003-03-05 21:35:12 -05:00
|
|
|
#define thread_rwlock_create(x) do{}while(0)
|
|
|
|
#define thread_rwlock_destroy(x) do{}while(0)
|
|
|
|
#define thread_rwlock_rlock(x) do{}while(0)
|
|
|
|
#define thread_rwlock_wlock(x) do{}while(0)
|
|
|
|
#define thread_rwlock_unlock(x) do{}while(0)
|
2003-03-05 20:55:20 -05:00
|
|
|
#endif
|
2002-02-11 04:11:18 -05:00
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
typedef struct avl_node_tag {
|
2003-03-14 21:10:19 -05:00
|
|
|
void * key;
|
|
|
|
struct avl_node_tag * left;
|
|
|
|
struct avl_node_tag * right;
|
|
|
|
struct avl_node_tag * parent;
|
2001-09-09 22:24:39 -04:00
|
|
|
/*
|
|
|
|
* The lower 2 bits of <rank_and_balance> specify the balance
|
|
|
|
* factor: 00==-1, 01==0, 10==+1.
|
|
|
|
* The rest of the bits are used for <rank>
|
|
|
|
*/
|
2007-12-15 12:51:33 -05:00
|
|
|
unsigned int rank_and_balance;
|
2008-07-18 21:30:16 -04:00
|
|
|
#if !defined(NO_THREAD) && defined(HAVE_AVL_NODE_LOCK)
|
2001-09-09 22:24:39 -04:00
|
|
|
rwlock_t rwlock;
|
2003-03-05 20:55:20 -05:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
} avl_node;
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
#define AVL_GET_BALANCE(n) ((int)(((n)->rank_and_balance & 3) - 1))
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
#define AVL_GET_RANK(n) (((n)->rank_and_balance >> 2))
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#define AVL_SET_BALANCE(n,b) \
|
|
|
|
((n)->rank_and_balance) = \
|
|
|
|
(((n)->rank_and_balance & (~3)) | ((int)((b) + 1)))
|
|
|
|
|
|
|
|
#define AVL_SET_RANK(n,r) \
|
|
|
|
((n)->rank_and_balance) = \
|
|
|
|
(((n)->rank_and_balance & 3) | (r << 2))
|
|
|
|
|
|
|
|
struct _avl_tree;
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
typedef int (*avl_key_compare_fun_type) (void * compare_arg, void * a, void * b);
|
|
|
|
typedef int (*avl_iter_fun_type) (void * key, void * iter_arg);
|
|
|
|
typedef int (*avl_iter_index_fun_type) (unsigned long index, void * key, void * iter_arg);
|
|
|
|
typedef int (*avl_free_key_fun_type) (void * key);
|
|
|
|
typedef int (*avl_key_printer_fun_type) (char *, void *);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* <compare_fun> and <compare_arg> let us associate a particular compare
|
|
|
|
* function with each tree, separately.
|
|
|
|
*/
|
|
|
|
|
2003-07-06 21:10:14 -04:00
|
|
|
#ifdef _mangle
|
|
|
|
# define avl_tree_new _mangle(avl_tree_new)
|
|
|
|
# define avl_node_new _mangle(avl_node_new)
|
|
|
|
# define avl_tree_free _mangle(avl_tree_free)
|
|
|
|
# define avl_insert _mangle(avl_insert)
|
|
|
|
# define avl_delete _mangle(avl_delete)
|
|
|
|
# define avl_get_by_index _mangle(avl_get_by_index)
|
|
|
|
# define avl_get_by_key _mangle(avl_get_by_key)
|
|
|
|
# define avl_iterate_inorder _mangle(avl_iterate_inorder)
|
|
|
|
# define avl_iterate_index_range _mangle(avl_iterate_index_range)
|
|
|
|
# define avl_tree_rlock _mangle(avl_tree_rlock)
|
|
|
|
# define avl_tree_wlock _mangle(avl_tree_wlock)
|
|
|
|
# define avl_tree_wlock _mangle(avl_tree_wlock)
|
|
|
|
# define avl_tree_unlock _mangle(avl_tree_unlock)
|
|
|
|
# define avl_node_rlock _mangle(avl_node_rlock)
|
|
|
|
# define avl_node_wlock _mangle(avl_node_wlock)
|
|
|
|
# define avl_node_unlock _mangle(avl_node_unlock)
|
|
|
|
# define avl_get_span_by_key _mangle(avl_get_span_by_key)
|
|
|
|
# define avl_get_span_by_two_keys _mangle(avl_get_span_by_two_keys)
|
|
|
|
# define avl_verify _mangle(avl_verify)
|
|
|
|
# define avl_print_tree _mangle(avl_print_tree)
|
|
|
|
# define avl_get_first _mangle(avl_get_first)
|
|
|
|
# define avl_get_prev _mangle(avl_get_prev)
|
|
|
|
# define avl_get_next _mangle(avl_get_next)
|
|
|
|
# define avl_get_item_by_key_most _mangle(avl_get_item_by_key_most)
|
|
|
|
# define avl_get_item_by_key_least _mangle(avl_get_item_by_key_least)
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
typedef struct _avl_tree {
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_node * root;
|
2007-12-15 12:51:33 -05:00
|
|
|
unsigned int height;
|
|
|
|
unsigned int length;
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_key_compare_fun_type compare_fun;
|
|
|
|
void * compare_arg;
|
2003-03-05 21:35:12 -05:00
|
|
|
#ifndef NO_THREAD
|
2001-09-09 22:24:39 -04:00
|
|
|
rwlock_t rwlock;
|
2003-03-05 20:55:20 -05:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
} avl_tree;
|
|
|
|
|
|
|
|
avl_tree * avl_tree_new (avl_key_compare_fun_type compare_fun, void * compare_arg);
|
|
|
|
avl_node * avl_node_new (void * key, avl_node * parent);
|
|
|
|
|
|
|
|
void avl_tree_free (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
|
|
|
avl_free_key_fun_type free_key_fun
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int avl_insert (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * ob,
|
|
|
|
void * key
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int avl_delete (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
|
|
|
void * key,
|
|
|
|
avl_free_key_fun_type free_key_fun
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int avl_get_by_index (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
|
|
|
unsigned long index,
|
|
|
|
void ** value_address
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int avl_get_by_key (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
|
|
|
void * key,
|
|
|
|
void ** value_address
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int avl_iterate_inorder (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
|
|
|
avl_iter_fun_type iter_fun,
|
|
|
|
void * iter_arg
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int avl_iterate_index_range (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
2001-09-09 22:24:39 -04:00
|
|
|
avl_iter_index_fun_type iter_fun,
|
2003-03-14 21:10:19 -05:00
|
|
|
unsigned long low,
|
|
|
|
unsigned long high,
|
|
|
|
void * iter_arg
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int avl_get_span_by_key (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
|
|
|
void * key,
|
|
|
|
unsigned long * low,
|
|
|
|
unsigned long * high
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int avl_get_span_by_two_keys (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
|
|
|
void * key_a,
|
|
|
|
void * key_b,
|
|
|
|
unsigned long * low,
|
|
|
|
unsigned long * high
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int avl_verify (avl_tree * tree);
|
|
|
|
|
|
|
|
void avl_print_tree (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
2001-09-09 22:24:39 -04:00
|
|
|
avl_key_printer_fun_type key_printer
|
|
|
|
);
|
|
|
|
|
|
|
|
avl_node *avl_get_first(avl_tree *tree);
|
|
|
|
|
|
|
|
avl_node *avl_get_prev(avl_node * node);
|
|
|
|
|
|
|
|
avl_node *avl_get_next(avl_node * node);
|
|
|
|
|
|
|
|
/* These two are from David Ascher <david_ascher@brown.edu> */
|
|
|
|
|
|
|
|
int avl_get_item_by_key_most (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
|
|
|
void * key,
|
|
|
|
void ** value_address
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int avl_get_item_by_key_least (
|
2003-03-14 21:10:19 -05:00
|
|
|
avl_tree * tree,
|
|
|
|
void * key,
|
|
|
|
void ** value_address
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
/* optional locking stuff */
|
|
|
|
void avl_tree_rlock(avl_tree *tree);
|
|
|
|
void avl_tree_wlock(avl_tree *tree);
|
|
|
|
void avl_tree_unlock(avl_tree *tree);
|
|
|
|
void avl_node_rlock(avl_node *node);
|
|
|
|
void avl_node_wlock(avl_node *node);
|
|
|
|
void avl_node_unlock(avl_node *node);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __AVL_H */
|