aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-06-13 15:54:59 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2013-06-13 15:54:59 +0200
commit813cfcf08f6ed19f5dd652405b08a61044b617c3 (patch)
tree6f6fabc42d6af3764b7aae688eeaf2d3ea627591
parenteina_list_reduce: 3 arg NONNULL (diff)
downloadefl-devs/jeyzu/eina-api.tar.gz
add EAPI void eina_list_filter_map_reduce(…)devs/jeyzu/eina-api
- filter then map then reduce
-rw-r--r--src/lib/eina/eina_list.c26
-rw-r--r--src/lib/eina/eina_list.h18
-rw-r--r--src/tests/eina/eina_test_list.c20
3 files changed, 64 insertions, 0 deletions
diff --git a/src/lib/eina/eina_list.c b/src/lib/eina/eina_list.c
index a018dd0595..3e5981b256 100644
--- a/src/lib/eina/eina_list.c
+++ b/src/lib/eina/eina_list.c
@@ -1214,6 +1214,32 @@ eina_list_reduce(Eina_List *list, Eina_Reduce_Cb func, void *acc)
return list;
}
+EAPI void
+eina_list_filter_map_reduce(Eina_List *list,
+ Eina_Filter_Cb filter,
+ Eina_Map_Cb map,
+ Eina_Reduce_Cb reduce, void *acc)
+{
+ const Eina_List *l;
+ void *data;
+
+ if (!list)
+ return;
+
+ EINA_MAGIC_CHECK_LIST(list);
+
+ EINA_LIST_FOREACH(list, l, data)
+ {
+ if ((filter == NULL) || (filter(data) == EINA_TRUE))
+ {
+ if (map)
+ map(data);
+ if (reduce)
+ reduce(data, acc);
+ }
+ }
+}
+
EAPI Eina_List *
eina_list_shuffle(Eina_List *list, Eina_Random_Cb func)
{
diff --git a/src/lib/eina/eina_list.h b/src/lib/eina/eina_list.h
index 17dc261b77..f5947d1c0e 100644
--- a/src/lib/eina/eina_list.h
+++ b/src/lib/eina/eina_list.h
@@ -984,6 +984,24 @@ EAPI Eina_List *eina_list_map(Eina_List *list, Eina_Map_Cb func) EINA
EAPI Eina_List *eina_list_reduce(Eina_List *list, Eina_Reduce_Cb func, void *acc) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(3);
/**
+ * @brief Apply a filter, then a map then a reduce function on all nodes of list
+ *
+ * @param list The list to filter, map then reduce.
+ * @param filter The filter function.
+ * @param map The reduce function to apply on nodes.
+ * @param reduce The reduce function to apply on nodes.
+ * @param acc The pointer to the accumulator of the reduce function.
+ * @since 1.8
+ *
+ * @see eina_list_filter()
+ * @see eina_list_map()
+ * @see eina_list_reduce()
+ *
+ * @warning @p list must be a pointer to the first element of the list.
+ */
+EAPI void eina_list_filter_map_reduce(Eina_List *list, Eina_Filter_Cb filter, Eina_Map_Cb map, Eina_Reduce_Cb reduce, void *acc) EINA_ARG_NONNULL(5);
+
+/**
* @brief Shuffle list.
*
* @param list The list handle to shuffle.
diff --git a/src/tests/eina/eina_test_list.c b/src/tests/eina/eina_test_list.c
index 2e01061d73..57e96e9de6 100644
--- a/src/tests/eina/eina_test_list.c
+++ b/src/tests/eina/eina_test_list.c
@@ -552,11 +552,31 @@ START_TEST(eina_test_filter_map_reduce)
fail_if(*p != result2[i]);
}
+ sum = 0;
l = eina_list_reduce(l, EINA_REDUCE_CB(reduce_cb), &sum);
fail_if(sum != 666);
l = eina_list_free(l);
+
+ sum = 0;
+ l = eina_list_clone(list);
+ eina_list_filter_map_reduce(l,
+ EINA_FILTER_CB(filter_cb),
+ EINA_MAP_CB(map_cb),
+ EINA_REDUCE_CB(reduce_cb), &sum);
+ l = eina_list_free(l);
+ fail_if(sum != 864);
+
+ sum = 0;
+ l = eina_list_clone(list);
+ eina_list_filter_map_reduce(l,
+ NULL,
+ EINA_MAP_CB(map_cb),
+ EINA_REDUCE_CB(reduce_cb), &sum);
+ l = eina_list_free(l);
+ fail_if(sum != 32301);
+
list = eina_list_free(list);
eina_shutdown();