Debian Codegraph @

libmowgli-2/src/libmowgli/container/patricia.h

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * patricia.h: Dictionary-based storage.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 * Copyright (c) 2007-2008 Jilles Tjoelker <jilles -at- stack.nl>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #ifndef MOWGLI_SRC_LIBMOWGLI_CONTAINER_PATRICIA_H_INCLUDE_GUARD
36 #define MOWGLI_SRC_LIBMOWGLI_CONTAINER_PATRICIA_H_INCLUDE_GUARD 1
37
38 #include "core/stdinc.h"
39 #include "platform/attributes.h"
40
41 struct mowgli_patricia_;/* defined in src/patricia.c */
42
43 struct mowgli_patricia_elem_; /* defined in src/patricia.c */
44
45 typedef struct mowgli_patricia_ mowgli_patricia_t;
46 typedef struct mowgli_patricia_elem_ mowgli_patricia_elem_t;
47
48 /*
49 * mowgli_patricia_iteration_state_t, private.
50 */
51 struct mowgli_patricia_iteration_state_
52 {
53 mowgli_patricia_elem_t *cur, *next;
54 void *pspare[4];
55 int ispare[4];
56 };
57
58 typedef struct mowgli_patricia_iteration_state_ mowgli_patricia_iteration_state_t;
59
60 /*
61 * this is a convenience macro for inlining iteration of dictionaries.
62 */
63 #define MOWGLI_PATRICIA_FOREACH(element, state, dict) \
64 for (mowgli_patricia_foreach_start((dict), (state)); (element = mowgli_patricia_foreach_cur((dict), (state))); mowgli_patricia_foreach_next((dict), (state)))
65
66 /*
67 * mowgli_patricia_create() creates a new patricia tree of the defined resolution.
68 * compare_cb is the canonizing function.
69 */
70
71 /* defined if this version of Mowgli allows canonize_cb to be NULL */
72 #define MOWGLI_PATRICIA_ALLOWS_NULL_CANONIZE
73
74 extern mowgli_patricia_t *mowgli_patricia_create(void (*canonize_cb)(char *key))
75 MOWGLI_FATTR_MALLOC;
76 extern mowgli_patricia_t *mowgli_patricia_create_named(const char *name, void (*canonize_cb)(char *key))
77 MOWGLI_FATTR_MALLOC;
78
79 /*
80 * mowgli_patricia_shutdown() deallocates all heaps used in patricia trees. This is
81 * useful on embedded devices with little memory, and/or when you know you won't need
82 * any more patricia trees.
83 */
84 extern void mowgli_patricia_shutdown(void);
85
86 /*
87 * mowgli_patricia_destroy() destroys all entries in a dtree, and also optionally calls
88 * a defined callback function to destroy any data attached to it.
89 */
90 extern void mowgli_patricia_destroy(mowgli_patricia_t *dtree, void (*destroy_cb)(const char *key, void *data, void *privdata), void *privdata);
91
92 /*
93 * mowgli_patricia_foreach() iterates all entries in a dtree, and also optionally calls
94 * a defined callback function to use any data attached to it.
95 *
96 * To shortcircuit iteration, return non-zero from the callback function.
97 */
98 extern void mowgli_patricia_foreach(mowgli_patricia_t *dtree, int (*foreach_cb)(const char *key, void *data, void *privdata), void *privdata);
99
100 /*
101 * mowgli_patricia_search() iterates all entries in a dtree, and also optionally calls
102 * a defined callback function to use any data attached to it.
103 *
104 * When the object is found, a non-NULL is returned from the callback, which results
105 * in that object being returned to the user.
106 */
107 extern void *mowgli_patricia_search(mowgli_patricia_t *dtree, void *(*foreach_cb)(const char *key, void *data, void *privdata), void *privdata);
108
109 /*
110 * mowgli_patricia_foreach_start() begins an iteration over all items
111 * keeping state in the given struct. If there is only one iteration
112 * in progress at a time, it is permitted to remove the current element
113 * of the iteration (but not any other element).
114 */
115 extern void mowgli_patricia_foreach_start(mowgli_patricia_t *dtree, mowgli_patricia_iteration_state_t *state);
116
117 /*
118 * mowgli_patricia_foreach_cur() returns the current element of the iteration,
119 * or NULL if there are no more elements.
120 */
121 extern void *mowgli_patricia_foreach_cur(mowgli_patricia_t *dtree, mowgli_patricia_iteration_state_t *state);
122
123 /*
124 * mowgli_patricia_foreach_next() moves to the next element.
125 */
126 extern void mowgli_patricia_foreach_next(mowgli_patricia_t *dtree, mowgli_patricia_iteration_state_t *state);
127
128 /*
129 * mowgli_patricia_add() adds a key->value entry to the patricia tree.
130 */
131 extern mowgli_boolean_t mowgli_patricia_add(mowgli_patricia_t *dtree, const char *key, void *data);
132
133 /*
134 * mowgli_patricia_find() returns data from a dtree for key 'key'.
135 */
136 extern void *mowgli_patricia_retrieve(mowgli_patricia_t *dtree, const char *key);
137
138 /*
139 * mowgli_patricia_delete() deletes a key->value entry from the patricia tree.
140 */
141 extern void *mowgli_patricia_delete(mowgli_patricia_t *dtree, const char *key);
142
143 /* Low-level functions */
144 mowgli_patricia_elem_t *mowgli_patricia_elem_add(mowgli_patricia_t *dtree, const char *key, void *data);
145 mowgli_patricia_elem_t *mowgli_patricia_elem_find(mowgli_patricia_t *dtree, const char *key);
146 void mowgli_patricia_elem_delete(mowgli_patricia_t *dtree, mowgli_patricia_elem_t *elem);
147 const char *mowgli_patricia_elem_get_key(mowgli_patricia_elem_t *elem);
148 void mowgli_patricia_elem_set_data(mowgli_patricia_elem_t *elem, void *data);
149 void *mowgli_patricia_elem_get_data(mowgli_patricia_elem_t *elem);
150
151 unsigned int mowgli_patricia_size(mowgli_patricia_t *dict);
152 void mowgli_patricia_stats(mowgli_patricia_t *dict, void (*cb)(const char *line, void *privdata), void *privdata);
153
154 #endif /* MOWGLI_SRC_LIBMOWGLI_CONTAINER_PATRICIA_H_INCLUDE_GUARD */