create better file structure
This commit is contained in:
55
include/array.h
Normal file
55
include/array.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#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;
|
||||
} LArray;
|
||||
|
||||
LArray* l_array_create();
|
||||
void l_array_delete(LArray* array);
|
||||
int l_array_append(LArray* array, void* item);
|
||||
void l_array_clear(LArray* array);
|
||||
|
||||
#ifdef L_ARRAY_IMPLEMENTATION
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
LArray* l_array_create() {
|
||||
return (LArray*) calloc(1, sizeof(LArray));
|
||||
}
|
||||
|
||||
void l_array_delete(LArray* array) {
|
||||
free(array);
|
||||
}
|
||||
|
||||
int l_array_append(LArray* array, void* item) {
|
||||
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;
|
||||
}
|
||||
|
||||
void l_array_clear(LArray* array) {
|
||||
free(array->items);
|
||||
array->items = NULL;
|
||||
array->capacity = 0;
|
||||
array->count = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user