} \
while( 0 )
-
-#define TAB_APPEND( count, tab, p ) \
+#define TAB_INIT( count, tab ) \
+ do { \
+ (count) = 0; \
+ (tab) = NULL; \
+ } while(0)
+
+#define TAB_CLEAN( count, tab ) \
+ do { \
+ if( tab ) free( tab ); \
+ (count)= 0; \
+ (tab)= NULL; \
+ } while(0)
+
+#define TAB_APPEND_CAST( cast, count, tab, p ) \
+ do { \
if( (count) > 0 ) \
- { \
- (tab) = realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
- } \
+ (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
else \
- { \
- (tab) = malloc( sizeof( void ** ) ); \
- } \
- (tab)[count] = (p); \
- (count)++
+ (tab) = cast malloc( sizeof( void ** ) ); \
+ (tab)[count] = (p); \
+ (count)++; \
+ } while(0)
+
+#define TAB_APPEND( count, tab, p ) \
+ TAB_APPEND_CAST( , count, tab, p )
+#define TAB_APPEND_CPP( type, count, tab, p ) \
+ TAB_APPEND_CAST( (type**), count, tab, p )
#define TAB_FIND( count, tab, p, index ) \
- { \
+ do { \
int _i_; \
(index) = -1; \
for( _i_ = 0; _i_ < (count); _i_++ ) \
{ \
- if( (tab)[_i_] == (p) ) \
+ if( (tab)[_i_] == (p) ) \
{ \
(index) = _i_; \
break; \
} \
} \
- }
+ } while(0)
#define TAB_REMOVE( count, tab, p ) \
- { \
+ do { \
int _i_index_; \
TAB_FIND( count, tab, p, _i_index_ ); \
if( _i_index_ >= 0 ) \
{ \
- if( (count) > 1 ) \
+ if( (count) > 1 ) \
{ \
memmove( ((void**)(tab) + _i_index_), \
((void**)(tab) + _i_index_+1), \
(tab) = NULL; \
} \
} \
- }
+ } while(0)
+
+#define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
+ if( (count) > 0 ) \
+ (tab) = cast realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
+ else \
+ (tab) = cast malloc( sizeof( void ** ) ); \
+ if( (count) - (index) > 0 ) \
+ memmove( (void**)(tab) + (index) + 1, \
+ (void**)(tab) + (index), \
+ ((count) - (index)) * sizeof(*(tab)) );\
+ (tab)[(index)] = (p); \
+ (count)++; \
+} while(0)
+
+#define TAB_INSERT( count, tab, p, index ) \
+ TAB_INSERT_CAST( , count, tab, p, index )
/**
* Binary search in a sorted array. The key must be comparable by < and >
* \param key value of the key
* \param answer index of answer within the array. -1 if not found
*/
-#define BSEARCH( entries, count, elem, zetype, key, answer ) { \
+#define BSEARCH( entries, count, elem, zetype, key, answer ) \
+ do { \
int low = 0, high = count - 1; \
answer = -1; \
while( low <= high ) {\
answer = mid; break; \
}\
} \
-}
+ } while(0)
/************************************************************************
* Dictionaries