File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / src / array.c
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Mon Jan 21 12:27:37 2019 UTC (5 years, 4 months ago) by misho
Branches: MAIN
CVS tags: elwix4_18, HEAD, ELWIX4_17
ver 4.17

    1: /*************************************************************************
    2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: array.c,v 1.7 2019/01/21 12:27:37 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004 - 2019
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: /*
   50:  * array_Init() - Create and initialize dynamic array
   51:  *
   52:  * @numItems = Number of Items
   53:  * return: NULL error, != NULL allocated memory for array
   54:  */
   55: array_t *
   56: array_Init(int numItems)
   57: {
   58: 	array_t *arr = NULL;
   59: 
   60: 	arr = e_malloc(sizeof(array_t));
   61: 	if (!arr)
   62: 		return NULL;
   63: 
   64: 	arr->arr_last = -1;
   65: 	arr->arr_num = numItems;
   66: 	arr->arr_data = e_calloc(array_Size(arr), sizeof(intptr_t));
   67: 	if (!arr->arr_data) {
   68: 		e_free(arr);
   69: 		return NULL;
   70: 	} else
   71: 		array_Zero(arr);
   72: 
   73: 	return arr;
   74: }
   75: 
   76: /*
   77:  * array_From() - Create and fill array from array with pointers
   78:  *
   79:  * @pargv = Array with pointers
   80:  * @argc = Number of Items, if 0 walk through argv and stop when reach NULL item
   81:  * return: NULL error, != NULL allocated new array
   82:  */
   83: array_t *
   84: array_From(const char *** __restrict pargv, int argc)
   85: {
   86: 	array_t *arr = NULL;
   87: 	const char **a = NULL;
   88: 	register int num = 0;
   89: 
   90: 	if (!pargv || !*pargv || argc < 0)
   91: 		return NULL;
   92: 
   93: 	if (argc)
   94: 		num = argc;
   95: 	else
   96: 		for (a = *pargv; *a; a++, num++);
   97: 
   98: 	arr = array_Init(num);
   99: 	if (!arr)
  100: 		return NULL;
  101: 	else {
  102: 		memcpy(arr->arr_data, *pargv, array_Size(arr) * sizeof(intptr_t));
  103: 		arr->arr_last = array_Size(arr) - 1;
  104: 	}
  105: 
  106: 	return arr;
  107: }
  108: 
  109: /*
  110:  * array_To() - Create and fill array with pointers from dynamic array
  111:  *
  112:  * @arr = Array
  113:  * return: NULL error, != NULL allocated memory for array, NULL terminated
  114:  */
  115: char **
  116: array_To(array_t * __restrict arr)
  117: {
  118: 	char **args = NULL;
  119: 
  120: 	if (!arr || !array_Size(arr))
  121: 		return NULL;
  122: 
  123: 	args = (char **) e_calloc(array_Size(arr) + 1, sizeof(char*));
  124: 	if (!args)
  125: 		return NULL;
  126: 	else
  127: 		memcpy(args, arr->arr_data, array_Size(arr) * sizeof(char*));
  128: 	args[array_Size(arr)] = NULL;
  129: 
  130: 	return args;
  131: }
  132: 
  133: /*
  134:  * array_Free() - Free all data in dynamic array items
  135:  *	(WARNING! If assign static array dont use this!!!)
  136:  *
  137:  * @arr = Array
  138:  * return: none
  139:  */
  140: void
  141: array_Free(array_t * __restrict arr)
  142: {
  143: 	register int i;
  144: 
  145: 	if (!arr)
  146: 		return;
  147: 
  148: 	for (i = 0; i < array_Size(arr); i++)
  149: 		if (arr->arr_data[i]) {
  150: 			e_free(arr->arr_data[i]);
  151: 			arr->arr_data[i] = NULL;
  152: 		}
  153: 
  154: 	arr->arr_last = -1;
  155: }
  156: 
  157: /*
  158:  * array_Destroy() - Free and destroy dynamic array
  159:  *
  160:  * @parr = Array
  161:  * return: none
  162:  */
  163: void
  164: array_Destroy(array_t ** __restrict parr)
  165: {
  166: 	if (!parr || !*parr)
  167: 		return;
  168: 
  169: 	if ((*parr)->arr_data)
  170: 		e_free((*parr)->arr_data);
  171: 	e_free(*parr);
  172: 	*parr = NULL;
  173: }
  174: 
  175: /*
  176:  * array_Reset() - Reset array to initial state
  177:  *
  178:  * @parr = Array
  179:  * @purge = Purge data, if <>0 then will be free entire data memory
  180:  * return: none
  181:  */
  182: void
  183: array_Reset(array_t * __restrict arr, int purge)
  184: {
  185: 	if (!arr)
  186: 		return;
  187: 
  188: 	if (purge && arr->arr_data) {
  189: 		e_free(arr->arr_data);
  190: 		arr->arr_num = 0;
  191: 		arr->arr_data = e_calloc(array_Size(arr), sizeof(intptr_t));
  192: 	}
  193: 
  194: 	array_Zero(arr);
  195: 	arr->arr_last = -1;
  196: }
  197: 
  198: /*
  199:  * array_Len() - Get last used element in dynamic array (array Length)
  200:  *
  201:  * @arr = Array
  202:  * return: -1 empty or >-1 position of last used element
  203:  */
  204: int
  205: array_Len(array_t * __restrict arr)
  206: {
  207: 	register int i;
  208: 
  209: 	if (!arr)
  210: 		return -1;
  211: 
  212: 	for (i = array_Size(arr); i && !arr->arr_data[i - 1]; i--);
  213: 
  214: 	return --i;
  215: }
  216: 
  217: /*
  218:  * array_Grow() - Grow/Shrink dynamic array, Use with care when it shrink!!!
  219:  *
  220:  * @arr = Array
  221:  * @newNumItems = Number of Items
  222:  * @freeShrink = Free elements before shrink array
  223:  * return: -1 error, 0 ok
  224:  */
  225: int
  226: array_Grow(array_t * __restrict arr, int newNumItems, int freeShrink)
  227: {
  228: 	void **data;
  229: 	int n = 0;
  230: 	register int i;
  231: 
  232: 	if (!arr)
  233: 		return -1;
  234: 
  235: 	if (arr->arr_num == newNumItems)
  236: 		return 0;
  237: 	if (array_Size(arr) < newNumItems) {
  238: 		n = newNumItems - array_Size(arr);
  239: 	} else if (freeShrink)
  240: 		for (i = newNumItems; i < arr->arr_num; i++)
  241: 			if (arr->arr_data[i])
  242: 				e_free(arr->arr_data[i]);
  243: 
  244: 	arr->arr_num = newNumItems;
  245: 	if (array_Size(arr)) {
  246: 		data = e_realloc(arr->arr_data, array_Size(arr) * sizeof(intptr_t));
  247: 		if (!data)
  248: 			return -1;
  249: 		else
  250: 			arr->arr_data = data;
  251: 
  252: 		memset(arr->arr_data + (array_Size(arr) - n), 0, n * sizeof(intptr_t));
  253: 		arr->arr_last = array_Len(arr);
  254: 	} else {
  255: 		if (arr->arr_data)
  256: 			e_free(arr->arr_data);
  257: 		arr->arr_data = NULL;
  258: 		arr->arr_last = -1;
  259: 	}
  260: 
  261: 	return 0;
  262: }
  263: 
  264: /*
  265:  * array_Vacuum() - Vacuum dynamic array, empty elements will be deleted
  266:  *
  267:  * @arr = Array
  268:  * @fromWhere = 1 begin, 2 ALL empty elements
  269:  * return: -1 error, 0 ok
  270:  */
  271: int
  272: array_Vacuum(array_t * __restrict arr, int fromWhere)
  273: {
  274: 	register int i, j, num;
  275: 	int cx = 0;
  276: 
  277: 	if (!arr)
  278: 		return -1;
  279: 	else
  280: 		fromWhere &= 0x7;
  281: 
  282: 	num = array_Size(arr);
  283: #ifdef VACUUM_RIGHT
  284: 	if (fromWhere & VACUUM_RIGHT) {
  285: 		for (cx = 0, i = num - 1; i && !arr->arr_data[i]; i--, cx++);
  286: 		num -= cx;
  287: 	}
  288: #endif
  289: 	if (fromWhere & VACUUM_LEFT) {
  290: 		for (i = 0; i < num && !arr->arr_data[i]; i++);
  291: 		if (i) {
  292: 			memmove(arr->arr_data, arr->arr_data + i, (num - i) * sizeof(intptr_t));
  293: 			memset(arr->arr_data + (num - i), 0, i * sizeof(intptr_t));
  294: 
  295: 			num -= i;
  296: 			cx += i;
  297: 		}
  298: 	}
  299: 	if (fromWhere & VACUUM_BETWEEN) {
  300: 		for (i = 0; i < num; i++) {
  301: 			if (arr->arr_data[i])
  302: 				continue;
  303: 
  304: 			for (j = i; j < num && !arr->arr_data[j]; j++);
  305: 
  306: 			memmove(arr->arr_data + i, arr->arr_data + j, (num - j) * sizeof(intptr_t));
  307: 			memset(arr->arr_data + i + (num - j), 0, (j - i) * sizeof(intptr_t));
  308: 
  309: 			num -= j - i;
  310: 			cx += j - i;
  311: 		}
  312: 	}
  313: 
  314: 	arr->arr_last = array_Len(arr);
  315: 	return cx;
  316: }
  317: 
  318: /*
  319:  * array_Concat() Concat source array to destination array
  320:  *
  321:  * @dest = Destination array
  322:  * @src = Source array
  323:  * return: -1 error; >0 new count of destination array
  324:  */
  325: int
  326: array_Concat(array_t * __restrict dest, array_t * __restrict src)
  327: {
  328: 	int n;
  329: 
  330: 	if (!dest || !src)
  331: 		return -1;
  332: 
  333: 	n = array_Size(dest);
  334: 	if (array_Grow(dest, n + array_Size(src), 0))
  335: 		return -1;
  336: 	memcpy(dest->arr_data + n, src->arr_data, array_Size(src) * sizeof(intptr_t));
  337: 
  338: 	dest->arr_last = array_Len(dest);
  339: 	return array_Size(dest);
  340: }
  341: 
  342: /*
  343:  * array_Copy() Copy source array to destination array
  344:  *
  345:  * @dest = Destination array, after use free with array_Destroy()
  346:  * @src = Source array
  347:  * return: -1 error; >0 count of destination array
  348:  */
  349: int
  350: array_Copy(array_t ** __restrict dest, array_t * __restrict src)
  351: {
  352: 	if (!dest || !src)
  353: 		return -1;
  354: 
  355: 	*dest = array_Init(array_Size(src));
  356: 	if (!*dest)
  357: 		return -1;
  358: 	else
  359: 		(*dest)->arr_last = src->arr_last;
  360: 
  361: 	memcpy((*dest)->arr_data, src->arr_data, array_Size(*dest) * sizeof(intptr_t));
  362: 	return array_Size(*dest);
  363: }
  364: 
  365: /*
  366:  * array_Elem() - Always GET/PUT element into dynamic array, if not enough elements grow array
  367:  *
  368:  * @arr = Array
  369:  * @n = Position
  370:  * @data = Element, if set NULL GET element at position or !=NULL PUT element at position
  371:  * return: -1 error or !=-1 return element at position
  372:  */
  373: void *
  374: array_Elem(array_t * __restrict arr, int n, void *data)
  375: {
  376: 	void *dat = NULL;
  377: 
  378: 	if (!arr)
  379: 		return (void*) -1;
  380: 
  381: 	if (n >= array_Size(arr) && array_Grow(arr, n + 1, 0))
  382: 		return (void*) -1;
  383: 
  384: 	dat = array_Get(arr, n);
  385: 	if (data)
  386: 		array_Set(arr, n, data);
  387: 
  388: 	return dat;
  389: }
  390: 
  391: /*
  392:  * array_Push() - Push element into dynamic array like stack manner, place at first empty position
  393:  *
  394:  * @arr = Array
  395:  * @data = Element, if set NULL return only first empty position
  396:  * @nogrow = Don't grow array if not enough space
  397:  * return: -1 not found empty position, array is full!, >-1 return position of stored element into array
  398:  */
  399: int
  400: array_Push(array_t * __restrict arr, void *data, int nogrow)
  401: {
  402: 	int ret = -1;
  403: 
  404: 	if (!arr)
  405: 		return -1;
  406: 	else
  407: 		ret = array_Last(arr) + 1;
  408: 	if (nogrow && ret >= array_Size(arr))
  409: 		return -1;
  410: 	if (!nogrow && ret >= array_Size(arr) && array_Grow(arr, ret + 1, 0))
  411: 		return -1;
  412: 
  413: 	ret = ++arr->arr_last;
  414: 	arr->arr_data[arr->arr_last] = data;
  415: 
  416: 	return ret;
  417: }
  418: 
  419: /*
  420:  * array_Pop() - Pop element from dynamic array like stack manner, last used position
  421:  *
  422:  * @arr = Array
  423:  * @data = Element, if set NULL return only last used position
  424:  * @nodel = Don't delete after Pop element
  425:  * return: -1 not found used position, array is empty!, >-1 return element position
  426:  */
  427: int
  428: array_Pop(array_t * __restrict arr, void ** __restrict data, int nodel)
  429: {
  430: 	int ret = -1;
  431: 
  432: 	if (!arr)
  433: 		return -1;
  434: 
  435: 	if ((ret = array_Last(arr)) != -1) {
  436: 		if (data)
  437: 			*data = arr->arr_data[arr->arr_last];
  438: 		if (!nodel)
  439: 			arr->arr_data[arr->arr_last] = NULL;
  440: 		arr->arr_last--;
  441: 	}
  442: 
  443: 	return ret;
  444: }
  445: 
  446: /*
  447:  * array_Args() Parse and make array from arguments ... (input string will be modified!!! 
  448:  *	and output array must be free with array_Destroy() after use!)
  449:  *
  450:  * @psArgs = Input arguments line, after execute string is modified!!!
  451:  * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs
  452:  * @csDelim = Delimiter(s) for separate
  453:  * @parr = Output array of arguments ... (must be free with array_Destroy() after use!)
  454:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  455:  */
  456: int
  457: array_Args(char * __restrict psArgs, int nargs, const char *csDelim, array_t ** __restrict parr)
  458: {
  459: 	char **app;
  460: 	register int i;
  461: 
  462: 	if (!psArgs || !csDelim || !parr)
  463: 		return -1;
  464: 
  465: 	if (nargs)
  466: 		i = nargs;
  467: 	else
  468: 		i = str_ArgsNum(psArgs, csDelim);
  469: 
  470: 	*parr = array_Init(i);
  471: 	if (!*parr)
  472: 		return -1;
  473: 
  474: 	for (i = 0, app = (char**) (*parr)->arr_data; 
  475: 			app < (char**) (*parr)->arr_data + (*parr)->arr_num && 
  476: 			(*app = strsep((char **) &psArgs, csDelim)); 
  477: 		       	**app ? i++ : i, **app ? app++ : app);
  478: 
  479: 	(*parr)->arr_last = i - 1;
  480: 	return i;
  481: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>