--- libelwix/src/array.c 2013/01/17 10:05:35 1.1 +++ libelwix/src/array.c 2019/01/23 17:40:37 1.8.4.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: array.c,v 1.1 2013/01/17 10:05:35 misho Exp $ +* $Id: array.c,v 1.8.4.2 2019/01/23 17:40:37 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 +Copyright 2004 - 2019 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -52,7 +52,7 @@ SUCH DAMAGE. * @numItems = Number of Items * return: NULL error, != NULL allocated memory for array */ -inline array_t * +array_t * array_Init(int numItems) { array_t *arr = NULL; @@ -68,12 +68,41 @@ array_Init(int numItems) e_free(arr); return NULL; } else - array_Zero(arr); + memset(arr->arr_data, 0, array_Size(arr) * sizeof(intptr_t)); return arr; } /* + * array_Init2() - Initialize dynamic array + * + * @arr = Allocated array variable + * @numItems = Number of Items + * return: NULL error, != NULL allocated memory for array + */ +array_t * +array_Init2(array_t * __restrict arr, int numItems) +{ + if (!arr) + return array_Init(numItems); + + if (array_Size(arr)) + return NULL; /* already allocated array! */ + + arr->arr_last = -1; + arr->arr_num = numItems; + arr->arr_data = e_calloc(array_Size(arr), sizeof(intptr_t)); + if (!arr->arr_data) { + e_free(arr); + return NULL; + } else + memset(arr->arr_data, 0, array_Size(arr) * sizeof(intptr_t)); + + return arr; +} + + +/* * array_From() - Create and fill array from array with pointers * * @pargv = Array with pointers @@ -137,7 +166,7 @@ array_To(array_t * __restrict arr) * @arr = Array * return: none */ -inline void +void array_Free(array_t * __restrict arr) { register int i; @@ -146,12 +175,10 @@ array_Free(array_t * __restrict arr) return; for (i = 0; i < array_Size(arr); i++) - if (arr->arr_data[i]) { + if (arr->arr_data[i]) e_free(arr->arr_data[i]); - arr->arr_data[i] = NULL; - } - arr->arr_last = -1; + array_Zero(arr); } /* @@ -160,7 +187,7 @@ array_Free(array_t * __restrict arr) * @parr = Array * return: none */ -inline void +void array_Destroy(array_t ** __restrict parr) { if (!parr || !*parr) @@ -173,12 +200,50 @@ array_Destroy(array_t ** __restrict parr) } /* + * array_Destroy2() - Free data in dynamic array + * + * @parr = Array + * return: none + */ +void +array_Destroy2(array_t * __restrict arr) +{ + if (!arr) + return; + + if (arr->arr_data) + e_free(arr->arr_data); + memset(arr, 0, sizeof(array_t)); +} +/* + * array_Reset() - Reset array to initial state + * + * @parr = Array + * @purge = Purge all data, if <>0 then will be free entire data memory + * return: none + */ +void +array_Reset(array_t * __restrict arr, int purge) +{ + if (!arr) + return; + + if (purge && arr->arr_data) { + e_free(arr->arr_data); + arr->arr_num = 0; + arr->arr_data = e_calloc(array_Size(arr), sizeof(intptr_t)); + } + + array_Zero(arr); +} + +/* * array_Len() - Get last used element in dynamic array (array Length) * * @arr = Array * return: -1 empty or >-1 position of last used element */ -inline int +int array_Len(array_t * __restrict arr) { register int i; @@ -319,7 +384,7 @@ array_Concat(array_t * __restrict dest, array_t * __re /* * array_Copy() Copy source array to destination array * - * @dest = Destination array, after use free with io_arrayDestroy() + * @dest = Destination array, after use free with array_Destroy() * @src = Source array * return: -1 error; >0 count of destination array */ @@ -347,7 +412,7 @@ array_Copy(array_t ** __restrict dest, array_t * __res * @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 * +void * array_Elem(array_t * __restrict arr, int n, void *data) { void *dat = NULL; @@ -373,8 +438,8 @@ array_Elem(array_t * __restrict arr, int n, void *data * @nogrow = Don't grow array if not enough space * return: -1 not found empty position, array is full!, >-1 return position of stored element into array */ -inline int -array_Push(array_t * __restrict arr, void **data, int nogrow) +int +array_Push(array_t * __restrict arr, void *data, int nogrow) { int ret = -1; @@ -387,9 +452,8 @@ array_Push(array_t * __restrict arr, void **data, int if (!nogrow && ret >= array_Size(arr) && array_Grow(arr, ret + 1, 0)) return -1; - ret = arr->arr_last++; - if (data) - arr->arr_data[arr->arr_last] = *data; + ret = ++arr->arr_last; + arr->arr_data[arr->arr_last] = data; return ret; } @@ -402,7 +466,7 @@ array_Push(array_t * __restrict arr, void **data, int * @nodel = Don't delete after Pop element * return: -1 not found used position, array is empty!, >-1 return element position */ -inline int +int array_Pop(array_t * __restrict arr, void ** __restrict data, int nodel) { int ret = -1; @@ -410,19 +474,20 @@ array_Pop(array_t * __restrict arr, void ** __restrict if (!arr) return -1; - ret = array_Last(arr); + if ((ret = array_Last(arr)) != -1) { + if (data) + *data = arr->arr_data[arr->arr_last]; + if (!nodel) + arr->arr_data[arr->arr_last] = NULL; + arr->arr_last--; + } - if (data) - *data = arr->arr_data[ret]; - if (!nodel) - arr->arr_data[arr->arr_last--] = NULL; - return ret; } /* * array_Args() Parse and make array from arguments ... (input string will be modified!!! - * and output array must be free with io_arrayDestroy() after use!) + * and output array must be free with array_Destroy() after use!) * * @psArgs = Input arguments line, after execute string is modified!!! * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs