pgbackrest.git / summary / log / commit / refs
commit ea2aef1d0ceff366b75f663281df511b8169eadd
Author: David Steele <david@pgmasters.net>
Date: Wed Feb 27 16:09:21 2019 +0000
Add lstRemove() to List object.
Because it is sometimes useful to remove items from a list.
doc/xml/release.xml | 4 ++++
src/common/type/list.c | 23 +++++++++++++++++++++++
src/common/type/list.h | 1 +
test/src/module/common/typeListTest.c | 22 +++++++++++++++++++++-
4 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/doc/xml/release.xml b/doc/xml/release.xml
index 07fe17877..96114caaf 100644
--- a/doc/xml/release.xml
+++ b/doc/xml/release.xml
@@ -79,6 +79,10 @@
<p>Add <code>kvKeyExists()</code> to <code>KeyValue</code> object.</p>
</release-item>
+ <release-item>
+ <p>Add <code>lstRemove()</code> to <code>List</code> object.</p>
+ </release-item>
+
<release-item>
<p>Allow <code>cfgExecParam()</code> to exclude options.</p>
</release-item>
diff --git a/src/common/type/list.c b/src/common/type/list.c
index 6224157f9..bbcb949b0 100644
--- a/src/common/type/list.c
+++ b/src/common/type/list.c
@@ -134,6 +134,29 @@ lstInsert(List *this, unsigned int listIdx, const void *item)
FUNCTION_TEST_RETURN(this);
}
+/***********************************************************************************************************************************
+Remove an item from the list
+***********************************************************************************************************************************/
+List *
+lstRemove(List *this, unsigned int listIdx)
+{
+ FUNCTION_TEST_BEGIN();
+ FUNCTION_TEST_PARAM(LIST, this);
+ FUNCTION_TEST_END();
+
+ ASSERT(this != NULL);
+ ASSERT(listIdx <= lstSize(this));
+
+ // Remove the item by moving the items after it down
+ this->listSize--;
+
+ memmove(
+ this->list + (listIdx * this->itemSize), this->list + ((listIdx + 1) * this->itemSize),
+ (lstSize(this) - listIdx) * this->itemSize);
+
+ FUNCTION_TEST_RETURN(this);
+}
+
/***********************************************************************************************************************************
Return the memory context for this list
***********************************************************************************************************************************/
diff --git a/src/common/type/list.h b/src/common/type/list.h
index 32c83bcec..12a4169e1 100644
--- a/src/common/type/list.h
+++ b/src/common/type/list.h
@@ -24,6 +24,7 @@ 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);
+List *lstRemove(List *this, unsigned int listIdx);
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 087734548..b14d699f2 100644
--- a/test/src/module/common/typeListTest.c
+++ b/test/src/module/common/typeListTest.c
@@ -79,7 +79,27 @@ testRun(void)
TEST_RESULT_INT(*item, listIdx, "check item %u", listIdx);
}
- TEST_ERROR(lstGet(list, lstSize(list)), AssertError, "cannot get index 9 from list with 9 value(s)");
+ // Remove first item
+ TEST_RESULT_VOID(lstRemove(list, 0), "remove first item");
+
+ // Read them back and check values
+ for (unsigned int listIdx = 0; listIdx < lstSize(list); listIdx++)
+ {
+ int *item = lstGet(list, listIdx);
+ TEST_RESULT_INT(*item, listIdx + 1, "check item %u", listIdx);
+ }
+
+ // Remove last item
+ TEST_RESULT_VOID(lstRemove(list, lstSize(list) - 1), "remove last item");
+
+ // Read them back and check values
+ for (unsigned int listIdx = 0; listIdx < lstSize(list); listIdx++)
+ {
+ int *item = lstGet(list, listIdx);
+ TEST_RESULT_INT(*item, listIdx + 1, "check item %u", listIdx);
+ }
+
+ TEST_ERROR(lstGet(list, lstSize(list)), AssertError, "cannot get index 7 from list with 7 value(s)");
TEST_RESULT_VOID(lstMove(NULL, memContextTop()), "move null list");
}
[parent: 4be271ea2a1a]