1 /* dict-splay.c - Abstract dictionary type
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of srvx.
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
21 * Create new dictionary.
26 dict_t dict = calloc(1, sizeof(*dict));
31 * Return number of entries in the dictionary.
34 dict_size(dict_t dict)
40 * Set the function to be called when freeing a key structure.
41 * If the function is NULL, just forget about the pointer.
44 dict_set_free_keys(dict_t dict, free_f free_keys)
46 dict->free_keys = free_keys;
50 * Set the function to free data.
51 * If the function is NULL, just forget about the pointer.
54 dict_set_free_data(dict_t dict, free_f free_data)
56 dict->free_data = free_data;
60 dict_foreach(dict_t dict, dict_iterator_f it_f, void *extra)
64 for (it=dict_first(dict); it; it=iter_next(it)) {
65 if (it_f(iter_key(it), iter_data(it), extra)) return iter_key(it);
71 * This function finds a node and pulls it to the top of the tree.
72 * This helps balance the tree and auto-cache things you search for.
74 static struct dict_node*
75 dict_splay(struct dict_node *node, const char *key)
77 struct dict_node N, *l, *r, *y;
80 if (!node) return NULL;
86 res = irccasecmp(key, node->key);
90 res = irccasecmp(key, node->l->key);
101 } else { /* res > 0 */
103 res = irccasecmp(key, node->r->key);
124 * Free node. Free data/key using free_f functions.
127 dict_dispose_node(struct dict_node *node, free_f free_keys, free_f free_data)
129 if (free_keys && node->key) {
130 if (free_keys == free)
131 free((void*)node->key);
133 free_keys((void*)node->key);
135 if (free_data && node->data) {
136 if (free_data == free)
139 free_data(node->data);
145 * Insert an entry into the dictionary.
146 * Key ordering (and uniqueness) is determined by case-insensitive
150 dict_insert(dict_t dict, const char *key, void *data)
152 struct dict_node *new_node;
156 new_node = malloc(sizeof(struct dict_node));
158 new_node->data = data;
161 dict->root = dict_splay(dict->root, key);
162 res = irccasecmp(key, dict->root->key);
164 /* insert just "before" current root */
165 new_node->l = dict->root->l;
166 new_node->r = dict->root;
167 dict->root->l = NULL;
168 if (dict->root->prev) {
169 dict->root->prev->next = new_node;
171 dict->first = new_node;
173 new_node->prev = dict->root->prev;
174 new_node->next = dict->root;
175 dict->root->prev = new_node;
176 dict->root = new_node;
177 } else if (res > 0) {
178 /* insert just "after" current root */
179 new_node->r = dict->root->r;
180 new_node->l = dict->root;
181 dict->root->r = NULL;
182 if (dict->root->next) {
183 dict->root->next->prev = new_node;
185 dict->last = new_node;
187 new_node->next = dict->root->next;
188 new_node->prev = dict->root;
189 dict->root->next = new_node;
190 dict->root = new_node;
192 /* maybe we don't want to overwrite it .. oh well */
193 if (dict->free_data) {
194 if (dict->free_data == free)
195 free(dict->root->data);
197 dict->free_data(dict->root->data);
199 if (dict->free_keys) {
200 if (dict->free_keys == free)
201 free((void*)dict->root->key);
203 dict->free_keys((void*)dict->root->key);
206 dict->root->key = key;
207 dict->root->data = data;
208 /* decrement the count since we dropped the node */
212 new_node->l = new_node->r = NULL;
213 new_node->next = new_node->prev = NULL;
214 dict->root = dict->first = dict->last = new_node;
220 * Remove an entry from the dictionary.
221 * Return non-zero if it was found, or zero if the key was not in the
225 dict_remove2(dict_t dict, const char *key, int no_dispose)
227 struct dict_node *new_root, *old_root;
232 dict->root = dict_splay(dict->root, key);
233 if (irccasecmp(key, dict->root->key))
236 if (!dict->root->l) {
237 new_root = dict->root->r;
239 new_root = dict_splay(dict->root->l, key);
240 new_root->r = dict->root->r;
242 if (dict->root->prev) dict->root->prev->next = dict->root->next;
243 if (dict->first == dict->root) dict->first = dict->first->next;
244 if (dict->root->next) dict->root->next->prev = dict->root->prev;
245 if (dict->last == dict->root) dict->last = dict->last->prev;
246 old_root = dict->root;
247 dict->root = new_root;
252 dict_dispose_node(old_root, dict->free_keys, dict->free_data);
258 * Find an entry in the dictionary.
259 * If "found" is non-NULL, set it to non-zero if the key was found.
260 * Return the data associated with the key (or NULL if the key was
264 dict_find(dict_t dict, const char *key, int *found)
267 if (!dict || !dict->root || !key) {
273 dict->root = dict_splay(dict->root, key);
274 was_found = !irccasecmp(key, dict->root->key);
277 return was_found ? dict->root->data : NULL;
281 * Delete an entire dictionary.
284 dict_delete(dict_t dict)
286 dict_iterator_t it, next;
290 for (it=dict_first(dict); it; it=next) {
291 next = iter_next(it);
292 dict_dispose_node(it, dict->free_keys, dict->free_data);
297 struct dict_sanity_struct {
298 unsigned int node_count;
299 struct dict_node *bad_node;
304 dict_sanity_check_node(struct dict_node *node, struct dict_sanity_struct *dss)
308 snprintf(dss->error, sizeof(dss->error), "Node %p had null key", node);
312 if (dict_sanity_check_node(node->l, dss)) return 1;
313 if (irccasecmp(node->l->key, node->key) >= 0) {
314 snprintf(dss->error, sizeof(dss->error), "Node %p's left child's key '%s' >= its key '%s'", node, node->l->key, node->key);
319 if (dict_sanity_check_node(node->r, dss)) return 1;
320 if (irccasecmp(node->key, node->r->key) >= 0) {
321 snprintf(dss->error, sizeof(dss->error), "Node %p's right child's key '%s' <= its key '%s'", node, node->r->key, node->key);
330 * Perform sanity checks on the dict's internal structure.
333 dict_sanity_check(dict_t dict)
335 struct dict_sanity_struct dss;
340 if (dict->root && dict_sanity_check_node(dict->root, &dss)) {
341 return strdup(dss.error);
342 } else if (dss.node_count != dict->count) {
343 snprintf(dss.error, sizeof(dss.error), "Counted %d nodes but expected %d.", dss.node_count, dict->count);
344 return strdup(dss.error);