]>
Commit | Line | Data |
---|---|---|
d6bda36d AC |
1 | /* |
2 | * charybdis: an advanced ircd. | |
3 | * irc_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 __IRC_DICTIONARY_H__ | |
26 | #define __IRC_DICTIONARY_H__ | |
27 | ||
28 | struct Dictionary; /* defined in src/dictionary.c */ | |
29 | ||
30 | typedef int (*DCF)(const char *a, const char *b); | |
31 | ||
32 | struct DictionaryElement | |
33 | { | |
34 | struct DictionaryElement *left, *right, *prev, *next; | |
35 | void *data; | |
6db4fb0a | 36 | const char *key; |
d6bda36d AC |
37 | int position; |
38 | }; | |
39 | ||
40 | struct DictionaryIter | |
41 | { | |
42 | struct DictionaryElement *cur, *next; | |
43 | }; | |
44 | ||
45 | /* | |
46 | * this is a convenience macro for inlining iteration of dictionaries. | |
47 | */ | |
48 | #define DICTIONARY_FOREACH(element, state, dict) for (irc_dictionary_foreach_start((dict), (state)); (element = irc_dictionary_foreach_cur((dict), (state))); irc_dictionary_foreach_next((dict), (state))) | |
49 | ||
50 | /* | |
51 | * irc_dictionary_create() creates a new dictionary tree. | |
52 | * compare_cb is the comparison function, typically strcmp, strcasecmp or | |
53 | * irccasecmp. | |
54 | */ | |
55 | extern struct Dictionary *irc_dictionary_create(DCF compare_cb); | |
56 | ||
57 | /* | |
58 | * irc_dictionary_create_named() creates a new dictionary tree which has a name. | |
59 | * name is the name, compare_cb is the comparator. | |
60 | */ | |
61 | extern struct Dictionary *irc_dictionary_create_named(const char *name, DCF compare_cb); | |
62 | ||
63 | /* | |
64 | * irc_dictionary_set_comparator_func() resets the comparator used for lookups and | |
65 | * insertions in the DTree structure. | |
66 | */ | |
67 | extern void irc_dictionary_set_comparator_func(struct Dictionary *dict, | |
68 | DCF compare_cb); | |
69 | ||
70 | /* | |
71 | * irc_dictionary_get_comparator_func() returns the comparator used for lookups and | |
72 | * insertions in the DTree structure. | |
73 | */ | |
74 | extern DCF irc_dictionary_get_comparator_func(struct Dictionary *dict); | |
75 | ||
76 | /* | |
77 | * irc_dictionary_get_linear_index() returns the linear index of an object in the | |
78 | * DTree structure. | |
79 | */ | |
80 | extern int irc_dictionary_get_linear_index(struct Dictionary *dict, const char *key); | |
81 | ||
82 | /* | |
83 | * irc_dictionary_destroy() destroys all entries in a dtree, and also optionally calls | |
84 | * a defined callback function to destroy any data attached to it. | |
85 | */ | |
86 | extern void irc_dictionary_destroy(struct Dictionary *dtree, | |
87 | void (*destroy_cb)(struct DictionaryElement *delem, void *privdata), | |
88 | void *privdata); | |
89 | ||
90 | /* | |
91 | * irc_dictionary_foreach() iterates all entries in a dtree, and also optionally calls | |
92 | * a defined callback function to use any data attached to it. | |
93 | * | |
94 | * To shortcircuit iteration, return non-zero from the callback function. | |
95 | */ | |
96 | extern void irc_dictionary_foreach(struct Dictionary *dtree, | |
97 | int (*foreach_cb)(struct DictionaryElement *delem, void *privdata), | |
98 | void *privdata); | |
99 | ||
100 | /* | |
101 | * irc_dictionary_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 *irc_dictionary_search(struct Dictionary *dtree, | |
108 | void *(*foreach_cb)(struct DictionaryElement *delem, void *privdata), | |
109 | void *privdata); | |
110 | ||
111 | /* | |
112 | * irc_dictionary_foreach_start() begins an iteration over all items | |
113 | * keeping state in the given struct. If there is only one iteration | |
114 | * in progress at a time, it is permitted to remove the current element | |
115 | * of the iteration (but not any other element). | |
116 | */ | |
117 | extern void irc_dictionary_foreach_start(struct Dictionary *dtree, | |
118 | struct DictionaryIter *state); | |
119 | ||
120 | /* | |
121 | * irc_dictionary_foreach_cur() returns the current element of the iteration, | |
122 | * or NULL if there are no more elements. | |
123 | */ | |
124 | extern void *irc_dictionary_foreach_cur(struct Dictionary *dtree, | |
125 | struct DictionaryIter *state); | |
126 | ||
127 | /* | |
128 | * irc_dictionary_foreach_next() moves to the next element. | |
129 | */ | |
130 | extern void irc_dictionary_foreach_next(struct Dictionary *dtree, | |
131 | struct DictionaryIter *state); | |
132 | ||
133 | /* | |
134 | * irc_dictionary_add() adds a key->value entry to the dictionary tree. | |
135 | */ | |
6db4fb0a | 136 | extern struct DictionaryElement *irc_dictionary_add(struct Dictionary *dtree, const char *key, void *data); |
d6bda36d AC |
137 | |
138 | /* | |
139 | * irc_dictionary_find() returns a struct DictionaryElement container from a dtree for key 'key'. | |
140 | */ | |
141 | extern struct DictionaryElement *irc_dictionary_find(struct Dictionary *dtree, const char *key); | |
142 | ||
143 | /* | |
144 | * irc_dictionary_find() returns data from a dtree for key 'key'. | |
145 | */ | |
146 | extern void *irc_dictionary_retrieve(struct Dictionary *dtree, const char *key); | |
147 | ||
148 | /* | |
149 | * irc_dictionary_delete() deletes a key->value entry from the dictionary tree. | |
150 | */ | |
151 | extern void *irc_dictionary_delete(struct Dictionary *dtree, const char *key); | |
152 | ||
2e819b6b JT |
153 | /* |
154 | * irc_dictionary_size() returns the number of elements in a dictionary tree. | |
155 | */ | |
156 | extern unsigned int irc_dictionary_size(struct Dictionary *dtree); | |
157 | ||
d6bda36d AC |
158 | void irc_dictionary_stats(struct Dictionary *dict, void (*cb)(const char *line, void *privdata), void *privdata); |
159 | ||
160 | #endif |