Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
there is now a flush function
[simgrid.git] / src / xbt / dict_elm.c
index 162f7dc..29f06e9 100644 (file)
@@ -50,13 +50,13 @@ static gras_error_t _gras_dictelm_dump_rec(gras_dictelm_t *head,
 
 
 
-static gras_error_t _gras_dictelm_insert_rec(gras_dictelm_t *head,
+static gras_error_t _gras_dictelm_set_rec(gras_dictelm_t *head,
                                             char           *key,
                                             int             key_len,
                                             int             offset,
                                             void           *data,
                                             void_f_pvoid_t *free_ctn);
-static gras_error_t _gras_dictelm_retrieve_rec(gras_dictelm_t *head,
+static gras_error_t _gras_dictelm_get_rec(gras_dictelm_t *head,
                                               const char     *key,
                                               int             key_len,
                                               int             offset,
@@ -211,7 +211,9 @@ gras_dictelm_free(gras_dictelm_t **pp_elm)  {
 static
 void
 _dictelm_wrapper_free(void *pp_elm) {
-  DEBUG2("Free dictelm %p (*=%p)", pp_elm, *(void**)pp_elm);
+  DEBUG3("Free dictelm '%.*s' %p", 
+        (*(gras_dictelm_t**)pp_elm)->key_len, (*(gras_dictelm_t**)pp_elm)->key,
+        *(void**)pp_elm);
   gras_dictelm_free((gras_dictelm_t**)pp_elm);
 }
 
@@ -237,7 +239,8 @@ _str_prefix_lgr(const char *key1,
 
   m = 0;
 
-  /*CDEBUG3(dict_search, "%s: [%s] <=> [%s]", __FUNCTION__, s1, s2);*/
+  /*CDEBUG5(dict_search, "%s: [%.*s] <=> [%.*s]", __FUNCTION__, 
+            key1,key_len1,key2,key_len2);*/
 
   if (o < key_len1  &&  o < key_len2) {
 
@@ -312,8 +315,10 @@ _dict_child_cmp(gras_dictelm_t *p_dict,
     cmp =  1;
   }
 
-  CDEBUG3(dict_search, "Cmp %s and %s => %d", p_child->key + *p_offset,
-         key + *p_offset, cmp);
+  CDEBUG6(dict_search, "Cmp '%.*s' and '%.*s' (offset=%d) => %d", 
+         p_child->key_len - *p_offset, p_child->key + *p_offset,
+         key_len - *p_offset, key + *p_offset,
+         *p_offset,cmp);
 
  end:
   *p_offset = o;
@@ -358,9 +363,9 @@ _gras_dictelm_child_search(gras_dictelm_t *p_elm,
   int          len     = 0;
 
   
-  CDEBUG3(dict_search, "search child [%s] under [%s] (len=%d)",
-         key,
-          p_elm?p_elm->key:"(head)",
+  CDEBUG5(dict_search, "search child [%.*s] under [%.*s] (len=%d)",
+         key_len, key,
+          p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
          (p_elm&&p_elm->sub)?gras_dynar_length(p_elm->sub):0);
   
 
@@ -381,9 +386,9 @@ _gras_dictelm_child_search(gras_dictelm_t *p_elm,
   *p_offset = o;
   *p_pos    = p;
   *p_match  = m;
-  CDEBUG3(dict_search, "search [%s] in [%s] => %s",
-         key,
-          p_elm?p_elm->key:"(head)",
+  CDEBUG5(dict_search, "search [%.*s] in [%.*s] => %s",
+         key_len, key,
+          p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
          ( m == 0 ? "no child have a common prefix" :
            ( m == 1 ? "selected child have exactly this key" :
              ( m == 2 ? "selected child constitutes a prefix" :
@@ -413,22 +418,22 @@ _gras_dictelm_change_value(gras_dictelm_t    *p_elm,
 }
 
 /**
- * _gras_dictelm_insert_rec:
+ * _gras_dictelm_set_rec:
  *
  * @head: the head of the dict
- * @key: the key to insert the new data
+ * @key: the key to set the new data
  * @offset: offset on key.
  * @data: the data to add in the dict
  * @Returns: a gras_error
  *
- * Insert the @data in the structure under the @key. The @key is destroyed
+ * set the @data in the structure under the @key. The @key is destroyed
  * in the process. Think to strdup it before.
  *
- * This is a helper function to gras_dict_insert which locks the struct and
+ * This is a helper function to gras_dict_set which locks the struct and
  * strdup the key before action. 
  */
 gras_error_t
-_gras_dictelm_insert_rec(gras_dictelm_t     *p_head,
+_gras_dictelm_set_rec(gras_dictelm_t     *p_head,
                         char            *key,
                         int              key_len,
                         int              offset,
@@ -439,9 +444,11 @@ _gras_dictelm_insert_rec(gras_dictelm_t     *p_head,
   int          pos        = 0;
   const int    old_offset = offset;
 
-  CDEBUG4(dict_add, "--> Insert '%s' after '%s' (offset=%d) in tree %p",
-         key, ((p_head && p_head->key) ? p_head->key : "(head)"), offset,
-         p_head);
+  CDEBUG6(dict_add, "--> Insert '%.*s' after '%.*s' (offset=%d) in tree %p",
+         key_len, key, 
+         ((p_head && p_head->key) ? p_head->key_len : 6),
+         ((p_head && p_head->key) ? p_head->key : "(head)"), 
+         offset, p_head);
 
   /*** The trivial cases first ***/
 
@@ -495,7 +502,7 @@ _gras_dictelm_insert_rec(gras_dictelm_t     *p_head,
       gras_dynar_get(p_head->sub, pos, &p_child);
       CDEBUG2(dict_add,"-> Recurse on %p (offset=%d)", p_child, offset);
 
-      return _gras_dictelm_insert_rec(p_child, key, key_len, 
+      return _gras_dictelm_set_rec(p_child, key, key_len, 
                                      offset, data, free_ctn);
     }
 
@@ -533,7 +540,8 @@ _gras_dictelm_insert_rec(gras_dictelm_t     *p_head,
       TRY(_gras_dictelm_alloc(anc_key, anc_key_len, old_offset, 
                              NULL, NULL, &p_anc));
 
-      CDEBUG2(dict_add, "-> Make a common ancestor %p (%s)", p_anc, anc_key);
+      CDEBUG3(dict_add, "-> Make a common ancestor %p (%.*s)",
+             p_anc, anc_key_len, anc_key);
 
       if (key[offset] < p_child->key[offset]) {
         TRY(gras_dynar_push(p_anc->sub, &p_new));
@@ -557,18 +565,18 @@ _gras_dictelm_insert_rec(gras_dictelm_t     *p_head,
 }
 
 /**
- * gras_dictelm_insert_ext:
+ * gras_dictelm_set_ext:
  *
  * @head: the head of the dict
- * @key: the key to insert the new data
+ * @key: the key to set the new data
  * @data: the data to add in the dict
  * @Returns: a gras_error
  *
- * Insert the @data in the structure under the @key, which can be any kind 
+ * 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.
  */
 gras_error_t
-gras_dictelm_insert_ext(gras_dictelm_t **pp_head,
+gras_dictelm_set_ext(gras_dictelm_t **pp_head,
                        const char      *_key,
                        int              key_len,
                        void            *data,
@@ -598,31 +606,31 @@ gras_dictelm_insert_ext(gras_dictelm_t **pp_head,
     return errcode;
   }
 
-  return _gras_dictelm_insert_rec(p_head, key, key_len, 0, data, free_ctn);
+  return _gras_dictelm_set_rec(p_head, key, key_len, 0, data, free_ctn);
 }
 
 /**
- * gras_dictelm_insert:
+ * gras_dictelm_set:
  *
  * @head: the head of the dict
- * @key: the key to insert the new data
+ * @key: the key to set the new data
  * @data: the data to add in the dict
  * @Returns: a gras_error
  *
- * Insert the @data in the structure under the @key, which is a 
+ * set the @data in the structure under the @key, which is a 
  * null terminated string.
  */
 gras_error_t
-gras_dictelm_insert(gras_dictelm_t **pp_head,
+gras_dictelm_set(gras_dictelm_t **pp_head,
                    const char      *_key,
                    void            *data,
                    void_f_pvoid_t  *free_ctn) {
 
-  return gras_dictelm_insert_ext(pp_head, _key, 1+strlen(_key), data, free_ctn);
+  return gras_dictelm_set_ext(pp_head, _key, 1+strlen(_key), data, free_ctn);
 }
 
 /**
- * _gras_dict_retrieve_rec:
+ * _gras_dict_get_rec:
  *
  * @head: the head of the dict
  * @key: the key to find data
@@ -634,7 +642,7 @@ gras_dictelm_insert(gras_dictelm_t **pp_head,
  */
 static 
 gras_error_t
-_gras_dictelm_retrieve_rec(gras_dictelm_t *p_head,
+_gras_dictelm_get_rec(gras_dictelm_t *p_head,
                           const char     *key,
                           int             key_len,
                           int             offset,
@@ -642,7 +650,7 @@ _gras_dictelm_retrieve_rec(gras_dictelm_t *p_head,
 
   gras_error_t errcode = no_error;
 
-  CDEBUG2(dict_search, "Search %s in %p", key, p_head); 
+  CDEBUG3(dict_search, "Search %.*s in %p", key_len, key, p_head); 
 
   /*** The trivial case first ***/
 
@@ -683,7 +691,7 @@ _gras_dictelm_retrieve_rec(gras_dictelm_t *p_head,
 
         gras_dynar_get(p_head->sub, pos, &p_child);
 
-        return _gras_dictelm_retrieve_rec(p_child, key, key_len, offset, data);
+        return _gras_dictelm_get_rec(p_child, key, key_len, offset, data);
       }
 
     case 3: /* The key is a prefix of the child => not found */
@@ -699,7 +707,7 @@ _gras_dictelm_retrieve_rec(gras_dictelm_t *p_head,
 }
 
 /**
- * gras_dictelm_retrieve_ext:
+ * gras_dictelm_get_ext:
  *
  * @head: the head of the dict
  * @key: the key to find data
@@ -709,7 +717,7 @@ _gras_dictelm_retrieve_rec(gras_dictelm_t *p_head,
  * Search the given @key. mismatch_error when not found.
  */
 gras_error_t
-gras_dictelm_retrieve_ext(gras_dictelm_t *p_head,
+gras_dictelm_get_ext(gras_dictelm_t *p_head,
                          const char     *key,
                          int             key_len,
                          /* OUT */void **data) {
@@ -718,11 +726,11 @@ gras_dictelm_retrieve_ext(gras_dictelm_t *p_head,
     return mismatch_error;
   }
 
-  return _gras_dictelm_retrieve_rec(p_head, key, key_len, 0, data);
+  return _gras_dictelm_get_rec(p_head, key, key_len, 0, data);
 }
 
 /**
- * gras_dictelm_retrieve:
+ * gras_dictelm_get:
  *
  * @head: the head of the dict
  * @key: the key to find data
@@ -732,11 +740,11 @@ gras_dictelm_retrieve_ext(gras_dictelm_t *p_head,
  * Search the given @key. mismatch_error when not found.
  */
 gras_error_t
-gras_dictelm_retrieve(gras_dictelm_t    *p_head,
+gras_dictelm_get(gras_dictelm_t    *p_head,
                    const char     *key,
                    /* OUT */void **data) {
 
-  return gras_dictelm_retrieve_ext(p_head, key, 1+strlen(key), data);
+  return gras_dictelm_get_ext(p_head, key, 1+strlen(key), data);
 }
 
 /*----[ _gras_dict_collapse ]------------------------------------------------*/
@@ -777,7 +785,8 @@ _collapse_if_need(gras_dictelm_t *p_head,
   gras_dynar_get(p_head->sub, 0, &p_child);
 
   /* Get the child's key as new key */
-  CDEBUG1(dict_collapse, "Do collapse with only child %s", p_child->key); 
+  CDEBUG2(dict_collapse,
+         "Do collapse with only child %.*s", p_child->key_len, p_child->key);
 
   p_head->content  = p_child->content;
   p_head->free_ctn = p_child->free_ctn;
@@ -845,8 +854,8 @@ _gras_dictelm_remove_rec(gras_dictelm_t *p_head,
         gras_dictelm_t *p_child = NULL;
 
         gras_dynar_get(p_head->sub, pos, &p_child);
-        /*DEBUG4("Recurse on child %d of %p to remove %s (prefix=%d)",
-          pos, p_child, key+offset, offset);*/
+        /*DEBUG5("Recurse on child %d of %p to remove %.*s (prefix=%d)",
+          pos, p_child, key+offset, key_len-offset,offset);*/
         TRY(_gras_dictelm_remove_rec(p_child, key, key_len, offset));
 
         _collapse_if_need(p_head, pos, old_offset);
@@ -949,7 +958,7 @@ _gras_dictelm_dump_rec(gras_dictelm_t *p_head,
 
       _gras_bytes_to_string(key, key_len, key_string);
 
-      printf("%s|(%d)", key_string + offset, offset);
+      printf("%.*s|(%d)", key_len-offset, key_string + offset, offset);
 
       free(key_string);
     }