diff --git a/README.md b/README.md index 023696f..afd4804 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # barray -This is a tiny library containing a dynamic array implementation \ No newline at end of file +This is a tiny library containing a dynamic array implementation + +## changelog + +### v0.0.2 +* A new function, `barray_put_uninitialized`, was added to make it possible to insert elements without initializing them. An example use-case would be when you want to fill the element manually instead of copying the exact content of an existing memory location. diff --git a/include/barray.h b/include/barray.h index a345c0a..3526720 100644 --- a/include/barray.h +++ b/include/barray.h @@ -29,6 +29,7 @@ void* barray_get(BArray* array, size_t ix, BArray_ErrCode* errc); void barray_set(BArray* array, size_t ix, void* value, BArray_ErrCode* errc); void barray_put(BArray* array, size_t ix, void* value, BArray_ErrCode* errc); void barray_append(BArray* array, void* value, BArray_ErrCode* errc); +void* barray_put_uninitialized(BArray* array, size_t ix, BArray_ErrCode* errc); void barray_remove(BArray* array, size_t ix, BArray_ErrCode* errc); void barray_clear(BArray* array, BArray_ErrCode* errc); void barray_destroy(BArray* array, BArray_ErrCode* errc); diff --git a/meson.build b/meson.build index d01c5bb..bc34c85 100644 --- a/meson.build +++ b/meson.build @@ -1,5 +1,5 @@ project('barray', 'c', - version: '0.0.1', + version: '0.0.2', meson_version: '>=0.54.0', default_options: [ 'warning_level=3', 'c_std=c2x', diff --git a/src/barray.c b/src/barray.c index ad34b3a..79b1747 100644 --- a/src/barray.c +++ b/src/barray.c @@ -236,6 +236,40 @@ void barray_append(BArray* array, void* value, BArray_ErrCode* errc) { barray_put(array, array->len, value, errc); } +void* barray_put_uninitialized(BArray *array, size_t ix, BArray_ErrCode *errc) { + assert(errc != NULL); + if (array == NULL) { + *errc = BARRAY_ERR_NULL; + return NULL; + } + if (array->elem_size == 0) { + *errc = BARRAY_ERR_ELEM_SIZE_ZERO; + return NULL; + } + if (ix > array->len) { + *errc = BARRAY_ERR_OUT_OF_BOUNDS; + return NULL; + } + if (array->cap > (array->len + 1)) { + barray_grow(array, errc); + if (*errc != BARRAY_NO_ERR) { + return NULL; + } + } + unsigned char* ptr = array->ptr; + if (ix == array->len) { + goto nomove; + } + void* src = (void*) &ptr[(ix * array->elem_size)]; + void* dest = (void*) &ptr[((ix + 1) * array->elem_size)]; + memmove(dest, src, array->elem_size * (array->len - ix)); + nomove: + *errc = BARRAY_NO_ERR; + array->len = array->len + 1; + void* elem_ptr = (void*) &ptr[(ix * array->elem_size)]; + return elem_ptr; +} + void barray_remove(BArray *array, size_t ix, BArray_ErrCode *errc) { assert(errc != NULL); if (array == NULL) { diff --git a/src/meson.build b/src/meson.build index e69655b..c654885 100644 --- a/src/meson.build +++ b/src/meson.build @@ -4,4 +4,4 @@ barray_dep = declare_dependency( link_with : barray_lib, include_directories : include ) -meson.override_dependency('barray-v0.0.1', barray_dep) \ No newline at end of file +meson.override_dependency('barray-v0.0.2', barray_dep) \ No newline at end of file