summaryrefslogtreecommitdiffstats
path: root/avl-1.4.0/rb.h
blob: 26a9f495dd1ea9116c2c9d674dea5226c700fd9c (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* libavl - manipulates AVL trees.
   Copyright (C) 1998, 1999 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.

   The author may be contacted at <pfaffben@pilot.msu.edu> on the
   Internet, or as Ben Pfaff, 12167 Airport Rd, DeWitt MI 48820, USA
   through more mundane means. */

/* This is file rb.h in libavl. */

#if !rb_h
#define rb_h 1

/* The default maximum height of 32 allows for red-black trees having
   between 65,535 and 4,294,967,295 nodes, depending on order of
   insertion.  You may change this compile-time constant as you
   wish. */
#ifndef RB_MAX_HEIGHT
#define RB_MAX_HEIGHT	32
#endif

/* Node colors. */
enum
  {
    RB_BLACK,
    RB_RED,
  };

/* Structure for a node in a red-black tree. */
typedef struct rb_node
  {
    void *data;			/* Pointer to data. */
    struct rb_node *link[2];	/* Subtrees. */
    signed char color;		/* Color. */
    char cache;			/* Used during insertion. */
    signed char pad[2];		/* Unused.  Reserved for threaded trees. */
  }
rb_node;

/* Used for traversing a red-black tree. */
typedef struct rb_traverser
  {
    int init;			/* Initialized? */
    int nstack;			/* Top of stack. */
    const rb_node *p;		/* Used for traversal. */
    const rb_node *stack[RB_MAX_HEIGHT];/* Descended trees. */
  }
rb_traverser;

/* Initializer for rb_traverser. */
#define RB_TRAVERSER_INIT {0}

/* Function types. */
#if !AVL_FUNC_TYPES
#define AVL_FUNC_TYPES 1
typedef int (*avl_comparison_func) (const void *a, const void *b, void *param);
typedef void (*avl_node_func) (void *data, void *param);
typedef void *(*avl_copy_func) (void *data, void *param);
#endif

/* Structure which holds information about a red-black tree. */
typedef struct rb_tree
  {
#if PSPP
    struct arena **owner;	/* Arena to store nodes. */
#endif
    rb_node root;		/* Tree root node. */
    avl_comparison_func cmp;	/* Used to compare keys. */
    int count;			/* Number of nodes in the tree. */
    void *param;		/* Arbitary user data. */
  }
rb_tree;

#if PSPP
#define MAYBE_ARENA struct arena **owner,
#else
#define MAYBE_ARENA /* nothing */
#endif

/* General functions. */
rb_tree *rb_create (MAYBE_ARENA avl_comparison_func, void *param);
void rb_destroy (rb_tree *, avl_node_func);
void rb_free (rb_tree *);
int rb_count (const rb_tree *);
rb_tree *rb_copy (MAYBE_ARENA const rb_tree *, avl_copy_func);

/* Walk the tree. */
void rb_walk (const rb_tree *, avl_node_func, void *param);
void *rb_traverse (const rb_tree *, rb_traverser *);
#define rb_init_traverser(TRAVERSER) ((TRAVERSER)->init = 0)

/* Search for a given item. */
void **rb_probe (rb_tree *, void *);
void *rb_delete (rb_tree *, const void *);
void *rb_find (const rb_tree *, const void *);
void *rb_find_close (const rb_tree *, const void *);

#if __GCC__ >= 2
extern inline void *
rb_insert (rb_tree *tree, void *item)
{
  void **p = rb_probe (tree, item);
  return (*p == item) ? NULL : *p;
}

extern inline void *
rb_replace (rb_tree *tree, void *item)
{
  void **p = rb_probe (tree, item);
  if (*p == item)
    return NULL;
  else
    {
      void *r = *p;
      *p = item;
      return r;
    }
}
#else /* not gcc */
void *rb_insert (rb_tree *tree, void *item);
void *rb_replace (rb_tree *tree, void *item);
#endif /* not gcc */

/* Easy assertions on insertion & deletion. */
#ifndef NDEBUG
#define rb_force_insert(A, B)			\
	do					\
	  {					\
            void *r = rb_insert (A, B);		\
	    assert (r == NULL);			\
	  }					\
	while (0)
void *rb_force_delete (rb_tree *, void *);
#else
#define rb_force_insert(A, B)			\
	rb_insert (A, B)
#define rb_force_delete(A, B)			\
	rb_delete (A, B)
#endif

#endif /* rb_h */