pvjc24/cv4/list_ops.c
2024-03-10 16:59:32 +01:00

171 lines
3.8 KiB
C

#include "list_ops.h"
#include <stdlib.h>
#include <stdbool.h>
list_t *new_list(size_t length, list_element_t elements[]) {
size_t size = sizeof(list_t) + length * sizeof(list_element_t);
list_t *newList = (list_t *)malloc(size);
if (newList == NULL) {
return NULL;
}
newList->length = length;
for (size_t i = 0; i < length; ++i) {
newList->elements[i] = elements[i];
}
return newList;
}
list_t *append_list(list_t *list1, list_t *list2) {
if (list1 == NULL) {
return (list2 != NULL) ? new_list(list2->length, list2->elements) : NULL;
}
if (list2 == NULL) {
return new_list(list1->length, list1->elements);
}
size_t totalSize = sizeof(list_t) + (list1->length + list2->length) * sizeof(list_element_t);
list_t *newList = (list_t *)malloc(totalSize);
if (!newList) {
return NULL;
}
newList->length = list1->length + list2->length;
for (size_t i = 0; i < list1->length; ++i) {
newList->elements[i] = list1->elements[i];
}
for (size_t i = 0; i < list2->length; ++i) {
newList->elements[list1->length + i] = list2->elements[i];
}
return newList;
}
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
if (list == NULL || filter == NULL) {
return NULL;
}
size_t count = 0;
for (size_t i = 0; i < list->length; ++i) {
if (filter(list->elements[i])) {
count++;
}
}
if (count == 0) {
return new_list(0, NULL);
}
size_t totalSize = sizeof(list_t) + count * sizeof(list_element_t);
list_t *filtered_list = (list_t *)malloc(totalSize);
if (!filtered_list) {
return NULL;
}
filtered_list->length = count;
size_t filteredIndex = 0;
for (size_t i = 0; i < list->length; ++i) {
if (filter(list->elements[i])) {
filtered_list->elements[filteredIndex] = list->elements[i];
filteredIndex++;
}
}
return filtered_list;
}
size_t length_list(list_t *list) {
if (list == NULL) {
return 0;
}
return list->length;
}
list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)) {
if (list == NULL || map == NULL) {
return NULL;
}
list_t *mapped_list = new_list(list->length, NULL);
if (mapped_list == NULL) {
return NULL;
}
for (size_t i = 0; i < list->length; ++i) {
mapped_list->elements[i] = map(list->elements[i]);
}
return mapped_list;
}
list_element_t foldl_list(list_t *list, list_element_t initial,
list_element_t (*foldl)(list_element_t,
list_element_t)) {
if (list == NULL || foldl == NULL) {
return initial;
}
list_element_t accumulator = initial;
for (size_t i = 0; i < list->length; ++i) {
accumulator = foldl(accumulator, list->elements[i]);
}
return accumulator;
}
list_element_t foldr_list(list_t *list, list_element_t initial,
list_element_t (*foldr)(list_element_t,
list_element_t)) {
if (!list || !foldr) {
return initial;
}
list_element_t result = initial;
for (size_t i = 0; i < list->length; ++i) {
result = foldr(list->elements[list->length - 1 - i], result);
}
return result;
}
list_t *reverse_list(list_t *list) {
if (!list) {
return NULL;
}
list_t *reversed_list = new_list(list->length, NULL);
if (!reversed_list) {
return NULL;
}
for (size_t i = list->length; i > 0; --i) {
reversed_list->elements[list->length - i] = list->elements[i - 1];
}
return reversed_list;
}
void delete_list(list_t *list) {
if (list != NULL) {
free(list);
}
}