Debian Codegraph @

libmowgli-2/src/libmowgli/container/dictionary.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * dictionary.c: Dictionary-based information 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 #include "mowgli.h"
26
27 static mowgli_heap_t *elem_heap = NULL;
28
29 struct mowgli_dictionary_
30 {
31 mowgli_dictionary_comparator_func_t compare_cb;
32 mowgli_dictionary_elem_t *root, *head, *tail;
33 unsigned int count;
34 char *id;
35 bool dirty;
36 };
37
38 /*
39 * mowgli_dictionary_create(mowgli_dictionary_comparator_func_t compare_cb)
40 *
41 * Dictionary object factory.
42 *
43 * Inputs:
44 * - function to use for comparing two entries in the dtree
45 *
46 * Outputs:
47 * - on success, a new dictionary object.
48 *
49 * Side Effects:
50 * - if services runs out of memory and cannot allocate the object,
51 * the program will abort.
52 */
53 mowgli_dictionary_t *
54 mowgli_dictionary_create(mowgli_dictionary_comparator_func_t compare_cb)
55 {
56 return mowgli_dictionary_create_named(NULL, compare_cb);
57 }
58
59 /*
60 * mowgli_dictionary_create_named(const char *name,
61 * mowgli_dictionary_comparator_func_t compare_cb)
62 *
63 * Dictionary object factory.
64 *
65 * Inputs:
66 * - dictionary name
67 * - function to use for comparing two entries in the dtree
68 *
69 * Outputs:
70 * - on success, a new dictionary object.
71 *
72 * Side Effects:
73 * - if services runs out of memory and cannot allocate the object,
74 * the program will abort.
75 */
76 mowgli_dictionary_t *
77 mowgli_dictionary_create_named(const char *name, mowgli_dictionary_comparator_func_t compare_cb)
78 {
79 mowgli_dictionary_t *dtree = mowgli_alloc(sizeof *dtree);
80
81 dtree->compare_cb = compare_cb;
82
83 if (name && *name)
84 dtree->id = mowgli_strdup(name);
85
86 if (!elem_heap)
87 elem_heap = mowgli_heap_create(sizeof(mowgli_dictionary_elem_t), 1024, BH_NOW);
88
89 return dtree;
90 }
91
92 /*
93 * mowgli_dictionary_set_comparator_func(mowgli_dictionary_t *dict,
94 * mowgli_dictionary_comparator_func_t compare_cb)
95 *
96 * Resets the comparator function used by the dictionary code for
97 * updating the DTree structure.
98 *
99 * Inputs:
100 * - dictionary object
101 * - new comparator function (passed as functor)
102 *
103 * Outputs:
104 * - nothing
105 *
106 * Side Effects:
107 * - the dictionary comparator function is reset.
108 */
109 void
110 mowgli_dictionary_set_comparator_func(mowgli_dictionary_t *dict, mowgli_dictionary_comparator_func_t compare_cb)
111 {
112 return_if_fail(dict != NULL);
113 return_if_fail(compare_cb != NULL);
114
115 dict->compare_cb = compare_cb;
116 }
117
118 /*
119 * mowgli_dictionary_get_comparator_func(mowgli_dictionary_t *dict)
120 *
121 * Returns the current comparator function used by the dictionary.
122 *
123 * Inputs:
124 * - dictionary object
125 *
126 * Outputs:
127 * - comparator function (returned as functor)
128 *
129 * Side Effects:
130 * - none
131 */
132 mowgli_dictionary_comparator_func_t
133 mowgli_dictionary_get_comparator_func(mowgli_dictionary_t *dict)
134 {
135 return_val_if_fail(dict != NULL, NULL);
136
137 return dict->compare_cb;
138 }
139
140 /*
141 * mowgli_dictionary_get_linear_index(mowgli_dictionary_t *dict,
142 * const void *key)
143 *
144 * Gets a linear index number for key.
145 *
146 * Inputs:
147 * - dictionary tree object
148 * - pointer to data
149 *
150 * Outputs:
151 * - position, from zero.
152 *
153 * Side Effects:
154 * - rebuilds the linear index if the tree is marked as dirty.
155 */
156 int
157 mowgli_dictionary_get_linear_index(mowgli_dictionary_t *dict, const void *key)
158 {
159 mowgli_dictionary_elem_t *elem;
160
161 return_val_if_fail(dict != NULL, 0);
162 return_val_if_fail(key != NULL, 0);
163
164 elem = mowgli_dictionary_find(dict, key);
165
166 if (elem == NULL)
167 return -1;
168
169 if (!dict->dirty)
170 {
171 return elem->position;
172 }
173 else
174 {
175 mowgli_dictionary_elem_t *delem;
176 int i;
177
178 for (delem = dict->head, i = 0; delem != NULL; delem = delem->next, i++)
179 delem->position = i;
180
181 dict->dirty = false;
182 }
183
184 return elem->position;
185 }
186
187 /*
188 * mowgli_dictionary_retune(mowgli_dictionary_t *dict, const void *key)
189 *
190 * Retunes the tree, self-optimizing for the element which belongs to key.
191 *
192 * Tuning the tree structure is a very complex operation. Unlike
193 * 2-3-4 trees and BTree/BTree+ structures, this structure is a
194 * constantly evolving algorithm.
195 *
196 * Instead of maintaining a balanced tree, we constantly adapt the
197 * tree by nominating a new root nearby the most recently looked up
198 * or added data. We are constantly retuning ourselves instead of
199 * doing massive O(n) rebalance operations as seen in BTrees,
200 * and the level of data stored in a tree is dynamic, instead of being
201 * held to a restricted design like other trees.
202 *
203 * Moreover, we are different than a radix/patricia tree, because we
204 * don't statically allocate positions. Radix trees have the advantage
205 * of not requiring tuning or balancing operations while having the
206 * disadvantage of requiring a large amount of memory to store
207 * large trees. Our efficiency as far as speed goes is not as
208 * fast as a radix tree; but is close to it.
209 *
210 * The retuning algorithm uses the comparison callback that is
211 * passed in the initialization of the tree container. If the
212 * comparator returns a value which is less than zero, we push the
213 * losing node out of the way, causing it to later be reparented
214 * with another node. The winning child of this comparison is always
215 * the right-most node.
216 *
217 * Once we have reached the key which has been targeted, or have reached
218 * a deadend, we nominate the nearest node as the new root of the tree.
219 * If an exact match has been found, the new root becomes the node which
220 * represents key.
221 *
222 * This results in a tree which can self-optimize for both critical
223 * conditions: nodes which are distant and similar and trees which
224 * have ordered lookups.
225 *
226 * Inputs:
227 * - node to begin search from
228 *
229 * Outputs:
230 * - none
231 *
232 * Side Effects:
233 * - a new root node is nominated.
234 */
235 static void
236 mowgli_dictionary_retune(mowgli_dictionary_t *dict, const void *key)
237 {
238 mowgli_dictionary_elem_t n, *tn, *left, *right, *node;
239 ptrdiff_t ret;
240
241 return_if_fail(dict != NULL);
242
243 if (dict->root == NULL)
244 return;
245
246 /*
247 * we initialize n with known values, since it's on stack
248 * memory. otherwise the dict would become corrupted.
249 *
250 * n is used for temporary storage while the tree is retuned.
251 * -nenolod
252 */
253 n.left = n.right = NULL;
254 left = right = &n;
255
256 /* this for(;;) loop is the main workhorse of the rebalancing */
257 for (node = dict->root;;)
258 {
259 if ((ret = dict->compare_cb(key, node->key)) == 0)
260 break;
261
262 if (ret < 0)
263 {
264 if (node->left == NULL)
265 break;
266
267 if ((ret = dict->compare_cb(key, node->left->key)) < 0)
268 {
269 tn = node->left;
270 node->left = tn->right;
271 tn->right = node;
272 node = tn;
273
274 if (node->left == NULL)
275 break;
276 }
277
278 right->left = node;
279 right = node;
280 node = node->left;
281 }
282 else
283 {
284 if (node->right == NULL)
285 break;
286
287 if ((ret = dict->compare_cb(key, node->right->key)) > 0)
288 {
289 tn = node->right;
290 node->right = tn->left;
291 tn->left = node;
292 node = tn;
293
294 if (node->right == NULL)
295 break;
296 }
297
298 left->right = node;
299 left = node;
300 node = node->right;
301 }
302 }
303
304 left->right = node->left;
305 right->left = node->right;
306
307 node->left = n.right;
308 node->right = n.left;
309
310 dict->root = node;
311 }
312
313 /*
314 * mowgli_dictionary_link(mowgli_dictionary_t *dict,
315 * mowgli_dictionary_elem_t *delem)
316 *
317 * Links a dictionary tree element to the dictionary.
318 *
319 * When we add new nodes to the tree, it becomes the
320 * next nominated root. This is perhaps not a wise
321 * optimization because of automatic retuning, but
322 * it keeps the code simple.
323 *
324 * Inputs:
325 * - dictionary tree
326 * - dictionary tree element
327 *
328 * Outputs:
329 * - nothing
330 *
331 * Side Effects:
332 * - a node is linked to the dictionary tree
333 */
334 static void
335 mowgli_dictionary_link(mowgli_dictionary_t *dict, mowgli_dictionary_elem_t *delem)
336 {
337 return_if_fail(dict != NULL);
338 return_if_fail(delem != NULL);
339
340 dict->dirty = true;
341
342 dict->count++;
343
344 if (dict->root == NULL)
345 {
346 delem->left = delem->right = NULL;
347 delem->next = delem->prev = NULL;
348 dict->head = dict->tail = dict->root = delem;
349 }
350 else
351 {
352 int ret;
353
354 mowgli_dictionary_retune(dict, delem->key);
355
356 if ((ret = dict->compare_cb(delem->key, dict->root->key)) < 0)
357 {
358 delem->left = dict->root->left;
359 delem->right = dict->root;
360 dict->root->left = NULL;
361
362 if (dict->root->prev)
363 dict->root->prev->next = delem;
364 else
365 dict->head = delem;
366
367 delem->prev = dict->root->prev;
368 delem->next = dict->root;
369 dict->root->prev = delem;
370 dict->root = delem;
371 }
372 else if (ret > 0)
373 {
374 delem->right = dict->root->right;
375 delem->left = dict->root;
376 dict->root->right = NULL;
377
378 if (dict->root->next)
379 dict->root->next->prev = delem;
380 else
381 dict->tail = delem;
382
383 delem->next = dict->root->next;
384 delem->prev = dict->root;
385 dict->root->next = delem;
386 dict->root = delem;
387 }
388 else
389 {
390 dict->root->key = delem->key;
391 dict->root->data = delem->data;
392 dict->count--;
393
394 mowgli_heap_free(elem_heap, delem);
395 }
396 }
397 }
398
399 /*
400 * mowgli_dictionary_unlink_root(mowgli_dictionary_t *dict)
401 *
402 * Unlinks the root dictionary tree element from the dictionary.
403 *
404 * Inputs:
405 * - dictionary tree
406 *
407 * Outputs:
408 * - nothing
409 *
410 * Side Effects:
411 * - the root node is unlinked from the dictionary tree
412 */
413 static void
414 mowgli_dictionary_unlink_root(mowgli_dictionary_t *dict)
415 {
416 mowgli_dictionary_elem_t *delem, *nextnode, *parentofnext;
417
418 dict->dirty = true;
419
420 delem = dict->root;
421
422 if (delem == NULL)
423 return;
424
425 if (dict->root->left == NULL)
426 {
427 dict->root = dict->root->right;
428 }
429 else if (dict->root->right == NULL)
430 {
431 dict->root = dict->root->left;
432 }
433 else
434 {
435 /* Make the node with the next highest key the new root.
436 * This node has a NULL left pointer. */
437 nextnode = delem->next;
438 soft_assert(nextnode->left == NULL);
439
440 if (nextnode == delem->right)
441 {
442 dict->root = nextnode;
443 dict->root->left = delem->left;
444 }
445 else
446 {
447 parentofnext = delem->right;
448
449 while (parentofnext->left != NULL && parentofnext->left != nextnode)
450 {
451 parentofnext = parentofnext->left;
452 }
453
454 soft_assert(parentofnext->left == nextnode);
455 parentofnext->left = nextnode->right;
456 dict->root = nextnode;
457 dict->root->left = delem->left;
458 dict->root->right = delem->right;
459 }
460 }
461
462 /* linked list */
463 if (delem->prev != NULL)
464 delem->prev->next = delem->next;
465
466 if (dict->head == delem)
467 dict->head = delem->next;
468
469 if (delem->next)
470 delem->next->prev = delem->prev;
471
472 if (dict->tail == delem)
473 dict->tail = delem->prev;
474
475 dict->count--;
476 }
477
478 /*
479 * mowgli_dictionary_destroy(mowgli_dictionary_t *dtree,
480 * void (*destroy_cb)(dictionary_elem_t *delem, void *privdata),
481 * void *privdata);
482 *
483 * Recursively destroys all nodes in a dictionary tree.
484 *
485 * Inputs:
486 * - dictionary tree object
487 * - optional iteration callback
488 * - optional opaque/private data to pass to callback
489 *
490 * Outputs:
491 * - nothing
492 *
493 * Side Effects:
494 * - on success, a dtree and optionally it's children are destroyed.
495 *
496 * Notes:
497 * - if this is called without a callback, the objects bound to the
498 * DTree will not be destroyed.
499 */
500 void
501 mowgli_dictionary_destroy(mowgli_dictionary_t *dtree, void (*destroy_cb)(mowgli_dictionary_elem_t *delem, void *privdata), void *privdata)
502 {
503 mowgli_dictionary_elem_t *n, *tn;
504
505 return_if_fail(dtree != NULL);
506
507 MOWGLI_LIST_FOREACH_SAFE(n, tn, dtree->head)
508 {
509 if (destroy_cb != NULL)
510 (*destroy_cb)(n, privdata);
511
512 mowgli_heap_free(elem_heap, n);
513 }
514
515 mowgli_free(dtree);
516 }
517
518 /*
519 * mowgli_dictionary_foreach(mowgli_dictionary_t *dtree,
520 * void (*destroy_cb)(dictionary_elem_t *delem, void *privdata),
521 * void *privdata);
522 *
523 * Iterates over all entries in a DTree.
524 *
525 * Inputs:
526 * - dictionary tree object
527 * - optional iteration callback
528 * - optional opaque/private data to pass to callback
529 *
530 * Outputs:
531 * - nothing
532 *
533 * Side Effects:
534 * - on success, a dtree is iterated
535 */
536 void
537 mowgli_dictionary_foreach(mowgli_dictionary_t *dtree, int (*foreach_cb)(mowgli_dictionary_elem_t *delem, void *privdata), void *privdata)
538 {
539 mowgli_dictionary_elem_t *n, *tn;
540
541 return_if_fail(dtree != NULL);
542
543 MOWGLI_LIST_FOREACH_SAFE(n, tn, dtree->head)
544 {
545 /* delem_t is a subclass of node_t. */
546 mowgli_dictionary_elem_t *delem = (mowgli_dictionary_elem_t *) n;
547
548 if (foreach_cb != NULL)
549 (*foreach_cb)(delem, privdata);
550 }
551 }
552
553 /*
554 * mowgli_dictionary_search(mowgli_dictionary_t *dtree,
555 * void (*destroy_cb)(mowgli_dictionary_elem_t *delem, void *privdata),
556 * void *privdata);
557 *
558 * Searches all entries in a DTree using a custom callback.
559 *
560 * Inputs:
561 * - dictionary tree object
562 * - optional iteration callback
563 * - optional opaque/private data to pass to callback
564 *
565 * Outputs:
566 * - on success, the requested object
567 * - on failure, NULL.
568 *
569 * Side Effects:
570 * - a dtree is iterated until the requested conditions are met
571 */
572 void *
573 mowgli_dictionary_search(mowgli_dictionary_t *dtree, void *(*foreach_cb)(mowgli_dictionary_elem_t * delem, void *privdata), void *privdata)
574 {
575 mowgli_dictionary_elem_t *n, *tn;
576 void *ret = NULL;
577
578 return_val_if_fail(dtree != NULL, NULL);
579
580 MOWGLI_LIST_FOREACH_SAFE(n, tn, dtree->head)
581 {
582 /* delem_t is a subclass of node_t. */
583 mowgli_dictionary_elem_t *delem = (mowgli_dictionary_elem_t *) n;
584
585 if (foreach_cb != NULL)
586 ret = (*foreach_cb)(delem, privdata);
587
588 if (ret)
589 break;
590 }
591
592 return ret;
593 }
594
595 /*
596 * mowgli_dictionary_foreach_start(mowgli_dictionary_t *dtree,
597 * mowgli_dictionary_iteration_state_t *state);
598 *
599 * Initializes a static DTree iterator.
600 *
601 * Inputs:
602 * - dictionary tree object
603 * - static DTree iterator
604 *
605 * Outputs:
606 * - nothing
607 *
608 * Side Effects:
609 * - the static iterator, &state, is initialized.
610 */
611 void
612 mowgli_dictionary_foreach_start(mowgli_dictionary_t *dtree, mowgli_dictionary_iteration_state_t *state)
613 {
614 return_if_fail(dtree != NULL);
615 return_if_fail(state != NULL);
616
617 state->cur = NULL;
618 state->next = NULL;
619
620 /* find first item */
621 state->cur = dtree->head;
622
623 if (state->cur == NULL)
624 return;
625
626 /* make state->cur point to first item and state->next point to
627 * second item */
628 state->next = state->cur;
629 mowgli_dictionary_foreach_next(dtree, state);
630 }
631
632 /*
633 * mowgli_dictionary_foreach_cur(mowgli_dictionary_t *dtree,
634 * mowgli_dictionary_iteration_state_t *state);
635 *
636 * Returns the data from the current node being iterated by the
637 * static iterator.
638 *
639 * Inputs:
640 * - dictionary tree object
641 * - static DTree iterator
642 *
643 * Outputs:
644 * - reference to data in the current dtree node being iterated
645 *
646 * Side Effects:
647 * - none
648 */
649 void *
650 mowgli_dictionary_foreach_cur(mowgli_dictionary_t *dtree, mowgli_dictionary_iteration_state_t *state)
651 {
652 return_val_if_fail(dtree != NULL, NULL);
653 return_val_if_fail(state != NULL, NULL);
654
655 return state->cur != NULL ? state->cur->data : NULL;
656 }
657
658 /*
659 * mowgli_dictionary_foreach_next(mowgli_dictionary_t *dtree,
660 * mowgli_dictionary_iteration_state_t *state);
661 *
662 * Advances a static DTree iterator.
663 *
664 * Inputs:
665 * - dictionary tree object
666 * - static DTree iterator
667 *
668 * Outputs:
669 * - nothing
670 *
671 * Side Effects:
672 * - the static iterator, &state, is advanced to a new DTree node.
673 */
674 void
675 mowgli_dictionary_foreach_next(mowgli_dictionary_t *dtree, mowgli_dictionary_iteration_state_t *state)
676 {
677 return_if_fail(dtree != NULL);
678 return_if_fail(state != NULL);
679
680 if (state->cur == NULL)
681 {
682 mowgli_log("mowgli_dictionary_foreach_next(): called again after iteration finished on dtree<%p>", (void *) dtree);
683 return;
684 }
685
686 state->cur = state->next;
687
688 if (state->next == NULL)
689 return;
690
691 state->next = state->next->next;
692 }
693
694 /*
695 * mowgli_dictionary_find(mowgli_dictionary_t *dtree, const void *key)
696 *
697 * Looks up a DTree node by name.
698 *
699 * Inputs:
700 * - dictionary tree object
701 * - name of node to lookup
702 *
703 * Outputs:
704 * - on success, the dtree node requested
705 * - on failure, NULL
706 *
707 * Side Effects:
708 * - none
709 */
710 mowgli_dictionary_elem_t *
711 mowgli_dictionary_find(mowgli_dictionary_t *dict, const void *key)
712 {
713 return_val_if_fail(dict != NULL, NULL);
714 return_val_if_fail(key != NULL, NULL);
715
716 /* retune for key, key will be the tree's root if it's available */
717 mowgli_dictionary_retune(dict, key);
718
719 if (dict->root && !dict->compare_cb(key, dict->root->key))
720 return dict->root;
721
722 return NULL;
723 }
724
725 /*
726 * mowgli_dictionary_add(mowgli_dictionary_t *dtree, const void *key, void *data)
727 *
728 * Creates a new DTree node and binds data to it.
729 *
730 * Inputs:
731 * - dictionary tree object
732 * - name for new DTree node
733 * - data to bind to the new DTree node
734 *
735 * Outputs:
736 * - on success, a new DTree node
737 * - on failure, NULL
738 *
739 * Side Effects:
740 * - data is inserted into the DTree.
741 */
742 mowgli_dictionary_elem_t *
743 mowgli_dictionary_add(mowgli_dictionary_t *dict, const void *key, void *data)
744 {
745 mowgli_dictionary_elem_t *delem;
746
747 return_val_if_fail(dict != NULL, NULL);
748 return_val_if_fail(key != NULL, NULL);
749 return_val_if_fail(data != NULL, NULL);
750 return_val_if_fail(mowgli_dictionary_find(dict, key) == NULL, NULL);
751
752 delem = mowgli_heap_alloc(elem_heap);
753 delem->key = key;
754 delem->data = data;
755
756 mowgli_dictionary_link(dict, delem);
757
758 return delem;
759 }
760
761 /*
762 * mowgli_dictionary_delete(mowgli_dictionary_t *dtree, const void *key)
763 *
764 * Deletes data from a dictionary tree.
765 *
766 * Inputs:
767 * - dictionary tree object
768 * - name of DTree node to delete
769 *
770 * Outputs:
771 * - on success, the remaining data that needs to be mowgli_freed
772 * - on failure, NULL
773 *
774 * Side Effects:
775 * - data is removed from the DTree.
776 *
777 * Notes:
778 * - the returned data needs to be mowgli_freed/released manually!
779 */
780 void *
781 mowgli_dictionary_delete(mowgli_dictionary_t *dtree, const void *key)
782 {
783 mowgli_dictionary_elem_t *delem = mowgli_dictionary_find(dtree, key);
784 void *data;
785
786 if (delem == NULL)
787 return NULL;
788
789 data = delem->data;
790
791 mowgli_dictionary_unlink_root(dtree);
792 mowgli_heap_free(elem_heap, delem);
793
794 return data;
795 }
796
797 /*
798 * mowgli_dictionary_retrieve(mowgli_dictionary_t *dtree, const void *key)
799 *
800 * Retrieves data from a dictionary.
801 *
802 * Inputs:
803 * - dictionary tree object
804 * - name of node to lookup
805 *
806 * Outputs:
807 * - on success, the data bound to the DTree node.
808 * - on failure, NULL
809 *
810 * Side Effects:
811 * - none
812 */
813 void *
814 mowgli_dictionary_retrieve(mowgli_dictionary_t *dtree, const void *key)
815 {
816 mowgli_dictionary_elem_t *delem = mowgli_dictionary_find(dtree, key);
817
818 if (delem != NULL)
819 return delem->data;
820
821 return NULL;
822 }
823
824 /*
825 * mowgli_dictionary_size(mowgli_dictionary_t *dict)
826 *
827 * Returns the size of a dictionary.
828 *
829 * Inputs:
830 * - dictionary tree object
831 *
832 * Outputs:
833 * - size of dictionary
834 *
835 * Side Effects:
836 * - none
837 */
838 unsigned int
839 mowgli_dictionary_size(mowgli_dictionary_t *dict)
840 {
841 return_val_if_fail(dict != NULL, 0);
842
843 return dict->count;
844 }
845
846 /* returns the sum of the depths of the subtree rooted in delem at depth depth */
847 static unsigned int
848 stats_recurse(mowgli_dictionary_elem_t *delem, unsigned int depth, unsigned int *pmaxdepth)
849 {
850 unsigned int result;
851
852 if (depth > *pmaxdepth)
853 *pmaxdepth = depth;
854
855 result = depth;
856
857 if (delem->left)
858 result += stats_recurse(delem->left, depth + 1, pmaxdepth);
859
860 if (delem->right)
861 result += stats_recurse(delem->right, depth + 1, pmaxdepth);
862
863 return result;
864 }
865
866 /*
867 * mowgli_dictionary_stats(mowgli_dictionary_t *dict, void (*cb)(const char *line, void *privdata), void *privdata)
868 *
869 * Returns the size of a dictionary.
870 *
871 * Inputs:
872 * - dictionary tree object
873 * - callback
874 * - data for callback
875 *
876 * Outputs:
877 * - none
878 *
879 * Side Effects:
880 * - callback called with stats text
881 */
882 void
883 mowgli_dictionary_stats(mowgli_dictionary_t *dict, void (*cb)(const char *line, void *privdata), void *privdata)
884 {
885 char str[256];
886 unsigned int sum, maxdepth;
887
888 return_if_fail(dict != NULL);
889
890 if (dict->id != NULL)
891 snprintf(str, sizeof str, "Dictionary stats for %s (%u)",
892 dict->id, dict->count);
893 else
894 snprintf(str, sizeof str, "Dictionary stats for <%p> (%u)",
895 (void *) dict, dict->count);
896
897 cb(str, privdata);
898 maxdepth = 0;
899
900 if (dict->root != NULL)
901 {
902 sum = stats_recurse(dict->root, 0, &maxdepth);
903 snprintf(str, sizeof str, "Depth sum %u Avg depth %u Max depth %u", sum, sum / dict->count, maxdepth);
904 }
905 else
906 {
907 snprintf(str, sizeof str, "Depth sum 0 Avg depth 0 Max depth 0");
908 }
909
910 cb(str, privdata);
911 return;
912 }