summaryrefslogtreecommitdiff
path: root/usual/cbtree.h
blob: 5eafe42ffd363fa9cd9f4997d8d5c169c544a21d (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
/*
 * Copyright (c) 2009 Marko Kreen
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */ 

/** @file
 *
 * Crit-bit tree / binary radix tree.
 */
#ifndef _USUAL_CBTREE_H_
#define _USUAL_CBTREE_H_

#include <usual/cxalloc.h>

/** returns length of the key */
typedef size_t		(*cbtree_getkey_func)(void *ctx, void *obj, const void **dst_p);
/** walk over tree */
typedef bool		(*cbtree_walker_func)(void *ctx, void *obj);

/** Handle to tree */
struct CBTree;

/**
 * Create new tree.
 *
 * @param obj_key_cb    callback to get the key for a object
 * @param obj_free_cb   callback to free the object when tree node is freed (optional)
 * @param cb_ctx 	extra pointer passed to callbacks
 * @param cx            memory context where from allocate
 */
struct CBTree *cbtree_create(cbtree_getkey_func obj_key_cb,
			     cbtree_walker_func obj_free_cb,
			     void *cb_ctx,
			     CxMem *cx);
/**
 * frees all resources allocated.
 * If obj_free_cb is non-NULL, it will be called per each object.
 */
void cbtree_destroy(struct CBTree *tree);

/** Inserts new node to tree */
bool cbtree_insert(struct CBTree *tree, void *obj) _MUSTCHECK;

/** Removed node from tree.
 * If obj_free_cb is non-NULL, it will be called for the object.
 *
 * @returns   true if key was found, false otherwise.
 */
bool cbtree_delete(struct CBTree *tree, const void *key, size_t klen);

/**
 * Lookup a key.
 *
 * @returns object pointer if found, NULL ohterwise
 */
void *cbtree_lookup(struct CBTree *tree, const void *key, size_t klen);

/** Walk over tree */
bool cbtree_walk(struct CBTree *tree, cbtree_walker_func cb_func, void *cb_arg);

#endif