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. */
12 #include "gras_private.h"
13 #include "dict_private.h" /* prototypes of this module */
15 GRAS_LOG_EXTERNAL_CATEGORY(dict);
16 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dict_elm,dict,"Dictionaries internals");
18 GRAS_LOG_NEW_SUBCATEGORY(dict_add,dict,"Dictionaries internals: elements addition");
19 GRAS_LOG_NEW_SUBCATEGORY(dict_search,dict,"Dictionaries internals: searching");
20 GRAS_LOG_NEW_SUBCATEGORY(dict_remove,dict,"Dictionaries internals: elements removal");
21 GRAS_LOG_NEW_SUBCATEGORY(dict_collapse,dict,"Dictionaries internals: post-removal cleanup");
22 GRAS_LOG_NEW_SUBCATEGORY(dict_multi,dict,"Dictionaries internals: dictionaries of dictionaries");
24 /*####[ Private prototypes ]#################################################*/
26 static _GRAS_INLINE void _gras_dictelm_alloc(char *key,
30 void_f_pvoid_t *free_ctn,
31 /*OUT*/gras_dictelm_t **where);
32 static void _dictelm_wrapper_free(void*);
34 static _GRAS_INLINE void _str_prefix_lgr(const char *key1,
42 static void _gras_dictelm_dump_rec(gras_dictelm_t *head,
44 void_f_pvoid_t *output);
48 static void _gras_dictelm_set_rec(gras_dictelm_t *head,
53 void_f_pvoid_t *free_ctn);
54 static gras_error_t _gras_dictelm_get_rec(gras_dictelm_t *head,
58 /* OUT */void **data);
59 static gras_error_t _gras_dictelm_remove_rec(gras_dictelm_t *head,
66 _collapse_if_need(gras_dictelm_t *p_head,
74 gras_memdup(const void * const ptr,
75 const size_t length) {
76 void * new_ptr = NULL;
78 new_ptr = gras_malloc(length);
79 memcpy(new_ptr, ptr, length);
85 * _gras_nibble_to_char:
87 * Change any byte to a printable char
92 _gras_nibble_to_char(unsigned char c) {
94 return c>9 ? c-10+'a' : c + '0';
98 * _gras_bytes_to_string:
100 * Change any byte array to a printable string
101 * The length of string_container should at least be data_len*2+1
105 _gras_bytes_to_string(char * const ptr,
107 char * const string_container) {
108 unsigned char *src = (unsigned char *)ptr;
109 char *dst = string_container;
112 *dst++ = _gras_nibble_to_char(*src & 0x0f );
113 *dst++ = _gras_nibble_to_char(*src++ & 0xf0 >> 4);
124 * _gras_dictelm_alloc:
126 * Alloc a dict element with no child.
130 _gras_dictelm_alloc(char *key,
134 void_f_pvoid_t *free_ctn,
135 /*OUT*/gras_dictelm_t **pp_elm) {
136 gras_error_t errcode = no_error;
137 gras_dictelm_t *p_elm = NULL;
139 p_elm = gras_new(gras_dictelm_t,1);
142 p_elm->key_len = key_len;
143 p_elm->offset = offset;
144 p_elm->content = data;
145 p_elm->free_ctn = free_ctn;
147 gras_dynar_new(&(p_elm->sub), sizeof(gras_dictelm_t*), _dictelm_wrapper_free);
156 * @pp_elm: the dict elem to be freed
158 * Frees a dictionnary element with all its childs.
161 gras_dictelm_free(gras_dictelm_t **pp_elm) {
163 gras_dictelm_t *p_elm = *pp_elm;
165 gras_dynar_free(p_elm->sub);
168 gras_free(p_elm->key);
171 if (p_elm->free_ctn && p_elm->content) {
172 p_elm->free_ctn(p_elm->content);
175 memset(p_elm, 0, sizeof (*p_elm));
183 * _dictelm_wrapper_free:
185 * a wrapper to free dictelm with the right prototype to be usable within dynar
189 _dictelm_wrapper_free(void *pp_elm) {
190 DEBUG3("Free dictelm '%.*s' %p",
191 (*(gras_dictelm_t**)pp_elm)->key_len, (*(gras_dictelm_t**)pp_elm)->key,
193 gras_dictelm_free((gras_dictelm_t**)pp_elm);
196 /*####[ utility functions ]##################################################*/
201 * Returns the length of the common prefix of @str1 and @str2.
202 * Do make sure the strings are not null
206 _str_prefix_lgr(const char *key1,
212 const int old_offset = *p_offset;
218 /*CDEBUG5(dict_search, "%s: [%.*s] <=> [%.*s]", __FUNCTION__,
219 key1,key_len1,key2,key_len2);*/
221 if (o < key_len1 && o < key_len2) {
223 while (key1[o] == key2[o]) {
226 if (!(o < key_len1 && o < key_len2))
234 if (o != old_offset) {
244 } else if (o >= key_len2) {
257 * _dictelm_child_cmp:
259 * Compares two dictelm keys and return their matching (using the same
260 * convention than @_gras_dict_child_search() )
264 _dict_child_cmp(gras_dictelm_t *p_dict,
271 gras_dictelm_t *p_child = NULL;
276 gras_dynar_get(p_dict->sub, pos, &p_child);
278 /* Compute the length of the prefix
279 and if the searched key is before or after cur */
280 _str_prefix_lgr(p_child->key, p_child->key_len,
285 if (m) /* found, get out */
288 if (o < p_child->key_len && (o >= key_len || key[o] < p_child->key[o])) {
294 CDEBUG6(dict_search, "Cmp '%.*s' and '%.*s' (offset=%d) => %d",
295 p_child->key_len - *p_offset, p_child->key + *p_offset,
296 key_len - *p_offset, key + *p_offset,
306 * _gras_dict_child_search:
308 * Search where would be inserted @key between the childs of @p_elm.
310 * Returns position of the child having a common prefix with this key
311 * If *match==0, no child have a common prefix
312 * *pos is where to add the key
313 * If *match==1, A child (located at *pos) have exactly this key
314 * If *match==2, A child (located at *pos) constitutes a prefix of the key
315 * the recursion have to go on that guy
316 * *prefix = the size of the key eaten at this level
317 * If *match==3 The key is a prefix of the child at *pos
318 * If *match==4, A child (loc. at *pos) share a common prefix with this key
319 * *prefix = size of the prefix.
320 * If searching, that's a mismatch.
321 * If inserting, you have to break the child and create an
322 * internal node having {child, key} as childs
323 * offset is used in input and output. In input, that's the length of the key
324 * handled by previous levels of recursion. In output, that the one counting
329 _gras_dictelm_child_search(gras_dictelm_t *p_elm,
342 CDEBUG5(dict_search, "search child [%.*s] under [%.*s] (len=%lu)",
344 p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
345 (p_elm&&p_elm->sub)?gras_dynar_length(p_elm->sub):0);
348 len = gras_dynar_length(p_elm->sub);
350 for (p = 0; p < len; p++) {
353 _dict_child_cmp(p_elm, p, key, key_len, &o, &m, &cmp);
365 CDEBUG5(dict_search, "search [%.*s] in [%.*s] => %s",
367 p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
368 ( m == 0 ? "no child have a common prefix" :
369 ( m == 1 ? "selected child have exactly this key" :
370 ( m == 2 ? "selected child constitutes a prefix" :
371 ( m == 3 ? "key is a prefix of selected child" :
372 (m == 4 ? "selected child share a prefix" :
373 "internal error")))))
378 * _gras_dictelm_change_value:
380 * Change the value of the dictelm, making sure to free the old one, if any.
384 _gras_dictelm_change_value(gras_dictelm_t *p_elm,
386 void_f_pvoid_t *free_ctn) {
388 if (p_elm->content && p_elm->free_ctn) {
389 p_elm->free_ctn(p_elm->content);
392 p_elm->free_ctn = free_ctn;
393 p_elm->content = data;
397 * _gras_dictelm_set_rec:
399 * @head: the head of the dict
400 * @key: the key to set the new data
401 * @offset: offset on key.
402 * @data: the data to add in the dict
403 * @Returns: a gras_error
405 * set the @data in the structure under the @key. The @key is destroyed
406 * in the process. Think to strdup it before.
408 * This is a helper function to gras_dict_set which locks the struct and
409 * strdup the key before action.
412 _gras_dictelm_set_rec(gras_dictelm_t *p_head,
417 void_f_pvoid_t *free_ctn) {
420 const int old_offset = offset;
422 CDEBUG6(dict_add, "--> Insert '%.*s' after '%.*s' (offset=%d) in tree %p",
424 ((p_head && p_head->key) ? p_head->key_len : 6),
425 ((p_head && p_head->key) ? p_head->key : "(head)"),
426 offset, (void*)p_head);
428 /*** The trivial cases first ***/
430 /* there is no key (we did enough recursion), change the value of head */
431 if (offset >= key_len) {
433 CDEBUG0(dict_add, "--> Change the value of head");
435 _gras_dictelm_change_value(p_head, data, free_ctn);
436 gras_free(key); /* Keep the key used in the tree */
441 /*** Search where to add this child, and how ***/
442 _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
444 CDEBUG3(dict_add, "child_search => pos=%d, offset=%d, match=%d",
449 case 0: /* no child have a common prefix */
451 gras_dictelm_t *p_child = NULL;
453 _gras_dictelm_alloc(key, key_len, offset, data, free_ctn, &p_child);
454 CDEBUG1(dict_add, "-> Add a child %p", (void*)p_child);
455 gras_dynar_insert_at(p_head->sub, pos, &p_child);
460 case 1: /* A child have exactly this key => change its value*/
462 gras_dictelm_t *p_child = NULL;
464 gras_dynar_get(p_head->sub, pos, &p_child);
465 CDEBUG1(dict_add, "-> Change the value of the child %p", (void*)p_child);
466 _gras_dictelm_change_value(p_child, data, free_ctn);
473 case 2: /* A child constitutes a prefix of the key => recurse */
475 gras_dictelm_t *p_child = NULL;
477 gras_dynar_get(p_head->sub, pos, &p_child);
478 CDEBUG2(dict_add,"-> Recurse on %p (offset=%d)", (void*)p_child, offset);
480 _gras_dictelm_set_rec(p_child, key, key_len,
481 offset, data, free_ctn);
485 case 3: /* The key is a prefix of the child => child becomes child of p_new */
487 gras_dictelm_t *p_new = NULL;
488 gras_dictelm_t *p_child = NULL;
490 gras_dynar_get(p_head->sub, pos, &p_child);
491 _gras_dictelm_alloc(key, key_len, old_offset, data, free_ctn, &p_new);
493 CDEBUG2(dict_add, "-> The child %p become child of new dict (%p)",
494 (void*)p_child, (void*)p_new);
496 gras_dynar_push(p_new->sub, &p_child);
497 p_child->offset = offset;
498 gras_dynar_set(p_head->sub, pos, &p_new);
503 case 4: /* A child share a common prefix with this key => Common ancestor */
505 gras_dictelm_t *p_new = NULL;
506 gras_dictelm_t *p_child = NULL;
507 gras_dictelm_t *p_anc = NULL;
508 char *anc_key = NULL;
509 int anc_key_len = offset;
511 _gras_dictelm_alloc(key, key_len, offset, data, free_ctn, &p_new);
512 gras_dynar_get(p_head->sub, pos, &p_child);
514 anc_key = gras_memdup(key, anc_key_len);
516 _gras_dictelm_alloc(anc_key, anc_key_len, old_offset, NULL, NULL, &p_anc);
518 CDEBUG3(dict_add, "-> Make a common ancestor %p (%.*s)",
519 (void*)p_anc, anc_key_len, anc_key);
521 if (key[offset] < p_child->key[offset]) {
522 gras_dynar_push(p_anc->sub, &p_new);
523 gras_dynar_push(p_anc->sub, &p_child);
525 gras_dynar_push(p_anc->sub, &p_child);
526 gras_dynar_push(p_anc->sub, &p_new);
529 p_child->offset = offset;
531 gras_dynar_set(p_head->sub, pos, &p_anc);
542 * gras_dictelm_set_ext:
544 * @head: the head of the dict
545 * @key: the key to set the new data
546 * @data: the data to add in the dict
547 * @Returns: a gras_error
549 * set the @data in the structure under the @key, which can be any kind
550 * of data, as long as its length is provided in @key_len.
553 gras_dictelm_set_ext(gras_dictelm_t **pp_head,
557 void_f_pvoid_t *free_ctn) {
558 gras_dictelm_t *p_head = *pp_head;
561 key = gras_memdup(_key, key_len);
563 /* there is no head, create it */
565 gras_dictelm_t *p_child = NULL;
567 CDEBUG0(dict_add, "Create an head");
569 /* The head is priviledged by being the only one with a NULL key */
570 _gras_dictelm_alloc(NULL, 0, 0, NULL, NULL, &p_head);
572 _gras_dictelm_alloc(key, key_len, 0, data, free_ctn, &p_child);
573 gras_dynar_insert_at(p_head->sub, 0, &p_child);
580 _gras_dictelm_set_rec(p_head, key, key_len, 0, data, free_ctn);
586 * @head: the head of the dict
587 * @key: the key to set the new data
588 * @data: the data to add in the dict
589 * @Returns: a gras_error
591 * set the @data in the structure under the @key, which is a
592 * null terminated string.
595 gras_dictelm_set(gras_dictelm_t **pp_head,
598 void_f_pvoid_t *free_ctn) {
600 gras_dictelm_set_ext(pp_head, _key, 1+strlen(_key), data, free_ctn);
604 * _gras_dict_get_rec:
606 * @head: the head of the dict
607 * @key: the key to find data
608 * @offset: offset on the key
609 * @data: the data that we are looking for
610 * @Returns: gras_error
612 * Search the given @key. mismatch_error when not found.
616 _gras_dictelm_get_rec(gras_dictelm_t *p_head,
623 CDEBUG3(dict_search, "Search %.*s in %p", key_len, key, (void*)p_head);
625 /*** The trivial case first ***/
627 /* we did enough recursion, we're done */
628 if (offset >= key_len) {
629 *data = p_head->content;
638 *data = NULL; /* Make it ready to answer 'not found' in one operation */
640 /*** Search where is the good child, and how good it is ***/
641 _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
645 case 0: /* no child have a common prefix */
646 return mismatch_error;
648 case 1: /* A child have exactly this key => Got it */
650 gras_dictelm_t *p_child = NULL;
652 gras_dynar_get(p_head->sub, pos, &p_child);
653 *data = p_child->content;
658 case 2: /* A child constitutes a prefix of the key => recurse */
660 gras_dictelm_t *p_child = NULL;
662 gras_dynar_get(p_head->sub, pos, &p_child);
664 return _gras_dictelm_get_rec(p_child, key, key_len, offset, data);
667 case 3: /* The key is a prefix of the child => not found */
668 return mismatch_error;
670 case 4: /* A child share a common prefix with this key => not found */
671 return mismatch_error;
680 * gras_dictelm_get_ext:
682 * @head: the head of the dict
683 * @key: the key to find data
684 * @data: the data that we are looking for
685 * @Returns: gras_error
687 * Search the given @key. mismatch_error when not found.
690 gras_dictelm_get_ext(gras_dictelm_t *p_head,
693 /* OUT */void **data) {
694 /* there is no head, go to hell */
696 return mismatch_error;
699 return _gras_dictelm_get_rec(p_head, key, key_len, 0, data);
705 * @head: the head of the dict
706 * @key: the key to find data
707 * @data: the data that we are looking for
708 * @Returns: gras_error
710 * Search the given @key. mismatch_error when not found.
713 gras_dictelm_get(gras_dictelm_t *p_head,
715 /* OUT */void **data) {
717 return gras_dictelm_get_ext(p_head, key, 1+strlen(key), data);
720 /*----[ _gras_dict_collapse ]------------------------------------------------*/
723 _collapse_if_need(gras_dictelm_t *p_head,
726 gras_dictelm_t *p_child = NULL;
728 CDEBUG2(dict_collapse, "Collapse %d of %p... ", pos, (void*)p_head);
731 /* Remove the child if |it's key| == 0 (meaning it's dead) */
732 gras_dynar_get(p_head->sub, pos, &p_child);
734 if (offset >= p_child->key_len) {
736 gras_assert0(gras_dynar_length(p_child->sub) == 0,
737 "Found a dead child with grand childs. Internal error");
739 CDEBUG1(dict_collapse, "Remove dead child %p.... ", (void*)p_child);
740 gras_dynar_remove_at(p_head->sub, pos, &p_child);
745 CDEBUG0(dict_collapse, "Do not collapse the head, you stupid programm");
749 if (p_head->content || p_head->free_ctn ||
750 gras_dynar_length(p_head->sub) != 1) {
751 CDEBUG0(dict_collapse, "Cannot collapse");
752 return; /* cannot collapse */
755 gras_dynar_get(p_head->sub, 0, &p_child);
757 /* Get the child's key as new key */
758 CDEBUG2(dict_collapse,
759 "Do collapse with only child %.*s", p_child->key_len, p_child->key);
761 p_head->content = p_child->content;
762 p_head->free_ctn = p_child->free_ctn;
763 gras_free(p_head->key);
764 p_head->key = p_child->key;
765 p_head->key_len = p_child->key_len;
767 gras_dynar_free_container(p_head->sub) ;
769 p_head->sub = p_child->sub;
774 * _gras_dict_remove_rec:
776 * @head: the head of the dict
777 * @key: the key of the data to be removed
778 * @offset: offset on the key
779 * @Returns: gras_error_t
781 * Remove the entry associated with the given @key
784 _gras_dictelm_remove_rec(gras_dictelm_t *p_head,
788 gras_error_t errcode = no_error;
790 /* there is no key to search, we did enough recursion => kill current */
791 if (offset >= key_len) {
792 int killme = 0; /* do I need to suicide me ? */
794 if (p_head->content && p_head->free_ctn) {
795 p_head->free_ctn(p_head->content);
798 killme = !gras_dynar_length(p_head->sub);
799 p_head->content = NULL;
800 p_head->free_ctn = NULL;
801 _collapse_if_need(p_head, -1, offset);
804 DEBUG0("kill this node");
805 p_head->key_len = 0; /* killme. Cleanup done one step higher in recursion */
813 int old_offset = offset;
815 /*** Search where is the good child, and how good it is ***/
816 _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
820 case 1: /* A child have exactly this key => recurse */
821 case 2: /* A child constitutes a prefix of the key => recurse */
824 gras_dictelm_t *p_child = NULL;
826 gras_dynar_get(p_head->sub, pos, &p_child);
827 /*DEBUG5("Recurse on child %d of %p to remove %.*s (prefix=%d)",
828 pos, (void*)p_child, key+offset, key_len-offset,offset);*/
829 TRY(_gras_dictelm_remove_rec(p_child, key, key_len, offset));
831 _collapse_if_need(p_head, pos, old_offset);
836 case 0: /* no child have a common prefix */
837 case 3: /* The key is a prefix of the child => not found */
838 case 4: /* A child share a common prefix with this key => not found */
840 return mismatch_error;
851 * gras_dictelm_remove_ext:
853 * @head: the head of the dict
854 * @key: the key of the data to be removed
855 * @Returns: gras_error_t
857 * Remove the entry associated with the given @key
860 gras_dictelm_remove_ext(gras_dictelm_t *p_head,
863 /* there is no head, go to hell */
865 RAISE0(mismatch_error, "there is no head, go to hell");
868 return _gras_dictelm_remove_rec(p_head, key, key_len, 0);
872 * gras_dictelm_remove:
874 * @head: the head of the dict
875 * @key: the key of the data to be removed
876 * @Returns: gras_error_t
878 * Remove the entry associated with the given @key
881 gras_dictelm_remove(gras_dictelm_t *p_head,
883 return _gras_dictelm_remove_rec(p_head, key, 1+strlen(key),0);
886 /*----[ _gras_dict_dump_rec ]------------------------------------------------*/
887 /* private function to do the job of gras_dict_dump recursively */
888 /*---------------------------------------------------------------------------*/
891 _gras_dictelm_dump_rec(gras_dictelm_t *p_head,
893 void_f_pvoid_t *output) {
894 gras_dictelm_t *p_child = NULL;
902 printf("[%p] ", (void*)p_head);
905 key_len = p_head->key_len;
910 for (i = 0; i < offset; i++)
920 char *key_string = NULL;
922 key_string = gras_malloc(key_len*2+1);
923 _gras_bytes_to_string(key, key_len, key_string);
925 printf("%.*s|(%d)", key_len-offset, key_string + offset, offset);
927 gras_free(key_string);
934 if (p_head->content) {
937 output(p_head->content);
946 printf(" \t\t\t[ %lu child(s) ]\n", gras_dynar_length(p_head->sub));
948 gras_dynar_foreach(p_head->sub, i, p_child)
949 _gras_dictelm_dump_rec(p_child, p_child->offset, output);
956 * @head: the head of the dict
957 * @output: a function to dump each data in the tree
958 * @Returns: gras_error_t
960 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
961 * function to output the data. If NULL, data won't be displayed.
965 gras_dictelm_dump(gras_dictelm_t *p_head,
966 void_f_pvoid_t *output) {
967 _gras_dictelm_dump_rec(p_head, 0, output);
971 * gras_dictelm_print_fct:
975 * To dump multidict, this function dumps a dict
979 gras_dictelm_print_fct(void *data) {
980 printf("tree %p", (void*)data);