/** * array-utils.h * Various utilities for dealing with code. */ #ifndef __ARRAY_UTILS_H__ #define __ARRAY_UTILS_H__ // +---------+---------------------------------------------------------- // | Headers | // +---------+ #include "misc-utils.h" // +--------+----------------------------------------------------------- // | Macros | // +--------+ /** * Macro: * ARRAY_COMPARE * Parameters: * A1, an array * A2, an array * SIZE, an integer * TYPE, a C type * COMPARE, a function of the form C(TYPE, TYPE) * Purpose: * Compare the first SIZE elements of A1 and A2, using COMPARE to * compare individual pairs of elements. * Produces: * comparison, an integer * Preconditions: * C can be applied to all pairs of elements from A1 and A2. * Produces: * If for all i, 0 <= i < SIZE, COMPARE (A1[i], A2[i]) is 0, * then comparison is 0. * If exists j s.t., (a) for all i, 0 <= i < j, COMPARE (A1[i], A2[i]) * is 0, and COMPARE (A1[j], A2[j]) < 0, then comparison is negative. * Otherwise, comparison is positive. */ #define ARRAY_COMPARE(A1, A2, SIZE, TYPE, COMPARE) \ BLOCK ( \ type *_A1_ = A1; \ type *_A2_ = A2; \ int _i_; \ int _comparison_; \ for (_i_ = 0; _i_ < SIZE; _i_++) \ { \ _comparison_ = COMPARE (_A1_[_i_], _A2_[_i_]); \ if (_comparison_ != 0) \ return _comparison_; \ } \ return 0; \ ) /** * Macro: * ARRAY_PRINT * Parameters: * ARR, an array * SIZE, an integer * TYPE, a C type * TYPE_PRINT, a function of the form PV(FILE *, TYPE) * Purpose: * Prints the first SIZE values in ARR, using TYPE_PRINT to * print each element. * Produces: * Nothing, called for the side effect. * Preconditions: * No additional. * Practica: * ARRAY_PRINT (strings, 5, char *, print_string) */ #define ARRAY_PRINT(ARR, SIZE, TYPE, TYPE_PRINT) \ BLOCK( \ TYPE *_stuff_ = ARR; \ if (SIZE == 0) \ { \ fprintf (stderr, "{ }"); \ } \ else \ { \ int _i_; \ fprintf (stderr, "{"); \ TYPE_PRINT (stderr, _stuff_[0]); \ for (_i_ = 1; _i_ < SIZE; _i_++) \ { \ fprintf (stderr, ", "); \ TYPE_PRINT (stderr, _stuff_[_i_]); \ } \ fprintf (stderr, "}"); \ } \ ) // +-----------+-------------------------------------------------------- // | Functions | // +-----------+ #endif // __ARRAY_UTILS_H__