From f345db3f7ca15480c5aa70d015150b399f3ce33a Mon Sep 17 00:00:00 2001 From: David Steele Date: Fri, 19 Oct 2018 11:52:17 +0200 Subject: [PATCH] Add lstInsert() to List object. Add general purpose insert function and make lstAdd() a special insert case. --- doc/xml/release.xml | 4 ++ src/common/type/list.c | 75 +++++++++++++++++++-------- src/common/type/list.h | 1 + test/src/module/common/typeListTest.c | 9 +++- 4 files changed, 64 insertions(+), 25 deletions(-) diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 418a52ab2..be1c60e33 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -15,6 +15,10 @@ + +

Add lstInsert() to List object.

+
+ diff --git a/src/common/type/list.c b/src/common/type/list.c index 9b936514c..fbc4690fb 100644 --- a/src/common/type/list.c +++ b/src/common/type/list.c @@ -57,29 +57,7 @@ lstAdd(List *this, const void *item) FUNCTION_TEST_ASSERT(item != NULL); FUNCTION_TEST_END(); - // If list size = max then allocate more space - if (this->listSize == this->listSizeMax) - { - MEM_CONTEXT_BEGIN(this->memContext) - { - // If nothing has been allocated yet - if (this->listSizeMax == 0) - { - this->listSizeMax = LIST_INITIAL_SIZE; - this->list = memNewRaw(this->listSizeMax * this->itemSize); - } - // Else the list needs to be extended - else - { - this->listSizeMax *= 2; - this->list = memGrowRaw(this->list, this->listSizeMax * this->itemSize); - } - } - MEM_CONTEXT_END(); - } - - memcpy(this->list + (this->listSize * this->itemSize), item, this->itemSize); - this->listSize++; + lstInsert(this, lstSize(this), item); FUNCTION_TEST_RESULT(LIST, this); } @@ -105,6 +83,57 @@ lstGet(const List *this, unsigned int listIdx) FUNCTION_TEST_RESULT(VOIDP, this->list + (listIdx * this->itemSize)); } +/*********************************************************************************************************************************** +Insert an item into the list +***********************************************************************************************************************************/ +List * +lstInsert(List *this, unsigned int listIdx, const void *item) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(LIST, this); + FUNCTION_TEST_PARAM(VOIDP, item); + + FUNCTION_TEST_ASSERT(this != NULL); + FUNCTION_TEST_ASSERT(listIdx <= lstSize(this)); + FUNCTION_TEST_ASSERT(item != NULL); + FUNCTION_TEST_END(); + + // If list size = max then allocate more space + if (this->listSize == this->listSizeMax) + { + MEM_CONTEXT_BEGIN(this->memContext) + { + // If nothing has been allocated yet + if (this->listSizeMax == 0) + { + this->listSizeMax = LIST_INITIAL_SIZE; + this->list = memNewRaw(this->listSizeMax * this->itemSize); + } + // Else the list needs to be extended + else + { + this->listSizeMax *= 2; + this->list = memGrowRaw(this->list, this->listSizeMax * this->itemSize); + } + } + MEM_CONTEXT_END(); + } + + // If not inserting at the end then move items down to make space + if (listIdx != lstSize(this)) + { + memmove( + this->list + ((listIdx + 1) * this->itemSize), this->list + (listIdx * this->itemSize), + (lstSize(this) - listIdx) * this->itemSize); + } + + // Copy item into the list + memcpy(this->list + (listIdx * this->itemSize), item, this->itemSize); + this->listSize++; + + FUNCTION_TEST_RESULT(LIST, this); +} + /*********************************************************************************************************************************** Return the memory context for this list ***********************************************************************************************************************************/ diff --git a/src/common/type/list.h b/src/common/type/list.h index 34789057b..d6fc71fe5 100644 --- a/src/common/type/list.h +++ b/src/common/type/list.h @@ -23,6 +23,7 @@ Functions List *lstNew(size_t itemSize); List *lstAdd(List *this, const void *item); void *lstGet(const List *this, unsigned int listIdx); +List *lstInsert(List *this, unsigned int listIdx, const void *item); MemContext *lstMemContext(const List *this); List *lstMove(List *this, MemContext *parentNew); unsigned int lstSize(const List *this); diff --git a/test/src/module/common/typeListTest.c b/test/src/module/common/typeListTest.c index 7b327b656..cf43f21ad 100644 --- a/test/src/module/common/typeListTest.c +++ b/test/src/module/common/typeListTest.c @@ -47,7 +47,7 @@ testRun(void) } // ***************************************************************************************************************************** - if (testBegin("lstAdd(), lstMove(), and lstSize()")) + if (testBegin("lstAdd(), lstInsert(), lstMove(), and lstSize()")) { List *list = NULL; @@ -56,13 +56,18 @@ testRun(void) list = lstNew(sizeof(int)); // Add ints to the list - for (int listIdx = 0; listIdx <= LIST_INITIAL_SIZE; listIdx++) + for (int listIdx = 1; listIdx <= LIST_INITIAL_SIZE; listIdx++) TEST_RESULT_PTR(lstAdd(list, &listIdx), list, "add item %d", listIdx); lstMove(list, MEM_CONTEXT_OLD()); } MEM_CONTEXT_TEMP_END(); + // Insert an int at the beginning + int insertIdx = 0; + TEST_RESULT_PTR(lstInsert(list, 0, &insertIdx), list, "insert item %d", insertIdx); + + // Check the size TEST_RESULT_INT(lstSize(list), 9, "list size"); // Read them back and check values