00001
00010 #ifndef ARRAY_H
00011 #define ARRAY_H
00012
00013 #ifndef countof
00014
00020 #define countof(a) (sizeof (a)/sizeof (*a))
00021 #endif
00022
00030 #ifndef indexof
00031 #define indexof(a,p) ((p)-(a))
00032 #endif
00033
00034 #ifndef inbounds
00035
00041 #define inbounds(a,i) ( (size_t)(i) < countof(a) )
00042 #endif
00043
00044 #ifndef memberof
00045
00052 #define memberof(a,p) ( inbounds((a),indexof((a),(p))) && ((void*)((a)+indexof((a),(p))) == (void*)(p)) )
00053 #endif
00054
00055
00066 #define array_bsort( atype, array, count, compare )\
00067 {\
00068 atype* aStart = (array);\
00069 atype* aEnd = aStart + (count);\
00070 int bSorted;\
00071 do {\
00072 atype* aCurr = aStart;\
00073 aEnd--;\
00074 bSorted = 0;\
00075 while( aCurr < aEnd )\
00076 {\
00077 if( compare( *aCurr, *(aCurr+1) ) > 0 )\
00078 {\
00079 atype tmp = *aCurr;\
00080 *aCurr = *(aCurr+1);\
00081 *(aCurr+1) = tmp;\
00082 bSorted = 1;\
00083 }\
00084 aCurr++;\
00085 }\
00086 } while( bSorted );\
00087 }
00088
00099 #define array_qsort( atype, array, count, compare ) qsort((array),(count),sizeof(atype),(int (*)(const void *, const void *))(compare))
00100
00110 #ifdef MSVC
00111 #define array_lsearch( atype, array, count, compare, key ) _lsearch( (key), (array), (count), sizeof(atype), (compare) )
00112 #else
00113 #define array_lsearch( atype, array, count, compare, key ) lsearch( (key), (array), (count), sizeof(atype), (compare) )
00114 #endif
00115
00125 #define array_bsearch( atype, array, count, compare, key ) bsearch( (key), (array), (count), sizeof(atype), (compare) )
00126
00135 #define array_fill( type, dst, count, value )\
00136 {\
00137 type* ppScr2(fill,begin) = (dst);\
00138 type* ppScr2(fill,curr) = ppScr2(fill,begin)+(count);\
00139 type ppScr2(fill,val) = (value);\
00140 while( ppScr2(fill,curr)-- > ppScr2(fill,begin) )\
00141 *ppScr2(fill,curr) = ppScr2(fill,val);\
00142 }
00143
00144
00145 #endif