From e167edc7d4d963d175112c636adb57d34054fe0a Mon Sep 17 00:00:00 2001 From: Vasylenko Date: Tue, 23 Apr 2024 16:52:49 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=9Cprogram=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv4/list_ops.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 cv4/list_ops.c diff --git a/cv4/list_ops.c b/cv4/list_ops.c new file mode 100644 index 0000000..5b8d6c5 --- /dev/null +++ b/cv4/list_ops.c @@ -0,0 +1,86 @@ +#include "list_ops.h" +#include +#include + +list_t *new_list(size_t length, list_element_t elements[]) { + list_t *list = malloc(sizeof(list_t) + length * sizeof(list_element_t)); + if (list == NULL) { + return NULL; + } + list->length = length; + for (size_t i = 0; i < length; i++) { + list->elements[i] = elements[i]; + } + return list; +} + +list_t *append_list(list_t *list1, list_t *list2) { + size_t new_length = list1->length + list2->length; + list_t *new_list = realloc(list1, sizeof(list_t) + new_length * sizeof(list_element_t)); + if (new_list == NULL) { + return NULL; + } + for (size_t i = 0; i < list2->length; i++) { + new_list->elements[list1->length + i] = list2->elements[i]; + } + new_list->length = new_length; + return new_list; +} + +list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) { + list_t *new_list = malloc(sizeof(list_t)); + new_list->length = 0; + new_list->elements[0] = 0; + + for (size_t i = 0; i < list->length; i++) { + if (filter(list->elements[i])) { + new_list = realloc(new_list, sizeof(list_t) + (new_list->length + 1) * sizeof(list_element_t)); + new_list->elements[new_list->length++] = list->elements[i]; + } + } + return new_list; +} + +size_t length_list(list_t *list) { + return list->length; +} + +list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)) { + list_t *new_list = malloc(sizeof(list_t) + list->length * sizeof(list_element_t)); + new_list->length = list->length; + for (size_t i = 0; i < list->length; i++) { + new_list->elements[i] = map(list->elements[i]); + } + return new_list; +} + +list_element_t foldl_list(list_t *list, list_element_t initial, list_element_t (*foldl)(list_element_t, list_element_t)) { + list_element_t accumulator = initial; + for (size_t i = 0; i < list->length; i++) { + accumulator = foldl(list->elements[i], accumulator); + } + return accumulator; +} + +list_element_t foldr_list(list_t *list, list_element_t initial, list_element_t (*foldr)(list_element_t, list_element_t)) { + list_element_t accumulator = initial; + for (int i = list->length - 1; i >= 0; i--) { + accumulator = foldr(list->elements[i], accumulator); + } + return accumulator; +} + +list_t *reverse_list(list_t *list) { + list_t *new_list = malloc(sizeof(list_t) + list->length * sizeof(list_element_t)); + new_list->length = list->length; + for (size_t i = 0; i < list->length; i++) { + new_list->elements[i] = list->elements[list->length - 1 - i]; + } + return new_list; +} + +void delete_list(list_t *list) { + free(list); +} + +