aboutsummaryrefslogtreecommitdiffhomepage
path: root/util.c
diff options
context:
space:
mode:
authorGravatar axel <axel@liljencrantz.se>2006-10-02 06:17:28 +1000
committerGravatar axel <axel@liljencrantz.se>2006-10-02 06:17:28 +1000
commit8b2059c628000023e5ba745a7fa9d9b6729314b6 (patch)
tree07ecbe536444844a642ede16f807b5769316d2fc /util.c
parent15724d079801df9183d0d86cadfbb7dec5b2821b (diff)
Add function to insert a range of new elements into the middle of an array_list
darcs-hash:20061001201728-ac50b-db2f7bccee53224f50347c8995b2f6496940243d.gz
Diffstat (limited to 'util.c')
-rw-r--r--util.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/util.c b/util.c
index 1561eb13..3b315282 100644
--- a/util.c
+++ b/util.c
@@ -721,6 +721,51 @@ int al_push_all( array_list_t *a, array_list_t *b )
return 1;
}
+int al_insert( array_list_t *a, int pos, int count )
+{
+
+ assert( pos >= 0 );
+ assert( count >= 0 );
+ assert( a );
+
+ if( !count )
+ return 0;
+
+ /*
+ Reallocate, if needed
+ */
+ if( maxi( pos, a->pos) + count > a->size )
+ {
+ /*
+ If we reallocate, add a few extra elements just in case we
+ want to do some more reallocating any time soon
+ */
+ size_t new_size = maxi( maxi( pos, a->pos ) + count +32, a->size*2);
+ void *tmp = realloc( a->arr, sizeof( anything_t )*new_size );
+ if( tmp )
+ {
+ a->arr = tmp;
+ }
+ else
+ {
+ DIE_MEM();
+ }
+
+ }
+
+ if( a->pos > pos )
+ {
+ memmove( &a->arr[pos],
+ &a->arr[pos+count],
+ sizeof(anything_t ) * (a->pos-pos) );
+ }
+
+ memset( &a->arr[pos], 0, sizeof(anything_t)*count );
+ a->pos += count;
+
+ return 1;
+}
+
static int al_set_generic( array_list_t *l, int pos, anything_t v )
{