--- libaitio/src/Attic/array.c 2011/08/31 12:29:32 1.4.2.1 +++ libaitio/src/Attic/array.c 2011/12/13 10:08:53 1.6.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: array.c,v 1.4.2.1 2011/08/31 12:29:32 misho Exp $ +* $Id: array.c,v 1.6.2.1 2011/12/13 10:08:53 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -210,7 +210,6 @@ io_arrayGrow(array_t * __restrict arr, int newNumItems int n = 0; register int i; - assert(arr); if (!arr) return -1; @@ -254,7 +253,6 @@ io_arrayVacuum(array_t * __restrict arr, int fromWhere register int i, j, num; int cx = 0; - assert(arr); if (!arr) return -1; else @@ -296,6 +294,31 @@ io_arrayVacuum(array_t * __restrict arr, int fromWhere } /* + * io_arrayElem() - Always GET/PUT element into dynamic array, if not enough elements grow array + * @arr = Array + * @n = Position + * @data = Element, if set NULL GET element at position or !=NULL PUT element at position + * return: -1 error or !=-1 return element at position + */ +inline void * +io_arrayElem(array_t * __restrict arr, int n, void *data) +{ + void *dat = NULL; + + if (!arr) + return (void*) -1; + + if (n > io_arraySize(arr) && io_arrayGrow(arr, n + 1, 0)) + return (void*) -1; + + dat = io_arrayGet(arr, n); + if (data) + io_arraySet(arr, n, data); + + return dat; +} + +/* * io_arrayPush() - Push element into dynamic array like stack manner, place at first empty position * @arr = Array * @data = Element, if set NULL return only first empty position @@ -307,7 +330,8 @@ io_arrayPush(array_t * __restrict arr, void **data) register int i; int ret = -1; - assert(arr); + if (!arr) + return -1; for (i = 0; i < io_arraySize(arr); i++) if (!arr->arr_data[i]) { @@ -333,7 +357,8 @@ io_arrayPop(array_t * __restrict arr, void ** __restri register int i; int ret = -1; - assert(arr); + if (!arr) + return -1; for (i = io_arraySize(arr) - 1; i >= 0; i--) if (arr->arr_data[i]) { @@ -370,6 +395,28 @@ io_arrayConcat(array_t * __restrict dest, array_t * __ memcpy(dest->arr_data + n, src->arr_data, io_arraySize(src) * sizeof(void*)); return io_arraySize(dest); +} + +/* + * io_arrayCopy() Copy source array to destination array + * @dest = Destination array, after use free with io_arrayDestroy() + * @src = Source array + * return: -1 error; >0 count of destination array + */ +int +io_arrayCopy(array_t ** __restrict dest, array_t * __restrict src) +{ + assert(dest); + assert(src); + if (!dest || !src) + return -1; + + *dest = io_arrayInit(io_arraySize(src)); + if (!*dest) + return -1; + + memcpy((*dest)->arr_data, src->arr_data, io_arraySize(*dest) * sizeof(void*)); + return io_arraySize(*dest); } /*