2026-04-13 17:55:45 -06:00
|
|
|
#include "arraylist.h"
|
2026-04-14 20:30:02 -06:00
|
|
|
#include <stdatomic.h>
|
2026-04-13 17:55:45 -06:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdlib.h>
|
2026-04-14 19:33:15 -06:00
|
|
|
#include <string.h>
|
2026-04-13 17:55:45 -06:00
|
|
|
|
2026-04-14 20:30:02 -06:00
|
|
|
struct ArrayList{
|
|
|
|
|
void *data;
|
|
|
|
|
size_t capacity;
|
|
|
|
|
size_t len;
|
|
|
|
|
size_t elem_size;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Grow and shrink factor is 2, 1.5 might give different results i guess
|
|
|
|
|
// but i'm basing myself in that Rust implementation of array does use
|
|
|
|
|
// 2 as the factor so i guess is good :)
|
|
|
|
|
static ArrayListErr arraylist_grow(ArrayList *arr);
|
|
|
|
|
static ArrayListErr arraylist_shrink(ArrayList *arr);
|
|
|
|
|
|
|
|
|
|
ArrayList *arraylist_init(size_t capacity, size_t elem_size) {
|
2026-04-13 17:55:45 -06:00
|
|
|
if (capacity == 0) {
|
2026-04-14 20:30:02 -06:00
|
|
|
return NULL;
|
2026-04-13 17:55:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (elem_size == 0) {
|
2026-04-14 20:30:02 -06:00
|
|
|
return NULL;
|
2026-04-13 17:55:45 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-14 20:30:02 -06:00
|
|
|
ArrayList *arr = malloc(sizeof(ArrayList));
|
|
|
|
|
if (arr == NULL) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
arr->capacity = capacity;
|
|
|
|
|
arr->elem_size = elem_size;
|
|
|
|
|
arr->len = 0;
|
|
|
|
|
arr->data = malloc(capacity * elem_size);
|
|
|
|
|
if (arr->data == NULL) {
|
|
|
|
|
free(arr);
|
|
|
|
|
return NULL;
|
2026-04-13 17:55:45 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-14 20:30:02 -06:00
|
|
|
return arr;
|
2026-04-13 17:55:45 -06:00
|
|
|
}
|
2026-04-14 19:33:15 -06:00
|
|
|
|
|
|
|
|
ArrayListErr arraylist_destroy(ArrayList *arr) {
|
|
|
|
|
if (arr == NULL) {
|
|
|
|
|
return ARRLIST_NULL_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arr->capacity = 0;
|
|
|
|
|
arr->elem_size = 0;
|
|
|
|
|
arr->len = 0;
|
|
|
|
|
free(arr->data);
|
|
|
|
|
arr->data = NULL;
|
2026-04-14 20:30:02 -06:00
|
|
|
free(arr);
|
2026-04-14 19:33:15 -06:00
|
|
|
return ARRLIST_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayListErr arraylist_clear(ArrayList *arr) {
|
|
|
|
|
if (arr == NULL) {
|
|
|
|
|
return ARRLIST_NULL_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(arr->data, 0, arr->capacity * arr->elem_size);
|
|
|
|
|
arr->capacity = 0;
|
|
|
|
|
arr->elem_size = 0;
|
|
|
|
|
arr->len = 0;
|
|
|
|
|
return ARRLIST_OK;
|
|
|
|
|
}
|
2026-04-14 19:38:56 -06:00
|
|
|
|
2026-04-14 20:30:02 -06:00
|
|
|
size_t arraylist_size(const ArrayList *arr) {
|
2026-04-14 19:38:56 -06:00
|
|
|
if (arr == NULL) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return arr->len;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 20:30:02 -06:00
|
|
|
size_t arraylist_capacity(const ArrayList *arr) {
|
2026-04-14 19:38:56 -06:00
|
|
|
if (arr == NULL) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return arr->capacity;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 20:30:02 -06:00
|
|
|
bool arraylist_is_empty(const ArrayList *arr) {
|
2026-04-14 19:38:56 -06:00
|
|
|
if (arr == NULL) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return arr->capacity == 0;
|
|
|
|
|
}
|
2026-04-14 20:30:02 -06:00
|
|
|
|
|
|
|
|
static ArrayListErr arraylist_grow(ArrayList *arr) {
|
|
|
|
|
// We can asume is not null because is internal
|
|
|
|
|
// and only used by funtions that already verified
|
|
|
|
|
// arr is not null, same with shrink.
|
|
|
|
|
size_t new_capacity = arr->capacity * 2;
|
|
|
|
|
void *tmp = realloc(arr->data, new_capacity * arr->elem_size);
|
|
|
|
|
if (tmp == NULL) {
|
|
|
|
|
return ARRLIST_BAD_ALLOC;
|
|
|
|
|
}
|
|
|
|
|
arr->capacity = new_capacity;
|
|
|
|
|
arr->data = tmp;
|
|
|
|
|
return ARRLIST_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ArrayListErr arraylist_shrink(ArrayList *arr) {
|
|
|
|
|
size_t new_capacity = arr->capacity / 2;
|
|
|
|
|
void *tmp = realloc(arr->data, new_capacity *arr->elem_size);
|
|
|
|
|
if (tmp == NULL) {
|
|
|
|
|
return ARRLIST_BAD_ALLOC;
|
|
|
|
|
}
|
|
|
|
|
arr->capacity = new_capacity;
|
|
|
|
|
arr->data = tmp;
|
|
|
|
|
return ARRLIST_OK;
|
|
|
|
|
}
|