diff --git a/cv4/list_ops.c b/cv4/list_ops.c index ee3c710..5589de1 100644 --- a/cv4/list_ops.c +++ b/cv4/list_ops.c @@ -1,48 +1,166 @@ -#ifndef LIST_OPS_H -#define LIST_OPS_H - -#include -#include -#include #include "list_ops.h" -#include "test-framework/unity.h" -typedef int list_element_t; +// Constructs a new list +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) { + list->length = length; + if (elements) { + for (size_t i = 0; i < length; i++) { + list->elements[i] = elements[i]; + } + } + } + return list; +} -typedef struct { - size_t length; - list_element_t elements[]; -} list_t; +// Appends entries to a list and returns the new list +list_t *append_list(list_t *list1, list_t *list2) { + size_t length1 = list1->length; + size_t length2 = list2->length; + size_t new_length = length1 + length2; + list_t *new_list = malloc(sizeof(list_t) + new_length * sizeof(list_element_t)); + if (new_list) { + new_list->length = new_length; + for (size_t i = 0; i < length1; i++) { + new_list->elements[i] = list1->elements[i]; + } + for (size_t i = 0; i < length2; i++) { + new_list->elements[length1 + i] = list2->elements[i]; + } + } + return new_list; +} -// constructs a new list -list_t *new_list(size_t length, list_element_t elements[]); +// Filters list returning only values that satisfy the filter function +list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) { + size_t length = list->length; + size_t new_length = 0; + // Count the number of elements that satisfy the filter + for (size_t i = 0; i < length; i++) { + if (filter(list->elements[i])) { + new_length++; + } + } + // Create a new list with filtered elements + list_t *new_list = malloc(sizeof(list_t) + new_length * sizeof(list_element_t)); + if (new_list) { + new_list->length = new_length; + size_t j = 0; + for (size_t i = 0; i < length; i++) { + if (filter(list->elements[i])) { + new_list->elements[j++] = list->elements[i]; + } + } + } + return new_list; +} -// append entries to a list and return the new list -list_t *append_list(list_t *list1, list_t *list2); +// Returns the length of the list +size_t length_list(list_t *list) { + return list->length; +} -// filter list returning only values that satisfy the filter function -list_t *filter_list(list_t *list, bool (*filter)(list_element_t)); +// Returns 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)) { + size_t length = list->length; + list_t *new_list = malloc(sizeof(list_t) + length * sizeof(list_element_t)); + if (new_list) { + new_list->length = length; + for (size_t i = 0; i < length; i++) { + new_list->elements[i] = map(list->elements[i]); + } + } + return new_list; +} -// returns the length of the list -size_t length_list(list_t *list); - -// 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)); - -// folds (reduces) the given list from the left with a function +// 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 (*foldl)(list_element_t, list_element_t)) { + size_t length = list->length; + list_element_t result = initial; + for (size_t i = 0; i < length; i++) { + result = foldl(result, list->elements[i]); + } + return result; +} -// folds (reduces) the given list from the right with a function +// 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 (*foldr)(list_element_t, list_element_t)) { + size_t length = list->length; + list_element_t result = initial; + for (size_t i = length; i > 0; i--) { + result = foldr(list->elements[i - 1], result); + } + return result; +} + +// Reverse the elements of the list +list_t *reverse_list(list_t *list) { + size_t length = list->length; + list_t *new_list = malloc(sizeof(list_t) + length * sizeof(list_element_t)); + if (new_list) { + new_list->length = length; + for (size_t i = 0; i < length; i++) { + new_list->elements[i] = list->elements[length - i - 1]; + } + } + return new_list; +} + +// Destroy the entire list +void delete_list(list_t *list) { + free(list); +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -// reverse the elements of the list -list_t *reverse_list(list_t *list); -// destroy the entire list -// list will be a dangling pointer after calling this method on it