-
-/**
- * xbt_dictelm_set_ext:
- *
- * @head: the head of the dict
- * @key: the key to set the new data
- * @data: the data to add in the dict
- * @Returns: a gras_error
- *
- * set the @data in the structure under the @key, which can be any kind
- * of data, as long as its length is provided in @key_len.
- */
-void
-xbt_dictelm_set_ext(s_xbt_dictelm_t **pp_head,
- const char *_key,
- int key_len,
- void *data,
- void_f_pvoid_t *free_f) {
- s_xbt_dictelm_t *p_head = *pp_head;
- char *key = NULL;
-
- key = xbt_memdup(_key, key_len);
-
- /* there is no head, create it */
- if (!p_head) {
- s_xbt_dictelm_t *p_child = NULL;
-
- CDEBUG0(dict_add, "Create an head");
-
- /* The head is priviledged by being the only one with a NULL key */
- _xbt_dictelm_alloc(NULL, 0, 0, NULL, NULL, &p_head);
-
- _xbt_dictelm_alloc(key, key_len, 0, data, free_f, &p_child);
- xbt_dynar_insert_at(p_head->sub, 0, &p_child);
-
- *pp_head = p_head;
-
- return;
- }
-
- _xbt_dictelm_set_rec(p_head, key, key_len, 0, data, free_f);
-}
-
-/**
- * xbt_dictelm_set:
- *
- * @head: the head of the dict
- * @key: the key to set the new data
- * @data: the data to add in the dict
- * @Returns: a gras_error
- *
- * set the @data in the structure under the @key, which is a
- * null terminated string.
- */
-void
-xbt_dictelm_set(s_xbt_dictelm_t **pp_head,
- const char *_key,
- void *data,
- void_f_pvoid_t *free_f) {
-
- xbt_dictelm_set_ext(pp_head, _key, 1+strlen(_key), data, free_f);
-}
-
-/**
- * _xbt_dict_get_rec:
- *
- * @head: the head of the dict
- * @key: the key to find data
- * @offset: offset on the key
- * @data: the data that we are looking for
- * @Returns: gras_error
- *
- * Search the given @key. mismatch_error when not found.
- */
-static
-xbt_error_t
-_xbt_dictelm_get_rec(s_xbt_dictelm_t *p_head,
- const char *key,
- int key_len,
- int offset,
- void **data) {
- void *res;
-
- CDEBUG3(dict_search, "Search %.*s in %p", key_len, key, (void*)p_head);
-
- /*** The trivial case first ***/
-
- /* we did enough recursion, we're done */
- if (offset >= key_len) {
- *data = p_head->content;
-
- return no_error;
- }
-
- {
- int match = 0;
- int pos = 0;
-
- *data = NULL; /* Make it ready to answer 'not found' in one operation */
-
- /*** Search where is the good child, and how good it is ***/
- _xbt_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
-
- switch (match) {
-
- case 0: /* no child have a common prefix */
- return mismatch_error;
-
- case 1: /* A child have exactly this key => Got it */
- {
- s_xbt_dictelm_t *p_child = NULL;
-
- p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
- *data = p_child->content;
-
- return no_error;
- }
-
- case 2: /* A child constitutes a prefix of the key => recurse */
- {
- s_xbt_dictelm_t *p_child = NULL;
-
- p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
-
- return _xbt_dictelm_get_rec(p_child, key, key_len, offset, data);
- }
-
- case 3: /* The key is a prefix of the child => not found */
- return mismatch_error;
-
- case 4: /* A child share a common prefix with this key => not found */
- return mismatch_error;
-
- default:
- RAISE_IMPOSSIBLE;
- }
- }
-}
-
-/**
- * xbt_dictelm_get_ext:
- *
- * @head: the head of the dict
- * @key: the key to find data
- * @data: the data that we are looking for
- * @Returns: gras_error
- *
- * Search the given @key. mismatch_error when not found.
- */
-xbt_error_t
-xbt_dictelm_get_ext(s_xbt_dictelm_t *p_head,
- const char *key,
- int key_len,
- /* OUT */void **data) {
- /* there is no head, go to hell */
- if (!p_head) {
- return mismatch_error;
- }
-
- return _xbt_dictelm_get_rec(p_head, key, key_len, 0, data);
-}
-
-/**
- * xbt_dictelm_get:
- *
- * @head: the head of the dict
- * @key: the key to find data
- * @data: the data that we are looking for
- * @Returns: gras_error
- *
- * Search the given @key. mismatch_error when not found.
- */
-xbt_error_t
-xbt_dictelm_get(s_xbt_dictelm_t *p_head,
- const char *key,
- /* OUT */void **data) {
-
- return xbt_dictelm_get_ext(p_head, key, 1+strlen(key), data);
-}
-
-/*----[ _xbt_dict_collapse ]------------------------------------------------*/
-static _XBT_INLINE
-void
-_collapse_if_need(xbt_dictelm_t head,
- int pos,
- int offset) {
- xbt_dictelm_t child = NULL;
-
- CDEBUG2(dict_collapse, "Collapse %d of %p... ", pos, (void*)head);
-
- if (pos >= 0) {
- /* Remove the child if |it's key| == 0 (meaning it's dead) */
- child = xbt_dynar_get_as(head->sub, pos, xbt_dictelm_t);
-
- if (offset >= child->key_len) {
-
- xbt_assert0(xbt_dynar_length(child->sub) == 0,
- "Found a dead child with grand childs. Internal error");
-
- CDEBUG1(dict_collapse, "Remove dead child %p.... ", (void*)child);
- xbt_dynar_remove_at(head->sub, pos, &child);
- xbt_dictelm_free(&child);
- }
- }
-
- if (!head->key) {
- CDEBUG0(dict_collapse, "Do not collapse the head, you stupid programm");
- return;
- }
-
- if (head->content || head->free_f ||
- xbt_dynar_length(head->sub) != 1) {
- CDEBUG0(dict_collapse, "Cannot collapse");
- return; /* cannot collapse */
- }
-
- child = xbt_dynar_get_as(head->sub, 0, xbt_dictelm_t);
-
- /* Get the child's key as new key */
- CDEBUG2(dict_collapse,
- "Do collapse with only child %.*s", child->key_len, child->key);
-
- head->content = child->content;
- head->free_f = child->free_f;
- xbt_free(head->key);
- head->key = child->key;
- head->key_len = child->key_len;
-
- xbt_dynar_free_container(&(head->sub)) ;
-
- head->sub = child->sub;
- xbt_free(child);
-}
-
-/**
- * _xbt_dict_remove_rec:
- *
- * @head: the head of the dict
- * @key: the key of the data to be removed
- * @offset: offset on the key
- * @Returns: xbt_error_t
- *
- * Remove the entry associated with the given @key
- */
-xbt_error_t
-_xbt_dictelm_remove_rec(xbt_dictelm_t head,
- const char *key,
- int key_len,
- int offset) {
- xbt_error_t errcode = no_error;
-
- /* there is no key to search, we did enough recursion => kill current */
- if (offset >= key_len) {
- int killme = 0; /* do I need to suicide me ? */
-
- if (head->content && head->free_f) {
- head->free_f(head->content);
- }
-
- killme = !xbt_dynar_length(head->sub);
- head->content = NULL;
- head->free_f = NULL;
- _collapse_if_need(head, -1, offset);
-
- if (killme) {
- DEBUG0("kill this node");
- head->key_len = 0; /* killme. Cleanup done one step higher in recursion */
- }
-
- return errcode;
-
- } else {
- int match = 0;
- int pos = 0;
- int old_offset = offset;
-
- /*** Search where is the good child, and how good it is ***/
- _xbt_dictelm_child_search(head, key, key_len, &pos, &offset, &match);
-
- switch (match) {
-
- case 1: /* A child have exactly this key => recurse */
- case 2: /* A child constitutes a prefix of the key => recurse */
-
- {
- s_xbt_dictelm_t *p_child = NULL;
-
- p_child = xbt_dynar_get_as(head->sub, pos, s_xbt_dictelm_t*);
- /*DEBUG5("Recurse on child %d of %p to remove %.*s (prefix=%d)",
- pos, (void*)p_child, key+offset, key_len-offset,offset);*/
- TRY(_xbt_dictelm_remove_rec(p_child, key, key_len, offset));
-
- _collapse_if_need(head, pos, old_offset);
- return no_error;
- }
-
-
- case 0: /* no child have a common prefix */
- case 3: /* The key is a prefix of the child => not found */
- case 4: /* A child share a common prefix with this key => not found */
-
- return mismatch_error;
-
-
- default:
- RAISE_IMPOSSIBLE;
-
- }
- }
-}
-
-/**
- * xbt_dictelm_remove_ext:
- *
- * @head: the head of the dict
- * @key: the key of the data to be removed
- * @Returns: xbt_error_t
- *
- * Remove the entry associated with the given @key
- */
-xbt_error_t
-xbt_dictelm_remove_ext(xbt_dictelm_t head,
- const char *key,
- int key_len) {
- /* there is no head, go to hell */
- if (!head) {
- RAISE0(mismatch_error, "there is no head, go to hell");
- }
-
- return _xbt_dictelm_remove_rec(head, key, key_len, 0);
-}
-
-/**
- * xbt_dictelm_remove:
- *
- * @head: the head of the dict
- * @key: the key of the data to be removed
- * @Returns: xbt_error_t
- *
- * Remove the entry associated with the given @key
- */
-xbt_error_t
-xbt_dictelm_remove(xbt_dictelm_t head,
- const char *key) {
- return _xbt_dictelm_remove_rec(head, key, 1+strlen(key),0);
-}
-
-/*----[ _xbt_dict_dump_rec ]------------------------------------------------*/
-/* private function to do the job of xbt_dict_dump recursively */
-/*---------------------------------------------------------------------------*/
-static
-void
-_xbt_dictelm_dump_rec(xbt_dictelm_t head,
- int offset,
- void_f_pvoid_t *output) {
- xbt_dictelm_t child = NULL;
- char *key = NULL;
- int key_len = 0;
- int i = 0;
-
- if (!head)
- return;
-
- printf("[%p] ", (void*)head);
-
- key = head->key;
- key_len = head->key_len;
-
- if (key_len)
- printf (" ");
-
- for (i = 0; i < offset; i++)
- printf("-");
-
- fflush(stdout);
-
- if (key) {
-
- if (!key_len) {
- printf ("HEAD");
- } else {
- char *key_string = NULL;
-
- key_string = xbt_malloc(key_len*2+1);
- _xbt_bytes_to_string(key, key_len, key_string);
-
- printf("%.*s|(%d)", key_len-offset, key_string + offset, offset);
-
- xbt_free(key_string);
- }
-
- }
-
- printf(" -> ");
-
- if (head->content) {
-
- if (output) {
- output(head->content);
- } else {
- printf("(data)");
- }
-
- } else {
- printf("(null)");
- }
-
- printf(" \t\t\t[ %lu child(s) ]\n", xbt_dynar_length(head->sub));
-
- xbt_dynar_foreach(head->sub, i, child)
- _xbt_dictelm_dump_rec(child, child->offset, output);
-
-}
-
-/**
- * xbt_dictelm_dump:
- *
- * @head: the head of the dict
- * @output: a function to dump each data in the tree
- * @Returns: xbt_error_t
- *
- * Ouputs the content of the structure. (for debuging purpose). @ouput is a
- * function to output the data. If NULL, data won't be displayed.
- */
-
-void
-xbt_dictelm_dump(xbt_dictelm_t head,
- void_f_pvoid_t *output) {
- _xbt_dictelm_dump_rec(head, 0, output);
-}
-
-/**
- * xbt_dictelm_print_fct:
- *
- * @data:
- *
- * To dump multidict, this function dumps a dict
- */
-
-void
-xbt_dictelm_print_fct(void *data) {
- printf("tree %p", (void*)data);
-}
-