Commit 01f10ca1 authored by Chris Müller's avatar Chris Müller

lib: add replace method for arrays.

parent ee0b4a5c
...@@ -121,7 +121,7 @@ cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size ...@@ -121,7 +121,7 @@ cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size
{ {
assert(array != 0); assert(array != 0);
if(index > array->length) if(index >= array->length)
return CRY_OUT_OF_INDEX_RANGE; return CRY_OUT_OF_INDEX_RANGE;
cry_array_ensure(array, array->length + size); cry_array_ensure(array, array->length + size);
...@@ -135,13 +135,25 @@ cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size ...@@ -135,13 +135,25 @@ cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size
} }
return_code
cry_array_replace(struct CryArray* array, size_t index, pointer data, size_t size)
{
assert(array != 0);
if(index >= array->length && index + size > array->length)
return CRY_OUT_OF_INDEX_RANGE;
cry_array_ensure(array, array->length + size);
memcpy(array->data + index, data, size);
}
return_code return_code
cry_array_remove(struct CryArray* array, size_t index, size_t size) cry_array_remove(struct CryArray* array, size_t index, size_t size)
{ {
assert(array != 0); assert(array != 0);
if(index > array->length) if(index >= array->length)
return CRY_OUT_OF_INDEX_RANGE; return CRY_OUT_OF_INDEX_RANGE;
memmove(array->data + index, array->data + index + size, array->length - index - size); memmove(array->data + index, array->data + index + size, array->length - index - size);
...@@ -260,7 +272,7 @@ cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr) ...@@ -260,7 +272,7 @@ cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr)
{ {
assert(array != 0); assert(array != 0);
if(index > array->length) if(index >= array->length)
return CRY_OUT_OF_INDEX_RANGE; return CRY_OUT_OF_INDEX_RANGE;
cry_ptr_array_ensure(array, array->length + 1); cry_ptr_array_ensure(array, array->length + 1);
...@@ -275,15 +287,30 @@ cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr) ...@@ -275,15 +287,30 @@ cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr)
} }
pointer
cry_ptr_array_replace(struct CryPtrArray* array, size_t index, pointer ptr)
{
assert(array != 0);
if(index >= array->length)
return 0;
pointer data = *(array->data + index);
*(array->data + index) = ptr;
return data;
}
pointer pointer
cry_ptr_array_remove(struct CryPtrArray* array, size_t index) cry_ptr_array_remove(struct CryPtrArray* array, size_t index)
{ {
assert(array != 0); assert(array != 0);
if(index > array->length) if(index >= array->length)
return 0; return 0;
pointer data = array->data + index; pointer data = *(array->data + index);
memmove(array->data + index, array->data + index + 1, (array->length - index - 1) * sizeof(pointer)); memmove(array->data + index, array->data + index + 1, (array->length - index - 1) * sizeof(pointer));
......
...@@ -54,6 +54,7 @@ size_t cry_array_trim(struct CryArray* array); ...@@ -54,6 +54,7 @@ size_t cry_array_trim(struct CryArray* array);
void cry_array_append(struct CryArray* array, pointer data, size_t size); void cry_array_append(struct CryArray* array, pointer data, size_t size);
return_code cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size); return_code cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size);
return_code cry_array_remove(struct CryArray* array, size_t index, size_t size); return_code cry_array_remove(struct CryArray* array, size_t index, size_t size);
return_code cry_array_replace(struct CryArray* array, size_t index, pointer data, size_t size);
pointer cry_array_get(struct CryArray* array, size_t index); pointer cry_array_get(struct CryArray* array, size_t index);
...@@ -71,7 +72,8 @@ size_t cry_ptr_array_trim(struct CryPtrArray* array); ...@@ -71,7 +72,8 @@ size_t cry_ptr_array_trim(struct CryPtrArray* array);
void cry_ptr_array_append(struct CryPtrArray* array, pointer ptr); void cry_ptr_array_append(struct CryPtrArray* array, pointer ptr);
return_code cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr); return_code cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr);
pointer cry_ptr_array_remove(struct CryPtrArray* array, size_t inndex); pointer cry_ptr_array_remove(struct CryPtrArray* array, size_t index);
pointer cry_ptr_array_replace(struct CryPtrArray* array, size_t index, pointer ptr);
pointer cry_ptr_array_get(struct CryPtrArray* array, size_t index); pointer cry_ptr_array_get(struct CryPtrArray* array, size_t index);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment