2026-04-13 17:55:45 -06:00
|
|
|
#include "arraylist.h"
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
ArrayListResult arraylist_init(size_t capacity, size_t elem_size) {
|
|
|
|
|
if (capacity == 0) {
|
|
|
|
|
return (ArrayListResult) {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = ARRLIST_INVALID_CAPACITY};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (elem_size == 0) {
|
|
|
|
|
return (ArrayListResult) {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = ARRLIST_INVALID_ELEM_SIZE,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList arr = {
|
|
|
|
|
.data = malloc(capacity * elem_size),
|
|
|
|
|
.capacity = capacity,
|
|
|
|
|
.elem_size = elem_size,
|
|
|
|
|
.len = 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (arr.data == NULL) {
|
|
|
|
|
return (ArrayListResult) {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = ARRLIST_BAD_ALLOC,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (ArrayListResult) {.is_valid = true, .array = arr};
|
|
|
|
|
}
|