pvjc24/cv4/list_ops.c

87 lines
2.6 KiB
C
Raw Normal View History

2024-03-10 15:16:55 +00:00
#include "list_ops.h"
2024-03-10 15:32:35 +00:00
#include <string.h>
2024-03-10 15:16:55 +00:00
list_t *new_list(size_t length, list_element_t elements[]){
list_t* list = (list_t*)malloc(sizeof(list_t) + length * sizeof(list_element_t));
list->length = length;
memcpy(list->elements,elements, length*sizeof(list_element_t));
return list;
}
list_t *append_list(list_t *list1, list_t *list2){
2024-03-10 16:37:21 +00:00
list_t* list3 = (list_t*)malloc(sizeof(list_t) + (list1->length+list2->length) * sizeof(list_element_t));
list3->length = list1->length+list2->length;
memcpy(list3->elements,list1->elements, list1->length*sizeof(list_element_t));
2024-03-10 17:42:57 +00:00
memcpy(list3->elements+list1->length,list2->elements, list2->length*sizeof(list_element_t));
2024-03-10 15:16:55 +00:00
2024-03-10 16:37:21 +00:00
return list3;
2024-03-10 15:16:55 +00:00
}
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){
2024-03-10 17:42:57 +00:00
for(size_t i=0; i<list->length; i++){
if(!filter(list->elements[i])){
for (size_t j = i; j < list->length - 1; j++) {
list->elements[j] = list->elements[j + 1];
}
list->length=list->length-1;
}
}
list_t *list_new = (list_t*)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
list_new->length=list->length;
memcpy(list_new->elements,list->elements, list->length*sizeof(list_element_t));
return list_new;
2024-03-10 15:16:55 +00:00
}
size_t length_list(list_t *list){
2024-03-10 17:42:57 +00:00
return list->length;
2024-03-10 15:16:55 +00:00
}
list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)){
2024-03-10 17:42:57 +00:00
for(size_t i=0; i<list->length; i++){
list->elements[i]=map(list->elements[i]);
}
list_t *list_new = (list_t*)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
list_new->length=list->length;
memcpy(list_new->elements,list->elements, list->length*sizeof(list_element_t));
return list_new;
2024-03-10 15:16:55 +00:00
}
2024-03-10 15:32:35 +00:00
list_element_t foldl_list(list_t *list, list_element_t initial,list_element_t (*foldl)(list_element_t,list_element_t)){
2024-03-10 17:42:57 +00:00
list_element_t fold_step=initial;
for(size_t i=0; i<list->length; i++){
fold_step=foldl(fold_step,list->elements[i]);
}
return fold_step;
2024-03-10 15:16:55 +00:00
}
2024-03-10 15:32:35 +00:00
list_element_t foldr_list(list_t *list, list_element_t initial,list_element_t (*foldr)(list_element_t,list_element_t)){
2024-03-10 17:42:57 +00:00
list_element_t fold_step=initial;
if(list->length>0){
for(size_t i=list->length-1; i!=0; i--){
fold_step=foldr(list->elements[i],fold_step);
}
fold_step=foldr(list->elements[0],fold_step);
}
return fold_step;
2024-03-10 15:16:55 +00:00
}
list_t *reverse_list(list_t *list){
2024-03-10 17:42:57 +00:00
list_t *list_new = (list_t*)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
list_new->length=list->length;
size_t i,j;
for( i=list->length-1 , j=0 ; j<list->length; j++, i--){
list_new->elements[i]=list->elements[j];
}
return list_new;
2024-03-10 15:16:55 +00:00
}
void delete_list(list_t *list){
free(list);
}