2021-04-15 18:59:26 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#ifndef FF_INCLUDED_FFLIST
|
|
|
|
|
#define FF_INCLUDED_FFLIST
|
|
|
|
|
|
2022-09-16 14:03:14 +08:00
|
|
|
#include "FFcheckmacros.h"
|
|
|
|
|
|
2021-05-07 14:55:43 +02:00
|
|
|
#include <stdbool.h>
|
2021-04-15 18:59:26 +02:00
|
|
|
#include <stdint.h>
|
2022-09-24 19:22:33 +08:00
|
|
|
#include <assert.h>
|
2022-10-17 13:19:48 +02:00
|
|
|
#include <stdlib.h>
|
2021-04-15 18:59:26 +02:00
|
|
|
|
|
|
|
|
#define FF_LIST_DEFAULT_ALLOC 16
|
|
|
|
|
|
|
|
|
|
typedef struct FFlist
|
|
|
|
|
{
|
2021-04-24 21:42:48 +02:00
|
|
|
char* data;
|
2021-04-15 18:59:26 +02:00
|
|
|
uint32_t elementSize;
|
|
|
|
|
uint32_t length;
|
|
|
|
|
uint32_t capacity;
|
|
|
|
|
} FFlist;
|
|
|
|
|
|
|
|
|
|
void ffListInitA(FFlist* list, uint32_t elementSize, uint32_t capacity);
|
|
|
|
|
|
|
|
|
|
void* ffListAdd(FFlist* list);
|
|
|
|
|
|
2022-09-16 14:03:14 +08:00
|
|
|
FF_C_NODISCARD uint32_t ffListFirstIndexComp(const FFlist* list, void* compElement, bool(*compFunc)(const void*, const void*));
|
2021-05-07 14:55:43 +02:00
|
|
|
|
2023-01-13 19:03:09 +08:00
|
|
|
// Removes the first element, and copy its value to `*result`
|
|
|
|
|
bool ffListShift(FFlist* list, void* result);
|
|
|
|
|
// Removes the last element, and copy its value to `*result`
|
|
|
|
|
bool ffListPop(FFlist* list, void* result);
|
|
|
|
|
|
2021-04-15 18:59:26 +02:00
|
|
|
void ffListDestroy(FFlist* list);
|
|
|
|
|
|
2022-09-24 19:22:33 +08:00
|
|
|
static inline void ffListInit(FFlist* list, uint32_t elementSize)
|
|
|
|
|
{
|
2022-09-24 23:17:38 +08:00
|
|
|
assert(elementSize > 0);
|
2022-09-24 19:22:33 +08:00
|
|
|
ffListInitA(list, elementSize, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void* ffListGet(const FFlist* list, uint32_t index)
|
|
|
|
|
{
|
2022-09-24 23:17:38 +08:00
|
|
|
assert(list->capacity > index);
|
2022-09-24 19:22:33 +08:00
|
|
|
return list->data + (index * list->elementSize);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 13:19:48 +02:00
|
|
|
static inline void ffListSort(FFlist* list, int(*compar)(const void*, const void*))
|
|
|
|
|
{
|
|
|
|
|
qsort(list->data, list->length, list->elementSize, compar);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 16:51:11 +08:00
|
|
|
#define FF_LIST_FOR_EACH(itemType, itemVarName, listVar) \
|
|
|
|
|
assert(sizeof(itemType) == (listVar).elementSize); \
|
|
|
|
|
for(itemType* itemVarName = (itemType*)(listVar).data; \
|
2023-01-15 00:59:33 +08:00
|
|
|
itemVarName - (itemType*)(listVar).data < (intptr_t)(listVar).length; \
|
2023-01-02 16:51:11 +08:00
|
|
|
++itemVarName)
|
|
|
|
|
|
2023-01-11 13:55:06 +08:00
|
|
|
#define FF_LIST_AUTO_DESTROY FFlist __attribute__((__cleanup__(ffListDestroy)))
|
2022-11-13 00:04:34 +08:00
|
|
|
|
2021-04-15 18:59:26 +02:00
|
|
|
#endif
|