diff --git a/functional_market/Fundamentals/toolbox.c b/functional_market/Fundamentals/toolbox.c index c1647bf..9a9bc5d 100644 --- a/functional_market/Fundamentals/toolbox.c +++ b/functional_market/Fundamentals/toolbox.c @@ -5,8 +5,11 @@ #include struct array *sort(struct array *arr, comparator f) { - + if(!arr || !f) return NULL; + void *temp = malloc(arr->elem_size); + if(!temp) return NULL; + if (arr->len > 0) { for(size_t index1 = 0; index1 < arr->len - 1; index1 ++ ){ @@ -30,7 +33,9 @@ struct array *sort(struct array *arr, comparator f) } struct array *map(struct array *arr, size_t output_size, mapper f) -{ +{ + if(!arr || !f)return NULL; + void *data = calloc(arr->len, output_size); if(!data) return NULL; @@ -52,6 +57,8 @@ struct array *map(struct array *arr, size_t output_size, mapper f) struct array *filter(struct array *arr, predicate f) { + if(!arr || !f) return NULL; + size_t index = 0; int flon = 0; while(index != arr->len){ @@ -66,8 +73,8 @@ struct array *filter(struct array *arr, predicate f) index = 0; void *data1 = calloc(flon, arr->elem_size); if(!data1 && flon > 0) return NULL; + size_t index2 = 0; - while(index != arr->len){ void *cur = arr->data + index * arr->elem_size; @@ -88,6 +95,8 @@ struct array *filter(struct array *arr, predicate f) void *reduce(struct array *arr, void *start_value, reducer f) { + if(!arr || !start_value || !f) return NULL; + void *rslt = start_value; size_t index = 0; @@ -103,6 +112,7 @@ void *reduce(struct array *arr, void *start_value, reducer f) void foreach(struct array *arr, acceptor f) { + if(!arr || !f) return; // arr.data est un array de x valeurs size_t index = 0;