Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Drop unused xbt dict functions.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 6 Jan 2020 13:47:45 +0000 (14:47 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 6 Jan 2020 14:29:56 +0000 (15:29 +0100)
ChangeLog
include/xbt/dict.h
src/xbt/dict.cpp
src/xbt/dict_test.cpp

index 9b69005..0c71553 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -21,12 +21,13 @@ Kernel:
 
 XBT:
 - Remove unused parameter 'free_ctn' for xbt_dict_set() and xbt_dict_set_ext().
+- Drop unused xbt_dict functions.
 - New module: random, providing classical random numbers generators.
 
 SMPI:
-- New option : "smpi/auto-shared-malloc-thresh" which sets a value for 
-  allocations' size above which they are considered "shared" by default (as if 
-  they were performed through SMPI_SHARED_MALLOC macros). 
+- New option : "smpi/auto-shared-malloc-thresh" which sets a value for
+  allocations' size above which they are considered "shared" by default (as if
+  they were performed through SMPI_SHARED_MALLOC macros).
   Default = 0 = disabled feature.
   Note : malloc, calloc and free are now overriden by smpicc/cxx by default.
   This can cause some troubles if codes are already overriding these. If this
@@ -47,7 +48,7 @@ Fixed bugs (FG#.. -> framagit bugs; FG!.. -> framagit merge requests):
  - GH#321: [S4U] Get task remaining work ratio
  - GH#323: Crash when an actor turn off his physical host
  - FG!19: Removing RngStream
- - https://lists.gforge.inria.fr/pipermail/simgrid-user/2019-November/004653.html : 
+ - https://lists.gforge.inria.fr/pipermail/simgrid-user/2019-November/004653.html:
    MPI_Cart_sub was not working properly. Kudos to Jonathan Borne for the report.
 
 ----------------------------------------------------------------------------
index 02ca030..9100c50 100644 (file)
@@ -71,14 +71,9 @@ XBT_PUBLIC unsigned int xbt_dict_size(const_xbt_dict_t dict);
  */
 
 XBT_PUBLIC void xbt_dict_set(xbt_dict_t dict, const char* key, void* data);
-XBT_PUBLIC void* xbt_dict_get(const_xbt_dict_t dict, const char* key);
 XBT_PUBLIC void* xbt_dict_get_or_null(const_xbt_dict_t dict, const char* key);
-XBT_PUBLIC char* xbt_dict_get_key(const_xbt_dict_t dict, const void* data);
-XBT_PUBLIC xbt_dictelm_t xbt_dict_get_elm(const_xbt_dict_t dict, const char* key);
 XBT_PUBLIC xbt_dictelm_t xbt_dict_get_elm_or_null(const_xbt_dict_t dict, const char* key);
 
-XBT_PUBLIC void xbt_dict_remove(xbt_dict_t dict, const char* key);
-XBT_PUBLIC void xbt_dict_reset(xbt_dict_t dict);
 XBT_PUBLIC int xbt_dict_length(const_xbt_dict_t dict);
 XBT_PUBLIC int xbt_dict_is_empty(const_xbt_dict_t dict);
 
@@ -91,7 +86,6 @@ XBT_PUBLIC int xbt_dict_is_empty(const_xbt_dict_t dict);
  *  @{
  */
 XBT_PUBLIC void xbt_dict_set_ext(xbt_dict_t dict, const char* key, int key_len, void* data);
-XBT_PUBLIC void* xbt_dict_get_ext(const_xbt_dict_t dict, const char* key, int key_len);
 XBT_PUBLIC void* xbt_dict_get_or_null_ext(const_xbt_dict_t dict, const char* key, int key_len);
 XBT_PUBLIC void xbt_dict_remove_ext(xbt_dict_t dict, const char* key, int key_len);
 
@@ -127,11 +121,6 @@ struct s_xbt_dict_cursor {
 typedef struct s_xbt_dict_cursor *xbt_dict_cursor_t;
 typedef const struct s_xbt_dict_cursor* const_xbt_dict_cursor_t;
 
-static inline xbt_dictelm_t xbt_dict_cursor_get_elm(const_xbt_dict_cursor_t cursor)
-{
-  return cursor->current;
-}
-
 XBT_PUBLIC xbt_dict_cursor_t xbt_dict_cursor_new(const_xbt_dict_t dict);
 XBT_PUBLIC void xbt_dict_cursor_free(xbt_dict_cursor_t* cursor);
 
index 317fcb3..1fb5b29 100644 (file)
@@ -192,25 +192,8 @@ void xbt_dict_set(xbt_dict_t dict, const char* key, void* data)
  * @param key_len the size of the @a key
  * @return the data that we are looking for
  *
- * Search the given @a key. Throws std::out_of_range when not found.
+ * Search the given @a key. Returns nullptr when not found.
  */
-void* xbt_dict_get_ext(const_xbt_dict_t dict, const char* key, int key_len)
-{
-  unsigned int hash_code = xbt_str_hash_ext(key, key_len);
-  const s_xbt_dictelm* current = dict->table[hash_code & dict->table_size];
-
-  while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
-          || memcmp(key, current->key, key_len))) {
-    current = current->next;
-  }
-
-  if (current == nullptr)
-    throw std::out_of_range(simgrid::xbt::string_printf("key %.*s not found", key_len, key));
-
-  return current->content;
-}
-
-/** @brief like xbt_dict_get_ext(), but returning nullptr when not found */
 void* xbt_dict_get_or_null_ext(const_xbt_dict_t dict, const char* key, int key_len)
 {
   unsigned int hash_code = xbt_str_hash_ext(key, key_len);
@@ -227,24 +210,6 @@ void* xbt_dict_get_or_null_ext(const_xbt_dict_t dict, const char* key, int key_l
   return current->content;
 }
 
-/**
- * @brief retrieve the key associated to that object. Warning, that's a linear search
- *
- * Returns nullptr if the object cannot be found
- */
-char* xbt_dict_get_key(const_xbt_dict_t dict, const void* data)
-{
-  for (int i = 0; i <= dict->table_size; i++) {
-    const s_xbt_dictelm* current = dict->table[i];
-    while (current != nullptr) {
-      if (current->content == data)
-        return current->key;
-      current = current->next;
-    }
-  }
-  return nullptr;
-}
-
 /**
  * @brief Retrieve data from the dict (null-terminated key)
  *
@@ -252,36 +217,7 @@ char* xbt_dict_get_key(const_xbt_dict_t dict, const void* data)
  * @param key the key to find data
  * @return the data that we are looking for
  *
- * Search the given @a key. Throws std::out_of_range when not found.
- * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found.
- */
-void* xbt_dict_get(const_xbt_dict_t dict, const char* key)
-{
-  return xbt_dict_get_elm(dict, key)->content;
-}
-
-/**
- * @brief Retrieve element from the dict (null-terminated key)
- *
- * @param dict the dealer of data
- * @param key the key to find data
- * @return the s_xbt_dictelm_t that we are looking for
- *
- * Search the given @a key. Throws std::out_of_range when not found.
- * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found.
- */
-xbt_dictelm_t xbt_dict_get_elm(const_xbt_dict_t dict, const char* key)
-{
-  xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
-
-  if (current == nullptr)
-    throw std::out_of_range(simgrid::xbt::string_printf("key %s not found", key));
-
-  return current;
-}
-
-/**
- * @brief like xbt_dict_get(), but returning nullptr when not found
+ * Search the given @a key. Returns nullptr when not found.
  */
 void* xbt_dict_get_or_null(const_xbt_dict_t dict, const char* key)
 {
@@ -294,7 +230,13 @@ void* xbt_dict_get_or_null(const_xbt_dict_t dict, const char* key)
 }
 
 /**
- * @brief like xbt_dict_get_elm(), but returning nullptr when not found
+ * @brief Retrieve element from the dict (null-terminated key)
+ *
+ * @param dict the dealer of data
+ * @param key the key to find data
+ * @return the s_xbt_dictelm_t that we are looking for
+ *
+ * Search the given @a key. Returns nullptr when not found.
  */
 xbt_dictelm_t xbt_dict_get_elm_or_null(const_xbt_dict_t dict, const char* key)
 {
@@ -344,40 +286,6 @@ void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
   dict->count--;
 }
 
-/**
- * @brief Remove data from the dict (null-terminated key)
- *
- * @param dict the dict
- * @param key the key of the data to be removed
- *
- * Remove the entry associated with the given @a key
- */
-void xbt_dict_remove(xbt_dict_t dict, const char *key)
-{
-  xbt_dict_remove_ext(dict, key, strlen(key));
-}
-
-/** @brief Remove all data from the dict */
-void xbt_dict_reset(xbt_dict_t dict)
-{
-  if (dict->count == 0)
-    return;
-
-  for (int i = 0; i <= dict->table_size; i++) {
-    xbt_dictelm_t previous = nullptr;
-    xbt_dictelm_t current = dict->table[i];
-    while (current != nullptr) {
-      previous = current;
-      current = current->next;
-      xbt_dictelm_free(dict, previous);
-    }
-    dict->table[i] = nullptr;
-  }
-
-  dict->count = 0;
-  dict->fill = 0;
-}
-
 /**
  * @brief Return the number of elements in the dict.
  * @param dict a dictionary
index f21816a..ed105ea 100644 (file)
@@ -52,7 +52,7 @@ static xbt_dict_t new_fixture()
 static void search_ext(const_xbt_dict_t head, const char* key, const char* data)
 {
   INFO("Search " << STR(key));
-  char* found = (char*)xbt_dict_get(head, key);
+  char* found = (char*)xbt_dict_get_or_null(head, key);
   INFO("Found " << STR(found));
   if (data) {
     REQUIRE(found); // data do not match expectations: found null while searching for data
@@ -71,7 +71,7 @@ static void search(const_xbt_dict_t head, const char* key)
 static void debugged_remove(xbt_dict_t head, const char* key)
 {
   INFO("Remove '" << STR(key) << "'");
-  xbt_dict_remove(head, key);
+  xbt_dict_remove_ext(head, key, strlen(key));
 }
 
 static void traverse(const_xbt_dict_t head)
@@ -90,7 +90,7 @@ static void traverse(const_xbt_dict_t head)
 static void search_not_found(const_xbt_dict_t head, const char* data)
 {
   INFO("Search " << STR(data) << " (expected not to be found)");
-  REQUIRE_THROWS_AS(xbt_dict_get(head, data), std::out_of_range);
+  REQUIRE(xbt_dict_get_or_null(head, data) == nullptr);
 }
 
 static void count(const_xbt_dict_t dict, int length)
@@ -108,25 +108,6 @@ static void count(const_xbt_dict_t dict, int length)
   REQUIRE(effective == length); // Effective length differs
 }
 
-static void count_check_get_key(const_xbt_dict_t dict, int length)
-{
-  xbt_dict_cursor_t cursor;
-  char* key;
-  void* data;
-  int effective = 0;
-
-  INFO("Count elements (expecting " << length << "), and test the getkey function");
-  REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
-
-  xbt_dict_foreach (dict, cursor, key, data) {
-    effective++;
-    char* key2 = xbt_dict_get_key(dict, data);
-    xbt_assert(not strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key);
-  }
-
-  REQUIRE(effective == length); // Effective length differs
-}
-
 static int countelems(const_xbt_dict_t head)
 {
   xbt_dict_cursor_t cursor;
@@ -155,7 +136,7 @@ TEST_CASE("xbt::dict: dict data container", "dict")
 
     INFO("Traverse the full dictionary");
     head = new_fixture();
-    count_check_get_key(head, 7);
+    count(head, 7);
 
     debugged_add_ext(head, "toto", "tutu");
     search_ext(head, "toto", "tutu");
@@ -170,29 +151,29 @@ TEST_CASE("xbt::dict: dict data container", "dict")
 
     /* CHANGING */
     head = new_fixture();
-    count_check_get_key(head, 7);
+    count(head, 7);
     INFO("Change 123 to 'Changed 123'");
     xbt_dict_set(head, "123", xbt_strdup("Changed 123"));
-    count_check_get_key(head, 7);
+    count(head, 7);
 
     INFO("Change 123 back to '123'");
     xbt_dict_set(head, "123", xbt_strdup("123"));
-    count_check_get_key(head, 7);
+    count(head, 7);
 
     INFO("Change 12a to 'Dummy 12a'");
     xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"));
-    count_check_get_key(head, 7);
+    count(head, 7);
 
     INFO("Change 12a to '12a'");
     xbt_dict_set(head, "12a", xbt_strdup("12a"));
-    count_check_get_key(head, 7);
+    count(head, 7);
 
     INFO("Traverse the resulting dictionary");
     traverse(head);
 
     /* RETRIEVE */
     INFO("Search 123");
-    const char* data = (char*)xbt_dict_get(head, "123");
+    const char* data = (char*)xbt_dict_get_or_null(head, "123");
     REQUIRE((data && strcmp("123", data) == 0));
 
     search_not_found(head, "Can't be found");
@@ -252,13 +233,6 @@ TEST_CASE("xbt::dict: dict data container", "dict")
     REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
     traverse(head);
 
-    INFO("Free dict, create new fresh one, and then reset the dict");
-    xbt_dict_free(&head);
-    head = new_fixture();
-    xbt_dict_reset(head);
-    count(head, 0);
-    traverse(head);
-
     INFO("Free the dictionary twice");
     xbt_dict_free(&head);
     xbt_dict_free(&head);
@@ -311,8 +285,8 @@ TEST_CASE("xbt::dict: dict data container", "dict")
         } while (data != nullptr);
 
         xbt_dict_set(head, key, key);
-        data = (char*)xbt_dict_get(head, key);
-        REQUIRE(not strcmp(key, data)); // Retrieved value != Injected value
+        data = (char*)xbt_dict_get_or_null(head, key);
+        REQUIRE((data && not strcmp(key, data))); // Retrieved value != Injected value
 
         count(head, j + 1);
       }
@@ -338,10 +312,10 @@ TEST_CASE("xbt::dict: dict data container", "dict")
     for (int i = 0; i < 20; i++) {
       for (int j = 0; j < NB_ELM; j++) {
         snprintf(key, 10, "%d", j);
-        void* data = xbt_dict_get(head, key);
-        REQUIRE(not strcmp(key, (char*)data)); // with get, key != data
-        data = xbt_dict_get_ext(head, key, strlen(key));
-        REQUIRE(not strcmp(key, (char*)data)); // with get_ext, key != data
+        void* data = xbt_dict_get_or_null(head, key);
+        REQUIRE((data && not strcmp(key, (char*)data))); // with get, key != data
+        data = xbt_dict_get_or_null_ext(head, key, strlen(key));
+        REQUIRE((data && not strcmp(key, (char*)data))); // with get_ext, key != data
       }
     }
     xbt_free(key);
@@ -350,7 +324,7 @@ TEST_CASE("xbt::dict: dict data container", "dict")
     key = (char*)xbt_malloc(10);
     for (int j = 0; j < NB_ELM; j++) {
       snprintf(key, 10, "%d", j);
-      xbt_dict_remove(head, key);
+      xbt_dict_remove_ext(head, key, strlen(key));
     }
     xbt_free(key);
 
@@ -371,8 +345,8 @@ TEST_CASE("xbt::dict: dict data container", "dict")
 
     INFO("Check elements");
     for (int i = 0; i < count; ++i) {
-      xbt_dict_get_ext(dict, (char*)&i, sizeof(i));
-      REQUIRE(xbt_dict_size(dict) == (unsigned)count); // Unexpected value at index i
+      void* data = xbt_dict_get_or_null_ext(dict, (char*)&i, sizeof(i));
+      REQUIRE((intptr_t)data == i); // Unexpected value at index i
     }
 
     INFO("Free the array");