From a8e47c38c6d70c202070fa585d4096592a9961cf Mon Sep 17 00:00:00 2001 From: David Steele Date: Fri, 31 Jul 2020 16:27:57 -0400 Subject: [PATCH] Add lstGetLast() and lstRemoveLast(). When a list is being treated as a stack it is useful to get/remove the last (top) item. --- src/common/type/list.c | 33 +++++++++++++++++++++++++++ src/common/type/list.h | 2 ++ test/src/module/common/typeListTest.c | 8 +++++-- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/common/type/list.c b/src/common/type/list.c index bd716edca..c5b017c1b 100644 --- a/src/common/type/list.c +++ b/src/common/type/list.c @@ -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) diff --git a/src/common/type/list.h b/src/common/type/list.h index 3651be8ca..c0f698fcf 100644 --- a/src/common/type/list.h +++ b/src/common/type/list.h @@ -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); diff --git a/test/src/module/common/typeListTest.c b/test/src/module/common/typeListTest.c index 59dd82bf3..60ffdb73c 100644 --- a/test/src/module/common/typeListTest.c +++ b/test/src/module/common/typeListTest.c @@ -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++)