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 XBT_LOG_EXTERNAL_CATEGORY(dict);
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict_elm,dict,"Dictionaries internals");
16 XBT_LOG_NEW_SUBCATEGORY(dict_add,dict,"Dictionaries internals: elements addition");
17 XBT_LOG_NEW_SUBCATEGORY(dict_search,dict,"Dictionaries internals: searching");
18 XBT_LOG_NEW_SUBCATEGORY(dict_remove,dict,"Dictionaries internals: elements removal");
19 XBT_LOG_NEW_SUBCATEGORY(dict_collapse,dict,"Dictionaries internals: post-removal cleanup");
20 XBT_LOG_NEW_SUBCATEGORY(dict_multi,dict,"Dictionaries internals: dictionaries of dictionaries");
22 /*####[ Private prototypes ]#################################################*/
24 static _XBT_INLINE void _xbt_dictelm_alloc(char *key,
28 void_f_pvoid_t *free_f,
29 /*OUT*/s_xbt_dictelm_t **where);
30 static void _dictelm_wrapper_free(void*);
32 static _XBT_INLINE void _str_prefix_lgr(const char *key1,
40 static void _xbt_dictelm_dump_rec(s_xbt_dictelm_t *head,
42 void_f_pvoid_t *output);
46 static void _xbt_dictelm_set_rec(s_xbt_dictelm_t *head,
51 void_f_pvoid_t *free_f);
52 static xbt_error_t _xbt_dictelm_get_rec(s_xbt_dictelm_t *head,
56 /* OUT */void **data);
57 static xbt_error_t _xbt_dictelm_remove_rec(s_xbt_dictelm_t *head,
64 _collapse_if_need(s_xbt_dictelm_t *p_head,
72 xbt_memdup(const void * const ptr,
73 const size_t length) {
74 void * new_ptr = NULL;
76 new_ptr = xbt_malloc(length);
77 memcpy(new_ptr, ptr, length);
83 * _xbt_nibble_to_char:
85 * Change any byte to a printable char
90 _xbt_nibble_to_char(unsigned char c) {
92 return c>9 ? c-10+'a' : c + '0';
96 * _xbt_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 _xbt_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++ = _xbt_nibble_to_char(*src & 0x0f );
111 *dst++ = _xbt_nibble_to_char(*src++ & 0xf0 >> 4);
122 * _xbt_dictelm_alloc:
124 * Alloc a dict element with no child.
128 _xbt_dictelm_alloc(char *key,
132 void_f_pvoid_t *free_f,
133 /*OUT*/s_xbt_dictelm_t **pp_elm) {
134 xbt_error_t errcode = no_error;
135 s_xbt_dictelm_t *p_elm = NULL;
137 p_elm = xbt_new(s_xbt_dictelm_t,1);
140 p_elm->key_len = key_len;
141 p_elm->offset = offset;
142 p_elm->content = data;
143 p_elm->free_f = free_f;
144 p_elm->sub = xbt_dynar_new(sizeof(s_xbt_dictelm_t*), _dictelm_wrapper_free);
153 * @pp_elm: the dict elem to be freed
155 * Frees a dictionnary element with all its childs.
158 xbt_dictelm_free(s_xbt_dictelm_t **pp_elm) {
160 s_xbt_dictelm_t *p_elm = *pp_elm;
162 xbt_dynar_free(&(p_elm->sub));
165 xbt_free(p_elm->key);
168 if (p_elm->free_f && p_elm->content) {
169 p_elm->free_f(p_elm->content);
178 * _dictelm_wrapper_free:
180 * a wrapper to free dictelm with the right prototype to be usable within dynar
184 _dictelm_wrapper_free(void *pp_elm) {
185 DEBUG3("Free dictelm '%.*s' %p",
186 (*(s_xbt_dictelm_t**)pp_elm)->key_len, (*(s_xbt_dictelm_t**)pp_elm)->key,
188 xbt_dictelm_free((s_xbt_dictelm_t**)pp_elm);
191 /*####[ utility functions ]##################################################*/
196 * Returns the length of the common prefix of @str1 and @str2.
197 * Do make sure the strings are not null
201 _str_prefix_lgr(const char *key1,
207 const int old_offset = *p_offset;
213 /*CDEBUG5(dict_search, "%s: [%.*s] <=> [%.*s]", __FUNCTION__,
214 key1,key_len1,key2,key_len2);*/
216 if (o < key_len1 && o < key_len2) {
218 while (key1[o] == key2[o]) {
221 if (!(o < key_len1 && o < key_len2))
229 if (o != old_offset) {
239 } else if (o >= key_len2) {
252 * _dictelm_child_cmp:
254 * Compares two dictelm keys and return their matching (using the same
255 * convention than @_xbt_dict_child_search() )
259 _dict_child_cmp(s_xbt_dictelm_t *p_dict,
266 s_xbt_dictelm_t *p_child = NULL;
271 p_child = xbt_dynar_get_as(p_dict->sub, pos, s_xbt_dictelm_t*);
273 /* Compute the length of the prefix
274 and if the searched key is before or after cur */
275 _str_prefix_lgr(p_child->key, p_child->key_len,
280 if (m) /* found, get out */
283 if (o < p_child->key_len && (o >= key_len || key[o] < p_child->key[o])) {
289 CDEBUG6(dict_search, "Cmp '%.*s' and '%.*s' (offset=%d) => %d",
290 p_child->key_len - *p_offset, p_child->key + *p_offset,
291 key_len - *p_offset, key + *p_offset,
301 * _xbt_dict_child_search:
303 * Search where would be inserted @key between the childs of @p_elm.
305 * Returns position of the child having a common prefix with this key
306 * If *match==0, no child have a common prefix
307 * *pos is where to add the key
308 * If *match==1, A child (located at *pos) have exactly this key
309 * If *match==2, A child (located at *pos) constitutes a prefix of the key
310 * the recursion have to go on that guy
311 * *prefix = the size of the key eaten at this level
312 * If *match==3 The key is a prefix of the child at *pos
313 * If *match==4, A child (loc. at *pos) share a common prefix with this key
314 * *prefix = size of the prefix.
315 * If searching, that's a mismatch.
316 * If inserting, you have to break the child and create an
317 * internal node having {child, key} as childs
318 * offset is used in input and output. In input, that's the length of the key
319 * handled by previous levels of recursion. In output, that the one counting
324 _xbt_dictelm_child_search(s_xbt_dictelm_t *p_elm,
337 CDEBUG5(dict_search, "search child [%.*s] under [%.*s] (len=%lu)",
339 p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
340 (p_elm&&p_elm->sub)?xbt_dynar_length(p_elm->sub):0);
343 len = xbt_dynar_length(p_elm->sub);
354 _dict_child_cmp(p_elm, p_min, key, key_len, &o, &m, &cmp);
355 if(!m) { /* OK, maybe it is somewhere else. */
357 if (cmp<0) { /* Insert at the very beginning */
359 } else if (p_max<=0) { /* No way. It is not there. Insert à the very end */
364 _dict_child_cmp(p_elm, p_max, key, key_len, &o, &m, &cmp);
367 if(cmp>0) { /* Should be located at the end of the table */
369 } else { /* Too bad, let's go for a dichotomic search. */
370 while(p_max-p_min>1) {
371 _dict_child_cmp(p_elm, (p_min+p_max)/2, key, key_len, &o, &m, &cmp);
374 if(cmp<0) p_max=(p_min+p_max)/2;
375 if(cmp>0) p_min=(p_min+p_max)/2;
377 if(m) /* We have the element */
379 else /* it should be inserted just after p_min */
387 for (p = 0; p < len; p++) {
390 _dict_child_cmp(p_elm, p, key, key_len, &o, &m, &cmp);
403 CDEBUG5(dict_search, "search [%.*s] in [%.*s] => %s",
405 p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
406 ( m == 0 ? "no child have a common prefix" :
407 ( m == 1 ? "selected child have exactly this key" :
408 ( m == 2 ? "selected child constitutes a prefix" :
409 ( m == 3 ? "key is a prefix of selected child" :
410 (m == 4 ? "selected child share a prefix" :
411 "internal error")))))
416 * _xbt_dictelm_change_value:
418 * Change the value of the dictelm, making sure to free the old one, if any.
422 _xbt_dictelm_change_value(s_xbt_dictelm_t *p_elm,
424 void_f_pvoid_t *free_f) {
426 if (p_elm->content && p_elm->free_f) {
427 p_elm->free_f(p_elm->content);
430 p_elm->free_f = free_f;
431 p_elm->content = data;
435 * _xbt_dictelm_set_rec:
437 * @head: the head of the dict
438 * @key: the key to set the new data
439 * @offset: offset on key.
440 * @data: the data to add in the dict
442 * set the @data in the structure under the @key. The @key is destroyed
443 * in the process. Think to strdup it before.
445 * This is a helper function to xbt_dict_set which locks the struct and
446 * strdup the key before action.
449 _xbt_dictelm_set_rec(s_xbt_dictelm_t *p_head,
454 void_f_pvoid_t *free_f) {
457 const int old_offset = offset;
459 CDEBUG6(dict_add, "--> Insert '%.*s' after '%.*s' (offset=%d) in tree %p",
461 ((p_head && p_head->key) ? p_head->key_len : 6),
462 ((p_head && p_head->key) ? p_head->key : "(head)"),
463 offset, (void*)p_head);
465 /*** The trivial cases first ***/
467 /* there is no key (we did enough recursion), change the value of head */
468 if (offset >= key_len) {
470 CDEBUG0(dict_add, "--> Change the value of head");
472 _xbt_dictelm_change_value(p_head, data, free_f);
473 xbt_free(key); /* Keep the key used in the tree */
478 /*** Search where to add this child, and how ***/
479 _xbt_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
481 CDEBUG3(dict_add, "child_search => pos=%d, offset=%d, match=%d",
486 case 0: /* no child have a common prefix */
488 s_xbt_dictelm_t *p_child = NULL;
490 _xbt_dictelm_alloc(key, key_len, offset, data, free_f, &p_child);
491 CDEBUG1(dict_add, "-> Add a child %p", (void*)p_child);
492 xbt_dynar_insert_at(p_head->sub, pos, &p_child);
497 case 1: /* A child have exactly this key => change its value*/
499 s_xbt_dictelm_t *p_child = NULL;
501 p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
502 CDEBUG1(dict_add, "-> Change the value of the child %p", (void*)p_child);
503 _xbt_dictelm_change_value(p_child, data, free_f);
510 case 2: /* A child constitutes a prefix of the key => recurse */
512 s_xbt_dictelm_t *p_child = NULL;
514 p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
515 CDEBUG2(dict_add,"-> Recurse on %p (offset=%d)", (void*)p_child, offset);
517 _xbt_dictelm_set_rec(p_child, key, key_len,
518 offset, data, free_f);
522 case 3: /* The key is a prefix of the child => child becomes child of p_new */
524 s_xbt_dictelm_t *p_new = NULL;
525 s_xbt_dictelm_t *p_child = NULL;
527 p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
528 _xbt_dictelm_alloc(key, key_len, old_offset, data, free_f, &p_new);
530 CDEBUG2(dict_add, "-> The child %p become child of new dict (%p)",
531 (void*)p_child, (void*)p_new);
533 xbt_dynar_push(p_new->sub, &p_child);
534 p_child->offset = offset;
535 xbt_dynar_set(p_head->sub, pos, &p_new);
540 case 4: /* A child share a common prefix with this key => Common ancestor */
542 s_xbt_dictelm_t *p_new = NULL;
543 s_xbt_dictelm_t *p_child = NULL;
544 s_xbt_dictelm_t *p_anc = NULL;
545 char *anc_key = NULL;
546 int anc_key_len = offset;
548 _xbt_dictelm_alloc(key, key_len, offset, data, free_f, &p_new);
549 p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
551 anc_key = xbt_memdup(key, anc_key_len);
553 _xbt_dictelm_alloc(anc_key, anc_key_len, old_offset, NULL, NULL, &p_anc);
555 CDEBUG3(dict_add, "-> Make a common ancestor %p (%.*s)",
556 (void*)p_anc, anc_key_len, anc_key);
558 if (key[offset] < p_child->key[offset]) {
559 xbt_dynar_push(p_anc->sub, &p_new);
560 xbt_dynar_push(p_anc->sub, &p_child);
562 xbt_dynar_push(p_anc->sub, &p_child);
563 xbt_dynar_push(p_anc->sub, &p_new);
566 p_child->offset = offset;
568 xbt_dynar_set(p_head->sub, pos, &p_anc);
579 * xbt_dictelm_set_ext:
581 * @head: the head of the dict
582 * @key: the key to set the new data
583 * @data: the data to add in the dict
585 * set the @data in the structure under the @key, which can be any kind
586 * of data, as long as its length is provided in @key_len.
589 xbt_dictelm_set_ext(s_xbt_dictelm_t **pp_head,
593 void_f_pvoid_t *free_f) {
594 s_xbt_dictelm_t *p_head = *pp_head;
597 key = xbt_memdup(_key, key_len);
599 /* there is no head, create it */
601 s_xbt_dictelm_t *p_child = NULL;
603 CDEBUG0(dict_add, "Create an head");
605 /* The head is priviledged by being the only one with a NULL key */
606 _xbt_dictelm_alloc(NULL, 0, 0, NULL, NULL, &p_head);
608 _xbt_dictelm_alloc(key, key_len, 0, data, free_f, &p_child);
609 xbt_dynar_insert_at(p_head->sub, 0, &p_child);
616 _xbt_dictelm_set_rec(p_head, key, key_len, 0, data, free_f);
622 * @head: the head of the dict
623 * @key: the key to set the new data
624 * @data: the data to add in the dict
626 * set the @data in the structure under the @key, which is a
627 * null terminated string.
630 xbt_dictelm_set(s_xbt_dictelm_t **pp_head,
633 void_f_pvoid_t *free_f) {
635 xbt_dictelm_set_ext(pp_head, _key, 1+strlen(_key), data, free_f);
641 * @head: the head of the dict
642 * @key: the key to find data
643 * @offset: offset on the key
644 * @data: the data that we are looking for
645 * @Returns: xbt_error
647 * Search the given @key. mismatch_error when not found.
651 _xbt_dictelm_get_rec(s_xbt_dictelm_t *p_head,
658 CDEBUG3(dict_search, "Search %.*s in %p", key_len, key, (void*)p_head);
660 /*** The trivial case first ***/
662 /* we did enough recursion, we're done */
663 if (offset >= key_len) {
664 *data = p_head->content;
673 *data = NULL; /* Make it ready to answer 'not found' in one operation */
675 /*** Search where is the good child, and how good it is ***/
676 _xbt_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
680 case 0: /* no child have a common prefix */
681 return mismatch_error;
683 case 1: /* A child have exactly this key => Got it */
685 s_xbt_dictelm_t *p_child = NULL;
687 p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
688 *data = p_child->content;
693 case 2: /* A child constitutes a prefix of the key => recurse */
695 s_xbt_dictelm_t *p_child = NULL;
697 p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
699 return _xbt_dictelm_get_rec(p_child, key, key_len, offset, data);
702 case 3: /* The key is a prefix of the child => not found */
703 return mismatch_error;
705 case 4: /* A child share a common prefix with this key => not found */
706 return mismatch_error;
715 * xbt_dictelm_get_ext:
717 * @head: the head of the dict
718 * @key: the key to find data
719 * @data: the data that we are looking for
720 * @Returns: xbt_error
722 * Search the given @key. mismatch_error when not found.
725 xbt_dictelm_get_ext(s_xbt_dictelm_t *p_head,
728 /* OUT */void **data) {
729 /* there is no head, go to hell */
731 return mismatch_error;
734 return _xbt_dictelm_get_rec(p_head, key, key_len, 0, data);
740 * @head: the head of the dict
741 * @key: the key to find data
742 * @data: the data that we are looking for
743 * @Returns: xbt_error
745 * Search the given @key. mismatch_error when not found.
748 xbt_dictelm_get(s_xbt_dictelm_t *p_head,
750 /* OUT */void **data) {
752 return xbt_dictelm_get_ext(p_head, key, 1+strlen(key), data);
755 /*----[ _xbt_dict_collapse ]------------------------------------------------*/
758 _collapse_if_need(xbt_dictelm_t head,
761 xbt_dictelm_t child = NULL;
763 CDEBUG2(dict_collapse, "Collapse %d of %p... ", pos, (void*)head);
766 /* Remove the child if |it's key| == 0 (meaning it's dead) */
767 child = xbt_dynar_get_as(head->sub, pos, xbt_dictelm_t);
769 if (offset >= child->key_len) {
771 xbt_assert0(xbt_dynar_length(child->sub) == 0,
772 "Found a dead child with grand childs. Internal error");
774 CDEBUG1(dict_collapse, "Remove dead child %p.... ", (void*)child);
775 xbt_dynar_remove_at(head->sub, pos, &child);
776 xbt_dictelm_free(&child);
781 CDEBUG0(dict_collapse, "Do not collapse the head, you stupid programm");
785 if (head->content || head->free_f ||
786 xbt_dynar_length(head->sub) != 1) {
787 CDEBUG0(dict_collapse, "Cannot collapse");
788 return; /* cannot collapse */
791 child = xbt_dynar_get_as(head->sub, 0, xbt_dictelm_t);
793 /* Get the child's key as new key */
794 CDEBUG2(dict_collapse,
795 "Do collapse with only child %.*s", child->key_len, child->key);
797 head->content = child->content;
798 head->free_f = child->free_f;
800 head->key = child->key;
801 head->key_len = child->key_len;
803 xbt_dynar_free_container(&(head->sub)) ;
805 head->sub = child->sub;
810 * _xbt_dict_remove_rec:
812 * @head: the head of the dict
813 * @key: the key of the data to be removed
814 * @offset: offset on the key
815 * @Returns: xbt_error_t
817 * Remove the entry associated with the given @key
820 _xbt_dictelm_remove_rec(xbt_dictelm_t head,
824 xbt_error_t errcode = no_error;
826 /* there is no key to search, we did enough recursion => kill current */
827 if (offset >= key_len) {
828 int killme = 0; /* do I need to suicide me ? */
830 if (head->content && head->free_f) {
831 head->free_f(head->content);
834 killme = !xbt_dynar_length(head->sub);
835 head->content = NULL;
837 _collapse_if_need(head, -1, offset);
840 DEBUG0("kill this node");
841 head->key_len = 0; /* killme. Cleanup done one step higher in recursion */
849 int old_offset = offset;
851 /*** Search where is the good child, and how good it is ***/
852 _xbt_dictelm_child_search(head, key, key_len, &pos, &offset, &match);
856 case 1: /* A child have exactly this key => recurse */
857 case 2: /* A child constitutes a prefix of the key => recurse */
860 s_xbt_dictelm_t *p_child = NULL;
862 p_child = xbt_dynar_get_as(head->sub, pos, s_xbt_dictelm_t*);
863 /*DEBUG5("Recurse on child %d of %p to remove %.*s (prefix=%d)",
864 pos, (void*)p_child, key+offset, key_len-offset,offset);*/
865 TRY(_xbt_dictelm_remove_rec(p_child, key, key_len, offset));
867 _collapse_if_need(head, pos, old_offset);
872 case 0: /* no child have a common prefix */
873 case 3: /* The key is a prefix of the child => not found */
874 case 4: /* A child share a common prefix with this key => not found */
876 return mismatch_error;
887 * xbt_dictelm_remove_ext:
889 * @head: the head of the dict
890 * @key: the key of the data to be removed
891 * @Returns: xbt_error_t
893 * Remove the entry associated with the given @key
896 xbt_dictelm_remove_ext(xbt_dictelm_t head,
899 /* there is no head, go to hell */
901 RAISE0(mismatch_error, "there is no head, go to hell");
904 return _xbt_dictelm_remove_rec(head, key, key_len, 0);
908 * xbt_dictelm_remove:
910 * @head: the head of the dict
911 * @key: the key of the data to be removed
912 * @Returns: xbt_error_t
914 * Remove the entry associated with the given @key
917 xbt_dictelm_remove(xbt_dictelm_t head,
919 return _xbt_dictelm_remove_rec(head, key, 1+strlen(key),0);
922 /*----[ _xbt_dict_dump_rec ]------------------------------------------------*/
923 /* private function to do the job of xbt_dict_dump recursively */
924 /*---------------------------------------------------------------------------*/
927 _xbt_dictelm_dump_rec(xbt_dictelm_t head,
929 void_f_pvoid_t *output) {
930 xbt_dictelm_t child = NULL;
938 printf("[%p] ", (void*)head);
941 key_len = head->key_len;
946 for (i = 0; i < offset; i++)
956 char *key_string = NULL;
958 key_string = xbt_malloc(key_len*2+1);
959 _xbt_bytes_to_string(key, key_len, key_string);
961 printf("%.*s|(%d)", key_len-offset, key_string + offset, offset);
963 xbt_free(key_string);
973 output(head->content);
982 printf(" \t\t\t[ %lu child(s) ]\n", xbt_dynar_length(head->sub));
984 xbt_dynar_foreach(head->sub, i, child)
985 _xbt_dictelm_dump_rec(child, child->offset, output);
992 * @head: the head of the dict
993 * @output: a function to dump each data in the tree
994 * @Returns: xbt_error_t
996 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
997 * function to output the data. If NULL, data won't be displayed.
1001 xbt_dictelm_dump(xbt_dictelm_t head,
1002 void_f_pvoid_t *output) {
1003 _xbt_dictelm_dump_rec(head, 0, output);
1007 * xbt_dictelm_print_fct:
1011 * To dump multidict, this function dumps a dict
1015 xbt_dictelm_print_fct(void *data) {
1016 printf("tree %p", (void*)data);