ZEDA  1.6.18
Macros | Functions
universal array operations.

Macros

#define zArrayClass(array_t, cell_t)
 generate array class. More...
 
#define zArraySize(a)   (a)->size
 
#define zArrayBuf(a)   (a)->buf
 
#define zArrayPosIsValid(a, p)   ( (p) < zArraySize(a) && (p) >= 0 )
 
#define zArrayElemSize(a)   sizeof(*zArrayBuf(a))
 
#define zArrayElemNC(a, i)   ( &zArrayBuf(a)[i] )
 
#define zArrayElem(a, i)   ( zArrayPosIsValid(a,i) ? zArrayElemNC(a,i) : NULL )
 
#define zArraySetElemNC(a, i, d)   memcpy( zArrayElemNC(a,i), (d), zArrayElemSize(a) )
 
#define zArraySetElem(a, i, d)   ( zArrayPosIsValid(a,i) ? zArraySetElemNC(a,i,d) : NULL )
 
#define zArrayHead(a)   zArrayElemNC( a, zArraySize(a)-1 )
 
#define zArrayNeck(a)   zArrayElemNC( a, zArraySize(a)-2 )
 
#define zArrayTail(a)   zArrayElemNC( a, 0 )
 
#define zArrayInit(arr)
 
#define zArrayAlloc(arr, type, n)
 allocate an array. More...
 
#define zArrayFree(arr)
 free an array. More...
 
#define zArrayFindName(arr, name, ptr)   zNameFind( zArrayBuf(arr), zArraySize(arr), name, ptr )
 
#define zArrayAdd(arr, type, dat)
 add a new cell to an array. More...
 
#define zArrayInsert(arr, type, pos, dat)
 insert a new cell into an array. More...
 
#define zArrayDelete(arr, type, pos)
 delete a cell from an array. More...
 
#define zArrayAppend(arr, subarr, type)
 append an array to another. More...
 
#define zArrayQuickSort(arr, cmp, priv)   zQuickSort( (void*)zArrayBuf(arr), zArraySize(arr), zArrayElemSize(arr), cmp, priv )
 quick sort for an array. More...
 

Functions

void zQuickSort (void *array, size_t nmemb, size_t size, int(*cmp)(void *, void *, void *), void *priv)
 quick sort for a pointer array. More...
 

Detailed Description

Macro Definition Documentation

◆ zArrayClass

#define zArrayClass (   array_t,
  cell_t 
)
Value:
typedef struct{\
uint size;\
cell_t *buf;\
} array_t

generate array class.

A macro zArrayClass() can generate a new array class, which consists of the size of the array and the pointer to the array buffer. array_t is the class name to be defined. cell_t is the class name of the data to be arrayed.

The size of the array is acquired by zArraySize(). The array head can be accessed by zArrayBuf(). Each element can be accessed by zArrayElem(). For a faster access to each element, zArrayElemNC() is also available, which does not check the specified location.

The array can be freed by calling zArrayFree().

zArraySetElem() is available in order to set each element, which works only if the specified location is valid. zArraySetElemNC() is also available, which provides a faster access to each element without checking the validity of specified location. For frequent accesses to the head, neck (one-cell-before-head) and tail, zArrayHead(), zArrayNeck() and zArrayTail() are available.

◆ zArraySize

#define zArraySize (   a)    (a)->size

◆ zArrayBuf

#define zArrayBuf (   a)    (a)->buf

◆ zArrayPosIsValid

#define zArrayPosIsValid (   a,
 
)    ( (p) < zArraySize(a) && (p) >= 0 )

◆ zArrayElemSize

#define zArrayElemSize (   a)    sizeof(*zArrayBuf(a))

◆ zArrayElemNC

#define zArrayElemNC (   a,
 
)    ( &zArrayBuf(a)[i] )

◆ zArrayElem

#define zArrayElem (   a,
 
)    ( zArrayPosIsValid(a,i) ? zArrayElemNC(a,i) : NULL )

◆ zArraySetElemNC

#define zArraySetElemNC (   a,
  i,
 
)    memcpy( zArrayElemNC(a,i), (d), zArrayElemSize(a) )

◆ zArraySetElem

#define zArraySetElem (   a,
  i,
 
)    ( zArrayPosIsValid(a,i) ? zArraySetElemNC(a,i,d) : NULL )

◆ zArrayHead

#define zArrayHead (   a)    zArrayElemNC( a, zArraySize(a)-1 )

◆ zArrayNeck

#define zArrayNeck (   a)    zArrayElemNC( a, zArraySize(a)-2 )

◆ zArrayTail

#define zArrayTail (   a)    zArrayElemNC( a, 0 )

◆ zArrayInit

#define zArrayInit (   arr)
Value:
do{\
zArraySize(arr) = 0;\
zArrayBuf(arr) = NULL;\
} while(0)

◆ zArrayAlloc

#define zArrayAlloc (   arr,
  type,
 
)
Value:
do{\
if( (n) <= 0 ) zArrayInit( arr );\
else{\
if( !( zArrayBuf(arr) = zAlloc( type, n ) ) ){\
ZALLOCERROR();\
zArraySize(arr) = 0;\
zArraySize(arr) = (n);\
}\
} while(0)
#define zArraySize(a)
Definition: zeda_array.h:60
#define zAlloc(t, n)
Definition: zeda_misc.h:81
#define zArrayInit(arr)
Definition: zeda_array.h:77
#define zArrayBuf(a)
Definition: zeda_array.h:61

allocate an array.

Parameters
arrarray class instance to be allocated.
typethe data type of the array cells.
nthe number of cells.

◆ zArrayFree

#define zArrayFree (   arr)
Value:
do{\
free( zArrayBuf(arr) );\
zArrayInit( arr );\
} while(0)
#define zArrayBuf(a)
Definition: zeda_array.h:61

free an array.

Parameters
arra pointer to the array to be freed.

◆ zArrayFindName

#define zArrayFindName (   arr,
  name,
  ptr 
)    zNameFind( zArrayBuf(arr), zArraySize(arr), name, ptr )

zArrayFindName() is valid for an array of a named class.

See also
zNameFind.

◆ zArrayAdd

#define zArrayAdd (   arr,
  type,
  dat 
)
Value:
do{\
type *__zarray_ap;\
__zarray_ap = zRealloc( zArrayBuf(arr), type, zArraySize(arr)+1 );\
if( __zarray_ap == NULL )\
ZALLOCERROR();\
else{\
zArraySize(arr)++;\
zArrayBuf(arr) = __zarray_ap;\
zArraySetElemNC( arr, zArraySize(arr)-1, dat );\
}\
} while(0)
#define zArraySize(a)
Definition: zeda_array.h:60
#define zRealloc(m, t, n)
Definition: zeda_misc.h:100
#define zArrayBuf(a)
Definition: zeda_array.h:61

add a new cell to an array.

zArrayAdd() adds a new cell pointed by dat to an array arr at the last, incrementing the size of arr. type is the data type of the cell.

◆ zArrayInsert

#define zArrayInsert (   arr,
  type,
  pos,
  dat 
)
Value:
do{\
type *__zarray_ap;\
if( (pos) == zArraySize(arr) ){\
zArrayAdd( arr, type, dat );\
} else if( zArrayPosIsValid(arr,pos) ){\
__zarray_ap = zRealloc( zArrayBuf(arr), type, zArraySize(arr)+1 );\
if( __zarray_ap == NULL )\
ZALLOCERROR();\
else{\
zArrayBuf(arr) = __zarray_ap;\
memmove( zArrayElemNC(arr,(pos)+1), zArrayElemNC(arr,pos), sizeof(type)*(zArraySize(arr)-(pos)) );\
zArraySetElemNC( arr, pos, dat );\
zArraySize(arr)++;\
}\
} else{\
ZRUNWARN( "invalid position %d/%d in array specified", pos, zArraySize(arr)-1 );\
}\
} while(0)
#define zArraySize(a)
Definition: zeda_array.h:60
#define zRealloc(m, t, n)
Definition: zeda_misc.h:100
#define zArrayElemNC(a, i)
Definition: zeda_array.h:68
#define zArrayPosIsValid(a, p)
Definition: zeda_array.h:63
#define zArrayBuf(a)
Definition: zeda_array.h:61

insert a new cell into an array.

zArrayInsert() inserts a new cell pointed by dat to in an array arr at the location specified by pos, incrementing the size of arr. type is the data type of the cell.

◆ zArrayDelete

#define zArrayDelete (   arr,
  type,
  pos 
)
Value:
do{\
type *__zarray_ap;\
if( zArrayPosIsValid(arr,pos) ){\
if( (pos) < zArraySize(arr)-1 )\
memmove( zArrayElemNC(arr,pos), zArrayElemNC(arr,(pos)+1), sizeof(type)*(zArraySize(arr)-(pos)-1) );\
if( zArraySize(arr) > 1 ){\
__zarray_ap = zRealloc( zArrayBuf(arr), type, zArraySize(arr)-1 );\
if( __zarray_ap == NULL )\
ZALLOCERROR();\
else{\
zArraySize(arr)--;\
zArrayBuf(arr) = __zarray_ap;\
}\
} else{\
zArrayFree( arr );\
}\
} else{\
ZRUNWARN( "invalid position %d/%d in array specified", pos, zArraySize(arr)-1 );\
}\
} while(0)
#define zArraySize(a)
Definition: zeda_array.h:60
#define zRealloc(m, t, n)
Definition: zeda_misc.h:100
#define zArrayElemNC(a, i)
Definition: zeda_array.h:68
#define zArrayPosIsValid(a, p)
Definition: zeda_array.h:63
#define zArrayBuf(a)
Definition: zeda_array.h:61

delete a cell from an array.

zArrayDelete() deletes a cell at the location specified by pos in an array arr at, decrementing the size of arr. type is the data type of the cell.

◆ zArrayAppend

#define zArrayAppend (   arr,
  subarr,
  type 
)
Value:
do{\
type *__zarray_ap;\
__zarray_ap = zRealloc( zArrayBuf(arr), type, zArraySize(arr)+zArraySize(subarr) );\
if( __zarray_ap == NULL )\
ZALLOCERROR();\
else{\
zArrayBuf(arr) = __zarray_ap;\
memcpy( zArrayElemNC(arr,zArraySize(arr)), zArrayBuf(subarr), zArraySize(subarr)*sizeof(type) );\
zArraySize(arr) += zArraySize(subarr);\
}\
} while(0)
#define zArraySize(a)
Definition: zeda_array.h:60
#define zRealloc(m, t, n)
Definition: zeda_misc.h:100
#define zArrayElemNC(a, i)
Definition: zeda_array.h:68
#define zArrayBuf(a)
Definition: zeda_array.h:61

append an array to another.

zArrayAppend() appends an array pointed by subarr to another array pointed by arr. type is the data type of the cell.

◆ zArrayQuickSort

#define zArrayQuickSort (   arr,
  cmp,
  priv 
)    zQuickSort( (void*)zArrayBuf(arr), zArraySize(arr), zArrayElemSize(arr), cmp, priv )

quick sort for an array.

zArrayQuickSort() is a macro which is a wrapper of zQuickSort() that sorts an array pointed by array based on the quick sort algorithm. cmp and priv have the same roles with those for zQuickSort().

See also
zQuickSort()

Function Documentation

◆ zQuickSort()

void zQuickSort ( void *  array,
size_t  nmemb,
size_t  size,
int(*)(void *, void *, void *)  cmp,
void *  priv 
)

quick sort for a pointer array.

zQuickSort() sorts an array pointed by array based on the quick sort algorithm. nmemb is the number of components of array, and size is the size of each component. The components of array will be sorted in ascending order according to the comparison function cmp. Namely, a factor 'a' in the array is put after another factor 'b' if cmp(a,b,priv) > 0, where priv is for programmer's utility.

Returns
zQuickSort() returns no value.