3 /* dict - a generic dictionnary, variation over the B-tree concept */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "dict_private.h" /* prototypes of this module */
12 XBT_LOG_EXTERNAL_CATEGORY(dict);
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict_elm,dict,"Dictionaries internals");
15 XBT_LOG_NEW_SUBCATEGORY(dict_add,dict,"Dictionaries internals: elements addition");
16 XBT_LOG_NEW_SUBCATEGORY(dict_search,dict,"Dictionaries internals: searching");
17 XBT_LOG_NEW_SUBCATEGORY(dict_remove,dict,"Dictionaries internals: elements removal");
18 XBT_LOG_NEW_SUBCATEGORY(dict_collapse,dict,"Dictionaries internals: post-removal cleanup");
19 XBT_LOG_NEW_SUBCATEGORY(dict_multi,dict,"Dictionaries internals: dictionaries of dictionaries");
21 /*####[ Private prototypes ]#################################################*/
23 static _XBT_INLINE void _xbt_dictelm_alloc(char *key,
27 void_f_pvoid_t *free_f,
28 /*OUT*/s_xbt_dictelm_t **where);
29 static void _dictelm_wrapper_free(void*);
31 static _XBT_INLINE void _str_prefix_lgr(const char *key1,
39 static void _xbt_dictelm_dump_rec(s_xbt_dictelm_t *head,
41 void_f_pvoid_t *output);
45 static void _xbt_dictelm_set_rec(s_xbt_dictelm_t *head,
50 void_f_pvoid_t *free_f);
51 static xbt_error_t _xbt_dictelm_get_rec(s_xbt_dictelm_t *head,
55 /* OUT */void **data);
56 static xbt_error_t _xbt_dictelm_remove_rec(s_xbt_dictelm_t *head,
63 _collapse_if_need(s_xbt_dictelm_t *p_head,
71 xbt_memdup(const void * const ptr,
72 const size_t length) {
73 void * new_ptr = NULL;
75 new_ptr = xbt_malloc(length);
76 memcpy(new_ptr, ptr, length);
82 * _xbt_nibble_to_char:
84 * Change any byte to a printable char
89 _xbt_nibble_to_char(unsigned char c) {
91 return c>9 ? c-10+'a' : c + '0';
95 * _xbt_bytes_to_string:
97 * Change any byte array to a printable string
98 * The length of string_container should at least be data_len*2+1
102 _xbt_bytes_to_string(char * const ptr,
104 char * const string_container) {
105 unsigned char *src = (unsigned char *)ptr;
106 char *dst = string_container;
109 *dst++ = _xbt_nibble_to_char(*src & 0x0f );
110 *dst++ = _xbt_nibble_to_char(*src++ & 0xf0 >> 4);
121 * _xbt_dictelm_alloc:
123 * Alloc a dict element with no child.
127 _xbt_dictelm_alloc(char *key,
131 void_f_pvoid_t *free_f,
132 /*OUT*/s_xbt_dictelm_t **pp_elm) {
133 s_xbt_dictelm_t *p_elm = NULL;
135 p_elm = xbt_new(s_xbt_dictelm_t,1);
138 p_elm->key_len = key_len;
139 p_elm->offset = offset;
140 p_elm->content = data;
141 p_elm->free_f = free_f;
142 p_elm->sub = xbt_dynar_new(sizeof(s_xbt_dictelm_t*), _dictelm_wrapper_free);
151 * @pp_elm: the dict elem to be freed
153 * Frees a dictionnary element with all its childs.
156 xbt_dictelm_free(s_xbt_dictelm_t **pp_elm) {
158 s_xbt_dictelm_t *p_elm = *pp_elm;
160 xbt_dynar_free(&(p_elm->sub));
163 xbt_free(p_elm->key);
166 if (p_elm->free_f && p_elm->content) {
167 p_elm->free_f(p_elm->content);
176 * _dictelm_wrapper_free:
178 * a wrapper to free dictelm with the right prototype to be usable within dynar
182 _dictelm_wrapper_free(void *pp_elm) {
183 DEBUG3("Free dictelm '%.*s' %p",
184 (*(s_xbt_dictelm_t**)pp_elm)->key_len, (*(s_xbt_dictelm_t**)pp_elm)->key,
186 xbt_dictelm_free((s_xbt_dictelm_t**)pp_elm);
189 /*####[ utility functions ]##################################################*/
194 * Returns the length of the common prefix of @str1 and @str2.
195 * Do make sure the strings are not null
199 _str_prefix_lgr(const char *key1,
205 const int old_offset = *p_offset;
211 /*CDEBUG5(dict_search, "%s: [%.*s] <=> [%.*s]", __FUNCTION__,
212 key1,key_len1,key2,key_len2);*/
214 if (o < key_len1 && o < key_len2) {
216 while (key1[o] == key2[o]) {
219 if (!(o < key_len1 && o < key_len2))
227 if (o != old_offset) {
237 } else if (o >= key_len2) {
250 * _dictelm_child_cmp:
252 * Compares two dictelm keys and return their matching (using the same
253 * convention than @_xbt_dict_child_search() )
257 _dict_child_cmp(s_xbt_dictelm_t *p_dict,
264 s_xbt_dictelm_t *p_child = NULL;
269 p_child = xbt_dynar_get_as(p_dict->sub, pos, s_xbt_dictelm_t*);
271 /* Compute the length of the prefix
272 and if the searched key is before or after cur */
273 _str_prefix_lgr(p_child->key, p_child->key_len,
278 if (m) /* found, get out */
281 if (o < p_child->key_len && (o >= key_len || key[o] < p_child->key[o])) {
287 CDEBUG6(dict_search, "Cmp '%.*s' and '%.*s' (offset=%d) => %d",
288 p_child->key_len - *p_offset, p_child->key + *p_offset,
289 key_len - *p_offset, key + *p_offset,
299 * _xbt_dict_child_search:
301 * Search where would be inserted @key between the childs of @p_elm.
303 * Returns position of the child having a common prefix with this key
304 * If *match==0, no child have a common prefix
305 * *pos is where to add the key
306 * If *match==1, A child (located at *pos) have exactly this key
307 * If *match==2, A child (located at *pos) constitutes a prefix of the key
308 * the recursion have to go on that guy
309 * *prefix = the size of the key eaten at this level
310 * If *match==3 The key is a prefix of the child at *pos
311 * If *match==4, A child (loc. at *pos) share a common prefix with this key
312 * *prefix = size of the prefix.
313 * If searching, that's a mismatch.
314 * If inserting, you have to break the child and create an
315 * internal node having {child, key} as childs
316 * offset is used in input and output. In input, that's the length of the key
317 * handled by previous levels of recursion. In output, that the one counting
322 _xbt_dictelm_child_search(s_xbt_dictelm_t *p_elm,
335 CDEBUG5(dict_search, "search child [%.*s] under [%.*s] (len=%lu)",
337 p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
338 (p_elm&&p_elm->sub)?xbt_dynar_length(p_elm->sub):0);
341 len = xbt_dynar_length(p_elm->sub);
352 _dict_child_cmp(p_elm, p_min, key, key_len, &o, &m, &cmp);
353 if(!m) { /* OK, maybe it is somewhere else. */
355 if (cmp<0) { /* Insert at the very beginning */
357 } else if (p_max<=0) { /* No way. It is not there. Insert à the very end */
362 _dict_child_cmp(p_elm, p_max, key, key_len, &o, &m, &cmp);
365 if(cmp>0) { /* Should be located at the end of the table */
367 } else { /* Too bad, let's go for a dichotomic search. */
368 while(p_max-p_min>1) {
369 _dict_child_cmp(p_elm, (p_min+p_max)/2, key, key_len, &o, &m, &cmp);
372 if(cmp<0) p_max=(p_min+p_max)/2;
373 if(cmp>0) p_min=(p_min+p_max)/2;
375 if(m) /* We have the element */
377 else /* it should be inserted just after p_min */
385 for (p = 0; p < len; p++) {
388 _dict_child_cmp(p_elm, p, key, key_len, &o, &m, &cmp);
401 CDEBUG5(dict_search, "search [%.*s] in [%.*s] => %s",
403 p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
404 ( m == 0 ? "no child have a common prefix" :
405 ( m == 1 ? "selected child have exactly this key" :
406 ( m == 2 ? "selected child constitutes a prefix" :
407 ( m == 3 ? "key is a prefix of selected child" :
408 (m == 4 ? "selected child share a prefix" :
409 "internal error")))))
414 * _xbt_dictelm_change_value:
416 * Change the value of the dictelm, making sure to free the old one, if any.
420 _xbt_dictelm_change_value(s_xbt_dictelm_t *p_elm,
422 void_f_pvoid_t *free_f) {
424 if (p_elm->content && p_elm->free_f) {
425 p_elm->free_f(p_elm->content);
428 p_elm->free_f = free_f;
429 p_elm->content = data;
433 * _xbt_dictelm_set_rec:
435 * @head: the head of the dict
436 * @key: the key to set the new data
437 * @offset: offset on key.
438 * @data: the data to add in the dict
440 * set the @data in the structure under the @key. The @key is destroyed
441 * in the process. Think to strdup it before.
443 * This is a helper function to xbt_dict_set which locks the struct and
444 * strdup the key before action.
447 _xbt_dictelm_set_rec(s_xbt_dictelm_t *p_head,
452 void_f_pvoid_t *free_f) {
455 const int old_offset = offset;
457 CDEBUG6(dict_add, "--> Insert '%.*s' after '%.*s' (offset=%d) in tree %p",
459 ((p_head && p_head->key) ? p_head->key_len : 6),
460 ((p_head && p_head->key) ? p_head->key : "(head)"),
461 offset, (void*)p_head);
463 /*** The trivial cases first ***/
465 /* there is no key (we did enough recursion), change the value of head */
466 if (offset >= key_len) {
468 CDEBUG0(dict_add, "--> Change the value of head");
470 _xbt_dictelm_change_value(p_head, data, free_f);
471 xbt_free(key); /* Keep the key used in the tree */
476 /*** Search where to add this child, and how ***/
477 _xbt_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
479 CDEBUG3(dict_add, "child_search => pos=%d, offset=%d, match=%d",
484 case 0: /* no child have a common prefix */
486 s_xbt_dictelm_t *p_child = NULL;
488 _xbt_dictelm_alloc(key, key_len, offset, data, free_f, &p_child);
489 CDEBUG1(dict_add, "-> Add a child %p", (void*)p_child);
490 xbt_dynar_insert_at(p_head->sub, pos, &p_child);
495 case 1: /* A child have exactly this key => change its value*/
497 s_xbt_dictelm_t *p_child = NULL;
499 p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
500 CDEBUG1(dict_add, "-> Change the value of the child %p", (void*)p_child);
501 _xbt_dictelm_change_value(p_child, data, free_f);
508 case 2: /* A child constitutes a prefix of the key => recurse */
510 s_xbt_dictelm_t *p_child = NULL;
512 p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
513 CDEBUG2(dict_add,"-> Recurse on %p (offset=%d)", (void*)p_child, offset);
515 _xbt_dictelm_set_rec(p_child, key, key_len,
516 offset, data, free_f);
520 case 3: /* The key is a prefix of the child => child becomes child of p_new */
522 s_xbt_dictelm_t *p_new = NULL;
523 s_xbt_dictelm_t *p_child = NULL;
525 p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
526 _xbt_dictelm_alloc(key, key_len, old_offset, data, free_f, &p_new);
528 CDEBUG2(dict_add, "-> The child %p become child of new dict (%p)",
529 (void*)p_child, (void*)p_new);
531 xbt_dynar_push(p_new->sub, &p_child);
532 p_child->offset = offset;
533 xbt_dynar_set(p_head->sub, pos, &p_new);
538 case 4: /* A child share a common prefix with this key => Common ancestor */
540 s_xbt_dictelm_t *p_new = NULL;
541 s_xbt_dictelm_t *p_child = NULL;
542 s_xbt_dictelm_t *p_anc = NULL;
543 char *anc_key = NULL;
544 int anc_key_len = offset;
546 _xbt_dictelm_alloc(key, key_len, offset, data, free_f, &p_new);
547 p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
549 anc_key = xbt_memdup(key, anc_key_len);
551 _xbt_dictelm_alloc(anc_key, anc_key_len, old_offset, NULL, NULL, &p_anc);
553 CDEBUG3(dict_add, "-> Make a common ancestor %p (%.*s)",
554 (void*)p_anc, anc_key_len, anc_key);
556 if (key[offset] < p_child->key[offset]) {
557 xbt_dynar_push(p_anc->sub, &p_new);
558 xbt_dynar_push(p_anc->sub, &p_child);
560 xbt_dynar_push(p_anc->sub, &p_child);
561 xbt_dynar_push(p_anc->sub, &p_new);
564 p_child->offset = offset;
566 xbt_dynar_set(p_head->sub, pos, &p_anc);
577 * xbt_dictelm_set_ext:
579 * @head: the head of the dict
580 * @key: the key to set the new data
581 * @data: the data to add in the dict
583 * set the @data in the structure under the @key, which can be any kind
584 * of data, as long as its length is provided in @key_len.
587 xbt_dictelm_set_ext(s_xbt_dictelm_t **pp_head,
591 void_f_pvoid_t *free_f) {
592 s_xbt_dictelm_t *p_head = *pp_head;
595 key = xbt_memdup(_key, key_len);
597 /* there is no head, create it */
599 s_xbt_dictelm_t *p_child = NULL;
601 CDEBUG0(dict_add, "Create an head");
603 /* The head is priviledged by being the only one with a NULL key */
604 _xbt_dictelm_alloc(NULL, 0, 0, NULL, NULL, &p_head);
606 _xbt_dictelm_alloc(key, key_len, 0, data, free_f, &p_child);
607 xbt_dynar_insert_at(p_head->sub, 0, &p_child);
614 _xbt_dictelm_set_rec(p_head, key, key_len, 0, data, free_f);
620 * @head: the head of the dict
621 * @key: the key to set the new data
622 * @data: the data to add in the dict
624 * set the @data in the structure under the @key, which is a
625 * null terminated string.
628 xbt_dictelm_set(s_xbt_dictelm_t **pp_head,
631 void_f_pvoid_t *free_f) {
633 xbt_dictelm_set_ext(pp_head, _key, 1+strlen(_key), data, free_f);
639 * @head: the head of the dict
640 * @key: the key to find data
641 * @offset: offset on the key
642 * @data: the data that we are looking for
643 * @Returns: xbt_error
645 * Search the given @key. mismatch_error when not found.
649 _xbt_dictelm_get_rec(s_xbt_dictelm_t *p_head,
655 CDEBUG3(dict_search, "Search %.*s in %p", key_len, key, (void*)p_head);
657 /*** The trivial case first ***/
659 /* we did enough recursion, we're done */
660 if (offset >= key_len) {
661 *data = p_head->content;
670 *data = NULL; /* Make it ready to answer 'not found' in one operation */
672 /*** Search where is the good child, and how good it is ***/
673 _xbt_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
677 case 0: /* no child have a common prefix */
678 return mismatch_error;
680 case 1: /* A child have exactly this key => Got it */
682 s_xbt_dictelm_t *p_child = NULL;
684 p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
685 *data = p_child->content;
690 case 2: /* A child constitutes a prefix of the key => recurse */
692 s_xbt_dictelm_t *p_child = NULL;
694 p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
696 return _xbt_dictelm_get_rec(p_child, key, key_len, offset, data);
699 case 3: /* The key is a prefix of the child => not found */
700 return mismatch_error;
702 case 4: /* A child share a common prefix with this key => not found */
703 return mismatch_error;
712 * xbt_dictelm_get_ext:
714 * @head: the head of the dict
715 * @key: the key to find data
716 * @data: the data that we are looking for
717 * @Returns: xbt_error
719 * Search the given @key. mismatch_error when not found.
722 xbt_dictelm_get_ext(s_xbt_dictelm_t *p_head,
725 /* OUT */void **data) {
726 /* there is no head, go to hell */
728 return mismatch_error;
731 return _xbt_dictelm_get_rec(p_head, key, key_len, 0, data);
737 * @head: the head of the dict
738 * @key: the key to find data
739 * @data: the data that we are looking for
740 * @Returns: xbt_error
742 * Search the given @key. mismatch_error when not found.
745 xbt_dictelm_get(s_xbt_dictelm_t *p_head,
747 /* OUT */void **data) {
749 return xbt_dictelm_get_ext(p_head, key, 1+strlen(key), data);
752 /*----[ _xbt_dict_collapse ]------------------------------------------------*/
755 _collapse_if_need(xbt_dictelm_t head,
758 xbt_dictelm_t child = NULL;
760 CDEBUG2(dict_collapse, "Collapse %d of %p... ", pos, (void*)head);
763 /* Remove the child if |it's key| == 0 (meaning it's dead) */
764 child = xbt_dynar_get_as(head->sub, pos, xbt_dictelm_t);
766 if (offset >= child->key_len) {
768 xbt_assert0(xbt_dynar_length(child->sub) == 0,
769 "Found a dead child with grand childs. Internal error");
771 CDEBUG1(dict_collapse, "Remove dead child %p.... ", (void*)child);
772 xbt_dynar_remove_at(head->sub, pos, &child);
773 xbt_dictelm_free(&child);
778 CDEBUG0(dict_collapse, "Do not collapse the head, you stupid programm");
782 if (head->content || head->free_f ||
783 xbt_dynar_length(head->sub) != 1) {
784 CDEBUG0(dict_collapse, "Cannot collapse");
785 return; /* cannot collapse */
788 child = xbt_dynar_get_as(head->sub, 0, xbt_dictelm_t);
790 /* Get the child's key as new key */
791 CDEBUG2(dict_collapse,
792 "Do collapse with only child %.*s", child->key_len, child->key);
794 head->content = child->content;
795 head->free_f = child->free_f;
797 head->key = child->key;
798 head->key_len = child->key_len;
800 xbt_dynar_free_container(&(head->sub)) ;
802 head->sub = child->sub;
807 * _xbt_dict_remove_rec:
809 * @head: the head of the dict
810 * @key: the key of the data to be removed
811 * @offset: offset on the key
812 * @Returns: xbt_error_t
814 * Remove the entry associated with the given @key
817 _xbt_dictelm_remove_rec(xbt_dictelm_t head,
821 xbt_error_t errcode = no_error;
823 /* there is no key to search, we did enough recursion => kill current */
824 if (offset >= key_len) {
825 int killme = 0; /* do I need to suicide me ? */
827 if (head->content && head->free_f) {
828 head->free_f(head->content);
831 killme = !xbt_dynar_length(head->sub);
832 head->content = NULL;
834 _collapse_if_need(head, -1, offset);
837 DEBUG0("kill this node");
838 head->key_len = 0; /* killme. Cleanup done one step higher in recursion */
846 int old_offset = offset;
848 /*** Search where is the good child, and how good it is ***/
849 _xbt_dictelm_child_search(head, key, key_len, &pos, &offset, &match);
853 case 1: /* A child have exactly this key => recurse */
854 case 2: /* A child constitutes a prefix of the key => recurse */
857 s_xbt_dictelm_t *p_child = NULL;
859 p_child = xbt_dynar_get_as(head->sub, pos, s_xbt_dictelm_t*);
860 /*DEBUG5("Recurse on child %d of %p to remove %.*s (prefix=%d)",
861 pos, (void*)p_child, key+offset, key_len-offset,offset);*/
862 TRY(_xbt_dictelm_remove_rec(p_child, key, key_len, offset));
864 _collapse_if_need(head, pos, old_offset);
869 case 0: /* no child have a common prefix */
870 case 3: /* The key is a prefix of the child => not found */
871 case 4: /* A child share a common prefix with this key => not found */
873 return mismatch_error;
884 * xbt_dictelm_remove_ext:
886 * @head: the head of the dict
887 * @key: the key of the data to be removed
888 * @Returns: xbt_error_t
890 * Remove the entry associated with the given @key
893 xbt_dictelm_remove_ext(xbt_dictelm_t head,
896 /* there is no head, go to hell */
898 RAISE0(mismatch_error, "there is no head, go to hell");
901 return _xbt_dictelm_remove_rec(head, key, key_len, 0);
905 * xbt_dictelm_remove:
907 * @head: the head of the dict
908 * @key: the key of the data to be removed
909 * @Returns: xbt_error_t
911 * Remove the entry associated with the given @key
914 xbt_dictelm_remove(xbt_dictelm_t head,
916 return _xbt_dictelm_remove_rec(head, key, 1+strlen(key),0);
919 /*----[ _xbt_dict_dump_rec ]------------------------------------------------*/
920 /* private function to do the job of xbt_dict_dump recursively */
921 /*---------------------------------------------------------------------------*/
924 _xbt_dictelm_dump_rec(xbt_dictelm_t head,
926 void_f_pvoid_t *output) {
927 xbt_dictelm_t child = NULL;
935 printf("[%p] ", (void*)head);
938 key_len = head->key_len;
943 for (i = 0; i < offset; i++)
953 char *key_string = NULL;
955 key_string = xbt_malloc(key_len*2+1);
956 _xbt_bytes_to_string(key, key_len, key_string);
958 printf("%.*s|(%d)", key_len-offset, key_string + offset, offset);
960 xbt_free(key_string);
970 output(head->content);
979 printf(" \t\t\t[ %lu child(s) ]\n", xbt_dynar_length(head->sub));
981 xbt_dynar_foreach(head->sub, i, child)
982 _xbt_dictelm_dump_rec(child, child->offset, output);
989 * @head: the head of the dict
990 * @output: a function to dump each data in the tree
991 * @Returns: xbt_error_t
993 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
994 * function to output the data. If NULL, data won't be displayed.
998 xbt_dictelm_dump(xbt_dictelm_t head,
999 void_f_pvoid_t *output) {
1000 _xbt_dictelm_dump_rec(head, 0, output);
1004 * xbt_dictelm_print_fct:
1008 * To dump multidict, this function dumps a dict
1012 xbt_dictelm_print_fct(void *data) {
1013 printf("tree %p", (void*)data);