v0.0.2 - new function barray_put_uninitialized

master v0.0.2
brnrs 4 years ago
parent 3c5ff81443
commit 030545b514
  1. 5
      README.md
  2. 1
      include/barray.h
  3. 2
      meson.build
  4. 34
      src/barray.c
  5. 2
      src/meson.build

@ -1,3 +1,8 @@
# barray # barray
This is a tiny library containing a dynamic array implementation 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.

@ -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_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_put(BArray* array, size_t ix, void* value, BArray_ErrCode* errc);
void barray_append(BArray* array, 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_remove(BArray* array, size_t ix, BArray_ErrCode* errc);
void barray_clear(BArray* array, BArray_ErrCode* errc); void barray_clear(BArray* array, BArray_ErrCode* errc);
void barray_destroy(BArray* array, BArray_ErrCode* errc); void barray_destroy(BArray* array, BArray_ErrCode* errc);

@ -1,5 +1,5 @@
project('barray', 'c', project('barray', 'c',
version: '0.0.1', version: '0.0.2',
meson_version: '>=0.54.0', meson_version: '>=0.54.0',
default_options: [ 'warning_level=3', default_options: [ 'warning_level=3',
'c_std=c2x', 'c_std=c2x',

@ -236,6 +236,40 @@ void barray_append(BArray* array, void* value, BArray_ErrCode* errc) {
barray_put(array, array->len, value, 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) { void barray_remove(BArray *array, size_t ix, BArray_ErrCode *errc) {
assert(errc != NULL); assert(errc != NULL);
if (array == NULL) { if (array == NULL) {

@ -4,4 +4,4 @@ barray_dep = declare_dependency(
link_with : barray_lib, link_with : barray_lib,
include_directories : include include_directories : include
) )
meson.override_dependency('barray-v0.0.1', barray_dep) meson.override_dependency('barray-v0.0.2', barray_dep)
Loading…
Cancel
Save