ccan.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] darray: Add darray_insert() to insert a value at a specified index
@ 2017-08-26 18:01 Damien Grassart
  2017-08-27  2:59 ` David Gibson
  0 siblings, 1 reply; 4+ messages in thread
From: Damien Grassart @ 2017-08-26 18:01 UTC (permalink / raw)
  To: ccan

This module currently supports removing but not inserting at a
specified index, so this adds that. Inserting a value moves all
existing data beyond index over one element.
---
 ccan/darray/darray.h | 7 +++++++
 1 file changed, 7 insertions(+)

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__)
 
 
-- 
2.14.1

_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] darray: Add darray_insert() to insert a value at a specified index
  2017-08-26 18:01 [PATCH] darray: Add darray_insert() to insert a value at a specified index Damien Grassart
@ 2017-08-27  2:59 ` David Gibson
  2017-08-27 19:40   ` Damien Grassart
  0 siblings, 1 reply; 4+ messages in thread
From: David Gibson @ 2017-08-27  2:59 UTC (permalink / raw)
  To: Damien Grassart; +Cc: ccan


[-- Attachment #1.1: Type: text/plain, Size: 1657 bytes --]

On Sat, Aug 26, 2017 at 08:01:08PM +0200, Damien Grassart wrote:
> This module currently supports removing but not inserting at a
> specified index, so this adds that. Inserting a value moves all
> existing data beyond index over one element.

Looks good but I need an S-o-b.  Adding a testcase would also be good.

> ---
>  ccan/darray/darray.h | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> 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__)
>  
>  

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

[-- Attachment #2: Type: text/plain, Size: 127 bytes --]

_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] darray: Add darray_insert() to insert a value at a specified index
  2017-08-27  2:59 ` David Gibson
@ 2017-08-27 19:40   ` Damien Grassart
  2017-08-28  2:42     ` David Gibson
  0 siblings, 1 reply; 4+ messages in thread
From: Damien Grassart @ 2017-08-27 19:40 UTC (permalink / raw)
  To: ccan

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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] darray: Add darray_insert() to insert a value at a specified index
  2017-08-27 19:40   ` Damien Grassart
@ 2017-08-28  2:42     ` David Gibson
  0 siblings, 0 replies; 4+ messages in thread
From: David Gibson @ 2017-08-28  2:42 UTC (permalink / raw)
  To: Damien Grassart, '; +Cc: ccan


[-- Attachment #1.1: Type: text/plain, Size: 2934 bytes --]

On Sun, Aug 27, 2017 at 09:40:57PM +0200, Damien Grassart wrote:
> 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>

Applied, thanks.

> ---
>  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;

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

[-- Attachment #2: Type: text/plain, Size: 127 bytes --]

_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-08-28  3:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-26 18:01 [PATCH] darray: Add darray_insert() to insert a value at a specified index Damien Grassart
2017-08-27  2:59 ` David Gibson
2017-08-27 19:40   ` Damien Grassart
2017-08-28  2:42     ` David Gibson

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 NNTP newsgroup(s).