3 /* dict - a generic dictionnary, variation over the B-tree concept */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003 the OURAGAN project. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "dict_private.h" /* prototypes of this module */
13 GRAS_LOG_EXTERNAL_CATEGORY(dict);
14 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dict_elm,dict,"Dictionaries internals");
16 GRAS_LOG_NEW_SUBCATEGORY(dict_add,dict,"Dictionaries internals: elements addition");
17 GRAS_LOG_NEW_SUBCATEGORY(dict_search,dict,"Dictionaries internals: searching");
18 GRAS_LOG_NEW_SUBCATEGORY(dict_remove,dict,"Dictionaries internals: elements removal");
19 GRAS_LOG_NEW_SUBCATEGORY(dict_collapse,dict,"Dictionaries internals: post-removal cleanup");
20 GRAS_LOG_NEW_SUBCATEGORY(dict_multi,dict,"Dictionaries internals: dictionaries of dictionaries");
22 /*####[ Private prototypes ]#################################################*/
24 static _GRAS_INLINE void _gras_dictelm_alloc(char *key,
28 void_f_pvoid_t *free_ctn,
29 /*OUT*/gras_dictelm_t **where);
30 static void _dictelm_wrapper_free(void*);
32 static _GRAS_INLINE void _str_prefix_lgr(const char *key1,
40 static void _gras_dictelm_dump_rec(gras_dictelm_t *head,
42 void_f_pvoid_t *output);
46 static void _gras_dictelm_set_rec(gras_dictelm_t *head,
51 void_f_pvoid_t *free_ctn);
52 static gras_error_t _gras_dictelm_get_rec(gras_dictelm_t *head,
56 /* OUT */void **data);
57 static gras_error_t _gras_dictelm_remove_rec(gras_dictelm_t *head,
64 _collapse_if_need(gras_dictelm_t *p_head,
72 gras_memdup(const void * const ptr,
73 const size_t length) {
74 void * new_ptr = NULL;
76 new_ptr = gras_malloc(length);
77 memcpy(new_ptr, ptr, length);
83 * _gras_nibble_to_char:
85 * Change any byte to a printable char
90 _gras_nibble_to_char(unsigned char c) {
92 return c>9 ? c-10+'a' : c + '0';
96 * _gras_bytes_to_string:
98 * Change any byte array to a printable string
99 * The length of string_container should at least be data_len*2+1
103 _gras_bytes_to_string(char * const ptr,
105 char * const string_container) {
106 unsigned char *src = (unsigned char *)ptr;
107 char *dst = string_container;
110 *dst++ = _gras_nibble_to_char(*src & 0x0f );
111 *dst++ = _gras_nibble_to_char(*src++ & 0xf0 >> 4);
122 * _gras_dictelm_alloc:
124 * Alloc a dict element with no child.
128 _gras_dictelm_alloc(char *key,
132 void_f_pvoid_t *free_ctn,
133 /*OUT*/gras_dictelm_t **pp_elm) {
134 gras_error_t errcode = no_error;
135 gras_dictelm_t *p_elm = NULL;
137 p_elm = gras_new(gras_dictelm_t,1);
140 p_elm->key_len = key_len;
141 p_elm->offset = offset;
142 p_elm->content = data;
143 p_elm->free_ctn = free_ctn;
144 p_elm->sub = gras_dynar_new(sizeof(gras_dictelm_t*), _dictelm_wrapper_free);
153 * @pp_elm: the dict elem to be freed
155 * Frees a dictionnary element with all its childs.
158 gras_dictelm_free(gras_dictelm_t **pp_elm) {
160 gras_dictelm_t *p_elm = *pp_elm;
162 gras_dynar_free(p_elm->sub);
165 gras_free(p_elm->key);
168 if (p_elm->free_ctn && p_elm->content) {
169 p_elm->free_ctn(p_elm->content);
172 memset(p_elm, 0, sizeof (*p_elm));
180 * _dictelm_wrapper_free:
182 * a wrapper to free dictelm with the right prototype to be usable within dynar
186 _dictelm_wrapper_free(void *pp_elm) {
187 DEBUG3("Free dictelm '%.*s' %p",
188 (*(gras_dictelm_t**)pp_elm)->key_len, (*(gras_dictelm_t**)pp_elm)->key,
190 gras_dictelm_free((gras_dictelm_t**)pp_elm);
193 /*####[ utility functions ]##################################################*/
198 * Returns the length of the common prefix of @str1 and @str2.
199 * Do make sure the strings are not null
203 _str_prefix_lgr(const char *key1,
209 const int old_offset = *p_offset;
215 /*CDEBUG5(dict_search, "%s: [%.*s] <=> [%.*s]", __FUNCTION__,
216 key1,key_len1,key2,key_len2);*/
218 if (o < key_len1 && o < key_len2) {
220 while (key1[o] == key2[o]) {
223 if (!(o < key_len1 && o < key_len2))
231 if (o != old_offset) {
241 } else if (o >= key_len2) {
254 * _dictelm_child_cmp:
256 * Compares two dictelm keys and return their matching (using the same
257 * convention than @_gras_dict_child_search() )
261 _dict_child_cmp(gras_dictelm_t *p_dict,
268 gras_dictelm_t *p_child = NULL;
273 p_child = gras_dynar_get_as(p_dict->sub, pos, gras_dictelm_t*);
275 /* Compute the length of the prefix
276 and if the searched key is before or after cur */
277 _str_prefix_lgr(p_child->key, p_child->key_len,
282 if (m) /* found, get out */
285 if (o < p_child->key_len && (o >= key_len || key[o] < p_child->key[o])) {
291 CDEBUG6(dict_search, "Cmp '%.*s' and '%.*s' (offset=%d) => %d",
292 p_child->key_len - *p_offset, p_child->key + *p_offset,
293 key_len - *p_offset, key + *p_offset,
303 * _gras_dict_child_search:
305 * Search where would be inserted @key between the childs of @p_elm.
307 * Returns position of the child having a common prefix with this key
308 * If *match==0, no child have a common prefix
309 * *pos is where to add the key
310 * If *match==1, A child (located at *pos) have exactly this key
311 * If *match==2, A child (located at *pos) constitutes a prefix of the key
312 * the recursion have to go on that guy
313 * *prefix = the size of the key eaten at this level
314 * If *match==3 The key is a prefix of the child at *pos
315 * If *match==4, A child (loc. at *pos) share a common prefix with this key
316 * *prefix = size of the prefix.
317 * If searching, that's a mismatch.
318 * If inserting, you have to break the child and create an
319 * internal node having {child, key} as childs
320 * offset is used in input and output. In input, that's the length of the key
321 * handled by previous levels of recursion. In output, that the one counting
326 _gras_dictelm_child_search(gras_dictelm_t *p_elm,
339 CDEBUG5(dict_search, "search child [%.*s] under [%.*s] (len=%lu)",
341 p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
342 (p_elm&&p_elm->sub)?gras_dynar_length(p_elm->sub):0);
345 len = gras_dynar_length(p_elm->sub);
347 for (p = 0; p < len; p++) {
350 _dict_child_cmp(p_elm, p, key, key_len, &o, &m, &cmp);
362 CDEBUG5(dict_search, "search [%.*s] in [%.*s] => %s",
364 p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
365 ( m == 0 ? "no child have a common prefix" :
366 ( m == 1 ? "selected child have exactly this key" :
367 ( m == 2 ? "selected child constitutes a prefix" :
368 ( m == 3 ? "key is a prefix of selected child" :
369 (m == 4 ? "selected child share a prefix" :
370 "internal error")))))
375 * _gras_dictelm_change_value:
377 * Change the value of the dictelm, making sure to free the old one, if any.
381 _gras_dictelm_change_value(gras_dictelm_t *p_elm,
383 void_f_pvoid_t *free_ctn) {
385 if (p_elm->content && p_elm->free_ctn) {
386 p_elm->free_ctn(p_elm->content);
389 p_elm->free_ctn = free_ctn;
390 p_elm->content = data;
394 * _gras_dictelm_set_rec:
396 * @head: the head of the dict
397 * @key: the key to set the new data
398 * @offset: offset on key.
399 * @data: the data to add in the dict
400 * @Returns: a gras_error
402 * set the @data in the structure under the @key. The @key is destroyed
403 * in the process. Think to strdup it before.
405 * This is a helper function to gras_dict_set which locks the struct and
406 * strdup the key before action.
409 _gras_dictelm_set_rec(gras_dictelm_t *p_head,
414 void_f_pvoid_t *free_ctn) {
417 const int old_offset = offset;
419 CDEBUG6(dict_add, "--> Insert '%.*s' after '%.*s' (offset=%d) in tree %p",
421 ((p_head && p_head->key) ? p_head->key_len : 6),
422 ((p_head && p_head->key) ? p_head->key : "(head)"),
423 offset, (void*)p_head);
425 /*** The trivial cases first ***/
427 /* there is no key (we did enough recursion), change the value of head */
428 if (offset >= key_len) {
430 CDEBUG0(dict_add, "--> Change the value of head");
432 _gras_dictelm_change_value(p_head, data, free_ctn);
433 gras_free(key); /* Keep the key used in the tree */
438 /*** Search where to add this child, and how ***/
439 _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
441 CDEBUG3(dict_add, "child_search => pos=%d, offset=%d, match=%d",
446 case 0: /* no child have a common prefix */
448 gras_dictelm_t *p_child = NULL;
450 _gras_dictelm_alloc(key, key_len, offset, data, free_ctn, &p_child);
451 CDEBUG1(dict_add, "-> Add a child %p", (void*)p_child);
452 gras_dynar_insert_at(p_head->sub, pos, &p_child);
457 case 1: /* A child have exactly this key => change its value*/
459 gras_dictelm_t *p_child = NULL;
461 p_child = gras_dynar_get_as(p_head->sub, pos, gras_dictelm_t*);
462 CDEBUG1(dict_add, "-> Change the value of the child %p", (void*)p_child);
463 _gras_dictelm_change_value(p_child, data, free_ctn);
470 case 2: /* A child constitutes a prefix of the key => recurse */
472 gras_dictelm_t *p_child = NULL;
474 p_child=gras_dynar_get_as(p_head->sub, pos, gras_dictelm_t*);
475 CDEBUG2(dict_add,"-> Recurse on %p (offset=%d)", (void*)p_child, offset);
477 _gras_dictelm_set_rec(p_child, key, key_len,
478 offset, data, free_ctn);
482 case 3: /* The key is a prefix of the child => child becomes child of p_new */
484 gras_dictelm_t *p_new = NULL;
485 gras_dictelm_t *p_child = NULL;
487 p_child=gras_dynar_get_as(p_head->sub, pos, gras_dictelm_t*);
488 _gras_dictelm_alloc(key, key_len, old_offset, data, free_ctn, &p_new);
490 CDEBUG2(dict_add, "-> The child %p become child of new dict (%p)",
491 (void*)p_child, (void*)p_new);
493 gras_dynar_push(p_new->sub, &p_child);
494 p_child->offset = offset;
495 gras_dynar_set(p_head->sub, pos, &p_new);
500 case 4: /* A child share a common prefix with this key => Common ancestor */
502 gras_dictelm_t *p_new = NULL;
503 gras_dictelm_t *p_child = NULL;
504 gras_dictelm_t *p_anc = NULL;
505 char *anc_key = NULL;
506 int anc_key_len = offset;
508 _gras_dictelm_alloc(key, key_len, offset, data, free_ctn, &p_new);
509 p_child=gras_dynar_get_as(p_head->sub, pos, gras_dictelm_t*);
511 anc_key = gras_memdup(key, anc_key_len);
513 _gras_dictelm_alloc(anc_key, anc_key_len, old_offset, NULL, NULL, &p_anc);
515 CDEBUG3(dict_add, "-> Make a common ancestor %p (%.*s)",
516 (void*)p_anc, anc_key_len, anc_key);
518 if (key[offset] < p_child->key[offset]) {
519 gras_dynar_push(p_anc->sub, &p_new);
520 gras_dynar_push(p_anc->sub, &p_child);
522 gras_dynar_push(p_anc->sub, &p_child);
523 gras_dynar_push(p_anc->sub, &p_new);
526 p_child->offset = offset;
528 gras_dynar_set(p_head->sub, pos, &p_anc);
539 * gras_dictelm_set_ext:
541 * @head: the head of the dict
542 * @key: the key to set the new data
543 * @data: the data to add in the dict
544 * @Returns: a gras_error
546 * set the @data in the structure under the @key, which can be any kind
547 * of data, as long as its length is provided in @key_len.
550 gras_dictelm_set_ext(gras_dictelm_t **pp_head,
554 void_f_pvoid_t *free_ctn) {
555 gras_dictelm_t *p_head = *pp_head;
558 key = gras_memdup(_key, key_len);
560 /* there is no head, create it */
562 gras_dictelm_t *p_child = NULL;
564 CDEBUG0(dict_add, "Create an head");
566 /* The head is priviledged by being the only one with a NULL key */
567 _gras_dictelm_alloc(NULL, 0, 0, NULL, NULL, &p_head);
569 _gras_dictelm_alloc(key, key_len, 0, data, free_ctn, &p_child);
570 gras_dynar_insert_at(p_head->sub, 0, &p_child);
577 _gras_dictelm_set_rec(p_head, key, key_len, 0, data, free_ctn);
583 * @head: the head of the dict
584 * @key: the key to set the new data
585 * @data: the data to add in the dict
586 * @Returns: a gras_error
588 * set the @data in the structure under the @key, which is a
589 * null terminated string.
592 gras_dictelm_set(gras_dictelm_t **pp_head,
595 void_f_pvoid_t *free_ctn) {
597 gras_dictelm_set_ext(pp_head, _key, 1+strlen(_key), data, free_ctn);
601 * _gras_dict_get_rec:
603 * @head: the head of the dict
604 * @key: the key to find data
605 * @offset: offset on the key
606 * @data: the data that we are looking for
607 * @Returns: gras_error
609 * Search the given @key. mismatch_error when not found.
613 _gras_dictelm_get_rec(gras_dictelm_t *p_head,
620 CDEBUG3(dict_search, "Search %.*s in %p", key_len, key, (void*)p_head);
622 /*** The trivial case first ***/
624 /* we did enough recursion, we're done */
625 if (offset >= key_len) {
626 *data = p_head->content;
635 *data = NULL; /* Make it ready to answer 'not found' in one operation */
637 /*** Search where is the good child, and how good it is ***/
638 _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
642 case 0: /* no child have a common prefix */
643 return mismatch_error;
645 case 1: /* A child have exactly this key => Got it */
647 gras_dictelm_t *p_child = NULL;
649 p_child = gras_dynar_get_as(p_head->sub, pos, gras_dictelm_t*);
650 *data = p_child->content;
655 case 2: /* A child constitutes a prefix of the key => recurse */
657 gras_dictelm_t *p_child = NULL;
659 p_child = gras_dynar_get_as(p_head->sub, pos, gras_dictelm_t*);
661 return _gras_dictelm_get_rec(p_child, key, key_len, offset, data);
664 case 3: /* The key is a prefix of the child => not found */
665 return mismatch_error;
667 case 4: /* A child share a common prefix with this key => not found */
668 return mismatch_error;
677 * gras_dictelm_get_ext:
679 * @head: the head of the dict
680 * @key: the key to find data
681 * @data: the data that we are looking for
682 * @Returns: gras_error
684 * Search the given @key. mismatch_error when not found.
687 gras_dictelm_get_ext(gras_dictelm_t *p_head,
690 /* OUT */void **data) {
691 /* there is no head, go to hell */
693 return mismatch_error;
696 return _gras_dictelm_get_rec(p_head, key, key_len, 0, data);
702 * @head: the head of the dict
703 * @key: the key to find data
704 * @data: the data that we are looking for
705 * @Returns: gras_error
707 * Search the given @key. mismatch_error when not found.
710 gras_dictelm_get(gras_dictelm_t *p_head,
712 /* OUT */void **data) {
714 return gras_dictelm_get_ext(p_head, key, 1+strlen(key), data);
717 /*----[ _gras_dict_collapse ]------------------------------------------------*/
720 _collapse_if_need(gras_dictelm_t *p_head,
723 gras_dictelm_t *p_child = NULL;
725 CDEBUG2(dict_collapse, "Collapse %d of %p... ", pos, (void*)p_head);
728 /* Remove the child if |it's key| == 0 (meaning it's dead) */
729 p_child = gras_dynar_get_as(p_head->sub, pos, gras_dictelm_t*);
731 if (offset >= p_child->key_len) {
733 gras_assert0(gras_dynar_length(p_child->sub) == 0,
734 "Found a dead child with grand childs. Internal error");
736 CDEBUG1(dict_collapse, "Remove dead child %p.... ", (void*)p_child);
737 gras_dynar_remove_at(p_head->sub, pos, &p_child);
742 CDEBUG0(dict_collapse, "Do not collapse the head, you stupid programm");
746 if (p_head->content || p_head->free_ctn ||
747 gras_dynar_length(p_head->sub) != 1) {
748 CDEBUG0(dict_collapse, "Cannot collapse");
749 return; /* cannot collapse */
752 p_child = gras_dynar_get_as(p_head->sub, 0, gras_dictelm_t*);
754 /* Get the child's key as new key */
755 CDEBUG2(dict_collapse,
756 "Do collapse with only child %.*s", p_child->key_len, p_child->key);
758 p_head->content = p_child->content;
759 p_head->free_ctn = p_child->free_ctn;
760 gras_free(p_head->key);
761 p_head->key = p_child->key;
762 p_head->key_len = p_child->key_len;
764 gras_dynar_free_container(p_head->sub) ;
766 p_head->sub = p_child->sub;
771 * _gras_dict_remove_rec:
773 * @head: the head of the dict
774 * @key: the key of the data to be removed
775 * @offset: offset on the key
776 * @Returns: gras_error_t
778 * Remove the entry associated with the given @key
781 _gras_dictelm_remove_rec(gras_dictelm_t *p_head,
785 gras_error_t errcode = no_error;
787 /* there is no key to search, we did enough recursion => kill current */
788 if (offset >= key_len) {
789 int killme = 0; /* do I need to suicide me ? */
791 if (p_head->content && p_head->free_ctn) {
792 p_head->free_ctn(p_head->content);
795 killme = !gras_dynar_length(p_head->sub);
796 p_head->content = NULL;
797 p_head->free_ctn = NULL;
798 _collapse_if_need(p_head, -1, offset);
801 DEBUG0("kill this node");
802 p_head->key_len = 0; /* killme. Cleanup done one step higher in recursion */
810 int old_offset = offset;
812 /*** Search where is the good child, and how good it is ***/
813 _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
817 case 1: /* A child have exactly this key => recurse */
818 case 2: /* A child constitutes a prefix of the key => recurse */
821 gras_dictelm_t *p_child = NULL;
823 p_child = gras_dynar_get_as(p_head->sub, pos, gras_dictelm_t*);
824 /*DEBUG5("Recurse on child %d of %p to remove %.*s (prefix=%d)",
825 pos, (void*)p_child, key+offset, key_len-offset,offset);*/
826 TRY(_gras_dictelm_remove_rec(p_child, key, key_len, offset));
828 _collapse_if_need(p_head, pos, old_offset);
833 case 0: /* no child have a common prefix */
834 case 3: /* The key is a prefix of the child => not found */
835 case 4: /* A child share a common prefix with this key => not found */
837 return mismatch_error;
848 * gras_dictelm_remove_ext:
850 * @head: the head of the dict
851 * @key: the key of the data to be removed
852 * @Returns: gras_error_t
854 * Remove the entry associated with the given @key
857 gras_dictelm_remove_ext(gras_dictelm_t *p_head,
860 /* there is no head, go to hell */
862 RAISE0(mismatch_error, "there is no head, go to hell");
865 return _gras_dictelm_remove_rec(p_head, key, key_len, 0);
869 * gras_dictelm_remove:
871 * @head: the head of the dict
872 * @key: the key of the data to be removed
873 * @Returns: gras_error_t
875 * Remove the entry associated with the given @key
878 gras_dictelm_remove(gras_dictelm_t *p_head,
880 return _gras_dictelm_remove_rec(p_head, key, 1+strlen(key),0);
883 /*----[ _gras_dict_dump_rec ]------------------------------------------------*/
884 /* private function to do the job of gras_dict_dump recursively */
885 /*---------------------------------------------------------------------------*/
888 _gras_dictelm_dump_rec(gras_dictelm_t *p_head,
890 void_f_pvoid_t *output) {
891 gras_dictelm_t *p_child = NULL;
899 printf("[%p] ", (void*)p_head);
902 key_len = p_head->key_len;
907 for (i = 0; i < offset; i++)
917 char *key_string = NULL;
919 key_string = gras_malloc(key_len*2+1);
920 _gras_bytes_to_string(key, key_len, key_string);
922 printf("%.*s|(%d)", key_len-offset, key_string + offset, offset);
924 gras_free(key_string);
931 if (p_head->content) {
934 output(p_head->content);
943 printf(" \t\t\t[ %lu child(s) ]\n", gras_dynar_length(p_head->sub));
945 gras_dynar_foreach(p_head->sub, i, p_child)
946 _gras_dictelm_dump_rec(p_child, p_child->offset, output);
953 * @head: the head of the dict
954 * @output: a function to dump each data in the tree
955 * @Returns: gras_error_t
957 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
958 * function to output the data. If NULL, data won't be displayed.
962 gras_dictelm_dump(gras_dictelm_t *p_head,
963 void_f_pvoid_t *output) {
964 _gras_dictelm_dump_rec(p_head, 0, output);
968 * gras_dictelm_print_fct:
972 * To dump multidict, this function dumps a dict
976 gras_dictelm_print_fct(void *data) {
977 printf("tree %p", (void*)data);