-
Notifications
You must be signed in to change notification settings - Fork 0
/
pavl.h
110 lines (92 loc) · 4.05 KB
/
pavl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Produced by texiweb from libavl.w. */
/* libavl - library for manipulation of binary trees.
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
/* $Id$ */
#ifndef PAVL_H
#define PAVL_H 1
#include <stddef.h>
/* Function types. */
typedef int pavl_comparison_func (const void *pavl_a, const void *pavl_b,
void *pavl_param);
typedef void pavl_item_func (void *pavl_item, void *pavl_param);
typedef void *pavl_copy_func (void *pavl_item, void *pavl_param);
#ifndef LIBAVL_ALLOCATOR
#define LIBAVL_ALLOCATOR
/* Memory allocator. */
struct libavl_allocator
{
void *(*libavl_malloc) (struct libavl_allocator *, size_t libavl_size);
void (*libavl_free) (struct libavl_allocator *, void *libavl_block);
};
#endif
/* Default memory allocator. */
extern struct libavl_allocator pavl_allocator_default;
void *pavl_malloc (struct libavl_allocator *, size_t);
void pavl_free (struct libavl_allocator *, void *);
/* Maximum PAVL height. */
#ifndef PAVL_MAX_HEIGHT
#define PAVL_MAX_HEIGHT 32
#endif
/* Tree data structure. */
struct pavl_table
{
struct pavl_node *pavl_root; /* Tree's root. */
pavl_comparison_func *pavl_compare; /* Comparison function. */
void *pavl_param; /* Extra argument to |pavl_compare|. */
struct libavl_allocator *pavl_alloc; /* Memory allocator. */
size_t pavl_count; /* Number of items in tree. */
};
/* An PAVL tree node. */
struct pavl_node
{
struct pavl_node *pavl_link[2]; /* Subtrees. */
struct pavl_node *pavl_parent; /* Parent node. */
void *pavl_data; /* Pointer to data. */
signed char pavl_balance; /* Balance factor. */
};
/* PAVL traverser structure. */
struct pavl_traverser
{
struct pavl_table *pavl_table; /* Tree being traversed. */
struct pavl_node *pavl_node; /* Current node in tree. */
};
/* Table functions. */
struct pavl_table *pavl_create (pavl_comparison_func *, void *,
struct libavl_allocator *);
struct pavl_table *pavl_copy (const struct pavl_table *, pavl_copy_func *,
pavl_item_func *, struct libavl_allocator *);
void pavl_destroy (struct pavl_table *, pavl_item_func *);
void **pavl_probe (struct pavl_table *, void *);
void *pavl_insert (struct pavl_table *, void *);
void *pavl_replace (struct pavl_table *, void *);
void *pavl_delete (struct pavl_table *, const void *);
void *pavl_find (const struct pavl_table *, const void *);
void pavl_assert_insert (struct pavl_table *, void *);
void *pavl_assert_delete (struct pavl_table *, void *);
#define pavl_count(table) ((size_t) (table)->pavl_count)
/* Table traverser functions. */
void pavl_t_init (struct pavl_traverser *, struct pavl_table *);
void *pavl_t_first (struct pavl_traverser *, struct pavl_table *);
void *pavl_t_last (struct pavl_traverser *, struct pavl_table *);
void *pavl_t_find (struct pavl_traverser *, struct pavl_table *, void *);
void *pavl_t_insert (struct pavl_traverser *, struct pavl_table *, void *);
void *pavl_t_copy (struct pavl_traverser *, const struct pavl_traverser *);
void *pavl_t_next (struct pavl_traverser *);
void *pavl_t_prev (struct pavl_traverser *);
void *pavl_t_cur (struct pavl_traverser *);
void *pavl_t_replace (struct pavl_traverser *, void *);
#endif /* pavl.h */