Files
ltypes/array.h

55 lines
1.1 KiB
C
Raw Normal View History

2026-06-09 07:23:41 -05:00
#ifndef LTYPES_ARRAY_H
#define LTYPES_ARRAY_H
#define L_TYPES_AUTHOR "Lucielle <luci@git.lunarware.tech"
#define L_TYPES_REPO "https://git.lunarware.tech/lucielle/ltypes.git"
#define L_TYPES_VERSION "0.1.0"
#define L_TYPES_LICENSE "GNU GPL v3"
#include <stddef.h>
typedef struct {
size_t count;
size_t capacity;
void** items;
2026-06-10 08:39:58 -05:00
} LArray;
2026-06-09 07:23:41 -05:00
2026-06-10 08:39:58 -05:00
LArray* l_array_create();
void l_array_delete(LArray* array);
int l_array_append(LArray* array, void* item);
void l_array_clear(LArray* array);
2026-06-09 07:23:41 -05:00
#ifdef L_ARRAY_IMPLEMENTATION
#include <stdlib.h>
2026-06-10 08:39:58 -05:00
LArray* l_array_create() {
return (LArray*) calloc(1, sizeof(LArray));
2026-06-09 07:23:41 -05:00
}
2026-06-10 08:39:58 -05:00
void l_array_delete(LArray* array) {
2026-06-09 07:23:41 -05:00
free(array);
}
2026-06-10 08:39:58 -05:00
int l_array_append(LArray* array, void* item) {
2026-06-09 07:23:41 -05:00
if (array->count >= array->capacity) {
if (array->capacity == 0) array->capacity = 8;
else array->capacity *= 2;
array->items = realloc(array->items, array->capacity * sizeof(*array->items));
}
array->items[array->count++] = item;
return 0;
}
2026-06-10 08:39:58 -05:00
void l_array_clear(LArray* array) {
2026-06-09 07:23:41 -05:00
free(array->items);
array->items = NULL;
array->capacity = 0;
array->count = 0;
}
#endif
#endif