From 98369fd5cf35869fe5e433cae597840e8c410486 Mon Sep 17 00:00:00 2001 From: ak643du Date: Thu, 14 Mar 2024 13:47:52 +0100 Subject: [PATCH] Initialization --- cv4/list-ops.c | 62 -------- cv4/list_ops.c | 132 +++++++++++------- cv4/{list-ops => }/list_ops.h | 0 cv4/{list-ops => }/makefile | 0 cv4/{list-ops => }/test-framework/unity.c | 0 cv4/{list-ops => }/test-framework/unity.h | 0 .../test-framework/unity_internals.h | 0 cv4/{list-ops => }/test_list_ops.c | 0 8 files changed, 81 insertions(+), 113 deletions(-) delete mode 100644 cv4/list-ops.c rename cv4/{list-ops => }/list_ops.h (100%) rename cv4/{list-ops => }/makefile (100%) rename cv4/{list-ops => }/test-framework/unity.c (100%) rename cv4/{list-ops => }/test-framework/unity.h (100%) rename cv4/{list-ops => }/test-framework/unity_internals.h (100%) rename cv4/{list-ops => }/test_list_ops.c (100%) diff --git a/cv4/list-ops.c b/cv4/list-ops.c deleted file mode 100644 index 59bb2a9..0000000 --- a/cv4/list-ops.c +++ /dev/null @@ -1,62 +0,0 @@ -#include "list_ops.h" - -// constructs a new list -list_t *new_list(size_t length, list_element_t elements[]){ - return NULL; - -} - -// append entries to a list and return the new list -list_t *append_list(list_t *list1, list_t *list2){ - return NULL; - -} - -// filter list returning only values that satisfy the filter function -list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){ - return NULL; - -} - -// returns the length of the list -size_t length_list(list_t *list){ - return 0; - -} - -// return a list of elements whose values equal the list value transformed by -// the mapping function -list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)){ - return NULL; - -} - -// folds (reduces) the given list from the left with a function -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 res=0; - return res; - -} - -// folds (reduces) the given list from the right with a function -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 res=0; - return res; - -} - -// reverse the elements of the list -list_t *reverse_list(list_t *list){ - return NULL; - -} - -// destroy the entire list -// list will be a dangling pointer after calling this method on it -void delete_list(list_t *list){ - -} diff --git a/cv4/list_ops.c b/cv4/list_ops.c index 670b8e5..a488be1 100644 --- a/cv4/list_ops.c +++ b/cv4/list_ops.c @@ -1,61 +1,91 @@ #include "list_ops.h" +#include -// comstruct a new list - -list_t *new_list(size_t lenght, list_element_t elements[]){ - return NULL; +// constructs a new list +list_t *new_list(size_t length, list_element_t elements[]) { + list_t *newList = malloc(sizeof(list_t) + length * sizeof(list_element_t)); + if (newList != NULL) { + newList->length = length; + if (length > 0 && elements != NULL) { + memcpy(newList->elements, elements, length * sizeof(list_element_t)); + } + } + return newList; } -// append entries to a new list and return the new list -list_t *append_list(list_t *list1, list_t *list2){ - return NULL; +// append entries to a list and return the new list +list_t *append_list(list_t *list1, list_t *list2) { + if (list1 == NULL && list2 == NULL) { + return NULL; // Both lists are empty + } else if (list1 == NULL) { + return list2; // Only list1 is empty, return list2 + } else if (list2 == NULL) { + return list1; // Only list2 is empty, return list1 + } + + size_t new_length = list1->length + list2->length; + list_t *new_list = malloc(sizeof(list_t) + new_length * sizeof(list_element_t)); + if (new_list == NULL) { + return NULL; // Memory allocation failed + } + + memcpy(new_list->elements, list1->elements, list1->length * sizeof(list_element_t)); + memcpy(new_list->elements + list1->length, list2->elements, list2->length * sizeof(list_element_t)); + + new_list->length = new_length; + + return new_list; } -//filter list returninf only values that satisfy the filter function - +// filter list returning only values that satisfy the filter function list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){ - return NULL; -} - -//returns the lenght of the list - -size_t lenght_list(list_t *list){ - return 0; -} - -//return a list of elements whose values equal the list value transformed by -//the maaping function - -list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)){ - return NULL; -} - -//folds (reduces) the given list from the left with a function - -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 res=0; - return res; -} - -//folds (reduces) the given list from the right with a function - -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 res=0; - return res; -} - -//reverse elements of the list - -list_t *reserve_list(list_t *list){ - return NULL; -} - -//destroy the entire list -//list will ve a dangling pointer after calling this method on it - -void delete_list(list_t list*){ + return NULL; } + +// returns the length of the list +size_t length_list(list_t *list) { + return (list != NULL) ? list->length : 0; +} + +// return a list of elements whose values equal the list value transformed by +// the mapping function +list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)) { + if (list == NULL) return NULL; + + list_t *mappedList = new_list(list->length, list->elements); + if (mappedList != NULL) { + for (size_t i = 0; i < list->length; ++i) { + mappedList->elements[i] = map(list->elements[i]); + } + } + return mappedList; +} + +// folds (reduces) the given list from the left with a function +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; + + return res; + +} + +// folds (reduces) the given list from the right with a function +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 res=0; + return res; + +} + +// reverse the elements of the list +list_t *reverse_list(list_t *list){ + return NULL; + +} + +// destroy the entire list +// list will be a dangling pointer after calling this method on it +void delete_list(list_t *list){ + +} \ No newline at end of file diff --git a/cv4/list-ops/list_ops.h b/cv4/list_ops.h similarity index 100% rename from cv4/list-ops/list_ops.h rename to cv4/list_ops.h diff --git a/cv4/list-ops/makefile b/cv4/makefile similarity index 100% rename from cv4/list-ops/makefile rename to cv4/makefile diff --git a/cv4/list-ops/test-framework/unity.c b/cv4/test-framework/unity.c similarity index 100% rename from cv4/list-ops/test-framework/unity.c rename to cv4/test-framework/unity.c diff --git a/cv4/list-ops/test-framework/unity.h b/cv4/test-framework/unity.h similarity index 100% rename from cv4/list-ops/test-framework/unity.h rename to cv4/test-framework/unity.h diff --git a/cv4/list-ops/test-framework/unity_internals.h b/cv4/test-framework/unity_internals.h similarity index 100% rename from cv4/list-ops/test-framework/unity_internals.h rename to cv4/test-framework/unity_internals.h diff --git a/cv4/list-ops/test_list_ops.c b/cv4/test_list_ops.c similarity index 100% rename from cv4/list-ops/test_list_ops.c rename to cv4/test_list_ops.c