2001-09-09 22:24:39 -04:00
|
|
|
/*
|
2018-10-12 06:28:09 -04:00
|
|
|
* Copyright (C) 1995 Sam Rushing <rushing@nightmare.com>,
|
|
|
|
* Copyright (C) 2012-2018 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
|
2001-09-09 22:24:39 -04:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2018-10-12 06:16:06 -04:00
|
|
|
#ifndef _LIBIGLOO__AVL_H_
|
|
|
|
#define _LIBIGLOO__AVL_H_
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-11-01 09:23:50 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
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
|
2018-10-26 10:20:20 -04:00
|
|
|
#include "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)
|
2018-10-12 06:55:24 -04:00
|
|
|
#define igloo_thread_rwlock_destroy(x) do{}while(0)
|
2003-03-05 21:35:12 -05:00
|
|
|
#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
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
typedef struct igloo_avl_node_tag {
|
2003-03-14 21:10:19 -05:00
|
|
|
void * key;
|
2018-10-12 07:27:17 -04:00
|
|
|
struct igloo_avl_node_tag * left;
|
|
|
|
struct igloo_avl_node_tag * right;
|
|
|
|
struct igloo_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;
|
2018-11-01 09:23:50 -04:00
|
|
|
#if !defined(NO_THREAD) && defined(IGLOO_CTC_HAVE_AVL_NODE_LOCK)
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_rwlock_t rwlock;
|
2003-03-05 20:55:20 -05:00
|
|
|
#endif
|
2018-10-12 07:27:17 -04:00
|
|
|
} igloo_avl_node;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
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))
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
struct igloo__avl_tree;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
typedef int (*igloo_avl_key_compare_fun_type) (void * compare_arg, void * a, void * b);
|
|
|
|
typedef int (*igloo_avl_iter_fun_type) (void * key, void * iter_arg);
|
|
|
|
typedef int (*igloo_avl_iter_index_fun_type) (unsigned long index, void * key, void * iter_arg);
|
|
|
|
typedef int (*igloo_avl_free_key_fun_type) (void * key);
|
|
|
|
typedef int (*igloo_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.
|
|
|
|
*/
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
typedef struct igloo__avl_tree {
|
|
|
|
igloo_avl_node * root;
|
2007-12-15 12:51:33 -05:00
|
|
|
unsigned int height;
|
|
|
|
unsigned int length;
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_key_compare_fun_type compare_fun;
|
2003-03-14 21:10:19 -05:00
|
|
|
void * compare_arg;
|
2003-03-05 21:35:12 -05:00
|
|
|
#ifndef NO_THREAD
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_rwlock_t rwlock;
|
2003-03-05 20:55:20 -05:00
|
|
|
#endif
|
2018-10-12 07:27:17 -04:00
|
|
|
} igloo_avl_tree;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * igloo_avl_tree_new (igloo_avl_key_compare_fun_type compare_fun, void * compare_arg);
|
|
|
|
igloo_avl_node * igloo_avl_node_new (void * key, igloo_avl_node * parent);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
void igloo_avl_tree_free (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
|
|
|
igloo_avl_free_key_fun_type free_key_fun
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_avl_insert (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * ob,
|
2003-03-14 21:10:19 -05:00
|
|
|
void * key
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_avl_delete (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
2003-03-14 21:10:19 -05:00
|
|
|
void * key,
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_free_key_fun_type free_key_fun
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_avl_get_by_index (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
2003-03-14 21:10:19 -05:00
|
|
|
unsigned long index,
|
|
|
|
void ** value_address
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_avl_get_by_key (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
2003-03-14 21:10:19 -05:00
|
|
|
void * key,
|
|
|
|
void ** value_address
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_avl_iterate_inorder (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
|
|
|
igloo_avl_iter_fun_type iter_fun,
|
2003-03-14 21:10:19 -05:00
|
|
|
void * iter_arg
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_avl_iterate_index_range (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
|
|
|
igloo_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
|
|
|
);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_avl_get_span_by_key (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
2003-03-14 21:10:19 -05:00
|
|
|
void * key,
|
|
|
|
unsigned long * low,
|
|
|
|
unsigned long * high
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_avl_get_span_by_two_keys (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
2003-03-14 21:10:19 -05:00
|
|
|
void * key_a,
|
|
|
|
void * key_b,
|
|
|
|
unsigned long * low,
|
|
|
|
unsigned long * high
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_avl_verify (igloo_avl_tree * tree);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
void igloo_avl_print_tree (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
|
|
|
igloo_avl_key_printer_fun_type key_printer
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_node *igloo_avl_get_first(igloo_avl_tree *tree);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_node *igloo_avl_get_prev(igloo_avl_node * node);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_node *igloo_avl_get_next(igloo_avl_node * node);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* These two are from David Ascher <david_ascher@brown.edu> */
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_avl_get_item_by_key_most (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
2003-03-14 21:10:19 -05:00
|
|
|
void * key,
|
|
|
|
void ** value_address
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_avl_get_item_by_key_least (
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_avl_tree * tree,
|
2003-03-14 21:10:19 -05:00
|
|
|
void * key,
|
|
|
|
void ** value_address
|
2001-09-09 22:24:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
/* optional locking stuff */
|
2018-10-12 07:27:17 -04:00
|
|
|
void igloo_avl_tree_rlock(igloo_avl_tree *tree);
|
|
|
|
void igloo_avl_tree_wlock(igloo_avl_tree *tree);
|
|
|
|
void igloo_avl_tree_unlock(igloo_avl_tree *tree);
|
|
|
|
void avl_node_rlock(igloo_avl_node *node);
|
|
|
|
void avl_node_wlock(igloo_avl_node *node);
|
|
|
|
void avl_node_unlock(igloo_avl_node *node);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __AVL_H */
|