summaryrefslogtreecommitdiffstats
path: root/clib/tree.h
blob: 82b1a301e201d95fdbe61c4224ba4c2d58590080 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: clib/tree.h $                                                 */
/*                                                                        */
/* OpenPOWER FFS Project                                                  */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2015                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

/*!
 * @file tree.h
 * @brief Binary Tree Container
 * @details Trees are a kind of associative container that stores elements formed
 *          by the conbination of a @em key and a @em tree_node
 * @details For example,
 * @code
 * ...
 * int main (int argc, char *argv[]) {
 *     typedef struct {
 *         int i;
 *         float f;
 *         tree_node_t node;
 *     } data_t;
 *
 *     slab_t s;
 *     slab_init(&s, "my_slab", sizeof(data_t), 4096);
 *
 *     tree_t t;
 *     tree_init(&t, default_compare);
 *
 *     int i;
 *     for (i=0; i<25; i++) {
 *         data_t * d = (data_t *)slab_alloc(&s);
 *
 *         d->i = i;
 *         d->f = (float)i	// key
 *
 *         tree_node_init(&d->node, (const void *)(d->i));
 *         tree_insert(&t, &d->node);
 *
 *         printf("insert i[%d] --> %p\n", i, d);
 *     }
 * @endcode
 * @author Shaun Wetzstein <shaun@us.ibm.com>
 * @date 2010-2011
 */

#ifndef __TREE_H__
#define __TREE_H__

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "exception.h"
#include "builtin.h"
#include "compare.h"
#include "type.h"

#define INIT_TREE_NODE	{NULL,}
#define INIT_TREE	{NULL,NULL,NULL,NULL,0}

/* ==================================================================== */

typedef struct tree_node tree_node_t;	//!< Alias for the @em tree_node class

typedef int (*tree_walk_f) (tree_node_t *);	//!< Tree walk callback function

/*!
 * @brief tree node
 * @details Primitive types cannot be stored in the @em tree container, instead the user must
 * embed a @em tree_node object within the stored object.
 */
struct tree_node {
	tree_node_t *left;	//!< Reference the left tree_node (a.k.a. left sub-tree)
	tree_node_t *right;	//!< Reference the right tree_node (a.k.a. right sub-tree)
	tree_node_t *parent;	//!< Reference the parent tree_node

	const void *key;	//!< Reference to the key bytes for this tree_node
};

/*!
 * @brief tree container
 * @details Primitive types cannot be stored in the @em tree container, instead the user must
 * embed a @em tree_node object within the stored object.
 */
struct tree {
	tree_node_t *root;	//!< Reference to the root tree_node of the @em tree

	tree_node_t *min;	//!< Reference to the node with smallest 'key' in the tree
	tree_node_t *max;	//!< reference to the node with largest 'key' in the tree

	compare_f compare;	//!< Reference to the function used to distinguish tree_nodes
	size_t size;		//!< Cache of the number of tree_node's contained in the @em tree
};
typedef struct tree tree_t;	//!< Alias for the @em tree class

/* ==================================================================== */

/*!
 * @brief Constructs a @em tree_node object
 * @memberof tree_node
 * @param self [in] tree_node object @em self pointer
 * @param key [in] pointer to key bytes
 * @return Reference to an initialized tree_node object on SUCCESS
 */
static inline tree_node_t *tree_node_init(tree_node_t * self, const void *key)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Check if the @em tree_node object is a leaf node
 * @details A leaf node is one where both @em .left and @em .right are NULL
 * @memberof tree_node
 * @param self [in] tree_node object @em self pointer
 * @return True if the @em tree_node is a leaf node, false otherwise
 */
static inline bool tree_node_leaf(tree_node_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Check if the @em tree_node object is an internal node
 * @details An internal node is one where either (or both) @em .left and @em .right are non-NULL
 * @memberof tree_node
 * @param self [in] tree_node object @em self pointer
 * @return True if the @em tree_node is an internal node, false otherwise
 */
static inline bool tree_node_internal(tree_node_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return the left sub-tree of a @em tree_node
 * @memberof tree_node
 * @param self [in] tree_node object @em self pointer
 * @return Reference to a @em tree_node if @em self is non-NULL, NULL otherwise
 */
static inline tree_node_t *tree_node_left(tree_node_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return the right sub-tree of a @em tree_node
 * @memberof tree_node
 * @param self [in] tree_node object @em self pointer
 * @return Reference to a @em tree_node if @em self is non-NULL, NULL otherwise
 */
static inline tree_node_t *tree_node_right(tree_node_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return the parent tree_node of a @em tree_node
 * @memberof tree_node
 * @param self [in] tree_node object @em self pointer
 * @return Reference to a @em tree_node if @em self is non-NULL, NULL otherwise
 */
static inline tree_node_t *tree_node_parent(tree_node_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return the key pointer of a @em tree_node
 * @memberof tree_node
 * @param self [in] tree_node object @em self pointer
 * @return Reference to the key bytes if @em self is non-NULL, NULL otherwise
 */
static inline const void *tree_node_key(tree_node_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return the prev tree_node of a @em tree_node
 * @memberof tree_node
 * @param self [in] tree_node object @em self pointer
 * @return Reference to a @em tree_node if @em self is non-NULL, NULL otherwise
 */
extern tree_node_t *tree_node_prev(tree_node_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return the next tree_node of a @em tree_node
 * @memberof tree_node
 * @param self [in] tree_node object @em self pointer
 * @return Reference to a @em tree_node if @em self is non-NULL, NULL otherwise
 */
extern tree_node_t *tree_node_next(tree_node_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Dump the contents of a @em tree to @em out output stream
 * @memberof tree_node
 * @param self [in] tree_node object @em self pointer
 * @param out [in] Reference to the @em out output stream
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern void tree_node_dump(tree_node_t * self, FILE * out)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/* ======================================================== */

static inline tree_node_t *tree_node_init(tree_node_t * self, const void *key)
{
	self->left = self->right = NULL;
	self->parent = NULL;
	self->key = key;
	return self;
}

static inline bool tree_node_leaf(tree_node_t * self)
{
	return self ? self->left == NULL && self->right == NULL : false;
}

static inline bool tree_node_internal(tree_node_t * self)
{
	return self ? self->left != NULL && self->right != NULL : false;
}

static inline tree_node_t *tree_node_left(tree_node_t * self)
{
	return self ? self->left : NULL;
}

static inline tree_node_t *tree_node_right(tree_node_t * self)
{
	return self ? self->right : NULL;
}

static inline tree_node_t *tree_node_parent(tree_node_t * self)
{
	return self ? self->parent : NULL;
}

static inline const void *tree_node_key(tree_node_t * self)
{
	return self ? self->key : NULL;
}

/* ======================================================== */

/*!
 * @brief Constructs a @em tree object
 * @memberof tree
 * @param self [in] tree_node object @em self pointer
 * @param compare [in] Reference to the @em tree_node compare function
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int tree_init(tree_t * self, compare_f compare)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Insert a new @em tree_node into the @em tree container
 * @memberof tree
 * @param self [in] tree object @em self pointer
 * @param node [in] Reference to the @em tree_node to insert
 * @return @em true if the @em tree_node was inserted, @em false otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 * @throws UNEXPECTED if @em tree_node.key points to a duplicate key
 */
extern int tree_insert(tree_t * self, tree_node_t * node)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Removes a @em tree_node from the @em tree container
 * @memberof tree
 * @param self [in] tree object @em self pointer
 * @param node [in] Reference to the @em tree_node to remove
 * @return @em true if the @em tree_node was removed, @em false otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int tree_remove(tree_t * self, tree_node_t * node)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Find a @em tree_node within the @em tree container
 * @memberof tree
 * @param self [in] tree object @em self pointer
 * @param key [in] Reference to the @em key to find
 * @return Reference to a @em tree_node on SUCCESS, false otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern tree_node_t *tree_find(tree_t * self, const void *key)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Walk the @em tree and call walk_func for each node
 * @memberof tree
 * @param self [in] tree object @em self pointer
 * @param walk_func [in] Reference to the walk function callback
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int tree_walk(tree_t * self, tree_walk_f walk_func)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Hexdump the contents of a @em tree to @em out output stream
 * @memberof tree
 * @param self [in] tree object @em self pointer
 * @param out [in] Reference to the @em out output stream
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern void tree_dump(tree_t * self, FILE * out)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Return the root @em tree_node of a @em tree container
 * @memberof tree
 * @param self [in] tree object @em self pointer
 * @return Reference to a @em tree_node if @em self is non-NULL, NULL otherwise
 */
static inline tree_node_t *tree_root(tree_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return whether a @em tree container is empty
 * @memberof tree
 * @param self [in] tree object @em self pointer
 * @return @em true if @em tree is empty, false otherwise
 */
static inline bool tree_empty(tree_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return the node count of a @em tree container
 * @memberof tree
 * @param self [in] tree object @em self pointer
 * @return @em 0 if @em tree is empty, @em non-0 otherwise
 */
static inline size_t tree_size(tree_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return the minimum @em tree_node of a @em tree container
 * @memberof tree
 * @param self [in] tree object @em self pointer
 * @return Reference to a @em tree_node if @em self is non-NULL, NULL otherwise
 */
static inline tree_node_t *tree_min(tree_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return the maximum @em tree_node of a @em tree container
 * @memberof tree
 * @param self [in] tree object @em self pointer
 * @return Reference to a @em tree_node if @em self is non-NULL, NULL otherwise
 */
static inline tree_node_t *tree_max(tree_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/* ======================================================== */

static inline tree_node_t *tree_root(tree_t * self)
{
	return self ? self->root : NULL;
}

static inline bool tree_empty(tree_t * self)
{
	return self ? self->root == NULL : true;
}

static inline size_t tree_size(tree_t * self)
{
	return self ? self->size : 0;
}

static inline tree_node_t *tree_min(tree_t * self)
{
	return self ? self->min : NULL;
}

static inline tree_node_t *tree_max(tree_t * self)
{
	return self ? self->max : NULL;
}

/* ======================================================== */

/*!
 * @brief Insert a new @em tree_node into the @em tree container
 * @details @em splay_insert is similar to @em tree_insert except
 *          it will self-adjust using the splay algorithm
 * @memberof tree
 * @param self [in] splay object @em self pointer
 * @param node [in] Reference to the @em tree_node to insert
 * @return @em true if the @em tree_node was inserted, @em false otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 * @throws UNEXPECTED if @em tree_node.key points to a duplicate key
 */
extern int splay_insert(tree_t * self, tree_node_t * node)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Removes a @em tree_node from the splay @em tree container
 * @details @em splay_remove is similar to @em tree_remove except
 *          it will self-adjust using the splay algorithm
 * @memberof tree
 * @param self [in] splay object @em self pointer
 * @param node [in] Reference to the @em tree_node to remove
 * @return @em true if the @em tree_node was removed, @em false otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 * @throws UNEXPECTED if @em tree_node.key points to a duplicate key
 */
extern int splay_remove(tree_t * self, tree_node_t * node)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Find a @em tree_node within the splay @em tree container
 * @details @em splay_find is similar to @em tree_find except it will
 *          self-adjust using the splay algorithm
 * @memberof tree
 * @param self [in] splay object @em self pointer
 * @param key [in] Reference to the @em key to find
 * @return Reference to a @em tree_node on SUCCESS, false otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern tree_node_t *splay_find(tree_t * self, const void *key)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/* ======================================================== */

#endif				/* __TREE_H__ */
OpenPOWER on IntegriCloud