rierier
This commit is contained in:
parent
c910519200
commit
e1d5ba22cf
139
cv4/list_ops.c
139
cv4/list_ops.c
@ -1,53 +1,38 @@
|
||||
|
||||
|
||||
#include "list_ops.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.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) {
|
||||
list_t *newList = (list_t *)malloc(sizeof(list_t) + length * sizeof(list_element_t));
|
||||
if (newList == 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) {
|
||||
resultList->elements[i] = elements[i];
|
||||
}
|
||||
|
||||
return resultList;
|
||||
return newList;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (list1 == NULL || list2 == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
resultList->length = list1->length + list2->length;
|
||||
|
||||
for (size_t i = 0; i < list1->length; ++i) {
|
||||
resultList->elements[i] = list1->elements[i];
|
||||
list_t *combinedList = (list_t *)malloc(sizeof(list_t) + (list1->length + list2->length) * sizeof(list_element_t));
|
||||
if (combinedList == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < list2->length; ++i) {
|
||||
resultList->elements[list1->length + i] = list2->elements[i];
|
||||
}
|
||||
combinedList->length = list1->length + list2->length;
|
||||
|
||||
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)) {
|
||||
@ -55,68 +40,41 @@ list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
|
||||
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) {
|
||||
list_t *filteredList = (list_t *)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
|
||||
if (filteredList == 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;
|
||||
for (size_t i = 0; i < list->length; ++i) {
|
||||
if (filter(list->elements[i])) {
|
||||
filteredResult->elements[filteredIndex] = list->elements[i];
|
||||
filteredIndex++;
|
||||
filteredList->elements[filteredIndex++] = list->elements[i];
|
||||
}
|
||||
}
|
||||
|
||||
return filteredResult;
|
||||
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 *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) {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -129,38 +87,37 @@ list_element_t foldl_list(list_t *list, list_element_t initial,
|
||||
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) {
|
||||
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 foldResult = initial;
|
||||
list_element_t result = initial;
|
||||
|
||||
for (size_t i = 0; i < list->length; ++i) {
|
||||
foldResult = foldr(list->elements[list->length - 1 - i], foldResult);
|
||||
for (size_t i = list->length; i > 0; --i) {
|
||||
result = foldr(list->elements[i - 1], result);
|
||||
}
|
||||
|
||||
return foldResult;
|
||||
return result;
|
||||
}
|
||||
|
||||
list_t *reverse_list(list_t *list) {
|
||||
if (!list) {
|
||||
if (list == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_t *reversedResult = new_list(list->length, NULL);
|
||||
|
||||
if (!reversedResult) {
|
||||
list_t *reversedList = (list_t *)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
|
||||
if (reversedList == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (size_t i = list->length; i > 0; --i) {
|
||||
reversedResult->elements[list->length - i] = list->elements[i - 1];
|
||||
reversedList->length = list->length;
|
||||
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user