new array list header spec

This commit is contained in:
Gregory Wells
2025-08-03 14:51:15 -04:00
parent a9aa97be63
commit 1b0465f6eb

View File

@@ -1,30 +1,41 @@
#pragma once #pragma once
#include "stdint.h"
#include "stdlib.h" #include "stdlib.h"
#define GN_ARRAY_LIST(type)\ #define GN_ARRAY_LIST_HEADER(type)\
typedef struct type##ArrayList { \ typedef struct type##ArrayList_t* type##ArrayList; \
type##ArrayList type##ArrayListCreate(void); \
void type##ArrayListReserve(type##ArrayList list, int count); \
void type##ArrayListExpand(type##ArrayList list, int count); \
void type##ArrayListAdd(type##ArrayList list, type data); \
void type##ArrayListResize(type##ArrayList list, int count); \
void type##ArrayListRemove(type##ArrayList list); \
void type##ArrayListPopHead(type##ArrayList list); \
uint32_t type##ArrayListCount(type##ArrayList list); \
type type##ArrayAt(type##ArrayList list, int i);
#define GN_ARRAY_LIST_DEFINITION(type)\
typedef struct type##ArrayList_t { \
uint32_t count; \ uint32_t count; \
uint32_t maxSize; \ uint32_t maxSize; \
type* data; \ type* data; \
} type##ArrayList; \ } type##ArrayList_t; \
inline static type##ArrayList type##ArrayListCreate() { \ type##ArrayList type##ArrayListCreate(void) { \
type##ArrayList list;\ type##ArrayList list = malloc(sizeof(type##ArrayList_t));\
list.maxSize = 1; \ list->maxSize = 1; \
list.count = 0;\ list->count = 0;\
list.data = malloc(sizeof(type) * list.maxSize); \ list->data = malloc(sizeof(type) * list->maxSize); \
return list; \ return list; \
}\ }\
inline static void type##ArrayListReserve(type##ArrayList* list, int count) { \ void type##ArrayListReserve(type##ArrayList list, int count) { \
int newCount = count - (list->maxSize - list->count);\ int newCount = count - (list->maxSize - list->count);\
list->data = realloc(list->data, sizeof(type) * (newCount + list->maxSize)); \ list->data = realloc(list->data, sizeof(type) * (newCount + list->maxSize)); \
list->maxSize += newCount; \ list->maxSize += newCount; \
}\ }\
inline static void type##ArrayListExpand(type##ArrayList* list, int count) { \ void type##ArrayListExpand(type##ArrayList list, int count) { \
list->data = realloc(list->data, sizeof(type) * (count + list->maxSize)); \ list->data = realloc(list->data, sizeof(type) * (count + list->maxSize)); \
list->maxSize += count; \ list->maxSize += count; \
}\ }\
inline static void type##ArrayListAdd(type##ArrayList* list, type data) { \ void type##ArrayListAdd(type##ArrayList list, type data) { \
if (list->count >= list->maxSize) {\ if (list->count >= list->maxSize) {\
list->maxSize *= 2; \ list->maxSize *= 2; \
list->data = realloc(list->data, sizeof(type) * list->maxSize); \ list->data = realloc(list->data, sizeof(type) * list->maxSize); \
@@ -32,27 +43,33 @@ if (list->count >= list->maxSize) {\
list->data[list->count] = data;\ list->data[list->count] = data;\
list->count++;\ list->count++;\
}\ }\
inline static void type##ArrayListResize(type##ArrayList* list, int count) { \ void type##ArrayListResize(type##ArrayList list, int count) { \
int newCount = count - (list->maxSize - list->count);\ int newCount = count - (list->maxSize - list->count);\
list->data = realloc(list->data, sizeof(type) * (newCount + list->maxSize)); \ list->data = realloc(list->data, sizeof(type) * (newCount + list->maxSize)); \
list->maxSize += newCount; \ list->maxSize += newCount; \
list->count += count; \ list->count += count; \
} \ } \
inline static void type##ArrayListRemove(type##ArrayList* list) { \ void type##ArrayListRemove(type##ArrayList list) { \
if (list->count == 0) return; \ if (list->count == 0) return; \
list->count--; \ list->count--; \
} \ } \
inline static void type##ArrayListPopHead(type##ArrayList* list) { \ void type##ArrayListPopHead(type##ArrayList list) { \
if (list->count == 0) return; \ if (list->count == 0) return; \
list->count--; \ list->count--; \
for (int i = 0; i < list->count; i++) { \ for (uint32_t i = 0; i < list->count; i++) { \
list->data[i] = list->data[i + 1];\ list->data[i] = list->data[i + 1];\
} \ } \
} \
uint32_t type##ArrayListCount(type##ArrayList list) { \
return list->count; \
} \
type type##ArrayAt(type##ArrayList list, int i) { \
return list->data[i]; \
} }
GN_ARRAY_LIST_HEADER(uint32_t)
GN_ARRAY_LIST_DEFINITION(uint32_t)
//metalAvaliableTextureArrayListPopHead GN_ARRAY_LIST_HEADER(int)
GN_ARRAY_LIST_DEFINITION(int)
GN_ARRAY_LIST(int);
GN_ARRAY_LIST(uint32_t);