Musaran Cerveaulté | Pour les curieux, voici mon source C 'arraytools.h' complet:
Code :
- //Help macros for aggregates (array or structure)
- //Parts are for C++
- //Doesn't pretend to be standard compliant, portable, fully protected, or bugless.
- //Required
- #include <memory.h>
- #include <stddef.h>
- #include <assert.h>
- #ifndef MIN
- #define MIN(a,b) ((a)<(b)?(a):(b))
- #endif
- //Test if argument is an array, not a pointer or address
- //array : returns 1/true (string litterals are arrays)
- //pointer: returns 0/false
- //litteral: error '&' on constant
- //other : error subscript requires array or pointer type
- #define ISARRAY(a) ( (char*)&(a)==(char*)&(a)[0] )
- //#define ISPOINTER(p) ???
- //#define ISADDRESS(p) ???
- //#define ISSTRUCT(p) ???
- //Number of array elements
- #define DIMOF(a) ( assert(ISARRAY(a)) ,\
- sizeof(a)/sizeof(a)[0] )
- //Byte-wise compares any two instances (including aggregates) of same size.
- //Not reliable on structures with holes (allignment spacing).
- //May not match arithmetic ordering.
- //<0: i1 less than i2
- // 0: i1 identical to i2
- //>0: i1 greater than i2
- #define BYTECOMPARE(i1,i2) (assert(sizeof(i1)==sizeof(i2)),\
- memcmp(&(i1), &(i2), sizeof(i1)))
- //True if any two instances (including aggregates) of same size are byte-wise identical.
- //Not reliable on structures with holes (allignment spacing).
- #define BYTEEQUAL(i1,i2) (BYTECOMPARE(i1,i2)==0)
- //Sets all bytes of any instance (including aggregates) to 0.
- //May not suit floating-point or C++ allocating types
- #define BYTEZEROFILL(a) memset(&(a),0,sizeof(a))
- //Sets all the elements of array to value.
- #define ARRAYFILL(array,value) do{assert(ISARRAY(array));\
- for(ptrdiff_t i= DIMOF(array)-1 ; i>=0 ; --i)\
- (array)[i]= value;}while(0)
- //True if arrays have same dimensions, element sizes, and byte-wise content.
- #define ARRAYEQUALFAST(a1,a2) (assert( ISARRAY(a1) && ISARRAY(a2) && sizeof(a1)[0]==sizeof(a2)[0] ),\
- BYTEEQUAL(a1,a2))
- //The following come in 2 versions each:
- //fast : expression, doesn't suit C++ allocating types. MAY only be faster.
- //normal: statement , can work with different destination & source types.
- //(result is undefined if areas overlap !)
- //Copy n array elements from src into dst (arrays or pointers)
- #define ARRAYCOPYN(dst,src,n) do{for(ptrdiff_t i= (n)-1 ; i>=0 ; --i)\
- (dst)[i]= (src)[i] ;}while(0)
- #define ARRAYCOPYNFAST(dst,src,n) ( assert(sizeof(src)[0]==sizeof(dst)[0]) ,\
- memcpy( dst, src, (n) * sizeof (src)[0] ) )
- //Copy array elements...
- //...from srcfirst included (array or pointer)...
- //...to srcafter excluded (pointer on same array as srcfirst)...
- //...into dst (array or pointer).
- #define ARRAYCOPYRANGE(dst,srcfirst,srcafter) ARRAYCOPYN ( dst, srcfirst, (srcafter)-(srcfirst) )
- #define ARRAYCOPYRANGEFAST(dst,srcfirst,srcafter) ARRAYCOPYNFAST( dst, srcfirst, (srcafter)-(srcfirst) )
- //Copy src to dst (both arrays of same dimension)
- #define ARRAYCOPY(dst,src) do{ assert(ISARRAY(dst) && ISARRAY(src) && DIMOF(src)==DIMOF(dst)) ;\
- ARRAYCOPYN (dst, src, DIMOF(src));}while(0)
- #define ARRAYCOPYFAST(dst,src) ( assert(ISARRAY(dst) && ISARRAY(src) && DIMOF(src)==DIMOF(dst)) ,\
- ARRAYCOPYNFAST(dst, src, DIMOF(src)) )
- //Copy src to dst (both arrays), using max safe number of elements
- //#ifdef __cpluplus
- #define ARRAYCOPYAUTO(dst,src) ARRAYCOPYN ( dst, src, MIN(DIMOF(src), DIMOF(dst)) )
- #define ARRAYCOPYAUTOFAST(dst,src) ARRAYCOPYNFAST( dst, src, MIN(DIMOF(src), DIMOF(dst)) )
|
---------------
Bricocheap: Montage de ventilo sur paté de mastic silicone
|