mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2024-12-14 10:13:05 +02:00
Add lstInsert() to List object.
Add general purpose insert function and make lstAdd() a special insert case.
This commit is contained in:
parent
09a1ad2c10
commit
f345db3f7c
@ -15,6 +15,10 @@
|
||||
<release date="XXXX-XX-XX" version="2.07dev" title="UNDER DEVELOPMENT">
|
||||
<release-core-list>
|
||||
<release-development-list>
|
||||
<release-item>
|
||||
<p>Add <code>lstInsert()</code> to <code>List</code> object.</p>
|
||||
</release-item>
|
||||
|
||||
<release-item>
|
||||
<release-item-contributor-list>
|
||||
<release-item-contributor id="cynthia.shang"/>
|
||||
|
@ -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
|
||||
***********************************************************************************************************************************/
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user