2022-11-01 22:09:32 +01:00
|
|
|
#include "arraylist.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
ArrayList * list() {
|
|
|
|
ArrayList *this = malloc(sizeof(ArrayList));
|
|
|
|
this->capacity = 4;
|
|
|
|
this->size = 0;
|
|
|
|
this->items = malloc(this->capacity * sizeof(void *));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void * get_list(ArrayList *this, int index) {
|
|
|
|
if (index < 0 || this->size < index) {
|
|
|
|
fprintf(stderr, "reading outside the bounds of an arraylist, index=%d, size=%d\n", index, this->size);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return this->items[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_list(ArrayList *this, void *item) {
|
|
|
|
if (this->size == this->capacity) {
|
|
|
|
this->capacity *= 2;
|
|
|
|
this->items = realloc(this->items, this->capacity * sizeof(void *));
|
|
|
|
}
|
|
|
|
this->items[this->size++] = item;
|
|
|
|
}
|
|
|
|
|
2022-11-08 23:31:00 +01:00
|
|
|
void * fold_list(ArrayList *this, void *init, void * (*f)(void *acc, int index, void *item)) {
|
2022-11-01 22:09:32 +01:00
|
|
|
int i;
|
|
|
|
for(i = 0; i < this->size; i++) {
|
2022-11-08 23:31:00 +01:00
|
|
|
init = f(init, i, this->items[i]);
|
2022-11-01 22:09:32 +01:00
|
|
|
}
|
2022-11-08 23:31:00 +01:00
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
|
|
|
void * free_listitem(void *acc, int index, void *item) {
|
|
|
|
free(item);
|
|
|
|
return NULL;
|
2022-11-01 22:09:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_list(ArrayList *this) {
|
|
|
|
free(this->items);
|
|
|
|
free(this);
|
|
|
|
}
|