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,
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,
133 void_f_pvoid_t *free_f,
134 /*OUT*/s_xbt_dictelm_t **pp_elm) {
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->internal = internal;
143 p_elm->content = data;
144 p_elm->free_f = free_f;
145 p_elm->sub = xbt_dynar_new(sizeof(s_xbt_dictelm_t*), _dictelm_wrapper_free);
154 * @pp_elm: the dict elem to be freed
156 * Frees a dictionnary element with all its childs.
159 xbt_dictelm_free(s_xbt_dictelm_t **pp_elm) {
161 s_xbt_dictelm_t *p_elm = *pp_elm;
163 xbt_dynar_free(&(p_elm->sub));
169 if (p_elm->free_f && p_elm->content) {
170 p_elm->free_f(p_elm->content);
179 * _dictelm_wrapper_free:
181 * a wrapper to free dictelm with the right prototype to be usable within dynar
185 _dictelm_wrapper_free(void *pp_elm) {
186 DEBUG3("Free dictelm '%.*s' %p",
187 (*(s_xbt_dictelm_t**)pp_elm)->key_len, (*(s_xbt_dictelm_t**)pp_elm)->key,
189 xbt_dictelm_free((s_xbt_dictelm_t**)pp_elm);
192 /*####[ utility functions ]##################################################*/
197 * Returns the length of the common prefix of @str1 and @str2.
198 * Do make sure the strings are not null, this function don't
202 _str_prefix_lgr(const char *key1,
208 const int old_offset = *p_offset;
214 /*CDEBUG5(dict_search, "%s: [%.*s] <=> [%.*s]", __FUNCTION__,
215 key1,key_len1,key2,key_len2);*/
217 if (o < key_len1 && o < key_len2) {
219 while (key1[o] == key2[o]) {
222 if (!(o < key_len1 && o < key_len2))
230 if (o != old_offset) {
235 m = 1; /* exact match */
237 m = 2; /* child is prefix */
240 } else if (o >= key_len2) {
241 m = 3; /* key prefix of child */
243 DEBUG7("Common prefix. o=%d; key1=%.*s; key_len1=%d; key2=%.*s; key_len2=%d",
249 m = 4; /* Common prefix (=> common ancestor) */
259 * _dictelm_child_cmp:
261 * Compares two dictelm keys and return their matching (using the same
262 * convention than @_xbt_dict_child_search() )
266 _dict_child_cmp(s_xbt_dictelm_t *p_dict,
273 s_xbt_dictelm_t *p_child = NULL;
278 p_child = xbt_dynar_get_as(p_dict->sub, pos, s_xbt_dictelm_t*);
280 /* Compute the length of the prefix
281 and if the searched key is before or after cur */
282 _str_prefix_lgr(p_child->key, p_child->key_len,
287 if (m) /* found, get out */
290 if (o < p_child->key_len && (o >= key_len || key[o] < p_child->key[o])) {
296 CDEBUG6(dict_search, "Cmp '%.*s' and '%.*s' (offset=%d) => %d",
297 p_child->key_len - *p_offset, p_child->key + *p_offset,
298 key_len - *p_offset, key + *p_offset,
308 * _xbt_dict_child_search:
310 * Search where would be inserted @key between the childs of @p_elm.
312 * Returns position of the child having a common prefix with this key
313 * If *match==0, no child have a common prefix
314 * *pos is where to add the key
315 * If *match==1, A child (located at *pos) have exactly this key
316 * If *match==2, A child (located at *pos) constitutes a prefix of the key
317 * the recursion have to go on that guy
318 * *prefix = the size of the key eaten at this level
319 * If *match==3 The key is a prefix of the child at *pos
320 * If *match==4, A child (loc. at *pos) share a common prefix with this key
321 * *prefix = size of the prefix.
322 * If searching, that's a mismatch.
323 * If inserting, you have to break the child and create an
324 * internal node having {child, key} as childs
325 * offset is used in input and output. In input, that's the length of the key
326 * handled by previous levels of recursion. In output, that the one counting
331 _xbt_dictelm_child_search(s_xbt_dictelm_t *p_elm,
344 CDEBUG6(dict_search, "search child [%.*s] under [%.*s]=%p (len=%lu)",
346 p_elm ? (p_elm->key_len?p_elm->key_len:6) : 6,
347 p_elm ? (p_elm->key?p_elm->key:"(NULL)") : "(head)",
349 (p_elm&&p_elm->sub) ? xbt_dynar_length(p_elm->sub) : 0);
352 len = xbt_dynar_length(p_elm->sub);
363 _dict_child_cmp(p_elm, p_min, key, key_len, &o, &m, &cmp);
364 if(!m) { /* OK, maybe it is somewhere else. */
366 if (cmp<0) { /* Insert at the very beginning */
368 } else if (p_max<=0) { /* No way. It is not there. Insert at the very end */
373 _dict_child_cmp(p_elm, p_max, key, key_len, &o, &m, &cmp);
376 if(cmp>0) { /* Should be located at the end of the table */
378 } else { /* Too bad, let's go for a dichotomic search. */
379 while(p_max-p_min>1) {
380 _dict_child_cmp(p_elm, (p_min+p_max)/2, key, key_len, &o, &m, &cmp);
383 if(cmp<0) p_max=(p_min+p_max)/2;
384 if(cmp>0) p_min=(p_min+p_max)/2;
386 if(m) /* We have the element */
388 else /* it should be inserted just after p_min */
400 CDEBUG6(dict_search, "search [%.*s] in [%.*s]=%p => %s",
402 p_elm?(p_elm->key_len?p_elm->key_len:6):6, p_elm?(p_elm->key?p_elm->key:"(null)"):"(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. The node also become a non-internal one.
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;
430 p_elm->internal = FALSE;
434 * _xbt_dictelm_set_rec:
436 * @head: the head of the dict
437 * @key: the key to set the new data
438 * @offset: offset on key.
439 * @data: the data to add in the dict
441 * set the @data in the structure under the @key. The @key is destroyed
442 * in the process. Think to strdup it before.
444 * This is a helper function to xbt_dict_set which locks the struct and
445 * strdup the key before action.
448 _xbt_dictelm_set_rec(s_xbt_dictelm_t *p_head,
453 void_f_pvoid_t *free_f) {
456 const int old_offset = offset;
458 CDEBUG6(dict_add, "--> Insert '%.*s' after '%.*s' (offset=%d) in tree %p",
460 ((p_head && p_head->key) ? p_head->key_len : 6),
461 ((p_head && p_head->key) ? p_head->key : "(head)"),
462 offset, (void*)p_head);
464 /*** The trivial cases first ***/
466 /* there is no key (we did enough recursion), change the value of head */
467 if (offset >= key_len) {
469 CDEBUG0(dict_add, "--> Change the value of head");
471 _xbt_dictelm_change_value(p_head, data, free_f);
472 free(key); /* Keep the key used in the tree */
477 /*** Search where to add this child, and how ***/
478 _xbt_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
480 CDEBUG3(dict_add, "child_search => pos=%d, offset=%d, match=%d",
485 case 0: /* no child have a common prefix */
487 s_xbt_dictelm_t *p_child = NULL;
489 _xbt_dictelm_alloc(key, key_len, offset, FALSE, data, free_f, &p_child);
490 CDEBUG1(dict_add, "-> Add a child %p", (void*)p_child);
491 xbt_dynar_insert_at(p_head->sub, pos, &p_child);
496 case 1: /* A child have exactly this key => change its value*/
498 s_xbt_dictelm_t *p_child = NULL;
500 p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
501 CDEBUG1(dict_add, "-> Change the value of the child %p", (void*)p_child);
502 _xbt_dictelm_change_value(p_child, data, free_f);
509 case 2: /* A child constitutes a prefix of the key => recurse */
511 s_xbt_dictelm_t *p_child = NULL;
513 p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
514 CDEBUG2(dict_add,"-> Recurse on %p (offset=%d)", (void*)p_child, offset);
516 _xbt_dictelm_set_rec(p_child, key, key_len,
517 offset, data, free_f);
521 case 3: /* The key is a prefix of the child => child becomes child of p_new */
523 s_xbt_dictelm_t *p_new = NULL;
524 s_xbt_dictelm_t *p_child = NULL;
526 p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
527 _xbt_dictelm_alloc(key, key_len, old_offset, FALSE, data, free_f, &p_new);
529 CDEBUG2(dict_add, "-> The child %p become child of new dict (%p)",
530 (void*)p_child, (void*)p_new);
532 xbt_dynar_push(p_new->sub, &p_child);
533 p_child->offset = offset;
534 xbt_dynar_set(p_head->sub, pos, &p_new);
539 case 4: /* A child share a common prefix with this key => Common ancestor */
541 s_xbt_dictelm_t *p_new = NULL;
542 s_xbt_dictelm_t *p_child = NULL;
543 s_xbt_dictelm_t *p_anc = NULL;
544 char *anc_key = NULL;
545 int anc_key_len = offset;
547 _xbt_dictelm_alloc(key, key_len, offset, FALSE, data, free_f, &p_new);
548 p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
550 anc_key = xbt_memdup(key, anc_key_len);
552 _xbt_dictelm_alloc(anc_key, anc_key_len, old_offset, TRUE, NULL, NULL, &p_anc);
554 CDEBUG3(dict_add, "-> Make a common ancestor %p (%.*s)",
555 (void*)p_anc, anc_key_len, anc_key);
557 if (key[offset] < p_child->key[offset]) {
558 xbt_dynar_push(p_anc->sub, &p_new);
559 xbt_dynar_push(p_anc->sub, &p_child);
561 xbt_dynar_push(p_anc->sub, &p_child);
562 xbt_dynar_push(p_anc->sub, &p_new);
565 p_child->offset = offset;
567 xbt_dynar_set(p_head->sub, pos, &p_anc);
578 * xbt_dictelm_set_ext:
580 * @head: the head of the dict
581 * @key: the key to set the new data
582 * @data: the data to add in the dict
584 * set the @data in the structure under the @key, which can be any kind
585 * of data, as long as its length is provided in @key_len.
588 xbt_dictelm_set_ext(s_xbt_dictelm_t **pp_head,
592 void_f_pvoid_t *free_f) {
593 s_xbt_dictelm_t *p_head = *pp_head;
596 key = xbt_memdup(_key, key_len+1);
598 /* there is no head, create it */
600 s_xbt_dictelm_t *p_child = NULL;
602 CDEBUG0(dict_add, "Create an head");
604 /* The head is priviledged by being the only one with a NULL key */
605 _xbt_dictelm_alloc(NULL, 0, 0, TRUE, NULL, NULL, &p_head);
607 CDEBUG2(dict_add, "Push %.*s as child of this head",key_len,key);
608 _xbt_dictelm_alloc(key, key_len, 0, FALSE, 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, 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,
657 CDEBUG3(dict_search, "Search %.*s in %p", key_len, key, (void*)p_head);
659 /*** The trivial case first ***/
661 /* we did enough recursion, we're done */
662 if (offset >= key_len) {
663 *data = p_head->content;
672 *data = NULL; /* Let's be clean */
674 /*** Search where is the good child, and how good it is ***/
675 _xbt_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
679 case 0: /* no child have a common prefix */
680 return mismatch_error;
682 case 1: /* A child have exactly this key => Got it */
684 s_xbt_dictelm_t *p_child = NULL;
686 p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
687 *data = p_child->content;
692 case 2: /* A child constitutes a prefix of the key => recurse */
694 s_xbt_dictelm_t *p_child = NULL;
696 p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
698 return _xbt_dictelm_get_rec(p_child, key, key_len, offset, data);
701 case 3: /* The key is a prefix of the child => not found */
702 return mismatch_error;
704 case 4: /* A child share a common prefix with this key => not found */
705 return mismatch_error;
714 * xbt_dictelm_get_ext:
716 * @head: the head of the dict
717 * @key: the key to find data
718 * @data: the data that we are looking for
719 * @Returns: xbt_error
721 * Search the given @key. mismatch_error when not found.
724 xbt_dictelm_get_ext(s_xbt_dictelm_t *p_head,
727 /* OUT */void **data) {
728 /* there is no head, go to hell */
730 return mismatch_error;
733 return _xbt_dictelm_get_rec(p_head, key, key_len, 0, data);
739 * @head: the head of the dict
740 * @key: the key to find data
741 * @data: the data that we are looking for
742 * @Returns: xbt_error
744 * Search the given @key. mismatch_error when not found.
747 xbt_dictelm_get(s_xbt_dictelm_t *p_head,
749 /* OUT */void **data) {
751 return xbt_dictelm_get_ext(p_head, key, strlen(key), data);
754 /*----[ _xbt_dict_collapse ]------------------------------------------------*/
757 _collapse_if_need(xbt_dictelm_t head,
760 xbt_dictelm_t child = NULL;
762 CDEBUG2(dict_collapse, "Collapse %d of %p... ", pos, (void*)head);
765 /* Remove the child if |it's key| == 0 (meaning it's dead) */
766 child = xbt_dynar_get_as(head->sub, pos, xbt_dictelm_t);
768 if (offset >= child->key_len) {
770 xbt_assert0(xbt_dynar_length(child->sub) == 0,
771 "Found a dead child with grand childs. Internal error");
773 CDEBUG1(dict_collapse, "Remove dead child %p.... ", (void*)child);
774 xbt_dynar_remove_at(head->sub, pos, &child);
775 xbt_dictelm_free(&child);
780 CDEBUG0(dict_collapse, "Do not collapse the head, you stupid programm");
784 if (head->content || head->free_f ||
785 xbt_dynar_length(head->sub) != 1) {
786 CDEBUG0(dict_collapse, "Cannot collapse");
787 return; /* cannot collapse */
790 child = xbt_dynar_get_as(head->sub, 0, xbt_dictelm_t);
792 /* Get the child's key as new key */
793 CDEBUG2(dict_collapse,
794 "Do collapse with only child %.*s", child->key_len, child->key);
796 head->content = child->content;
797 head->free_f = child->free_f;
799 head->key = child->key;
800 head->key_len = child->key_len;
802 xbt_dynar_free_container(&(head->sub)) ;
804 head->sub = child->sub;
809 * _xbt_dict_remove_rec:
811 * @head: the head of the dict
812 * @key: the key of the data to be removed
813 * @offset: offset on the key
814 * @Returns: xbt_error_t
816 * Remove the entry associated with the given @key
819 _xbt_dictelm_remove_rec(xbt_dictelm_t head,
823 xbt_error_t errcode = no_error;
825 /* there is no key to search, we did enough recursion => kill current */
826 if (offset >= key_len) {
827 int killme = 0; /* do I need to suicide me ? */
829 if (head->content && head->free_f) {
830 head->free_f(head->content);
833 killme = !xbt_dynar_length(head->sub);
834 head->content = NULL;
836 _collapse_if_need(head, -1, offset);
839 DEBUG0("kill this node");
840 head->key_len = 0; /* killme. Cleanup done one step higher in recursion */
848 int old_offset = offset;
850 /*** Search where is the good child, and how good it is ***/
851 _xbt_dictelm_child_search(head, key, key_len, &pos, &offset, &match);
855 case 1: /* A child have exactly this key => recurse */
856 case 2: /* A child constitutes a prefix of the key => recurse */
859 s_xbt_dictelm_t *p_child = NULL;
861 p_child = xbt_dynar_get_as(head->sub, pos, s_xbt_dictelm_t*);
862 /*DEBUG5("Recurse on child %d of %p to remove %.*s (prefix=%d)",
863 pos, (void*)p_child, key+offset, key_len-offset,offset);*/
864 TRY(_xbt_dictelm_remove_rec(p_child, key, key_len, offset));
866 _collapse_if_need(head, pos, old_offset);
871 case 0: /* no child have a common prefix */
872 case 3: /* The key is a prefix of the child => not found */
873 case 4: /* A child share a common prefix with this key => not found */
875 return mismatch_error;
886 * xbt_dictelm_remove_ext:
888 * @head: the head of the dict
889 * @key: the key of the data to be removed
890 * @Returns: xbt_error_t
892 * Remove the entry associated with the given @key
895 xbt_dictelm_remove_ext(xbt_dictelm_t head,
898 /* there is no head, go to hell */
900 RAISE0(mismatch_error, "there is no head, go to hell");
903 return _xbt_dictelm_remove_rec(head, key, key_len, 0);
907 * xbt_dictelm_remove:
909 * @head: the head of the dict
910 * @key: the key of the data to be removed
911 * @Returns: xbt_error_t
913 * Remove the entry associated with the given @key
916 xbt_dictelm_remove(xbt_dictelm_t head,
918 return _xbt_dictelm_remove_rec(head, key, strlen(key),0);
921 /*----[ _xbt_dict_dump_rec ]------------------------------------------------*/
922 /* private function to do the job of xbt_dict_dump recursively */
923 /*---------------------------------------------------------------------------*/
926 _xbt_dictelm_dump_rec(xbt_dictelm_t head,
928 void_f_pvoid_t *output) {
929 xbt_dictelm_t child = NULL;
937 printf("[%p] ", (void*)head);
940 key_len = head->key_len;
945 for (i = 0; i < offset; i++)
954 } else if (key[key_len] != '\0') {
955 char *key_string = NULL;
957 key_string = xbt_malloc(key_len*2+1);
958 _xbt_bytes_to_string(key, key_len, key_string);
960 printf("%.*s|(%d)", key_len-2*offset, key_string + 2*offset, offset);
964 printf("%.*s|(%d)", key_len-offset, key + offset , offset);
973 output(head->content);
978 } else if (head->internal) {
979 printf("(internal node)");
984 printf(" \t\t\t[ %lu child(s) ]\n", xbt_dynar_length(head->sub));
986 xbt_dynar_foreach(head->sub, i, child)
987 _xbt_dictelm_dump_rec(child, child->offset, output);
994 * @head: the head of the dict
995 * @output: a function to dump each data in the tree
996 * @Returns: xbt_error_t
998 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
999 * function to output the data. If NULL, data won't be displayed.
1003 xbt_dictelm_dump(xbt_dictelm_t head,
1004 void_f_pvoid_t *output) {
1005 _xbt_dictelm_dump_rec(head, 0, output);
1009 * xbt_dictelm_print_fct:
1013 * To dump multidict, this function dumps a dict
1017 xbt_dictelm_print_fct(void *data) {
1018 printf("tree %p", (void*)data);