Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new function: xbt_dynar_sort_strings(), when the content is char*
authorMartin Quinson <martin.quinson@loria.fr>
Sun, 7 Feb 2016 13:40:15 +0000 (14:40 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Sun, 7 Feb 2016 13:47:24 +0000 (14:47 +0100)
And use it when possible

include/xbt/dynar.h
src/instr/jedule/jedule_platform.cpp
src/xbt/config.c
src/xbt/dynar.c

index 007926e..7dfff27 100644 (file)
@@ -105,6 +105,7 @@ XBT_PUBLIC(signed int) xbt_dynar_search_or_negative(xbt_dynar_t const dynar, voi
 XBT_PUBLIC(int) xbt_dynar_member(xbt_dynar_t const dynar, void *elem);
 XBT_PUBLIC(void) xbt_dynar_sort(xbt_dynar_t const dynar,
                                 int_f_cpvoid_cpvoid_t compar_fn);
+XBT_PUBLIC(xbt_dynar_t) xbt_dynar_sort_strings(xbt_dynar_t dynar);
 XBT_PUBLIC(void) xbt_dynar_three_way_partition(xbt_dynar_t const dynar,
     int_f_pvoid_t color);
 XBT_PUBLIC(int) xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
@@ -227,16 +228,6 @@ typedef struct xbt_dynar_s {
   void_f_pvoid_t free_f;
 } s_xbt_dynar_t;
 
-static XBT_INLINE void
-_xbt_dynar_cursor_first(const xbt_dynar_t dynar XBT_ATTRIB_UNUSED,
-                        unsigned int *const cursor)
-{
-  /* iterating over a NULL dynar is a no-op (but we don't want to have uninitialized counters) */
-
-  //XBT_DEBUG("Set cursor on %p to the first position", (void *) dynar);
-  *cursor = 0;
-}
-
 static XBT_INLINE int
 _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
                       unsigned int idx, void *const dst)
@@ -280,19 +271,19 @@ xbt_dynar_foreach (dyn,cpt,str) {
  * break or a return statement. 
  */
 #define xbt_dynar_foreach(_dynar,_cursor,_data) \
-       for (_xbt_dynar_cursor_first(_dynar,&(_cursor))      ; \
-      _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \
-            (_cursor)++         )
+       for ( (_cursor) = 0      ; \
+             _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \
+             (_cursor)++         )
 
 #ifndef __cplusplus
 #define xbt_dynar_foreach_ptr(_dynar,_cursor,_ptr) \
-       for (_xbt_dynar_cursor_first(_dynar,&(_cursor))       ; \
-      (_ptr = _cursor < _dynar->used ? xbt_dynar_get_ptr(_dynar,_cursor) : NULL) ; \
+       for ((_cursor) = 0       ; \
+            (_ptr = _cursor < _dynar->used ? xbt_dynar_get_ptr(_dynar,_cursor) : NULL) ; \
             (_cursor)++         )
 #else
 #define xbt_dynar_foreach_ptr(_dynar,_cursor,_ptr) \
-       for (_xbt_dynar_cursor_first(_dynar,&(_cursor))       ; \
-      (_ptr = _cursor < _dynar->used ? (decltype(_ptr)) xbt_dynar_get_ptr(_dynar,_cursor) : NULL) ; \
+       for ((_cursor) = 0       ; \
+            (_ptr = _cursor < _dynar->used ? (decltype(_ptr)) xbt_dynar_get_ptr(_dynar,_cursor) : NULL) ; \
             (_cursor)++         )
 #endif
 /** @} */
index 312b06f..e774f9d 100644 (file)
@@ -30,12 +30,6 @@ static void jed_free_container(jed_simgrid_container_t container);
 
 /********************************************************************/
 
-static int compare_hostnames(const void *host1, const void *host2) {
-  const char *hp1 = *((const char**) host1);
-  const char *hp2 = *((const char**) host2);
-  return strcmp (hp1, hp2);
-}
-
 static int compare_ids(const void *num1, const void *num2) {
   int *i1 = (int*) num1;
   int *i2 = (int*) num2;
@@ -97,7 +91,7 @@ void jed_simgrid_add_resources(jed_simgrid_container_t parent,
   parent->last_id = 0;
   parent->resource_list = xbt_dynar_new(sizeof(char *), NULL);
 
-  xbt_dynar_sort (host_names,  &compare_hostnames);
+  xbt_dynar_sort_strings(host_names);
 
   xbt_dynar_foreach(host_names, iter, host_name) {
     buf = bprintf("%d", parent->last_id);
index ff9ee32..b78bdfb 100644 (file)
@@ -340,11 +340,6 @@ void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
   free(entrycpy);               /* strdup'ed by dict mechanism, but cannot be const */
 }
 
-static int strcmp_voidp(const void *pa, const void *pb)
-{
-  return strcmp(*(const char **)pa, *(const char **)pb);
-}
-
 /** @brief Displays the declared options and their description */
 void xbt_cfg_help(xbt_cfg_t cfg)
 {
@@ -357,7 +352,7 @@ void xbt_cfg_help(xbt_cfg_t cfg)
   xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
     xbt_dynar_push(names, &name);
   }
-  xbt_dynar_sort(names, strcmp_voidp);
+  xbt_dynar_sort_strings(names);
 
   xbt_dynar_foreach(names, dynar_cursor, name) {
     int i;
index 28c6efc..5885ce2 100644 (file)
@@ -471,8 +471,8 @@ unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
       return it;
     }
 
-  THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem,
-         dynar);
+  THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem, dynar);
+  return -1; // Won't happen, just to please eclipse
 }
 
 /** @brief Returns the position of the element in the dynar (or -1 if not found)
@@ -630,6 +630,16 @@ XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar,
   qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
 }
 
+static int strcmp_voidp(const void *pa, const void *pb) {
+  return strcmp(*(const char **)pa, *(const char **)pb);
+}
+/** @brief Sorts a dynar of strings (ie, char* data) */
+xbt_dynar_t xbt_dynar_sort_strings(xbt_dynar_t dynar)
+{
+  xbt_dynar_sort(dynar, strcmp_voidp);
+  return dynar; // to enable functional uses
+}
+
 /** @brief Sorts a dynar according to their color assuming elements can have only three colors.
  * Since there are only three colors, it is linear and much faster than a classical sort.
  * See for example http://en.wikipedia.org/wiki/Dutch_national_flag_problem