--- libaitio/src/Attic/array.c 2011/05/19 04:05:14 1.2.4.4 +++ libaitio/src/Attic/array.c 2011/08/31 12:29:32 1.4.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: array.c,v 1.2.4.4 2011/05/19 04:05:14 misho Exp $ +* $Id: array.c,v 1.4.2.1 2011/08/31 12:29:32 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -69,32 +69,32 @@ io_arrayInit(int numItems) free(arr); return NULL; } else - memset(arr->arr_data, 0, io_arraySize(arr) * sizeof(void*)); + io_arrayZero(arr); return arr; } /* * io_arrayFrom() - Create and fill array from array with pointers - * @argv = Array with pointers + * @pargv = Array with pointers * @argc = Number of Items, if 0 walk through argv and stop when reach NULL item - * return: NULL error, != NULL allocated memory for array + * return: NULL error, != NULL allocated new array */ inline array_t * -io_arrayFrom(const char **argv, int argc) +io_arrayFrom(const char *** __restrict pargv, int argc) { array_t *arr = NULL; const char **a = NULL; register int num = 0; - assert(argv); - if (!argv || !*argv || argc < 0) + assert(pargv); + if (!pargv || !*pargv || argc < 0) return NULL; if (argc) num = argc; else - for (a = argv; *a; a++, num++); + for (a = *pargv; *a; a++, num++); arr = malloc(sizeof(array_t)); if (!arr) { @@ -109,7 +109,7 @@ io_arrayFrom(const char **argv, int argc) free(arr); return NULL; } else - memcpy(arr->arr_data, argv, io_arraySize(arr) * sizeof(void*)); + memcpy(arr->arr_data, *pargv, io_arraySize(arr) * sizeof(void*)); return arr; } @@ -117,7 +117,7 @@ io_arrayFrom(const char **argv, int argc) /* * io_arrayTo() - Create and fill array with pointers from dynamic array * @arr = Array - * return: NULL error, != NULL allocated memory for array + * return: NULL error, != NULL allocated memory for array, NULL terminated */ inline char ** io_arrayTo(array_t * __restrict arr) @@ -150,7 +150,6 @@ io_arrayFree(array_t * __restrict arr) { register int i; - assert(arr); if (!arr) return; @@ -169,8 +168,7 @@ io_arrayFree(array_t * __restrict arr) inline void io_arrayDestroy(array_t ** __restrict parr) { - assert(parr); - if (!parr) + if (!parr || !*parr) return; if ((*parr)->arr_data) @@ -202,14 +200,15 @@ io_arrayLen(array_t * __restrict arr) * io_arrayGrow() - Grow/Shrink dynamic array, Use with care when it shrink!!! * @arr = Array * @newNumItems = Number of Items + * @freeShrink = Free elements before shrink array * return: -1 error, 0 ok */ int -io_arrayGrow(array_t * __restrict arr, int newNumItems) +io_arrayGrow(array_t * __restrict arr, int newNumItems, int freeShrink) { void **data; int n = 0; -/* register int i; */ + register int i; assert(arr); if (!arr) @@ -219,11 +218,10 @@ io_arrayGrow(array_t * __restrict arr, int newNumItems return 0; if (io_arraySize(arr) < newNumItems) { n = newNumItems - io_arraySize(arr); - } /* else + } else if (freeShrink) for (i = newNumItems; i < arr->arr_num; i++) if (arr->arr_data[i]) free(arr->arr_data[i]); - */ arr->arr_num = newNumItems; if (io_arraySize(arr)) { @@ -271,12 +269,13 @@ io_arrayVacuum(array_t * __restrict arr, int fromWhere */ if (fromWhere & VACUUM_LEFT) { for (i = 0; i < num && !arr->arr_data[i]; i++); + if (i) { + memmove(arr->arr_data, arr->arr_data + i, (num - i) * sizeof(void*)); + memset(arr->arr_data + (num - i), 0, i * sizeof(void*)); - memmove(arr->arr_data, arr->arr_data + i, (num - i) * sizeof(void*)); - memset(arr->arr_data + (num - i), 0, i * sizeof(void*)); - - num -= i; - cx += i; + num -= i; + cx += i; + } } if (fromWhere & VACUUM_BETWEEN) { for (i = 0; i < num; i++) { @@ -324,7 +323,7 @@ io_arrayPush(array_t * __restrict arr, void **data) /* * io_arrayPop() - Pop element from dynamic array like stack manner, last used position * @arr = Array - * @data = Element, if set NULL return only first empty position + * @data = Element, if set NULL return only last used position * @delAfter = Delete after Pop element, !=0 delete element from array after return data * return: -1 not found used position, array is empty!, >-1 return element position */ @@ -366,7 +365,7 @@ io_arrayConcat(array_t * __restrict dest, array_t * __ return -1; n = io_arraySize(dest); - if (io_arrayGrow(dest, n + io_arraySize(src))) + if (io_arrayGrow(dest, n + io_arraySize(src), 0)) return -1; memcpy(dest->arr_data + n, src->arr_data, io_arraySize(src) * sizeof(void*));