From: Damien Grassart <damien@grassart.com>
To: ccan@lists.ozlabs.org
Subject: [PATCH 1/3] darray: Add darray_insert() to insert a value at a specified index
Date: Tue, 29 Aug 2017 12:08:40 +0200 [thread overview]
Message-ID: <20170829100842.25077-1-damien@grassart.com> (raw)
This module currently supports removing but not inserting at a
specified index, so this adds that along with some tests. Inserting a
value moves all existing data beyond index over one element.
Signed-off-by: Damien Grassart <damien@grassart.com>
---
ccan/darray/darray.h | 7 +++++++
ccan/darray/test/run.c | 22 ++++++++++++++++++++--
2 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/ccan/darray/darray.h b/ccan/darray/darray.h
index 75112419..fca20b8a 100644
--- a/ccan/darray/darray.h
+++ b/ccan/darray/darray.h
@@ -56,6 +56,7 @@
*
* void darray_append(darray(T) arr, T item);
* void darray_prepend(darray(T) arr, T item);
+ * void darray_insert(darray(T) arr, size_t index, T item);
* void darray_push(darray(T) arr, T item); // same as darray_append
*
* Insertion (multiple items):
@@ -169,6 +170,12 @@ typedef darray(unsigned long) darray_ulong;
memmove((arr).item+1, (arr).item, ((arr).size-1)*sizeof(*(arr).item)); \
(arr).item[0] = (__VA_ARGS__); \
} while(0)
+#define darray_insert(arr, index, ...) do { \
+ size_t __index = index; \
+ darray_resize(arr, (arr).size+1); \
+ memmove((arr).item+__index+1, (arr).item+__index, ((arr).size-__index-1)*sizeof(*(arr).item)); \
+ (arr).item[__index] = (__VA_ARGS__); \
+ } while(0)
#define darray_push(arr, ...) darray_append(arr, __VA_ARGS__)
diff --git a/ccan/darray/test/run.c b/ccan/darray/test/run.c
index 3d96fa56..5888af60 100644
--- a/ccan/darray/test/run.c
+++ b/ccan/darray/test/run.c
@@ -38,7 +38,7 @@ int main(void) {
trace("Generating amalgams (internal)");
generateAmalgams();
- plan_tests(41);
+ plan_tests(47);
testLits();
@@ -70,7 +70,25 @@ int main(void) {
ok1(darray_size(arr) == 0);
}
reset(arr);
-
+
+ testing(darray_insert);
+ {
+ size_t middle_i;
+
+ for (i=0; i < ARRAY_SIZE(lotsOfNumbers); i++)
+ darray_insert(arr, i, lotsOfNumbers[i]);
+ ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers));
+ ok1(darray_alloc(arr) >= darray_size(arr));
+ ok1(!memcmp(arr.item, lotsOfNumbers, sizeof(lotsOfNumbers)));
+
+ middle_i = ARRAY_SIZE(lotsOfNumbers) / 2;
+ darray_insert(arr, middle_i, 42);
+ ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers) + 1);
+ ok1(arr.item[middle_i] == 42);
+ ok1(arr.item[middle_i + 1] == lotsOfNumbers[middle_i]);
+ }
+ reset(arr);
+
testing(darray_from_c, darray_foreach, darray_foreach_reverse);
{
long *i;
--
2.14.1
_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan
next reply other threads:[~2017-08-29 10:08 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-29 10:08 Damien Grassart [this message]
2017-08-29 10:08 ` [PATCH 2/3] darray: Rename identifiers starting with an underscore Damien Grassart
2017-08-29 10:08 ` [PATCH 3/3] darray: Fix bug in the darray_remove() macro Damien Grassart
2017-08-29 13:04 ` [PATCH 1/3] darray: Add darray_insert() to insert a value at a specified index David Gibson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170829100842.25077-1-damien@grassart.com \
--to=damien@grassart.com \
--cc=ccan@lists.ozlabs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).