pvjc24/cv4/list_ops.c
2024-03-10 19:23:07 +01:00

142 lines
3.4 KiB
C

#include "list_ops.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
list_t *new_list(size_t length, list_element_t elements[]) {
list_t *newList = (list_t *)malloc(sizeof(list_t) + length * sizeof(list_element_t));
if (newList == NULL) {
return NULL;
}
newList->length = length;
memcpy(newList->elements, elements, length * sizeof(list_element_t));
return newList;
}
list_t *append_list(list_t *list1, list_t *list2) {
if (list1 == NULL || list2 == NULL) {
return NULL;
}
list_t *combinedList = (list_t *)malloc(sizeof(list_t) + (list1->length + list2->length) * sizeof(list_element_t));
if (combinedList == NULL) {
return NULL;
}
combinedList->length = list1->length + list2->length;
memcpy(combinedList->elements, list1->elements, list1->length * sizeof(list_element_t));
memcpy(combinedList->elements + list1->length, list2->elements, list2->length * sizeof(list_element_t));
return combinedList;
}
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;
}
}
list_t *filteredList = (list_t *)malloc(sizeof(list_t) + count * sizeof(list_element_t));
if (filteredList == NULL) {
return NULL;
}
filteredList->length = count;
for (size_t i = 0, filteredIndex = 0; i < list->length; ++i) {
if (filter(list->elements[i])) {
filteredList->elements[filteredIndex++] = list->elements[i];
}
}
return filteredList;
}
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 *mappedList = (list_t *)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
if (mappedList == NULL) {
return NULL;
}
mappedList->length = list->length;
for (size_t i = 0; i < list->length; ++i) {
mappedList->elements[i] = map(list->elements[i]);
}
return mappedList;
}
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) {
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 == NULL) {
return initial;
}
list_element_t result = initial;
for (size_t i = list->length; i > 0; --i) {
result = foldr(list->elements[i - 1], result);
}
return result;
}
list_t *reverse_list(list_t *list) {
if (list == NULL) {
return NULL;
}
list_t *reversedList = (list_t *)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
if (reversedList == NULL) {
return NULL;
}
reversedList->length = list->length;
for (size_t i = 0; i < list->length; ++i) {
reversedList->elements[i] = list->elements[list->length - i - 1];
}
return reversedList;
}
void delete_list(list_t *list) {
if (list != NULL) {
free(list);
}
}