new array list header spec
This commit is contained in:
@@ -1,30 +1,41 @@
|
||||
#pragma once
|
||||
#include "stdint.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
#define GN_ARRAY_LIST(type)\
|
||||
typedef struct type##ArrayList { \
|
||||
#define GN_ARRAY_LIST_HEADER(type)\
|
||||
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 maxSize; \
|
||||
type* data; \
|
||||
} type##ArrayList; \
|
||||
inline static type##ArrayList type##ArrayListCreate() { \
|
||||
type##ArrayList list;\
|
||||
list.maxSize = 1; \
|
||||
list.count = 0;\
|
||||
list.data = malloc(sizeof(type) * list.maxSize); \
|
||||
} type##ArrayList_t; \
|
||||
type##ArrayList type##ArrayListCreate(void) { \
|
||||
type##ArrayList list = malloc(sizeof(type##ArrayList_t));\
|
||||
list->maxSize = 1; \
|
||||
list->count = 0;\
|
||||
list->data = malloc(sizeof(type) * list->maxSize); \
|
||||
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);\
|
||||
list->data = realloc(list->data, sizeof(type) * (newCount + list->maxSize)); \
|
||||
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->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) {\
|
||||
list->maxSize *= 2; \
|
||||
list->data = realloc(list->data, sizeof(type) * list->maxSize); \
|
||||
@@ -32,27 +43,33 @@ if (list->count >= list->maxSize) {\
|
||||
list->data[list->count] = data;\
|
||||
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);\
|
||||
list->data = realloc(list->data, sizeof(type) * (newCount + list->maxSize)); \
|
||||
list->maxSize += newCount; \
|
||||
list->count += count; \
|
||||
} \
|
||||
inline static void type##ArrayListRemove(type##ArrayList* list) { \
|
||||
void type##ArrayListRemove(type##ArrayList list) { \
|
||||
if (list->count == 0) return; \
|
||||
list->count--; \
|
||||
} \
|
||||
inline static void type##ArrayListPopHead(type##ArrayList* list) { \
|
||||
void type##ArrayListPopHead(type##ArrayList list) { \
|
||||
if (list->count == 0) return; \
|
||||
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];\
|
||||
} \
|
||||
} \
|
||||
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(int);
|
||||
GN_ARRAY_LIST(uint32_t);
|
||||
GN_ARRAY_LIST_HEADER(int)
|
||||
GN_ARRAY_LIST_DEFINITION(int)
|
||||
|
Reference in New Issue
Block a user