87 lines
2.6 KiB
C
87 lines
2.6 KiB
C
|
#include "list_ops.h"
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
list_t *new_list(size_t length, list_element_t elements[]) {
|
||
|
list_t *list = malloc(sizeof(list_t) + length * sizeof(list_element_t));
|
||
|
if (list == NULL) {
|
||
|
return NULL;
|
||
|
}
|
||
|
list->length = length;
|
||
|
for (size_t i = 0; i < length; i++) {
|
||
|
list->elements[i] = elements[i];
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
list_t *append_list(list_t *list1, list_t *list2) {
|
||
|
size_t new_length = list1->length + list2->length;
|
||
|
list_t *new_list = realloc(list1, sizeof(list_t) + new_length * sizeof(list_element_t));
|
||
|
if (new_list == NULL) {
|
||
|
return NULL;
|
||
|
}
|
||
|
for (size_t i = 0; i < list2->length; i++) {
|
||
|
new_list->elements[list1->length + i] = list2->elements[i];
|
||
|
}
|
||
|
new_list->length = new_length;
|
||
|
return new_list;
|
||
|
}
|
||
|
|
||
|
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
|
||
|
list_t *new_list = malloc(sizeof(list_t));
|
||
|
new_list->length = 0;
|
||
|
new_list->elements[0] = 0;
|
||
|
|
||
|
for (size_t i = 0; i < list->length; i++) {
|
||
|
if (filter(list->elements[i])) {
|
||
|
new_list = realloc(new_list, sizeof(list_t) + (new_list->length + 1) * sizeof(list_element_t));
|
||
|
new_list->elements[new_list->length++] = list->elements[i];
|
||
|
}
|
||
|
}
|
||
|
return new_list;
|
||
|
}
|
||
|
|
||
|
size_t length_list(list_t *list) {
|
||
|
return list->length;
|
||
|
}
|
||
|
|
||
|
list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)) {
|
||
|
list_t *new_list = malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
|
||
|
new_list->length = list->length;
|
||
|
for (size_t i = 0; i < list->length; i++) {
|
||
|
new_list->elements[i] = map(list->elements[i]);
|
||
|
}
|
||
|
return new_list;
|
||
|
}
|
||
|
|
||
|
list_element_t foldl_list(list_t *list, list_element_t initial, list_element_t (*foldl)(list_element_t, list_element_t)) {
|
||
|
list_element_t accumulator = initial;
|
||
|
for (size_t i = 0; i < list->length; i++) {
|
||
|
accumulator = foldl(list->elements[i], accumulator);
|
||
|
}
|
||
|
return accumulator;
|
||
|
}
|
||
|
|
||
|
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 accumulator = initial;
|
||
|
for (int i = list->length - 1; i >= 0; i--) {
|
||
|
accumulator = foldr(list->elements[i], accumulator);
|
||
|
}
|
||
|
return accumulator;
|
||
|
}
|
||
|
|
||
|
list_t *reverse_list(list_t *list) {
|
||
|
list_t *new_list = malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
|
||
|
new_list->length = list->length;
|
||
|
for (size_t i = 0; i < list->length; i++) {
|
||
|
new_list->elements[i] = list->elements[list->length - 1 - i];
|
||
|
}
|
||
|
return new_list;
|
||
|
}
|
||
|
|
||
|
void delete_list(list_t *list) {
|
||
|
free(list);
|
||
|
}
|
||
|
|
||
|
|