diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-07-09 21:03:16 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-07-09 21:03:16 +0000 |
commit | a64f9e671936451d7a3a60191dcca6e37e97e585 (patch) | |
tree | f95182c1fc24b972bd4a7dd5dae4ba6a7175de83 /libdimension/dimension | |
parent | 9a634a53f0d7135ef82e6888a89091db173c7f7f (diff) | |
download | dimension-a64f9e671936451d7a3a60191dcca6e37e97e585.tar.xz |
Implement removing/inserting in the middle of a dmnsn_array*.
Diffstat (limited to 'libdimension/dimension')
-rw-r--r-- | libdimension/dimension/array.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h index c12ff9f..d835d60 100644 --- a/libdimension/dimension/array.h +++ b/libdimension/dimension/array.h @@ -139,4 +139,32 @@ dmnsn_array_pop(dmnsn_array *array, void *obj) dmnsn_array_resize(array, size - 1); /* Shrink the array */ } +/* Insert an item into the middle of the array */ +DMNSN_INLINE void +dmnsn_array_insert(dmnsn_array *array, size_t i, const void *obj) +{ + size_t size = dmnsn_array_size(array); + /* Increase the size by 1 */ + dmnsn_array_resize(array, size + 1); + /* Move the elements at and after `i' 1 to the right */ + memmove((char *)array->ptr + array->obj_size*(i + 1), + (char *)array->ptr + array->obj_size*i, + array->obj_size*(size - i)); + /* Insert `obj' at `i' */ + memcpy((char *)array->ptr + array->obj_size*i, obj, array->obj_size); +} + +/* Remove an item from the middle of the array */ +DMNSN_INLINE void +dmnsn_array_remove(dmnsn_array *array, size_t i) +{ + size_t size = dmnsn_array_size(array); + /* Move the array elements after `i' 1 to the left */ + memmove((char *)array->ptr + array->obj_size*i, + (char *)array->ptr + array->obj_size*(i + 1), + array->obj_size*(size - i)); + /* Decrease the size by 1 */ + dmnsn_array_resize(array, size - 1); +} + #endif /* DIMENSION_ARRAY_H */ |