pvjc24/cv4/list_ops.c

171 lines
3.8 KiB
C
Raw Normal View History

2024-03-10 14:35:01 +00:00
#include "list_ops.h"
2024-03-10 15:43:14 +00:00
#include <stdlib.h>
#include <stdbool.h>
2024-03-10 14:35:01 +00:00
2024-03-10 15:43:14 +00:00
list_t *new_list(size_t length, list_element_t elements[]) {
size_t size = sizeof(list_t) + length * sizeof(list_element_t);
2024-03-10 15:07:14 +00:00
2024-03-10 16:28:12 +00:00
list_t *resultList = (list_t *)malloc(size);
2024-03-10 15:07:14 +00:00
2024-03-10 16:28:12 +00:00
if (resultList == NULL) {
2024-03-10 15:43:14 +00:00
return NULL;
2024-03-10 15:07:14 +00:00
}
2024-03-10 16:28:12 +00:00
resultList->length = length;
2024-03-10 14:35:01 +00:00
2024-03-10 15:43:14 +00:00
for (size_t i = 0; i < length; ++i) {
2024-03-10 16:28:12 +00:00
resultList->elements[i] = elements[i];
2024-03-10 15:07:14 +00:00
}
2024-03-10 16:28:12 +00:00
return resultList;
2024-03-10 15:07:14 +00:00
}
2024-03-10 15:52:00 +00:00
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);
2024-03-10 16:28:12 +00:00
list_t *resultList = (list_t *)malloc(totalSize);
2024-03-10 15:52:00 +00:00
2024-03-10 16:28:12 +00:00
if (!resultList) {
2024-03-10 15:52:00 +00:00
return NULL;
}
2024-03-10 16:28:12 +00:00
resultList->length = list1->length + list2->length;
2024-03-10 15:52:00 +00:00
for (size_t i = 0; i < list1->length; ++i) {
2024-03-10 16:28:12 +00:00
resultList->elements[i] = list1->elements[i];
2024-03-10 15:52:00 +00:00
}
for (size_t i = 0; i < list2->length; ++i) {
2024-03-10 16:28:12 +00:00
resultList->elements[list1->length + i] = list2->elements[i];
2024-03-10 15:52:00 +00:00
}
2024-03-10 16:28:12 +00:00
return resultList;
2024-03-10 15:52:00 +00:00
}
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);
2024-03-10 16:28:12 +00:00
list_t *filteredResult = (list_t *)malloc(totalSize);
2024-03-10 15:52:00 +00:00
2024-03-10 16:28:12 +00:00
if (!filteredResult) {
2024-03-10 15:52:00 +00:00
return NULL;
}
2024-03-10 16:28:12 +00:00
filteredResult->length = count;
2024-03-10 15:52:00 +00:00
size_t filteredIndex = 0;
for (size_t i = 0; i < list->length; ++i) {
if (filter(list->elements[i])) {
2024-03-10 16:28:12 +00:00
filteredResult->elements[filteredIndex] = list->elements[i];
2024-03-10 15:52:00 +00:00
filteredIndex++;
}
}
2024-03-10 16:28:12 +00:00
return filteredResult;
2024-03-10 15:52:00 +00:00
}
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;
}
2024-03-10 16:28:12 +00:00
list_t *mappedResult = new_list(list->length, NULL);
2024-03-10 15:52:00 +00:00
2024-03-10 16:28:12 +00:00
if (mappedResult == NULL) {
2024-03-10 15:52:00 +00:00
return NULL;
}
for (size_t i = 0; i < list->length; ++i) {
2024-03-10 16:28:12 +00:00
mappedResult->elements[i] = map(list->elements[i]);
2024-03-10 15:52:00 +00:00
}
2024-03-10 16:28:12 +00:00
return mappedResult;
2024-03-10 15:52:00 +00:00
}
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;
}
2024-03-10 16:28:12 +00:00
list_element_t result = initial;
2024-03-10 15:52:00 +00:00
for (size_t i = 0; i < list->length; ++i) {
2024-03-10 16:28:12 +00:00
result = foldl(result, list->elements[i]);
2024-03-10 15:52:00 +00:00
}
2024-03-10 16:28:12 +00:00
return result;
2024-03-10 15:52:00 +00:00
}
2024-03-10 15:59:32 +00:00
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;
}
2024-03-10 16:28:12 +00:00
list_element_t foldResult = initial;
2024-03-10 15:59:32 +00:00
for (size_t i = 0; i < list->length; ++i) {
2024-03-10 16:28:12 +00:00
foldResult = foldr(list->elements[list->length - 1 - i], foldResult);
2024-03-10 15:59:32 +00:00
}
2024-03-10 16:28:12 +00:00
return foldResult;
2024-03-10 15:59:32 +00:00
}
2024-03-10 15:52:00 +00:00
list_t *reverse_list(list_t *list) {
if (!list) {
return NULL;
}
2024-03-10 16:28:12 +00:00
list_t *reversedResult = new_list(list->length, NULL);
2024-03-10 15:52:00 +00:00
2024-03-10 16:28:12 +00:00
if (!reversedResult) {
2024-03-10 15:52:00 +00:00
return NULL;
}
for (size_t i = list->length; i > 0; --i) {
2024-03-10 16:28:12 +00:00
reversedResult->elements[list->length - i] = list->elements[i - 1];
2024-03-10 15:52:00 +00:00
}
2024-03-10 16:28:12 +00:00
return reversedResult;
2024-03-10 15:52:00 +00:00
}
void delete_list(list_t *list) {
if (list != NULL) {
free(list);
}
}