summaryrefslogtreecommitdiff
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
parent587077ef4be16254e9ec916eb1518296ac7c944b (diff)
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)
1214 return list; 1214 return list;
1215} 1215}
1216 1216
1217EAPI void
1218eina_list_filter_map_reduce(Eina_List *list,
1219 Eina_Filter_Cb filter,
1220 Eina_Map_Cb map,
1221 Eina_Reduce_Cb reduce, void *acc)
1222{
1223 const Eina_List *l;
1224 void *data;
1225
1226 if (!list)
1227 return;
1228
1229 EINA_MAGIC_CHECK_LIST(list);
1230
1231 EINA_LIST_FOREACH(list, l, data)
1232 {
1233 if ((filter == NULL) || (filter(data) == EINA_TRUE))
1234 {
1235 if (map)
1236 map(data);
1237 if (reduce)
1238 reduce(data, acc);
1239 }
1240 }
1241}
1242
1217EAPI Eina_List * 1243EAPI Eina_List *
1218eina_list_shuffle(Eina_List *list, Eina_Random_Cb func) 1244eina_list_shuffle(Eina_List *list, Eina_Random_Cb func)
1219{ 1245{
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
984EAPI Eina_List *eina_list_reduce(Eina_List *list, Eina_Reduce_Cb func, void *acc) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(3); 984EAPI Eina_List *eina_list_reduce(Eina_List *list, Eina_Reduce_Cb func, void *acc) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(3);
985 985
986/** 986/**
987 * @brief Apply a filter, then a map then a reduce function on all nodes of list
988 *
989 * @param list The list to filter, map then reduce.
990 * @param filter The filter function.
991 * @param map The reduce function to apply on nodes.
992 * @param reduce The reduce function to apply on nodes.
993 * @param acc The pointer to the accumulator of the reduce function.
994 * @since 1.8
995 *
996 * @see eina_list_filter()
997 * @see eina_list_map()
998 * @see eina_list_reduce()
999 *
1000 * @warning @p list must be a pointer to the first element of the list.
1001 */
1002EAPI 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);
1003
1004/**
987 * @brief Shuffle list. 1005 * @brief Shuffle list.
988 * 1006 *
989 * @param list The list handle to shuffle. 1007 * @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)
552 fail_if(*p != result2[i]); 552 fail_if(*p != result2[i]);
553 } 553 }
554 554
555 sum = 0;
555 l = eina_list_reduce(l, EINA_REDUCE_CB(reduce_cb), &sum); 556 l = eina_list_reduce(l, EINA_REDUCE_CB(reduce_cb), &sum);
556 fail_if(sum != 666); 557 fail_if(sum != 666);
557 558
558 l = eina_list_free(l); 559 l = eina_list_free(l);
559 560
561
562 sum = 0;
563 l = eina_list_clone(list);
564 eina_list_filter_map_reduce(l,
565 EINA_FILTER_CB(filter_cb),
566 EINA_MAP_CB(map_cb),
567 EINA_REDUCE_CB(reduce_cb), &sum);
568 l = eina_list_free(l);
569 fail_if(sum != 864);
570
571 sum = 0;
572 l = eina_list_clone(list);
573 eina_list_filter_map_reduce(l,
574 NULL,
575 EINA_MAP_CB(map_cb),
576 EINA_REDUCE_CB(reduce_cb), &sum);
577 l = eina_list_free(l);
578 fail_if(sum != 32301);
579
560 list = eina_list_free(list); 580 list = eina_list_free(list);
561 581
562 eina_shutdown(); 582 eina_shutdown();