Commit ee0b4a5c authored by Chris Müller's avatar Chris Müller

lib: add pointer array implementation

lib: refactor dynamical resize for arrays (double allocation space).
parent f806ffb7
......@@ -26,48 +26,19 @@ struct CryArray
uint8_t* data;
size_t alloc;
size_t length;
size_t extend;
};
size_t cry_array_increase_alloc(struct CryArray* array, size_t size)
{
assert(array != 0);
while(array->alloc < size)
array->alloc += array->extend;
array->data = cry_realloc(uint8_t, array->data, array->extend);
return array->alloc;
}
size_t cry_array_reduce_alloc(struct CryArray* array)
{
assert(array != 0);
while(array->alloc - array->length <= array->extend)
array->alloc -= array->extend;
array->data = cry_realloc(uint8_t, array->data, array->extend);
return array->alloc;
}
struct CryArray*
cry_array_new(size_t init, size_t extend)
cry_array_new(size_t init_capacity)
{
assert(init_capacity != 0);
struct CryArray* array = cry_malloc(struct CryArray);
array->alloc = 0;
array->alloc = init_capacity;
array->length = 0;
array->extend = extend;
while(array->alloc < init)
array->alloc += extend;
array->data = cry_calloc(uint8_t, array->alloc);
return array;
......@@ -97,7 +68,7 @@ cry_array_size(struct CryArray* array)
size_t
cry_array_alloc(struct CryArray* array)
cry_array_capacity(struct CryArray* array)
{
assert(array != 0);
......@@ -105,14 +76,39 @@ cry_array_alloc(struct CryArray* array)
}
size_t
cry_array_ensure(struct CryArray* array, size_t min_capacity)
{
assert(array != 0);
if(array->alloc < min_capacity) {
size_t new_capacity = (array->alloc >> 1);
array->alloc = (new_capacity > min_capacity) ? new_capacity : min_capacity;
array->data = cry_realloc(uint8_t, array->data, array->alloc);
}
return array->alloc;
}
size_t
cry_array_trim(struct CryArray* array)
{
assert(array != 0);
array->data = cry_realloc(uint8_t, array->data, array->length);
array->alloc = array->length;
return array->alloc;
}
void
cry_array_append(struct CryArray* array, pointer data, size_t size)
{
assert(array != 0);
if(array->alloc < array->length + size)
cry_array_increase_alloc(array, array->length + size);
cry_array_ensure(array, array->length + size);
memcpy(array->data + array->length, data, size);
......@@ -128,8 +124,7 @@ cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size
if(index > array->length)
return CRY_OUT_OF_INDEX_RANGE;
if(array->alloc < array->length + size)
cry_array_increase_alloc(array, array->length + size);
cry_array_ensure(array, array->length + size);
memmove(array->data + index + size, array->data + index, array->length - index);
memcpy(array->data + index, data, size);
......@@ -168,3 +163,146 @@ cry_array_get(struct CryArray* array, size_t index)
return 0;
}
struct CryPtrArray
{
pointer* data;
size_t alloc;
size_t length;
};
struct CryPtrArray*
cry_ptr_array_new(size_t init_capacity)
{
assert(init_capacity != 0);
struct CryPtrArray* array = cry_malloc(struct CryPtrArray);
array->alloc = init_capacity;
array->length = 0;
array->data = cry_calloc(pointer, array->alloc);
return array;
}
void
cry_ptr_array_free(struct CryPtrArray* array)
{
assert(array != 0);
cry_free(array->data);
cry_free(array);
}
size_t
cry_ptr_array_size(struct CryPtrArray* array)
{
assert(array != 0);
return array->length;
}
size_t
cry_ptr_array_capacity(struct CryPtrArray* array)
{
assert(array != 0);
return array->alloc;
}
size_t
cry_ptr_array_ensure(struct CryPtrArray* array, size_t min_capacity)
{
assert(array != 0);
if(array->alloc < min_capacity) {
size_t new_capacity = (array->alloc >> 1);
array->alloc = (new_capacity > min_capacity) ? new_capacity : min_capacity;
array->data = cry_realloc(pointer, array->data, array->alloc);
}
return array->alloc;
}
size_t
cry_ptr_array_trim(struct CryPtrArray* array)
{
assert(array != 0);
array->data = cry_realloc(pointer, array->data, array->length);
array->alloc = array->length;
return array->alloc;
}
void
cry_ptr_array_append(struct CryPtrArray* array, pointer ptr)
{
assert(array != 0);
cry_ptr_array_ensure(array, array->length + 1);
*(array->data + array->length) = ptr;
array->length += 1;
}
return_code
cry_ptr_array_insert(struct CryPtrArray* array, size_t index, pointer ptr)
{
assert(array != 0);
if(index > array->length)
return CRY_OUT_OF_INDEX_RANGE;
cry_ptr_array_ensure(array, array->length + 1);
memmove(array->data + index + 1, array->data + index, (array->length - index) * sizeof(pointer));
*(array->data + index) = ptr;
array->length += 1;
return CRY_OKAY;
}
pointer
cry_ptr_array_remove(struct CryPtrArray* array, size_t index)
{
assert(array != 0);
if(index > array->length)
return 0;
pointer data = array->data + index;
memmove(array->data + index, array->data + index + 1, (array->length - index - 1) * sizeof(pointer));
array->length -= 1;
return data;
}
pointer
cry_ptr_array_get(struct CryPtrArray* array, size_t index)
{
assert(array != 0);
if(index < array->length)
return *(array->data + index);
return 0;
}
......@@ -27,6 +27,7 @@
#include "structures.h"
struct CryArray;
struct CryPtrArray;
/**
* Creates an CryArray instance with a given initialization- and extension size
......@@ -35,7 +36,7 @@ struct CryArray;
* \param extend Sets the size of memory blocks the array is dynamically extend.
* \return the reference pointer to the constructed array, used by other array calls.
*/
struct CryArray* cry_array_new(size_t initialsize, size_t extend);
struct CryArray* cry_array_new(size_t init_capacity);
/**
* Release all allocated memory for a created Array.
......@@ -44,10 +45,11 @@ struct CryArray* cry_array_new(size_t initialsize, size_t extend);
void cry_array_free(struct CryArray* array);
size_t cry_array_size(struct CryArray* array);
size_t cry_array_alloc(struct CryArray* array);
size_t cry_array_capacity(struct CryArray* array);
size_t cry_array_increase_alloc(struct CryArray* array, size_t size);
size_t cry_array_reduce_alloc(struct CryArray* array);
size_t cry_array_ensure(struct CryArray* array, size_t min_capacity);
size_t cry_array_trim(struct CryArray* array);
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);
......@@ -57,6 +59,22 @@ pointer cry_array_get(struct CryArray* array, size_t index);
struct CryPtrArray* cry_ptr_array_new(size_t init_capacity);
void cry_ptr_array_free(struct CryPtrArray* array);
size_t cry_ptr_array_size(struct CryPtrArray* array);
size_t cry_ptr_array_capacity(struct CryPtrArray* array);
size_t cry_ptr_array_ensure(struct CryPtrArray* array, size_t min_capacity);
size_t cry_ptr_array_trim(struct CryPtrArray* array);
void cry_ptr_array_append(struct CryPtrArray* array, 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_get(struct CryPtrArray* array, size_t index);
#endif // CRYSTAL_STRUCTURES_ARRAY_H
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