Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add xbt_fifo_search(), to search an item with a user-provided comparison function
authorMartin Quinson <martin.quinson@loria.fr>
Mon, 26 Nov 2012 13:08:25 +0000 (14:08 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Mon, 26 Nov 2012 13:08:25 +0000 (14:08 +0100)
ChangeLog
include/xbt/fifo.h
src/xbt/fifo.c

index 1c82035..70c634d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -22,7 +22,9 @@ SimGrid (3.9) NOT RELEASED; urgency=low
  XBT:
  * Kill synchronized dynars, and xbt_dynar_dopar(). We cannot think of a
    usecase where it's really mandatory, and maintaining it was a pain in
-   our codebase.
+   our codebase.   
+ * New: xbt_fifo_search(), search an item with a user-provided
+   comparison function instead of dumb pointer comparison.
 
  -- $date Da SimGrid team <simgrid-devel@lists.gforge.inria.fr>
 
index ee3832c..aaf2a37 100644 (file)
@@ -7,6 +7,7 @@
 #ifndef _XBT_FIFO_H
 #define _XBT_FIFO_H
 #include "xbt/misc.h"           /* SG_BEGIN_DECL */
+#include "xbt/function_types.h" /* int_f_pvoid_pvoid_t */
 
 SG_BEGIN_DECL()
 
@@ -45,6 +46,7 @@ XBT_PUBLIC(xbt_fifo_item_t) xbt_fifo_unshift(xbt_fifo_t, void *);
 XBT_PUBLIC(void *) xbt_fifo_shift(xbt_fifo_t);
 XBT_PUBLIC(int) xbt_fifo_size(xbt_fifo_t);
 XBT_PUBLIC(int) xbt_fifo_is_in(xbt_fifo_t, void *);
+XBT_PUBLIC(xbt_fifo_item_t) xbt_fifo_search_item(xbt_fifo_t f, int_f_pvoid_pvoid_t cmp_fun, void *closure);
 /** @} */
 
 /** @defgroup XBT_fifo_direct Direct access to fifo elements
index 7a425f6..69ba34a 100644 (file)
@@ -331,6 +331,40 @@ int xbt_fifo_is_in(xbt_fifo_t f, void *content)
   return 0;
 }
 
+/**
+ * @brief Search the given element in the fifo using a comparison function
+ *
+ * This function allows to search an item with a user provided function instead
+ * of the pointer comparison used elsewhere in this module. Assume for example that you have a fifo of
+ * strings. You cannot use xbt_fifo_remove to remove, say, "TOTO" from it because internally, xbt_fifo_remove()
+ * will do something like "if (item->content == "toto"), then remove it". And the pointer to the item content
+ * and the pointer to "toto" will never match. As a solution, this function provides a way to search elements
+ * that are semanticaly equivalent instead of only syntaxically. So, removing "Toto" from a fifo can be
+ * achieved this way:
+ *
+ *  int my_comparison_function(void *searched, void *seen) {
+ *    return !strcmp(searched, seen);
+ *  }
+ *
+ *  xbt_fifo_remove_item(fifo,
+ *                       xbt_fifo_search_item(fifo, my_comparison_function, "Toto"));
+ *
+ * \param f a fifo list
+ * \param cmp_fun the comparison function. Prototype: void *a,void *b -> int. Semantic: returns true iff a=b
+ * @param closure the element to search. It will be provided as first argument to each call of cmp_fun
+ * \return the first item matching the comparison function, or NULL if no such item exists
+ */
+xbt_fifo_item_t xbt_fifo_search_item(xbt_fifo_t f, int_f_pvoid_pvoid_t cmp_fun, void *closure) {
+  xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
+  while (item) {
+    if (cmp_fun(closure, item->content))
+      return item;
+    item = item->next;
+  }
+  return NULL;
+
+}
+
 /**
  * \param f a list
  * \return a table with the objects stored in \a f.