This commit is contained in:
Michal Utľák 2024-03-10 19:18:22 +01:00
parent c910519200
commit e1d5ba22cf

View File

@ -1,53 +1,38 @@
#include "list_ops.h" #include "list_ops.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <string.h>
list_t *new_list(size_t length, list_element_t elements[]) { 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(sizeof(list_t) + length * sizeof(list_element_t));
if (newList == NULL) {
list_t *resultList = (list_t *)malloc(size);
if (resultList == NULL) {
return NULL; return NULL;
} }
resultList->length = length; newList->length = length;
memcpy(newList->elements, elements, length * sizeof(list_element_t));
for (size_t i = 0; i < length; ++i) { return newList;
resultList->elements[i] = elements[i];
}
return resultList;
} }
list_t *append_list(list_t *list1, list_t *list2) { list_t *append_list(list_t *list1, list_t *list2) {
if (list1 == NULL) { if (list1 == NULL || list2 == 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; return NULL;
} }
resultList->length = list1->length + list2->length; list_t *combinedList = (list_t *)malloc(sizeof(list_t) + (list1->length + list2->length) * sizeof(list_element_t));
if (combinedList == NULL) {
for (size_t i = 0; i < list1->length; ++i) { return NULL;
resultList->elements[i] = list1->elements[i];
} }
for (size_t i = 0; i < list2->length; ++i) { combinedList->length = list1->length + list2->length;
resultList->elements[list1->length + i] = list2->elements[i];
}
return resultList; 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)) { list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
@ -55,68 +40,41 @@ list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
return NULL; return NULL;
} }
size_t count = 0; list_t *filteredList = (list_t *)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
for (size_t i = 0; i < list->length; ++i) { if (filteredList == NULL) {
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; return NULL;
} }
filteredResult->length = count; size_t count = 0;
for (size_t i = 0; i < list->length; ++i) {
if (filter(list->elements[i])) {
++count;
}
}
filteredList->length = count;
memcpy(filteredList->elements, list->elements, count * sizeof(list_element_t));
size_t filteredIndex = 0; size_t filteredIndex = 0;
for (size_t i = 0; i < list->length; ++i) { for (size_t i = 0; i < list->length; ++i) {
if (filter(list->elements[i])) { if (filter(list->elements[i])) {
filteredResult->elements[filteredIndex] = list->elements[i]; filteredList->elements[filteredIndex++] = list->elements[i];
filteredIndex++;
} }
} }
return filteredResult; return filteredList;
} }
size_t length_list(list_t *list) { size_t length_list(list_t *list) {
if (list == NULL) { if (list == NULL) {
return 0; return 0;
} }
return list->length; return list->length;
} }
list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)) { 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 || map == NULL) { if (list == 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; return initial;
} }
@ -129,38 +87,37 @@ list_element_t foldl_list(list_t *list, list_element_t initial,
return result; return result;
} }
list_element_t foldr_list(list_t *list, list_element_t initial, list_element_t foldr_list(list_t *list, list_element_t initial, list_element_t (*foldr)(list_element_t, list_element_t)) {
list_element_t (*foldr)(list_element_t, if (list == NULL) {
list_element_t)) {
if (!list || !foldr) {
return initial; return initial;
} }
list_element_t foldResult = initial; list_element_t result = initial;
for (size_t i = 0; i < list->length; ++i) { for (size_t i = list->length; i > 0; --i) {
foldResult = foldr(list->elements[list->length - 1 - i], foldResult); result = foldr(list->elements[i - 1], result);
} }
return foldResult; return result;
} }
list_t *reverse_list(list_t *list) { list_t *reverse_list(list_t *list) {
if (!list) { if (list == NULL) {
return NULL; return NULL;
} }
list_t *reversedResult = new_list(list->length, NULL); list_t *reversedList = (list_t *)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
if (reversedList == NULL) {
if (!reversedResult) {
return NULL; return NULL;
} }
for (size_t i = list->length; i > 0; --i) { reversedList->length = list->length;
reversedResult->elements[list->length - i] = list->elements[i - 1];
for (size_t i = 0; i < list->length; ++i) {
reversedList->elements[i] = list->elements[list->length - i - 1];
} }
return reversedResult; return reversedList;
} }
void delete_list(list_t *list) { void delete_list(list_t *list) {