#include #include #include #include "vec_bsrch.h" int compar(const void* lhs, const void* rhs) { return *(const int*)lhs - *(const int*)rhs; } int main(void) { int i, j, k, c, f, l; VEC_T(int) vec = VEC_0(int); /* Generate list via insertion sort. */ srandom(time(NULL)); for (i = 0; i < 30; ++i) { j = random() % 10; /* Number to insert. */ k = VEC_BSRCHF(int, &vec, &j, compar); /* Insertion location. */ VEC_INS1(int, &vec, k, j); } /* Print list. */ printf(" :"); for (i = 0; i < VEC_LEN(int, &vec); ++i) { printf("%2d", *VEC_ELT(int, &vec, i)); } putchar('\n'); /* Print horizontal divider line. */ printf("...:"); for (i = 0; i < VEC_LEN(int, &vec) * 2 + 2; ++i) putchar('.'); putchar('\n'); /* Print ranges. */ for (i = 0; i < 10; ++i) { #if 0 c = VEC_BSRCHC(int, &vec, &i, compar); /* Frequency. */ f = VEC_BSRCHF(int, &vec, &i, compar); /* First occurrence. */ l = VEC_BSRCHL(int, &vec, &i, compar); /* Last occurrence + 1. */ #else /* The above three operations can be efficiently combined. */ c = VEC_BSRCHR(int, &vec, &i, compar, &f, &l); #endif printf("%2d : %*d", c, 2 * f + 1, i); if (c > 0) printf("%*d\n", 2 * (l - f), i); else putchar('\n'); } return EXIT_SUCCESS; } /* vim: set ts=4 sts=4 sw=4 tw=80 et: */