pvjc24/cv4/list_ops.c

162 lines
4.0 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>
2024-03-10 18:18:22 +00:00
#include <string.h>
2024-03-10 18:21:26 +00:00
#include <stdio.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[]) {
2024-03-10 18:18:22 +00:00
list_t *newList = (list_t *)malloc(sizeof(list_t) + length * sizeof(list_element_t));
if (newList == NULL) {
2024-03-10 15:43:14 +00:00
return NULL;
2024-03-10 15:07:14 +00:00
}
2024-03-10 18:18:22 +00:00
newList->length = length;
memcpy(newList->elements, elements, length * sizeof(list_element_t));
2024-03-10 15:07:14 +00:00
2024-03-10 18:18:22 +00:00
return newList;
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) {
2024-03-10 18:18:22 +00:00
if (list1 == NULL || list2 == NULL) {
return NULL;
2024-03-10 15:52:00 +00:00
}
2024-03-10 18:18:22 +00:00
list_t *combinedList = (list_t *)malloc(sizeof(list_t) + (list1->length + list2->length) * sizeof(list_element_t));
if (combinedList == NULL) {
2024-03-10 15:52:00 +00:00
return NULL;
}
2024-03-10 18:18:22 +00:00
combinedList->length = list1->length + list2->length;
2024-03-10 15:52:00 +00:00
2024-03-10 18:18:22 +00:00
memcpy(combinedList->elements, list1->elements, list1->length * sizeof(list_element_t));
memcpy(combinedList->elements + list1->length, list2->elements, list2->length * sizeof(list_element_t));
2024-03-10 15:52:00 +00:00
2024-03-10 18:18:22 +00:00
return combinedList;
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;
}
2024-03-10 18:21:26 +00:00
list_t *filteredList = (list_t *)malloc(sizeof(list_t));
2024-03-10 18:18:22 +00:00
if (filteredList == NULL) {
return NULL;
}
2024-03-10 15:52:00 +00:00
size_t count = 0;
for (size_t i = 0; i < list->length; ++i) {
if (filter(list->elements[i])) {
2024-03-10 18:18:22 +00:00
++count;
2024-03-10 15:52:00 +00:00
}
}
2024-03-10 18:18:22 +00:00
filteredList->length = count;
2024-03-10 15:52:00 +00:00
2024-03-10 18:21:26 +00:00
filteredList->elements = (list_element_t *)malloc(count * sizeof(list_element_t));
if (filteredList->elements == NULL) {
free(filteredList);
return NULL;
}
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 18:18:22 +00:00
filteredList->elements[filteredIndex++] = list->elements[i];
2024-03-10 15:52:00 +00:00
}
}
2024-03-10 18:18:22 +00:00
return filteredList;
2024-03-10 15:52:00 +00:00
}
size_t length_list(list_t *list) {
if (list == NULL) {
return 0;
}
return list->length;
}
2024-03-10 18:21:26 +00:00
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;
mappedList->elements = (list_element_t *)malloc(list->length * sizeof(list_element_t));
if (mappedList->elements == NULL) {
free(mappedList);
return NULL;
}
for (size_t i = 0; i < list->length; ++i) {
mappedList->elements[i] = map(list->elements[i]);
}
return mappedList;
}
2024-03-10 18:18:22 +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) {
2024-03-10 15:52:00 +00:00
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 18:18:22 +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 == NULL) {
2024-03-10 15:59:32 +00:00
return initial;
}
2024-03-10 18:18:22 +00:00
list_element_t result = initial;
2024-03-10 15:59:32 +00:00
2024-03-10 18:18:22 +00:00
for (size_t i = list->length; i > 0; --i) {
result = foldr(list->elements[i - 1], result);
2024-03-10 15:59:32 +00:00
}
2024-03-10 18:18:22 +00:00
return result;
2024-03-10 15:59:32 +00:00
}
2024-03-10 15:52:00 +00:00
list_t *reverse_list(list_t *list) {
2024-03-10 18:18:22 +00:00
if (list == NULL) {
2024-03-10 15:52:00 +00:00
return NULL;
}
2024-03-10 18:18:22 +00:00
list_t *reversedList = (list_t *)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
if (reversedList == NULL) {
2024-03-10 15:52:00 +00:00
return NULL;
}
2024-03-10 18:18:22 +00:00
reversedList->length = list->length;
2024-03-10 18:21:26 +00:00
reversedList->elements = (list_element_t *)malloc(list->length * sizeof(list_element_t));
if (reversedList->elements == NULL) {
free(reversedList);
return NULL;
}
2024-03-10 18:18:22 +00:00
for (size_t i = 0; i < list->length; ++i) {
reversedList->elements[i] = list->elements[list->length - i - 1];
2024-03-10 15:52:00 +00:00
}
2024-03-10 18:18:22 +00:00
return reversedList;
2024-03-10 15:52:00 +00:00
}
void delete_list(list_t *list) {
if (list != NULL) {
2024-03-10 18:21:26 +00:00
free(list->elements);
2024-03-10 15:52:00 +00:00
free(list);
}
}