]>
jfr.im git - solanum.git/blob - src/irc_dictionary.c
2 * charybdis: an advanced ircd
3 * irc_dictionary.c: Dictionary-based information storage.
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 * Copyright (c) 2007 Jilles Tjoelker <jilles -at- stack.nl>
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.
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.
29 #include "irc_dictionary.h"
31 static rb_bh
*elem_heap
= NULL
;
36 struct DictionaryElement
*root
, *head
, *tail
;
43 * irc_dictionary_create(DCF compare_cb)
45 * Dictionary object factory.
48 * - function to use for comparing two entries in the dtree
51 * - on success, a new dictionary object.
54 * - if services runs out of memory and cannot allocate the object,
55 * the program will abort.
57 struct Dictionary
*irc_dictionary_create(DCF compare_cb
)
59 struct Dictionary
*dtree
= (struct Dictionary
*) rb_malloc(sizeof(struct Dictionary
));
61 dtree
->compare_cb
= compare_cb
;
64 elem_heap
= rb_bh_create(sizeof(struct DictionaryElement
), 1024, "dictionary_elem_heap");
70 * irc_dictionary_create_named(const char *name,
73 * Dictionary object factory.
77 * - function to use for comparing two entries in the dtree
80 * - on success, a new dictionary object.
83 * - if services runs out of memory and cannot allocate the object,
84 * the program will abort.
86 struct Dictionary
*irc_dictionary_create_named(const char *name
,
89 struct Dictionary
*dtree
= (struct Dictionary
*) rb_malloc(sizeof(struct Dictionary
));
91 dtree
->compare_cb
= compare_cb
;
92 dtree
->id
= rb_strdup(name
);
95 elem_heap
= rb_bh_create(sizeof(struct DictionaryElement
), 1024, "dictionary_elem_heap");
101 * irc_dictionary_set_comparator_func(struct Dictionary *dict,
104 * Resets the comparator function used by the dictionary code for
105 * updating the DTree structure.
108 * - dictionary object
109 * - new comparator function (passed as functor)
115 * - the dictionary comparator function is reset.
117 void irc_dictionary_set_comparator_func(struct Dictionary
*dict
,
120 s_assert(dict
!= NULL
);
121 s_assert(compare_cb
!= NULL
);
123 dict
->compare_cb
= compare_cb
;
127 * irc_dictionary_get_comparator_func(struct Dictionary *dict)
129 * Returns the current comparator function used by the dictionary.
132 * - dictionary object
135 * - comparator function (returned as functor)
141 irc_dictionary_get_comparator_func(struct Dictionary
*dict
)
143 s_assert(dict
!= NULL
);
145 return dict
->compare_cb
;
149 * irc_dictionary_get_linear_index(struct Dictionary *dict,
152 * Gets a linear index number for key.
155 * - dictionary tree object
159 * - position, from zero.
162 * - rebuilds the linear index if the tree is marked as dirty.
165 irc_dictionary_get_linear_index(struct Dictionary
*dict
, const char *key
)
167 struct DictionaryElement
*elem
;
169 s_assert(dict
!= NULL
);
170 s_assert(key
!= NULL
);
172 elem
= irc_dictionary_find(dict
, key
);
177 return elem
->position
;
180 struct DictionaryElement
*delem
;
183 for (delem
= dict
->head
, i
= 0; delem
!= NULL
; delem
= delem
->next
, i
++)
189 return elem
->position
;
193 * irc_dictionary_retune(struct Dictionary *dict, const char *key)
195 * Retunes the tree, self-optimizing for the element which belongs to key.
198 * - node to begin search from
204 * - a new root node is nominated.
207 irc_dictionary_retune(struct Dictionary
*dict
, const char *key
)
209 struct DictionaryElement n
, *tn
, *left
, *right
, *node
;
212 s_assert(dict
!= NULL
);
214 if (dict
->root
== NULL
)
218 * we initialize n with known values, since it's on stack
219 * memory. otherwise the dict would become corrupted.
221 * n is used for temporary storage while the tree is retuned.
224 n
.left
= n
.right
= NULL
;
227 /* this for(;;) loop is the main workhorse of the rebalancing */
228 for (node
= dict
->root
; ; )
230 if ((ret
= dict
->compare_cb(key
, node
->key
)) == 0)
235 if (node
->left
== NULL
)
238 if ((ret
= dict
->compare_cb(key
, node
->left
->key
)) < 0)
241 node
->left
= tn
->right
;
245 if (node
->left
== NULL
)
255 if (node
->right
== NULL
)
258 if ((ret
= dict
->compare_cb(key
, node
->right
->key
)) > 0)
261 node
->right
= tn
->left
;
265 if (node
->right
== NULL
)
275 left
->right
= node
->left
;
276 right
->left
= node
->right
;
278 node
->left
= n
.right
;
279 node
->right
= n
.left
;
285 * irc_dictionary_link(struct Dictionary *dict,
286 * struct DictionaryElement *delem)
288 * Links a dictionary tree element to the dictionary.
290 * When we add new nodes to the tree, it becomes the
291 * next nominated root. This is perhaps not a wise
292 * optimization because of automatic retuning, but
293 * it keeps the code simple.
297 * - dictionary tree element
303 * - a node is linked to the dictionary tree
306 irc_dictionary_link(struct Dictionary
*dict
,
307 struct DictionaryElement
*delem
)
309 s_assert(dict
!= NULL
);
310 s_assert(delem
!= NULL
);
316 if (dict
->root
== NULL
)
318 delem
->left
= delem
->right
= NULL
;
319 delem
->next
= delem
->prev
= NULL
;
320 dict
->head
= dict
->tail
= dict
->root
= delem
;
326 irc_dictionary_retune(dict
, delem
->key
);
328 if ((ret
= dict
->compare_cb(delem
->key
, dict
->root
->key
)) < 0)
330 delem
->left
= dict
->root
->left
;
331 delem
->right
= dict
->root
;
332 dict
->root
->left
= NULL
;
334 if (dict
->root
->prev
)
335 dict
->root
->prev
->next
= delem
;
339 delem
->prev
= dict
->root
->prev
;
340 delem
->next
= dict
->root
;
341 dict
->root
->prev
= delem
;
346 delem
->right
= dict
->root
->right
;
347 delem
->left
= dict
->root
;
348 dict
->root
->right
= NULL
;
350 if (dict
->root
->next
)
351 dict
->root
->next
->prev
= delem
;
355 delem
->next
= dict
->root
->next
;
356 delem
->prev
= dict
->root
;
357 dict
->root
->next
= delem
;
362 dict
->root
->key
= delem
->key
;
363 dict
->root
->data
= delem
->data
;
366 rb_bh_free(elem_heap
, delem
);
372 * irc_dictionary_unlink_root(struct Dictionary *dict)
374 * Unlinks the root dictionary tree element from the dictionary.
383 * - the root node is unlinked from the dictionary tree
386 irc_dictionary_unlink_root(struct Dictionary
*dict
)
388 struct DictionaryElement
*delem
, *nextnode
, *parentofnext
;
396 if (dict
->root
->left
== NULL
)
397 dict
->root
= dict
->root
->right
;
398 else if (dict
->root
->right
== NULL
)
399 dict
->root
= dict
->root
->left
;
402 /* Make the node with the next highest key the new root.
403 * This node has a NULL left pointer. */
404 nextnode
= delem
->next
;
405 s_assert(nextnode
->left
== NULL
);
406 if (nextnode
== delem
->right
)
408 dict
->root
= nextnode
;
409 dict
->root
->left
= delem
->left
;
413 parentofnext
= delem
->right
;
414 while (parentofnext
->left
!= NULL
&& parentofnext
->left
!= nextnode
)
415 parentofnext
= parentofnext
->left
;
416 s_assert(parentofnext
->left
== nextnode
);
417 parentofnext
->left
= nextnode
->right
;
418 dict
->root
= nextnode
;
419 dict
->root
->left
= delem
->left
;
420 dict
->root
->right
= delem
->right
;
425 if (delem
->prev
!= NULL
)
426 delem
->prev
->next
= delem
->next
;
428 if (dict
->head
== delem
)
429 dict
->head
= delem
->next
;
432 delem
->next
->prev
= delem
->prev
;
434 if (dict
->tail
== delem
)
435 dict
->tail
= delem
->prev
;
441 * irc_dictionary_destroy(struct Dictionary *dtree,
442 * void (*destroy_cb)(dictionary_elem_t *delem, void *privdata),
445 * Recursively destroys all nodes in a dictionary tree.
448 * - dictionary tree object
449 * - optional iteration callback
450 * - optional opaque/private data to pass to callback
456 * - on success, a dtree and optionally it's children are destroyed.
459 * - if this is called without a callback, the objects bound to the
460 * DTree will not be destroyed.
462 void irc_dictionary_destroy(struct Dictionary
*dtree
,
463 void (*destroy_cb
)(struct DictionaryElement
*delem
, void *privdata
),
466 struct DictionaryElement
*n
, *tn
;
468 s_assert(dtree
!= NULL
);
470 RB_DLINK_FOREACH_SAFE(n
, tn
, dtree
->head
)
472 if (destroy_cb
!= NULL
)
473 (*destroy_cb
)(n
, privdata
);
475 rb_bh_free(elem_heap
, n
);
482 * irc_dictionary_foreach(struct Dictionary *dtree,
483 * void (*destroy_cb)(dictionary_elem_t *delem, void *privdata),
486 * Iterates over all entries in a DTree.
489 * - dictionary tree object
490 * - optional iteration callback
491 * - optional opaque/private data to pass to callback
497 * - on success, a dtree is iterated
499 void irc_dictionary_foreach(struct Dictionary
*dtree
,
500 int (*foreach_cb
)(struct DictionaryElement
*delem
, void *privdata
),
503 struct DictionaryElement
*n
, *tn
;
505 s_assert(dtree
!= NULL
);
507 RB_DLINK_FOREACH_SAFE(n
, tn
, dtree
->head
)
509 /* delem_t is a subclass of node_t. */
510 struct DictionaryElement
*delem
= (struct DictionaryElement
*) n
;
512 if (foreach_cb
!= NULL
)
513 (*foreach_cb
)(delem
, privdata
);
518 * irc_dictionary_search(struct Dictionary *dtree,
519 * void (*destroy_cb)(struct DictionaryElement *delem, void *privdata),
522 * Searches all entries in a DTree using a custom callback.
525 * - dictionary tree object
526 * - optional iteration callback
527 * - optional opaque/private data to pass to callback
530 * - on success, the requested object
531 * - on failure, NULL.
534 * - a dtree is iterated until the requested conditions are met
536 void *irc_dictionary_search(struct Dictionary
*dtree
,
537 void *(*foreach_cb
)(struct DictionaryElement
*delem
, void *privdata
),
540 struct DictionaryElement
*n
, *tn
;
543 s_assert(dtree
!= NULL
);
545 RB_DLINK_FOREACH_SAFE(n
, tn
, dtree
->head
)
547 /* delem_t is a subclass of node_t. */
548 struct DictionaryElement
*delem
= (struct DictionaryElement
*) n
;
550 if (foreach_cb
!= NULL
)
551 ret
= (*foreach_cb
)(delem
, privdata
);
561 * irc_dictionary_foreach_start(struct Dictionary *dtree,
562 * struct DictionaryIter *state);
564 * Initializes a static DTree iterator.
567 * - dictionary tree object
568 * - static DTree iterator
574 * - the static iterator, &state, is initialized.
576 void irc_dictionary_foreach_start(struct Dictionary
*dtree
,
577 struct DictionaryIter
*state
)
579 s_assert(dtree
!= NULL
);
580 s_assert(state
!= NULL
);
585 /* find first item */
586 state
->cur
= dtree
->head
;
588 if (state
->cur
== NULL
)
591 /* make state->cur point to first item and state->next point to
593 state
->next
= state
->cur
;
594 irc_dictionary_foreach_next(dtree
, state
);
598 * irc_dictionary_foreach_cur(struct Dictionary *dtree,
599 * struct DictionaryIter *state);
601 * Returns the data from the current node being iterated by the
605 * - dictionary tree object
606 * - static DTree iterator
609 * - reference to data in the current dtree node being iterated
614 void *irc_dictionary_foreach_cur(struct Dictionary
*dtree
,
615 struct DictionaryIter
*state
)
617 s_assert(dtree
!= NULL
);
618 s_assert(state
!= NULL
);
620 return state
->cur
!= NULL
? state
->cur
->data
: NULL
;
624 * irc_dictionary_foreach_next(struct Dictionary *dtree,
625 * struct DictionaryIter *state);
627 * Advances a static DTree iterator.
630 * - dictionary tree object
631 * - static DTree iterator
637 * - the static iterator, &state, is advanced to a new DTree node.
639 void irc_dictionary_foreach_next(struct Dictionary
*dtree
,
640 struct DictionaryIter
*state
)
642 s_assert(dtree
!= NULL
);
643 s_assert(state
!= NULL
);
645 if (state
->cur
== NULL
)
647 ilog(L_MAIN
, "irc_dictionary_foreach_next(): called again after iteration finished on dtree<%p>", dtree
);
651 state
->cur
= state
->next
;
653 if (state
->next
== NULL
)
656 state
->next
= state
->next
->next
;
660 * irc_dictionary_find(struct Dictionary *dtree, const char *key)
662 * Looks up a DTree node by name.
665 * - dictionary tree object
666 * - name of node to lookup
669 * - on success, the dtree node requested
675 struct DictionaryElement
*irc_dictionary_find(struct Dictionary
*dict
, const char *key
)
677 s_assert(dict
!= NULL
);
678 s_assert(key
!= NULL
);
680 /* retune for key, key will be the tree's root if it's available */
681 irc_dictionary_retune(dict
, key
);
683 if (dict
->root
&& !dict
->compare_cb(key
, dict
->root
->key
))
690 * irc_dictionary_add(struct Dictionary *dtree, const char *key, void *data)
692 * Creates a new DTree node and binds data to it.
695 * - dictionary tree object
696 * - name for new DTree node
697 * - data to bind to the new DTree node
700 * - on success, a new DTree node
704 * - data is inserted into the DTree.
706 struct DictionaryElement
*irc_dictionary_add(struct Dictionary
*dict
, const char *key
, void *data
)
708 struct DictionaryElement
*delem
;
710 s_assert(dict
!= NULL
);
711 s_assert(key
!= NULL
);
712 s_assert(data
!= NULL
);
713 s_assert(irc_dictionary_find(dict
, key
) == NULL
);
715 delem
= rb_bh_alloc(elem_heap
);
719 /* TBD: is this needed? --nenolod */
720 if (delem
->key
== NULL
)
722 rb_bh_free(elem_heap
, delem
);
726 irc_dictionary_link(dict
, delem
);
732 * irc_dictionary_delete(struct Dictionary *dtree, const char *key)
734 * Deletes data from a dictionary tree.
737 * - dictionary tree object
738 * - name of DTree node to delete
741 * - on success, the remaining data that needs to be mowgli_freed
745 * - data is removed from the DTree.
748 * - the returned data needs to be mowgli_freed/released manually!
750 void *irc_dictionary_delete(struct Dictionary
*dtree
, const char *key
)
752 struct DictionaryElement
*delem
= irc_dictionary_find(dtree
, key
);
760 irc_dictionary_unlink_root(dtree
);
761 rb_bh_free(elem_heap
, delem
);
767 * irc_dictionary_retrieve(struct Dictionary *dtree, const char *key)
769 * Retrieves data from a dictionary.
772 * - dictionary tree object
773 * - name of node to lookup
776 * - on success, the data bound to the DTree node.
782 void *irc_dictionary_retrieve(struct Dictionary
*dtree
, const char *key
)
784 struct DictionaryElement
*delem
= irc_dictionary_find(dtree
, key
);
793 * irc_dictionary_size(struct Dictionary *dict)
795 * Returns the size of a dictionary.
798 * - dictionary tree object
801 * - size of dictionary
806 unsigned int irc_dictionary_size(struct Dictionary
*dict
)
808 s_assert(dict
!= NULL
);
813 /* returns the sum of the depths of the subtree rooted in delem at depth depth */
815 stats_recurse(struct DictionaryElement
*delem
, int depth
, int *pmaxdepth
)
819 if (depth
> *pmaxdepth
)
823 result
+= stats_recurse(delem
->left
, depth
+ 1, pmaxdepth
);
825 result
+= stats_recurse(delem
->right
, depth
+ 1, pmaxdepth
);
830 * irc_dictionary_stats(struct Dictionary *dict, void (*cb)(const char *line, void *privdata), void *privdata)
832 * Returns the size of a dictionary.
835 * - dictionary tree object
837 * - data for callback
843 * - callback called with stats text
845 void irc_dictionary_stats(struct Dictionary
*dict
, void (*cb
)(const char *line
, void *privdata
), void *privdata
)
850 s_assert(dict
!= NULL
);
852 if (dict
->id
!= NULL
)
853 snprintf(str
, sizeof str
, "Dictionary stats for %s (%d)",
854 dict
->id
, dict
->count
);
856 snprintf(str
, sizeof str
, "Dictionary stats for <%p> (%d)",
860 sum
= stats_recurse(dict
->root
, 0, &maxdepth
);
861 snprintf(str
, sizeof str
, "Depth sum %d Avg depth %d Max depth %d", sum
, sum
/ dict
->count
, maxdepth
);