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 #include <stdlib.h> /* malloc() */
16 #include <string.h> /* strlen() */
20 GRAS_LOG_EXTERNAL_CATEGORY(dict);
21 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dict_elm,dict);
23 GRAS_LOG_NEW_SUBCATEGORY(dict_add,dict);
24 GRAS_LOG_NEW_SUBCATEGORY(dict_search,dict);
25 GRAS_LOG_NEW_SUBCATEGORY(dict_remove,dict);
26 GRAS_LOG_NEW_SUBCATEGORY(dict_collapse,dict);
27 GRAS_LOG_NEW_SUBCATEGORY(dict_multi,dict);
29 /*####[ Private prototypes ]#################################################*/
31 static inline gras_error_t _gras_dictelm_alloc(char *key,
35 void_f_pvoid_t *free_ctn,
36 /*OUT*/gras_dictelm_t **where);
37 static void _dictelm_wrapper_free(void*);
39 static inline void _str_prefix_lgr(const char *key1,
47 static gras_error_t _gras_dictelm_dump_rec(gras_dictelm_t *head,
49 void_f_pvoid_t *output);
53 static gras_error_t _gras_dictelm_set_rec(gras_dictelm_t *head,
58 void_f_pvoid_t *free_ctn);
59 static gras_error_t _gras_dictelm_get_rec(gras_dictelm_t *head,
63 /* OUT */void **data);
64 static gras_error_t _gras_dictelm_remove_rec(gras_dictelm_t *head,
71 _collapse_if_need(gras_dictelm_t *p_head,
79 memdup(const void * const ptr,
80 const size_t length) {
81 void * new_ptr = NULL;
83 new_ptr = malloc(length);
86 memcpy(new_ptr, ptr, length);
93 * _gras_nibble_to_char:
95 * Change any byte to a printable char
100 _gras_nibble_to_char(unsigned char c) {
102 return c>9 ? c-10+'a' : c + '0';
106 * _gras_bytes_to_string:
108 * Change any byte array to a printable string
109 * The length of string_container should at least be data_len*2+1
113 _gras_bytes_to_string(char * const ptr,
115 char * const string_container) {
116 unsigned char *src = (unsigned char *)ptr;
117 char *dst = string_container;
120 *dst++ = _gras_nibble_to_char(*src & 0x0f );
121 *dst++ = _gras_nibble_to_char(*src++ & 0xf0 >> 4);
132 * _gras_dictelm_alloc:
134 * Alloc a dict element with no child.
139 _gras_dictelm_alloc(char *key,
143 void_f_pvoid_t *free_ctn,
144 /*OUT*/gras_dictelm_t **pp_elm) {
145 gras_error_t errcode = no_error;
146 gras_dictelm_t *p_elm = NULL;
148 if (!(p_elm = calloc(1, sizeof(gras_dictelm_t)))) {
149 if (free_ctn && data) {
157 p_elm->key_len = key_len;
158 p_elm->offset = offset;
159 p_elm->content = data;
160 p_elm->free_ctn = free_ctn;
162 errcode = gras_dynar_new(&(p_elm->sub), sizeof(gras_dictelm_t*),
163 _dictelm_wrapper_free);
164 if (errcode != no_error) {
165 if (free_ctn && data) {
180 * @pp_elm: the dict elem to be freed
182 * Frees a dictionnary element with all its childs.
185 gras_dictelm_free(gras_dictelm_t **pp_elm) {
187 gras_dictelm_t *p_elm = *pp_elm;
189 gras_dynar_free(p_elm->sub);
195 if (p_elm->free_ctn && p_elm->content) {
196 p_elm->free_ctn(p_elm->content);
199 memset(p_elm, 0, sizeof (*p_elm));
207 * _dictelm_wrapper_free:
209 * a wrapper to free dictelm with the right prototype to be usable within dynar
213 _dictelm_wrapper_free(void *pp_elm) {
214 DEBUG2("Free dictelm %p (*=%p)", pp_elm, *(void**)pp_elm);
215 gras_dictelm_free((gras_dictelm_t**)pp_elm);
218 /*####[ utility functions ]##################################################*/
223 * Returns the length of the common prefix of @str1 and @str2.
224 * Do make sure the strings are not null
228 _str_prefix_lgr(const char *key1,
234 const int old_offset = *p_offset;
240 /*CDEBUG5(dict_search, "%s: [%*s] <=> [%*s]", __FUNCTION__,
241 key1,key_len1,key2,key_len2);*/
243 if (o < key_len1 && o < key_len2) {
245 while (key1[o] == key2[o]) {
248 if (!(o < key_len1 && o < key_len2))
256 if (o != old_offset) {
266 } else if (o >= key_len2) {
279 * _dictelm_child_cmp:
281 * Compares two dictelm keys and return their matching (using the same
282 * convention than @_gras_dict_child_search() )
286 _dict_child_cmp(gras_dictelm_t *p_dict,
293 gras_dictelm_t *p_child = NULL;
298 gras_dynar_get(p_dict->sub, pos, &p_child);
300 /* Compute the length of the prefix
301 and if the searched key is before or after cur */
302 _str_prefix_lgr(p_child->key, p_child->key_len,
307 if (m) /* found, get out */
310 if (o < p_child->key_len && (o >= key_len || key[o] < p_child->key[o])) {
316 CDEBUG5(dict_search, "Cmp %*s and %*s => %d",
317 p_child->key_len - *p_offset, p_child->key + *p_offset,
318 p_child->key_len - *p_offset, key + *p_offset,
328 * _gras_dict_child_search:
330 * Search where would be inserted @key between the childs of @p_elm.
332 * Returns position of the child having a common prefix with this key
333 * If *match==0, no child have a common prefix
334 * *pos is where to add the key
335 * If *match==1, A child (located at *pos) have exactly this key
336 * If *match==2, A child (located at *pos) constitutes a prefix of the key
337 * the recursion have to go on that guy
338 * *prefix = the size of the key eaten at this level
339 * If *match==3 The key is a prefix of the child at *pos
340 * If *match==4, A child (loc. at *pos) share a common prefix with this key
341 * *prefix = size of the prefix.
342 * If searching, that's a mismatch.
343 * If inserting, you have to break the child and create an
344 * internal node having {child, key} as childs
345 * offset is used in input and output. In input, that's the length of the key
346 * handled by previous levels of recursion. In output, that the one counting
351 _gras_dictelm_child_search(gras_dictelm_t *p_elm,
364 CDEBUG5(dict_search, "search child [%*s] under [%*s] (len=%d)",
366 p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
367 (p_elm&&p_elm->sub)?gras_dynar_length(p_elm->sub):0);
370 len = gras_dynar_length(p_elm->sub);
372 for (p = 0; p < len; p++) {
375 _dict_child_cmp(p_elm, p, key, key_len, &o, &m, &cmp);
387 CDEBUG5(dict_search, "search [%*s] in [%*s] => %s",
389 p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
390 ( m == 0 ? "no child have a common prefix" :
391 ( m == 1 ? "selected child have exactly this key" :
392 ( m == 2 ? "selected child constitutes a prefix" :
393 ( m == 3 ? "key is a prefix of selected child" :
394 (m == 4 ? "selected child share a prefix" :
395 "internal error")))))
400 * _gras_dictelm_change_value:
402 * Change the value of the dictelm, making sure to free the old one, if any.
406 _gras_dictelm_change_value(gras_dictelm_t *p_elm,
408 void_f_pvoid_t *free_ctn) {
410 if (p_elm->content && p_elm->free_ctn) {
411 p_elm->free_ctn(p_elm->content);
414 p_elm->free_ctn = free_ctn;
415 p_elm->content = data;
419 * _gras_dictelm_set_rec:
421 * @head: the head of the dict
422 * @key: the key to set the new data
423 * @offset: offset on key.
424 * @data: the data to add in the dict
425 * @Returns: a gras_error
427 * set the @data in the structure under the @key. The @key is destroyed
428 * in the process. Think to strdup it before.
430 * This is a helper function to gras_dict_set which locks the struct and
431 * strdup the key before action.
434 _gras_dictelm_set_rec(gras_dictelm_t *p_head,
439 void_f_pvoid_t *free_ctn) {
440 gras_error_t errcode = no_error;
443 const int old_offset = offset;
445 CDEBUG6(dict_add, "--> Insert '%*s' after '%*s' (offset=%d) in tree %p",
447 ((p_head && p_head->key) ? p_head->key_len : 6),
448 ((p_head && p_head->key) ? p_head->key : "(head)"),
451 /*** The trivial cases first ***/
453 /* there is no key (we did enough recursion), change the value of head */
454 if (offset >= key_len) {
456 CDEBUG0(dict_add, "--> Change the value of head");
458 _gras_dictelm_change_value(p_head, data, free_ctn);
459 free(key); /* Keep the key used in the tree */
464 /*** Search where to add this child, and how ***/
465 _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
467 CDEBUG3(dict_add, "child_search => pos=%d, offset=%d, match=%d",
472 case 0: /* no child have a common prefix */
474 gras_dictelm_t *p_child = NULL;
476 TRY(_gras_dictelm_alloc(key, key_len, offset, data, free_ctn, &p_child));
477 CDEBUG1(dict_add, "-> Add a child %p", p_child);
478 TRY(gras_dynar_insert_at(p_head->sub, pos, &p_child));
483 case 1: /* A child have exactly this key => change its value*/
485 gras_dictelm_t *p_child = NULL;
487 gras_dynar_get(p_head->sub, pos, &p_child);
488 CDEBUG1(dict_add, "-> Change the value of the child %p", p_child);
489 _gras_dictelm_change_value(p_child, data, free_ctn);
496 case 2: /* A child constitutes a prefix of the key => recurse */
498 gras_dictelm_t *p_child = NULL;
500 gras_dynar_get(p_head->sub, pos, &p_child);
501 CDEBUG2(dict_add,"-> Recurse on %p (offset=%d)", p_child, offset);
503 return _gras_dictelm_set_rec(p_child, key, key_len,
504 offset, data, free_ctn);
507 case 3: /* The key is a prefix of the child => child becomes child of p_new */
509 gras_dictelm_t *p_new = NULL;
510 gras_dictelm_t *p_child = NULL;
512 gras_dynar_get(p_head->sub, pos, &p_child);
513 TRY(_gras_dictelm_alloc(key, key_len, old_offset, data, free_ctn, &p_new));
515 CDEBUG2(dict_add, "-> The child %p become child of new dict (%p)",
518 TRY(gras_dynar_push(p_new->sub, &p_child));
519 p_child->offset = offset;
520 TRY(gras_dynar_set(p_head->sub, pos, &p_new));
525 case 4: /* A child share a common prefix with this key => Common ancestor */
527 gras_dictelm_t *p_new = NULL;
528 gras_dictelm_t *p_child = NULL;
529 gras_dictelm_t *p_anc = NULL;
530 char *anc_key = NULL;
531 int anc_key_len = offset;
533 TRY(_gras_dictelm_alloc(key, key_len, offset, data, free_ctn, &p_new));
534 gras_dynar_get(p_head->sub, pos, &p_child);
536 anc_key = memdup(key, anc_key_len);
538 TRY(_gras_dictelm_alloc(anc_key, anc_key_len, old_offset,
539 NULL, NULL, &p_anc));
541 CDEBUG3(dict_add, "-> Make a common ancestor %p (%*s)",
542 p_anc, anc_key_len, anc_key);
544 if (key[offset] < p_child->key[offset]) {
545 TRY(gras_dynar_push(p_anc->sub, &p_new));
546 TRY(gras_dynar_push(p_anc->sub, &p_child));
548 TRY(gras_dynar_push(p_anc->sub, &p_child));
549 TRY(gras_dynar_push(p_anc->sub, &p_new));
552 p_child->offset = offset;
554 TRY(gras_dynar_set(p_head->sub, pos, &p_anc));
566 * gras_dictelm_set_ext:
568 * @head: the head of the dict
569 * @key: the key to set the new data
570 * @data: the data to add in the dict
571 * @Returns: a gras_error
573 * set the @data in the structure under the @key, which can be any kind
574 * of data, as long as its length is provided in @key_len.
577 gras_dictelm_set_ext(gras_dictelm_t **pp_head,
581 void_f_pvoid_t *free_ctn) {
582 gras_error_t errcode = no_error;
583 gras_dictelm_t *p_head = *pp_head;
586 key = memdup(_key, key_len);
590 /* there is no head, create it */
592 gras_dictelm_t *p_child = NULL;
594 CDEBUG0(dict_add, "Create an head");
596 /* The head is priviledged by being the only one with a NULL key */
597 TRY(_gras_dictelm_alloc(NULL, 0, 0, NULL, NULL, &p_head));
599 TRY(_gras_dictelm_alloc(key, key_len, 0, data, free_ctn, &p_child));
600 TRY(gras_dynar_insert_at(p_head->sub, 0, &p_child));
607 return _gras_dictelm_set_rec(p_head, key, key_len, 0, data, free_ctn);
613 * @head: the head of the dict
614 * @key: the key to set the new data
615 * @data: the data to add in the dict
616 * @Returns: a gras_error
618 * set the @data in the structure under the @key, which is a
619 * null terminated string.
622 gras_dictelm_set(gras_dictelm_t **pp_head,
625 void_f_pvoid_t *free_ctn) {
627 return gras_dictelm_set_ext(pp_head, _key, 1+strlen(_key), data, free_ctn);
631 * _gras_dict_get_rec:
633 * @head: the head of the dict
634 * @key: the key to find data
635 * @offset: offset on the key
636 * @data: the data that we are looking for
637 * @Returns: gras_error
639 * Search the given @key. mismatch_error when not found.
643 _gras_dictelm_get_rec(gras_dictelm_t *p_head,
647 /* OUT */void **data) {
649 gras_error_t errcode = no_error;
651 CDEBUG3(dict_search, "Search %*s in %p", key_len, key, p_head);
653 /*** The trivial case first ***/
655 /* we did enough recursion, we're done */
656 if (offset >= key_len) {
657 *data = p_head->content;
666 *data = NULL; // Make it ready to answer 'not found' in one operation
668 /*** Search where is the good child, and how good it is ***/
669 _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
673 case 0: /* no child have a common prefix */
674 return mismatch_error;
676 case 1: /* A child have exactly this key => Got it */
678 gras_dictelm_t *p_child = NULL;
680 gras_dynar_get(p_head->sub, pos, &p_child);
681 *data = p_child->content;
686 case 2: /* A child constitutes a prefix of the key => recurse */
688 gras_dictelm_t *p_child = NULL;
690 gras_dynar_get(p_head->sub, pos, &p_child);
692 return _gras_dictelm_get_rec(p_child, key, key_len, offset, data);
695 case 3: /* The key is a prefix of the child => not found */
696 return mismatch_error;
698 case 4: /* A child share a common prefix with this key => not found */
699 return mismatch_error;
708 * gras_dictelm_get_ext:
710 * @head: the head of the dict
711 * @key: the key to find data
712 * @data: the data that we are looking for
713 * @Returns: gras_error
715 * Search the given @key. mismatch_error when not found.
718 gras_dictelm_get_ext(gras_dictelm_t *p_head,
721 /* OUT */void **data) {
722 /* there is no head, go to hell */
724 return mismatch_error;
727 return _gras_dictelm_get_rec(p_head, key, key_len, 0, data);
733 * @head: the head of the dict
734 * @key: the key to find data
735 * @data: the data that we are looking for
736 * @Returns: gras_error
738 * Search the given @key. mismatch_error when not found.
741 gras_dictelm_get(gras_dictelm_t *p_head,
743 /* OUT */void **data) {
745 return gras_dictelm_get_ext(p_head, key, 1+strlen(key), data);
748 /*----[ _gras_dict_collapse ]------------------------------------------------*/
751 _collapse_if_need(gras_dictelm_t *p_head,
754 gras_dictelm_t *p_child = NULL;
756 CDEBUG2(dict_collapse, "Collapse %d of %p... ", pos, p_head); fflush(stdout);
759 /* Remove the child if |it's key| == 0 (meaning it's dead) */
760 gras_dynar_get(p_head->sub, pos, &p_child);
762 if (offset >= p_child->key_len) {
764 gras_assert0(gras_dynar_length(p_child->sub) == 0,
765 "Found a dead child with grand childs. Internal error");
767 CDEBUG1(dict_collapse, "Remove dead child %p.... ", p_child);
768 gras_dynar_remove_at(p_head->sub, pos, &p_child);
773 CDEBUG0(dict_collapse, "Do not collapse the head, you stupid programm");
777 if (p_head->content || p_head->free_ctn ||
778 gras_dynar_length(p_head->sub) != 1) {
779 CDEBUG0(dict_collapse, "Cannot collapse");
780 return; /* cannot collapse */
783 gras_dynar_get(p_head->sub, 0, &p_child);
785 /* Get the child's key as new key */
786 CDEBUG2(dict_collapse,
787 "Do collapse with only child %*s", p_child->key_len, p_child->key);
789 p_head->content = p_child->content;
790 p_head->free_ctn = p_child->free_ctn;
792 p_head->key = p_child->key;
793 p_head->key_len = p_child->key_len;
795 gras_dynar_free_container(p_head->sub) ;
797 p_head->sub = p_child->sub;
802 * _gras_dict_remove_rec:
804 * @head: the head of the dict
805 * @key: the key of the data to be removed
806 * @offset: offset on the key
807 * @Returns: gras_error_t
809 * Remove the entry associated with the given @key
812 _gras_dictelm_remove_rec(gras_dictelm_t *p_head,
816 gras_error_t errcode = no_error;
818 /* there is no key to search, we did enough recursion => kill current */
819 if (offset >= key_len) {
820 int killme = 0; /* do I need to suicide me ? */
822 if (p_head->content && p_head->free_ctn) {
823 p_head->free_ctn(p_head->content);
826 killme = !gras_dynar_length(p_head->sub);
827 p_head->content = NULL;
828 p_head->free_ctn = NULL;
829 _collapse_if_need(p_head, -1, offset);
832 DEBUG0("kill this node");
833 p_head->key_len = 0; /* killme. Cleanup done one step higher in recursion */
841 int old_offset = offset;
843 /*** Search where is the good child, and how good it is ***/
844 _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
848 case 1: /* A child have exactly this key => recurse */
849 case 2: /* A child constitutes a prefix of the key => recurse */
852 gras_dictelm_t *p_child = NULL;
854 gras_dynar_get(p_head->sub, pos, &p_child);
855 /*DEBUG5("Recurse on child %d of %p to remove %*s (prefix=%d)",
856 pos, p_child, key+offset, key_len-offset,offset);*/
857 TRY(_gras_dictelm_remove_rec(p_child, key, key_len, offset));
859 _collapse_if_need(p_head, pos, old_offset);
864 case 0: /* no child have a common prefix */
865 case 3: /* The key is a prefix of the child => not found */
866 case 4: /* A child share a common prefix with this key => not found */
868 return mismatch_error;
879 * gras_dictelm_remove_ext:
881 * @head: the head of the dict
882 * @key: the key of the data to be removed
883 * @Returns: gras_error_t
885 * Remove the entry associated with the given @key
888 gras_dictelm_remove_ext(gras_dictelm_t *p_head,
891 /* there is no head, go to hell */
893 RAISE0(mismatch_error, "there is no head, go to hell");
896 return _gras_dictelm_remove_rec(p_head, key, key_len, 0);
900 * gras_dictelm_remove:
902 * @head: the head of the dict
903 * @key: the key of the data to be removed
904 * @Returns: gras_error_t
906 * Remove the entry associated with the given @key
909 gras_dictelm_remove(gras_dictelm_t *p_head,
911 return _gras_dictelm_remove_rec(p_head, key, 1+strlen(key),0);
914 /*----[ _gras_dict_dump_rec ]------------------------------------------------*/
915 /* private function to do the job of gras_dict_dump recursively */
916 /*---------------------------------------------------------------------------*/
919 _gras_dictelm_dump_rec(gras_dictelm_t *p_head,
921 void_f_pvoid_t *output) {
922 gras_error_t errcode = no_error;
923 gras_dictelm_t *p_child = NULL;
931 printf("[%p] ", p_head);
934 key_len = p_head->key_len;
940 for (i = 0; i < offset; i++) {
951 char *key_string = NULL;
953 key_string = malloc(key_len*2+1);
957 _gras_bytes_to_string(key, key_len, key_string);
959 printf("%*s|(%d)", key_len-offset, key_string + offset, offset);
968 if (p_head->content) {
971 output(p_head->content);
980 printf(" \t\t\t[ %d child(s) ]\n", gras_dynar_length(p_head->sub));
982 gras_dynar_foreach(p_head->sub, i, p_child)
983 TRY(_gras_dictelm_dump_rec(p_child, p_child->offset, output));
991 * @head: the head of the dict
992 * @output: a function to dump each data in the tree
993 * @Returns: gras_error_t
995 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
996 * function to output the data. If NULL, data won't be displayed.
1000 gras_dictelm_dump(gras_dictelm_t *p_head,
1001 void_f_pvoid_t *output) {
1002 return _gras_dictelm_dump_rec(p_head, 0, output);
1006 * gras_dictelm_print_fct:
1010 * To dump multidict, this function dumps a dict
1014 gras_dictelm_print_fct(void *data) {
1015 printf("tree %p", data);