2021-04-15 18:59:26 +02:00
|
|
|
#include "FFlist.h"
|
|
|
|
|
|
2022-06-18 14:25:31 +02:00
|
|
|
#include <stdlib.h>
|
2021-04-15 18:59:26 +02:00
|
|
|
|
|
|
|
|
void ffListInitA(FFlist* list, uint32_t elementSize, uint32_t capacity)
|
|
|
|
|
{
|
|
|
|
|
list->elementSize = elementSize;
|
|
|
|
|
list->capacity = capacity;
|
|
|
|
|
list->length = 0;
|
2022-09-16 10:21:24 +08:00
|
|
|
list->data = capacity == 0 ? NULL : malloc((size_t)list->capacity * list->elementSize);
|
2021-04-15 18:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* ffListAdd(FFlist* list)
|
|
|
|
|
{
|
|
|
|
|
if(list->length == list->capacity)
|
2021-04-24 13:05:43 +02:00
|
|
|
{
|
2022-09-16 10:21:24 +08:00
|
|
|
list->capacity = list->capacity == 0 ? FF_LIST_DEFAULT_ALLOC : list->capacity * 2;
|
|
|
|
|
// realloc(NULL, newSize) is same as malloc(newSize)
|
|
|
|
|
list->data = realloc(list->data, (size_t)list->capacity * list->elementSize);
|
2021-04-24 13:05:43 +02:00
|
|
|
}
|
2022-09-18 15:53:51 +02:00
|
|
|
|
2021-04-15 18:59:26 +02:00
|
|
|
++list->length;
|
2022-09-18 15:53:51 +02:00
|
|
|
return ffListGet(list, list->length - 1);
|
2021-04-15 18:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-23 19:40:34 +02:00
|
|
|
uint32_t ffListFirstIndexComp(const FFlist* list, void* compElement, bool(*compFunc)(const void*, const void*))
|
2021-05-07 14:55:43 +02:00
|
|
|
{
|
|
|
|
|
for(uint32_t i = 0; i < list->length; i++)
|
|
|
|
|
{
|
|
|
|
|
if(compFunc(ffListGet(list, i), compElement))
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list->length;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 18:59:26 +02:00
|
|
|
void ffListDestroy(FFlist* list)
|
|
|
|
|
{
|
2022-09-20 22:45:36 +08:00
|
|
|
//Avoid free-after-use. These 3 assignments are cheap so don't remove them
|
|
|
|
|
list->capacity = list->length = 0;
|
|
|
|
|
free(list->data);
|
|
|
|
|
list->data = NULL;
|
2021-04-15 18:59:26 +02:00
|
|
|
}
|