]>
jfr.im git - irc/rqf/shadowircd.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.
26 #include "sprintf_irc.h"
28 #include "irc_string.h"
33 #include "irc_dictionary.h"
35 static BlockHeap
*elem_heap
= NULL
;
40 struct DictionaryElement
*root
, *head
, *tail
;
47 * irc_dictionary_create(DCF compare_cb)
49 * Dictionary object factory.
52 * - function to use for comparing two entries in the dtree
55 * - on success, a new dictionary object.
58 * - if services runs out of memory and cannot allocate the object,
59 * the program will abort.
61 struct Dictionary
*irc_dictionary_create(DCF compare_cb
)
63 struct Dictionary
*dtree
= (struct Dictionary
*) MyMalloc(sizeof(struct Dictionary
));
65 dtree
->compare_cb
= compare_cb
;
68 elem_heap
= BlockHeapCreate(sizeof(struct DictionaryElement
), 1024);
74 * irc_dictionary_create_named(const char *name,
77 * Dictionary object factory.
81 * - function to use for comparing two entries in the dtree
84 * - on success, a new dictionary object.
87 * - if services runs out of memory and cannot allocate the object,
88 * the program will abort.
90 struct Dictionary
*irc_dictionary_create_named(const char *name
,
93 struct Dictionary
*dtree
= (struct Dictionary
*) MyMalloc(sizeof(struct Dictionary
));
95 dtree
->compare_cb
= compare_cb
;
96 DupString(dtree
->id
, name
);
99 elem_heap
= BlockHeapCreate(sizeof(struct DictionaryElement
), 1024);
105 * irc_dictionary_set_comparator_func(struct Dictionary *dict,
108 * Resets the comparator function used by the dictionary code for
109 * updating the DTree structure.
112 * - dictionary object
113 * - new comparator function (passed as functor)
119 * - the dictionary comparator function is reset.
121 void irc_dictionary_set_comparator_func(struct Dictionary
*dict
,
124 s_assert(dict
!= NULL
);
125 s_assert(compare_cb
!= NULL
);
127 dict
->compare_cb
= compare_cb
;
131 * irc_dictionary_get_comparator_func(struct Dictionary *dict)
133 * Returns the current comparator function used by the dictionary.
136 * - dictionary object
139 * - comparator function (returned as functor)
145 irc_dictionary_get_comparator_func(struct Dictionary
*dict
)
147 s_assert(dict
!= NULL
);
149 return dict
->compare_cb
;
153 * irc_dictionary_get_linear_index(struct Dictionary *dict,
156 * Gets a linear index number for key.
159 * - dictionary tree object
163 * - position, from zero.
166 * - rebuilds the linear index if the tree is marked as dirty.
169 irc_dictionary_get_linear_index(struct Dictionary
*dict
, const char *key
)
171 struct DictionaryElement
*elem
;
173 s_assert(dict
!= NULL
);
174 s_assert(key
!= NULL
);
176 elem
= irc_dictionary_find(dict
, key
);
181 return elem
->position
;
184 struct DictionaryElement
*delem
;
187 for (delem
= dict
->head
, i
= 0; delem
!= NULL
; delem
= delem
->next
, i
++)
193 return elem
->position
;
197 * irc_dictionary_retune(struct Dictionary *dict, const char *key)
199 * Retunes the tree, self-optimizing for the element which belongs to key.
202 * - node to begin search from
208 * - a new root node is nominated.
211 irc_dictionary_retune(struct Dictionary
*dict
, const char *key
)
213 struct DictionaryElement n
, *tn
, *left
, *right
, *node
;
216 s_assert(dict
!= NULL
);
218 if (dict
->root
== NULL
)
222 * we initialize n with known values, since it's on stack
223 * memory. otherwise the dict would become corrupted.
225 * n is used for temporary storage while the tree is retuned.
228 n
.left
= n
.right
= NULL
;
231 /* this for(;;) loop is the main workhorse of the rebalancing */
232 for (node
= dict
->root
; ; )
234 if ((ret
= dict
->compare_cb(key
, node
->key
)) == 0)
239 if (node
->left
== NULL
)
242 if ((ret
= dict
->compare_cb(key
, node
->left
->key
)) < 0)
245 node
->left
= tn
->right
;
249 if (node
->left
== NULL
)
259 if (node
->right
== NULL
)
262 if ((ret
= dict
->compare_cb(key
, node
->right
->key
)) > 0)
265 node
->right
= tn
->left
;
269 if (node
->right
== NULL
)
279 left
->right
= node
->left
;
280 right
->left
= node
->right
;
282 node
->left
= n
.right
;
283 node
->right
= n
.left
;
289 * irc_dictionary_link(struct Dictionary *dict,
290 * struct DictionaryElement *delem)
292 * Links a dictionary tree element to the dictionary.
294 * When we add new nodes to the tree, it becomes the
295 * next nominated root. This is perhaps not a wise
296 * optimization because of automatic retuning, but
297 * it keeps the code simple.
301 * - dictionary tree element
307 * - a node is linked to the dictionary tree
310 irc_dictionary_link(struct Dictionary
*dict
,
311 struct DictionaryElement
*delem
)
313 s_assert(dict
!= NULL
);
314 s_assert(delem
!= NULL
);
320 if (dict
->root
== NULL
)
322 delem
->left
= delem
->right
= NULL
;
323 delem
->next
= delem
->prev
= NULL
;
324 dict
->head
= dict
->tail
= dict
->root
= delem
;
330 irc_dictionary_retune(dict
, delem
->key
);
332 if ((ret
= dict
->compare_cb(delem
->key
, dict
->root
->key
)) < 0)
334 delem
->left
= dict
->root
->left
;
335 delem
->right
= dict
->root
;
336 dict
->root
->left
= NULL
;
338 if (dict
->root
->prev
)
339 dict
->root
->prev
->next
= delem
;
343 delem
->prev
= dict
->root
->prev
;
344 delem
->next
= dict
->root
;
345 dict
->root
->prev
= delem
;
350 delem
->right
= dict
->root
->right
;
351 delem
->left
= dict
->root
;
352 dict
->root
->right
= NULL
;
354 if (dict
->root
->next
)
355 dict
->root
->next
->prev
= delem
;
359 delem
->next
= dict
->root
->next
;
360 delem
->prev
= dict
->root
;
361 dict
->root
->next
= delem
;
366 dict
->root
->key
= delem
->key
;
367 dict
->root
->data
= delem
->data
;
370 BlockHeapFree(elem_heap
, delem
);
376 * irc_dictionary_unlink_root(struct Dictionary *dict)
378 * Unlinks the root dictionary tree element from the dictionary.
387 * - the root node is unlinked from the dictionary tree
390 irc_dictionary_unlink_root(struct Dictionary
*dict
)
392 struct DictionaryElement
*delem
, *nextnode
, *parentofnext
;
400 if (dict
->root
->left
== NULL
)
401 dict
->root
= dict
->root
->right
;
402 else if (dict
->root
->right
== NULL
)
403 dict
->root
= dict
->root
->left
;
406 /* Make the node with the next highest key the new root.
407 * This node has a NULL left pointer. */
408 nextnode
= delem
->next
;
409 s_assert(nextnode
->left
== NULL
);
410 if (nextnode
== delem
->right
)
412 dict
->root
= nextnode
;
413 dict
->root
->left
= delem
->left
;
417 parentofnext
= delem
->right
;
418 while (parentofnext
->left
!= NULL
&& parentofnext
->left
!= nextnode
)
419 parentofnext
= parentofnext
->left
;
420 s_assert(parentofnext
->left
== nextnode
);
421 parentofnext
->left
= nextnode
->right
;
422 dict
->root
= nextnode
;
423 dict
->root
->left
= delem
->left
;
424 dict
->root
->right
= delem
->right
;
429 if (delem
->prev
!= NULL
)
430 delem
->prev
->next
= delem
->next
;
432 if (dict
->head
== delem
)
433 dict
->head
= delem
->next
;
436 delem
->next
->prev
= delem
->prev
;
438 if (dict
->tail
== delem
)
439 dict
->tail
= delem
->prev
;
445 * irc_dictionary_destroy(struct Dictionary *dtree,
446 * void (*destroy_cb)(dictionary_elem_t *delem, void *privdata),
449 * Recursively destroys all nodes in a dictionary tree.
452 * - dictionary tree object
453 * - optional iteration callback
454 * - optional opaque/private data to pass to callback
460 * - on success, a dtree and optionally it's children are destroyed.
463 * - if this is called without a callback, the objects bound to the
464 * DTree will not be destroyed.
466 void irc_dictionary_destroy(struct Dictionary
*dtree
,
467 void (*destroy_cb
)(struct DictionaryElement
*delem
, void *privdata
),
470 struct DictionaryElement
*n
, *tn
;
472 s_assert(dtree
!= NULL
);
474 DLINK_FOREACH_SAFE(n
, tn
, dtree
->head
)
476 if (destroy_cb
!= NULL
)
477 (*destroy_cb
)(n
, privdata
);
479 BlockHeapFree(elem_heap
, n
);
486 * irc_dictionary_foreach(struct Dictionary *dtree,
487 * void (*destroy_cb)(dictionary_elem_t *delem, void *privdata),
490 * Iterates over all entries in a DTree.
493 * - dictionary tree object
494 * - optional iteration callback
495 * - optional opaque/private data to pass to callback
501 * - on success, a dtree is iterated
503 void irc_dictionary_foreach(struct Dictionary
*dtree
,
504 int (*foreach_cb
)(struct DictionaryElement
*delem
, void *privdata
),
507 struct DictionaryElement
*n
, *tn
;
509 s_assert(dtree
!= NULL
);
511 DLINK_FOREACH_SAFE(n
, tn
, dtree
->head
)
513 /* delem_t is a subclass of node_t. */
514 struct DictionaryElement
*delem
= (struct DictionaryElement
*) n
;
516 if (foreach_cb
!= NULL
)
517 (*foreach_cb
)(delem
, privdata
);
522 * irc_dictionary_search(struct Dictionary *dtree,
523 * void (*destroy_cb)(struct DictionaryElement *delem, void *privdata),
526 * Searches all entries in a DTree using a custom callback.
529 * - dictionary tree object
530 * - optional iteration callback
531 * - optional opaque/private data to pass to callback
534 * - on success, the requested object
535 * - on failure, NULL.
538 * - a dtree is iterated until the requested conditions are met
540 void *irc_dictionary_search(struct Dictionary
*dtree
,
541 void *(*foreach_cb
)(struct DictionaryElement
*delem
, void *privdata
),
544 struct DictionaryElement
*n
, *tn
;
547 s_assert(dtree
!= NULL
);
549 DLINK_FOREACH_SAFE(n
, tn
, dtree
->head
)
551 /* delem_t is a subclass of node_t. */
552 struct DictionaryElement
*delem
= (struct DictionaryElement
*) n
;
554 if (foreach_cb
!= NULL
)
555 ret
= (*foreach_cb
)(delem
, privdata
);
565 * irc_dictionary_foreach_start(struct Dictionary *dtree,
566 * struct DictionaryIter *state);
568 * Initializes a static DTree iterator.
571 * - dictionary tree object
572 * - static DTree iterator
578 * - the static iterator, &state, is initialized.
580 void irc_dictionary_foreach_start(struct Dictionary
*dtree
,
581 struct DictionaryIter
*state
)
583 s_assert(dtree
!= NULL
);
584 s_assert(state
!= NULL
);
589 /* find first item */
590 state
->cur
= dtree
->head
;
592 if (state
->cur
== NULL
)
595 /* make state->cur point to first item and state->next point to
597 state
->next
= state
->cur
;
598 irc_dictionary_foreach_next(dtree
, state
);
602 * irc_dictionary_foreach_cur(struct Dictionary *dtree,
603 * struct DictionaryIter *state);
605 * Returns the data from the current node being iterated by the
609 * - dictionary tree object
610 * - static DTree iterator
613 * - reference to data in the current dtree node being iterated
618 void *irc_dictionary_foreach_cur(struct Dictionary
*dtree
,
619 struct DictionaryIter
*state
)
621 s_assert(dtree
!= NULL
);
622 s_assert(state
!= NULL
);
624 return state
->cur
!= NULL
? state
->cur
->data
: NULL
;
628 * irc_dictionary_foreach_next(struct Dictionary *dtree,
629 * struct DictionaryIter *state);
631 * Advances a static DTree iterator.
634 * - dictionary tree object
635 * - static DTree iterator
641 * - the static iterator, &state, is advanced to a new DTree node.
643 void irc_dictionary_foreach_next(struct Dictionary
*dtree
,
644 struct DictionaryIter
*state
)
646 s_assert(dtree
!= NULL
);
647 s_assert(state
!= NULL
);
649 if (state
->cur
== NULL
)
651 ilog(L_MAIN
, "irc_dictionary_foreach_next(): called again after iteration finished on dtree<%p>", dtree
);
655 state
->cur
= state
->next
;
657 if (state
->next
== NULL
)
660 state
->next
= state
->next
->next
;
664 * irc_dictionary_find(struct Dictionary *dtree, const char *key)
666 * Looks up a DTree node by name.
669 * - dictionary tree object
670 * - name of node to lookup
673 * - on success, the dtree node requested
679 struct DictionaryElement
*irc_dictionary_find(struct Dictionary
*dict
, const char *key
)
681 s_assert(dict
!= NULL
);
682 s_assert(key
!= NULL
);
684 /* retune for key, key will be the tree's root if it's available */
685 irc_dictionary_retune(dict
, key
);
687 if (dict
->root
&& !dict
->compare_cb(key
, dict
->root
->key
))
694 * irc_dictionary_add(struct Dictionary *dtree, const char *key, void *data)
696 * Creates a new DTree node and binds data to it.
699 * - dictionary tree object
700 * - name for new DTree node
701 * - data to bind to the new DTree node
704 * - on success, a new DTree node
708 * - data is inserted into the DTree.
710 struct DictionaryElement
*irc_dictionary_add(struct Dictionary
*dict
, char *key
, void *data
)
712 struct DictionaryElement
*delem
;
714 s_assert(dict
!= NULL
);
715 s_assert(key
!= NULL
);
716 s_assert(data
!= NULL
);
717 s_assert(irc_dictionary_find(dict
, key
) == NULL
);
719 delem
= BlockHeapAlloc(elem_heap
);
723 /* TBD: is this needed? --nenolod */
724 if (delem
->key
== NULL
)
726 BlockHeapFree(elem_heap
, delem
);
730 irc_dictionary_link(dict
, delem
);
736 * irc_dictionary_delete(struct Dictionary *dtree, const char *key)
738 * Deletes data from a dictionary tree.
741 * - dictionary tree object
742 * - name of DTree node to delete
745 * - on success, the remaining data that needs to be mowgli_freed
749 * - data is removed from the DTree.
752 * - the returned data needs to be mowgli_freed/released manually!
754 void *irc_dictionary_delete(struct Dictionary
*dtree
, const char *key
)
756 struct DictionaryElement
*delem
= irc_dictionary_find(dtree
, key
);
764 irc_dictionary_unlink_root(dtree
);
765 BlockHeapFree(elem_heap
, delem
);
771 * irc_dictionary_retrieve(struct Dictionary *dtree, const char *key)
773 * Retrieves data from a dictionary.
776 * - dictionary tree object
777 * - name of node to lookup
780 * - on success, the data bound to the DTree node.
786 void *irc_dictionary_retrieve(struct Dictionary
*dtree
, const char *key
)
788 struct DictionaryElement
*delem
= irc_dictionary_find(dtree
, key
);
797 * irc_dictionary_size(struct Dictionary *dict)
799 * Returns the size of a dictionary.
802 * - dictionary tree object
805 * - size of dictionary
810 unsigned int irc_dictionary_size(struct Dictionary
*dict
)
812 s_assert(dict
!= NULL
);
817 /* returns the sum of the depths of the subtree rooted in delem at depth depth */
819 stats_recurse(struct DictionaryElement
*delem
, int depth
, int *pmaxdepth
)
823 if (depth
> *pmaxdepth
)
827 result
+= stats_recurse(delem
->left
, depth
+ 1, pmaxdepth
);
829 result
+= stats_recurse(delem
->right
, depth
+ 1, pmaxdepth
);
834 * irc_dictionary_stats(struct Dictionary *dict, void (*cb)(const char *line, void *privdata), void *privdata)
836 * Returns the size of a dictionary.
839 * - dictionary tree object
841 * - data for callback
847 * - callback called with stats text
849 void irc_dictionary_stats(struct Dictionary
*dict
, void (*cb
)(const char *line
, void *privdata
), void *privdata
)
854 s_assert(dict
!= NULL
);
856 if (dict
->id
!= NULL
)
857 snprintf(str
, sizeof str
, "Dictionary stats for %s (%d)",
858 dict
->id
, dict
->count
);
860 snprintf(str
, sizeof str
, "Dictionary stats for <%p> (%d)",
864 sum
= stats_recurse(dict
->root
, 0, &maxdepth
);
865 snprintf(str
, sizeof str
, "Depth sum %d Avg depth %d Max depth %d", sum
, sum
/ dict
->count
, maxdepth
);