1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +02:00

Add lstGetLast() and lstRemoveLast().

When a list is being treated as a stack it is useful to get/remove the last (top) item.
This commit is contained in:
David Steele 2020-07-31 16:27:57 -04:00
parent bfb489a82d
commit a8e47c38c6
3 changed files with 41 additions and 2 deletions

View File

@ -143,6 +143,23 @@ lstGet(const List *this, unsigned int listIdx)
FUNCTION_TEST_RETURN(this->list + (listIdx * this->itemSize));
}
void *
lstGetLast(const List *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(LIST, this);
FUNCTION_TEST_END();
ASSERT(this != NULL);
// Ensure there are items in the list
if (this->listSize == 0)
THROW(AssertError, "cannot get last from list with no values");
// Return pointer to list item
FUNCTION_TEST_RETURN(lstGet(this, this->listSize - 1));
}
/**********************************************************************************************************************************/
bool
lstExists(const List *this, const void *item)
@ -300,6 +317,7 @@ lstRemoveIdx(List *this, unsigned int listIdx)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(LIST, this);
FUNCTION_TEST_PARAM(UINT, listIdx);
FUNCTION_TEST_END();
ASSERT(this != NULL);
@ -337,6 +355,21 @@ lstRemove(List *this, const void *item)
FUNCTION_TEST_RETURN(false);
}
List *
lstRemoveLast(List *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(LIST, this);
FUNCTION_TEST_END();
ASSERT(this != NULL);
if (this->listSize == 0)
THROW(AssertError, "cannot remove last from list with no values");
FUNCTION_TEST_RETURN(lstRemoveIdx(this, this->listSize - 1));
}
/**********************************************************************************************************************************/
MemContext *
lstMemContext(const List *this)

View File

@ -74,6 +74,7 @@ List *lstClear(List *this);
// Get an item from the list
void *lstGet(const List *this, unsigned int listIdx);
void *lstGetLast(const List *this);
// Does an item exist in the list?
bool lstExists(const List *this, const void *item);
@ -98,6 +99,7 @@ List *lstMove(List *this, MemContext *parentNew);
// Remove an item from the list
bool lstRemove(List *this, const void *item);
List *lstRemoveIdx(List *this, unsigned int listIdx);
List *lstRemoveLast(List *this);
// Return list size
unsigned int lstSize(const List *this);

View File

@ -79,6 +79,9 @@ testRun(void)
{
list = lstNewP(sizeof(int));
TEST_ERROR(lstGetLast(list), AssertError, "cannot get last from list with no values");
TEST_ERROR(lstRemoveLast(list), AssertError, "cannot remove last from list with no values");
// Add ints to the list
for (int listIdx = 1; listIdx <= LIST_INITIAL_SIZE; listIdx++)
TEST_RESULT_VOID(lstAdd(list, &listIdx), "add item %d", listIdx);
@ -107,12 +110,13 @@ testRun(void)
// Read them back and check values
for (unsigned int listIdx = 0; listIdx < lstSize(list); listIdx++)
{
int *item = lstGet(list, listIdx);
int *item = listIdx == lstSize(list) - 1 ? lstGetLast(list) : lstGet(list, listIdx);
TEST_RESULT_INT(*item, listIdx + 1, "check item %u", listIdx);
}
// Remove last item
TEST_RESULT_VOID(lstRemoveIdx(list, lstSize(list) - 1), "remove last item");
TEST_RESULT_VOID(lstRemoveLast(list), "remove last item");
// Read them back and check values
for (unsigned int listIdx = 0; listIdx < lstSize(list); listIdx++)