File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix / aarray.h
Revision 1.12: download - view: text, annotated - select for diffs - revision graph
Tue Oct 29 01:16:12 2024 UTC (4 weeks ago) by misho
Branches: MAIN
CVS tags: elwix6_6, HEAD, ELWIX6_5
Version 6.5

/*************************************************************************
* (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
*  by Michael Pounov <misho@elwix.org>
*
* $Author: misho $
* $Id: aarray.h,v 1.12 2024/10/29 01:16:12 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004 - 2024
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#ifndef __AARRAY_H
#define __AARRAY_H


typedef struct _tagArray {
	int	arr_last;
	int	arr_num;
	void	**arr_data;
} array_t;

#define array_Data(_arr)		((_arr) ? (_arr)->arr_data : NULL)
#define array_Size(_arr)		((_arr) ? (_arr)->arr_num : 0)
#define array_Last(_arr)		(array_Size((_arr)) ? (_arr)->arr_last : -1)
#define array_Zero(_arr)		(assert((_arr)), (_arr)->arr_last = -1, \
	       					memset((_arr)->arr_data, 0, array_Size((_arr)) * sizeof(intptr_t)))

#define array_Ptr(_arr, _d)		((_arr && (_arr)->arr_data) ? (_arr)->arr_data[_d] : NULL)
#define array_Get2(_arr, _d)		(assert((_arr) && (_arr)->arr_num > _d), ((_arr)->arr_data + _d))
#define array_Get(_arr, _d)		(assert((_arr) && (_arr)->arr_data), (_arr)->arr_data[_d])
#define array(_arr, _d, _type)		(assert((_arr) && (_arr)->arr_data), ((_type) (_arr)->arr_data[_d]))
#define array_Set(_arr, _d, _ptr)	do { int __d = _d; assert((_arr) && (_arr)->arr_num > __d); \
						if ((_arr)->arr_last < __d) \
							(_arr)->arr_last = __d; \
						(_arr)->arr_data[__d] = (void*) (_ptr); \
					} while (0)
#define array_Del(_arr, _d, _fri)	do { int __d = _d; assert((_arr) && (_arr)->arr_num > __d); \
						if (_fri && (_arr)->arr_data[__d]) \
							e_free((_arr)->arr_data[__d]); \
						(_arr)->arr_data[__d] = NULL; \
					} while (0)
#define array_Var(_arr, _d)		(assert((_arr) && (_arr)->arr_num > _d), (ait_val_t*) (_arr)->arr_data[_d])

/*
 * array_Init() - Create and initialize dynamic array
 *
 * @numItems = Number of Items
 * return: NULL error, != NULL allocated memory for array
 */
array_t *array_Init(int numItems);
/*
 * 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);
/*
 * array_Destroy() - Free and destroy dynamic array
 *
 * @parr = Array
 * return: none
 */
void array_Destroy(array_t ** __restrict parr);
/*
 * array_Destroy2() - Free data in dynamic array
 *
 * @parr = Array
 * return: none
 */
void array_Destroy2(array_t * __restrict arr);
/*
 * array_Free() - Free all data in dynamic array items
 *	(WARNING! If assign static array dont use this!!!)
 *
 * @arr = Array
 * return: none
 */
void array_Free(array_t * __restrict arr);
/*
 * 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);

/*
 * array_From() - Create and fill array from 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 new array
 */
array_t *array_From(const char *** __restrict pargv, int argc);
/*
 * array_Args() Parse and make array from arguments ... (input string will be modified!!! 
 *	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
 * @csDelim = Delimiter(s) for separate
 * @parr = Output array of arguments ... (must be free with array_Destroy() after use!)
 * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
 */
int array_Args(char * __restrict psArgs, int nargs, const char *csDelim, 
		array_t ** __restrict parr);
/*
 * array_To() - Create and fill array with pointers from dynamic array
 *
 * @arr = Array
 * return: NULL error, != NULL allocated memory for array, NULL terminated
 */
char **array_To(array_t * __restrict arr);

/*
 * array_Len() - Get last used element in dynamic array (array Length)
 *
 * @arr = Array
 * return: -1 empty or >-1 position of last used element
 */
int array_Len(array_t * __restrict arr);

/*
 * array_Grow() - 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 array_Grow(array_t * __restrict arr, int newNumItems, int freeShrink);
/*
 * array_Vacuum() - Vacuum dynamic array, empty elements will be deleted
 *
 * @arr = Array
 * @fromWhere = 1 begin, 2 ALL empty elements
 * return: -1 error, 0 ok
 */
int array_Vacuum(array_t * __restrict arr, int fromWhere);
/*
 * array_Concat() Concat source array to destination array
 *
 * @dest = Destination array
 * @src = Source array
 * return: -1 error; >0 new count of destination array
 */
int array_Concat(array_t * __restrict dest, array_t * __restrict src);
/*
 * array_Copy() Copy source array to destination array
 *
 * @dest = Destination array, after use free with array_Destroy()
 * @src = Source array
 * return: -1 error; >0 count of destination array
 */
int array_Copy(array_t ** __restrict dest, array_t * __restrict src);

/*
 * array_Elem() - 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
 */
void *array_Elem(array_t * __restrict arr, int n, void *data);
/*
 * array_Push() - 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
 * @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
 */
int array_Push(array_t * __restrict arr, void *data, int nogrow);
/*
 * array_Pop() - Pop element from dynamic array like stack manner, last used position
 *
 * @arr = Array
 * @data = Element, if set NULL return only last used position
 * @nodel = Don't delete after Pop element
 * return: -1 not found used position, array is empty!, >-1 return element position
 */
int array_Pop(array_t * __restrict arr, void ** __restrict data, int nodel);


#endif

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