| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* dictionary.h: Dictionary-based storage. |
| 4 |
* |
| 5 |
* Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk> |
| 6 |
* Copyright (c) 2007 Jilles Tjoelker <jilles -at- stack.nl> |
| 7 |
* |
| 8 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 9 |
* purpose with or without fee is hereby granted, provided that the above |
| 10 |
* copyright notice and this permission notice is present in all copies. |
| 11 |
* |
| 12 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 13 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 14 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 15 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 16 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 17 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 18 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 19 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 20 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 21 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 22 |
* POSSIBILITY OF SUCH DAMAGE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifndef MOWGLI_SRC_LIBMOWGLI_CONTAINER_DICTIONARY_H_INCLUDE_GUARD |
| 26 |
#define MOWGLI_SRC_LIBMOWGLI_CONTAINER_DICTIONARY_H_INCLUDE_GUARD 1 |
| 27 |
|
| 28 |
#include "core/stdinc.h" |
| 29 |
#include "platform/attributes.h" |
| 30 |
|
| 31 |
struct mowgli_dictionary_; /* defined in src/dictionary.c */ |
| 32 |
|
| 33 |
typedef struct mowgli_dictionary_ mowgli_dictionary_t; |
| 34 |
|
| 35 |
typedef struct mowgli_dictionary_elem_ mowgli_dictionary_elem_t; |
| 36 |
|
| 37 |
typedef ptrdiff_t (*mowgli_dictionary_comparator_func_t)(const void *a, const void *b); |
| 38 |
|
| 39 |
struct mowgli_dictionary_elem_ |
| 40 |
{ |
| 41 |
mowgli_dictionary_elem_t *left, *right, *prev, *next; |
| 42 |
void *data; |
| 43 |
const void *key; |
| 44 |
int position; |
| 45 |
}; |
| 46 |
|
| 47 |
/* |
| 48 |
* mowgli_dictionary_iteration_state_t, private. |
| 49 |
*/ |
| 50 |
struct mowgli_dictionary_iteration_state_ |
| 51 |
{ |
| 52 |
mowgli_dictionary_elem_t *cur, *next; |
| 53 |
void *pspare[4]; |
| 54 |
int ispare[4]; |
| 55 |
}; |
| 56 |
|
| 57 |
typedef struct mowgli_dictionary_iteration_state_ mowgli_dictionary_iteration_state_t; |
| 58 |
|
| 59 |
/* |
| 60 |
* this is a convenience macro for inlining iteration of dictionaries. |
| 61 |
*/ |
| 62 |
#define MOWGLI_DICTIONARY_FOREACH(element, state, dict) \ |
| 63 |
for (mowgli_dictionary_foreach_start((dict), (state)); (element = mowgli_dictionary_foreach_cur((dict), (state))); mowgli_dictionary_foreach_next((dict), (state))) |
| 64 |
|
| 65 |
/* |
| 66 |
* mowgli_dictionary_create() creates a new dictionary tree. |
| 67 |
* compare_cb is the comparison function, typically strcmp, strcasecmp or |
| 68 |
* irccasecmp. |
| 69 |
*/ |
| 70 |
extern mowgli_dictionary_t *mowgli_dictionary_create(mowgli_dictionary_comparator_func_t compare_cb) |
| 71 |
MOWGLI_FATTR_MALLOC; |
| 72 |
|
| 73 |
/* |
| 74 |
* mowgli_dictionary_create_named() creates a new dictionary tree which has a name. |
| 75 |
* name is the name, compare_cb is the comparator. |
| 76 |
*/ |
| 77 |
extern mowgli_dictionary_t *mowgli_dictionary_create_named(const char *name, mowgli_dictionary_comparator_func_t compare_cb) |
| 78 |
MOWGLI_FATTR_MALLOC; |
| 79 |
|
| 80 |
/* |
| 81 |
* mowgli_dictionary_set_comparator_func() resets the comparator used for lookups and |
| 82 |
* insertions in the DTree structure. |
| 83 |
*/ |
| 84 |
extern void mowgli_dictionary_set_comparator_func(mowgli_dictionary_t *dict, mowgli_dictionary_comparator_func_t compare_cb); |
| 85 |
|
| 86 |
/* |
| 87 |
* mowgli_dictionary_get_comparator_func() returns the comparator used for lookups and |
| 88 |
* insertions in the DTree structure. |
| 89 |
*/ |
| 90 |
extern mowgli_dictionary_comparator_func_t mowgli_dictionary_get_comparator_func(mowgli_dictionary_t *dict); |
| 91 |
|
| 92 |
/* |
| 93 |
* mowgli_dictionary_get_linear_index() returns the linear index of an object in the |
| 94 |
* DTree structure. |
| 95 |
*/ |
| 96 |
extern int mowgli_dictionary_get_linear_index(mowgli_dictionary_t *dict, const void *key); |
| 97 |
|
| 98 |
/* |
| 99 |
* mowgli_dictionary_destroy() destroys all entries in a dtree, and also optionally calls |
| 100 |
* a defined callback function to destroy any data attached to it. |
| 101 |
*/ |
| 102 |
extern void mowgli_dictionary_destroy(mowgli_dictionary_t *dtree, void (*destroy_cb)(mowgli_dictionary_elem_t *delem, void *privdata), void *privdata); |
| 103 |
|
| 104 |
/* |
| 105 |
* mowgli_dictionary_foreach() iterates all entries in a dtree, and also optionally calls |
| 106 |
* a defined callback function to use any data attached to it. |
| 107 |
* |
| 108 |
* To shortcircuit iteration, return non-zero from the callback function. |
| 109 |
*/ |
| 110 |
extern void mowgli_dictionary_foreach(mowgli_dictionary_t *dtree, int (*foreach_cb)(mowgli_dictionary_elem_t *delem, void *privdata), void *privdata); |
| 111 |
|
| 112 |
/* |
| 113 |
* mowgli_dictionary_search() iterates all entries in a dtree, and also optionally calls |
| 114 |
* a defined callback function to use any data attached to it. |
| 115 |
* |
| 116 |
* When the object is found, a non-NULL is returned from the callback, which results |
| 117 |
* in that object being returned to the user. |
| 118 |
*/ |
| 119 |
extern void *mowgli_dictionary_search(mowgli_dictionary_t *dtree, void *(*foreach_cb)(mowgli_dictionary_elem_t * delem, void *privdata), void *privdata); |
| 120 |
|
| 121 |
/* |
| 122 |
* mowgli_dictionary_foreach_start() begins an iteration over all items |
| 123 |
* keeping state in the given struct. If there is only one iteration |
| 124 |
* in progress at a time, it is permitted to remove the current element |
| 125 |
* of the iteration (but not any other element). |
| 126 |
*/ |
| 127 |
extern void mowgli_dictionary_foreach_start(mowgli_dictionary_t *dtree, mowgli_dictionary_iteration_state_t *state); |
| 128 |
|
| 129 |
/* |
| 130 |
* mowgli_dictionary_foreach_cur() returns the current element of the iteration, |
| 131 |
* or NULL if there are no more elements. |
| 132 |
*/ |
| 133 |
extern void *mowgli_dictionary_foreach_cur(mowgli_dictionary_t *dtree, mowgli_dictionary_iteration_state_t *state); |
| 134 |
|
| 135 |
/* |
| 136 |
* mowgli_dictionary_foreach_next() moves to the next element. |
| 137 |
*/ |
| 138 |
extern void mowgli_dictionary_foreach_next(mowgli_dictionary_t *dtree, mowgli_dictionary_iteration_state_t *state); |
| 139 |
|
| 140 |
/* |
| 141 |
* mowgli_dictionary_add() adds a key->value entry to the dictionary tree. |
| 142 |
*/ |
| 143 |
extern mowgli_dictionary_elem_t *mowgli_dictionary_add(mowgli_dictionary_t *dtree, const void *key, void *data) |
| 144 |
MOWGLI_FATTR_MALLOC; |
| 145 |
|
| 146 |
/* |
| 147 |
* mowgli_dictionary_find() returns a mowgli_dictionary_elem_t container from a dtree for key 'key'. |
| 148 |
*/ |
| 149 |
extern mowgli_dictionary_elem_t *mowgli_dictionary_find(mowgli_dictionary_t *dtree, const void *key); |
| 150 |
|
| 151 |
/* |
| 152 |
* mowgli_dictionary_find() returns data from a dtree for key 'key'. |
| 153 |
*/ |
| 154 |
extern void *mowgli_dictionary_retrieve(mowgli_dictionary_t *dtree, const void *key); |
| 155 |
|
| 156 |
/* |
| 157 |
* mowgli_dictionary_size(): returns the size of a dictionary. |
| 158 |
*/ |
| 159 |
extern unsigned int mowgli_dictionary_size(mowgli_dictionary_t *dict); |
| 160 |
|
| 161 |
/* |
| 162 |
* mowgli_dictionary_delete() deletes a key->value entry from the dictionary tree. |
| 163 |
*/ |
| 164 |
extern void *mowgli_dictionary_delete(mowgli_dictionary_t *dtree, const void *key); |
| 165 |
|
| 166 |
void mowgli_dictionary_stats(mowgli_dictionary_t *dict, void (*cb)(const char *line, void *privdata), void *privdata); |
| 167 |
|
| 168 |
#endif /* MOWGLI_SRC_LIBMOWGLI_CONTAINER_DICTIONARY_H_INCLUDE_GUARD */ |