mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-01 10:32:35 +01:00
FFlist: add new functions to make it be able to work as a queue
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "FFlist.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
|
||||
void ffListInitA(FFlist* list, uint32_t elementSize, uint32_t capacity)
|
||||
{
|
||||
@@ -34,6 +35,27 @@ uint32_t ffListFirstIndexComp(const FFlist* list, void* compElement, bool(*compF
|
||||
return list->length;
|
||||
}
|
||||
|
||||
bool ffListShift(FFlist* list, void* result)
|
||||
{
|
||||
if(list->length == 0)
|
||||
return false;
|
||||
|
||||
memcpy(result, list->data, list->elementSize);
|
||||
memmove(list->data, list->data + list->elementSize, list->elementSize * (list->length - 1));
|
||||
--list->length;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ffListPop(FFlist* list, void* result)
|
||||
{
|
||||
if(list->length == 0)
|
||||
return false;
|
||||
|
||||
memcpy(result, ffListGet(list, list->length - 1), list->elementSize);
|
||||
--list->length;
|
||||
return result;
|
||||
}
|
||||
|
||||
void ffListDestroy(FFlist* list)
|
||||
{
|
||||
//Avoid free-after-use. These 3 assignments are cheap so don't remove them
|
||||
|
||||
@@ -26,6 +26,11 @@ void* ffListAdd(FFlist* list);
|
||||
|
||||
FF_C_NODISCARD uint32_t ffListFirstIndexComp(const FFlist* list, void* compElement, bool(*compFunc)(const void*, const void*));
|
||||
|
||||
// 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);
|
||||
|
||||
void ffListDestroy(FFlist* list);
|
||||
|
||||
static inline void ffListInit(FFlist* list, uint32_t elementSize)
|
||||
|
||||
+25
-1
@@ -31,6 +31,7 @@ static bool numEqualsAdapter(const void* first, const void* second)
|
||||
int main(void)
|
||||
{
|
||||
FFlist list;
|
||||
uint32_t n = 0;
|
||||
|
||||
//initA
|
||||
|
||||
@@ -44,6 +45,14 @@ int main(void)
|
||||
FF_LIST_FOR_EACH(uint32_t, item, list)
|
||||
VERIFY(false);
|
||||
|
||||
//shift
|
||||
VERIFY(!ffListShift(&list, &n));
|
||||
VERIFY(list.length == 0);
|
||||
|
||||
//pop
|
||||
VERIFY(!ffListPop(&list, &n));
|
||||
VERIFY(list.length == 0);
|
||||
|
||||
//add
|
||||
for (uint32_t i = 1; i <= FF_LIST_DEFAULT_ALLOC + 1; ++i)
|
||||
{
|
||||
@@ -64,6 +73,7 @@ int main(void)
|
||||
VERIFY(*(uint32_t*)ffListGet(&list, 0) == 1);
|
||||
VERIFY(*(uint32_t*)ffListGet(&list, i - 1) == i);
|
||||
}
|
||||
VERIFY(list.length == FF_LIST_DEFAULT_ALLOC + 1);
|
||||
|
||||
uint32_t sum = 0;
|
||||
//forEach
|
||||
@@ -72,11 +82,25 @@ int main(void)
|
||||
VERIFY(sum == (1 + FF_LIST_DEFAULT_ALLOC + 1) * (FF_LIST_DEFAULT_ALLOC + 1) / 2);
|
||||
|
||||
// ffListFirstIndexComp
|
||||
uint32_t n = 10;
|
||||
n = 10;
|
||||
VERIFY(ffListFirstIndexComp(&list, &n, numEqualsAdapter) == 9);
|
||||
n = 999;
|
||||
VERIFY(ffListFirstIndexComp(&list, &n, numEqualsAdapter) == list.length);
|
||||
|
||||
//shift
|
||||
VERIFY(ffListShift(&list, &n));
|
||||
VERIFY(n == 1);
|
||||
VERIFY(list.length == FF_LIST_DEFAULT_ALLOC);
|
||||
VERIFY(*(uint32_t*) ffListGet(&list, 0) == 2);
|
||||
VERIFY(*(uint32_t*) ffListGet(&list, list.length - 1) == FF_LIST_DEFAULT_ALLOC + 1);
|
||||
|
||||
//pop
|
||||
VERIFY(ffListPop(&list, &n));
|
||||
VERIFY(n == FF_LIST_DEFAULT_ALLOC + 1);
|
||||
VERIFY(list.length == FF_LIST_DEFAULT_ALLOC - 1);
|
||||
VERIFY(*(uint32_t*) ffListGet(&list, 0) == 2);
|
||||
VERIFY(*(uint32_t*) ffListGet(&list, list.length - 1) == FF_LIST_DEFAULT_ALLOC);
|
||||
|
||||
//Destroy
|
||||
ffListDestroy(&list);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user