pvjc24/cv4/list_ops.c

167 lines
3.7 KiB
C
Raw Permalink Normal View History

2024-03-13 09:25:40 +00:00
#include "list_ops.h"
2024-03-12 14:19:24 +00:00
2024-03-13 09:39:29 +00:00
// Constructs a new list
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) {
list->length = length;
if (elements) {
for (size_t i = 0; i < length; i++) {
list->elements[i] = elements[i];
}
}
}
return list;
}
2024-03-12 14:19:24 +00:00
2024-03-13 09:39:29 +00:00
// Appends entries to a list and returns the new list
list_t *append_list(list_t *list1, list_t *list2) {
size_t length1 = list1->length;
size_t length2 = list2->length;
size_t new_length = length1 + length2;
list_t *new_list = malloc(sizeof(list_t) + new_length * sizeof(list_element_t));
if (new_list) {
new_list->length = new_length;
for (size_t i = 0; i < length1; i++) {
new_list->elements[i] = list1->elements[i];
}
for (size_t i = 0; i < length2; i++) {
new_list->elements[length1 + i] = list2->elements[i];
}
}
return new_list;
}
2024-03-12 14:19:24 +00:00
2024-03-13 09:39:29 +00:00
// Filters list returning only values that satisfy the filter function
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
size_t length = list->length;
size_t new_length = 0;
// Count the number of elements that satisfy the filter
for (size_t i = 0; i < length; i++) {
if (filter(list->elements[i])) {
new_length++;
}
}
// Create a new list with filtered elements
list_t *new_list = malloc(sizeof(list_t) + new_length * sizeof(list_element_t));
if (new_list) {
new_list->length = new_length;
size_t j = 0;
for (size_t i = 0; i < length; i++) {
if (filter(list->elements[i])) {
new_list->elements[j++] = list->elements[i];
}
}
}
return new_list;
}
2024-03-12 14:19:24 +00:00
2024-03-13 09:39:29 +00:00
// Returns the length of the list
size_t length_list(list_t *list) {
return list->length;
}
2024-03-12 14:19:24 +00:00
2024-03-13 09:39:29 +00:00
// Returns a list of elements whose values equal the list value transformed by the mapping function
list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)) {
size_t length = list->length;
list_t *new_list = malloc(sizeof(list_t) + length * sizeof(list_element_t));
if (new_list) {
new_list->length = length;
for (size_t i = 0; i < length; i++) {
new_list->elements[i] = map(list->elements[i]);
}
}
return new_list;
}
2024-03-12 14:19:24 +00:00
2024-03-13 09:39:29 +00:00
// Folds (reduces) the given list from the left with a function
2024-03-12 14:19:24 +00:00
list_element_t foldl_list(list_t *list, list_element_t initial,
2024-03-13 09:39:29 +00:00
list_element_t (*foldl)(list_element_t, list_element_t)) {
size_t length = list->length;
list_element_t result = initial;
for (size_t i = 0; i < length; i++) {
result = foldl(result, list->elements[i]);
}
return result;
}
2024-03-12 14:19:24 +00:00
2024-03-13 09:39:29 +00:00
// Folds (reduces) the given list from the right with a function
2024-03-12 14:19:24 +00:00
list_element_t foldr_list(list_t *list, list_element_t initial,
2024-03-13 09:39:29 +00:00
list_element_t (*foldr)(list_element_t, list_element_t)) {
size_t length = list->length;
list_element_t result = initial;
for (size_t i = length; i > 0; i--) {
result = foldr(list->elements[i - 1], result);
}
return result;
}
// Reverse the elements of the list
list_t *reverse_list(list_t *list) {
size_t length = list->length;
list_t *new_list = malloc(sizeof(list_t) + length * sizeof(list_element_t));
if (new_list) {
new_list->length = length;
for (size_t i = 0; i < length; i++) {
new_list->elements[i] = list->elements[length - i - 1];
}
}
return new_list;
}
// Destroy the entire list
void delete_list(list_t *list) {
free(list);
}
2024-03-12 14:19:24 +00:00
2024-03-12 14:26:23 +00:00