pvjc24/cv4/list_ops.c
2024-03-10 17:28:12 +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 *resultList = (list_t *)malloc(size);
if (resultList == NULL) {
return NULL;
}
resultList->length = length;
for (size_t i = 0; i < length; ++i) {
resultList->elements[i] = elements[i];
}
return resultList;
}
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 *resultList = (list_t *)malloc(totalSize);
if (!resultList) {
return NULL;
}
resultList->length = list1->length + list2->length;
for (size_t i = 0; i < list1->length; ++i) {
resultList->elements[i] = list1->elements[i];
}
for (size_t i = 0; i < list2->length; ++i) {
resultList->elements[list1->length + i] = list2->elements[i];
}
return resultList;
}
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 *filteredResult = (list_t *)malloc(totalSize);
if (!filteredResult) {
return NULL;
}
filteredResult->length = count;
size_t filteredIndex = 0;
for (size_t i = 0; i < list->length; ++i) {
if (filter(list->elements[i])) {
filteredResult->elements[filteredIndex] = list->elements[i];
filteredIndex++;
}
}
return filteredResult;
}
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 *mappedResult = new_list(list->length, NULL);
if (mappedResult == NULL) {
return NULL;
}
for (size_t i = 0; i < list->length; ++i) {
mappedResult->elements[i] = map(list->elements[i]);
}
return mappedResult;
}
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 result = initial;
for (size_t i = 0; i < list->length; ++i) {
result = foldl(result, list->elements[i]);
}
return result;
}
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 foldResult = initial;
for (size_t i = 0; i < list->length; ++i) {
foldResult = foldr(list->elements[list->length - 1 - i], foldResult);
}
return foldResult;
}
list_t *reverse_list(list_t *list) {
if (!list) {
return NULL;
}
list_t *reversedResult = new_list(list->length, NULL);
if (!reversedResult) {
return NULL;
}
for (size_t i = list->length; i > 0; --i) {
reversedResult->elements[list->length - i] = list->elements[i - 1];
}
return reversedResult;
}
void delete_list(list_t *list) {
if (list != NULL) {
free(list);
}
}