47 int (**compare) (
const void *,
const void *) = aux;
48 return (*compare) (a, b);
58qsort (
void *array,
size_t cnt,
size_t size,
59 int (*compare) (
const void *,
const void *))
67do_swap (
unsigned char *array,
size_t a_idx,
size_t b_idx,
size_t size)
69 unsigned char *a = array + (a_idx - 1) * size;
70 unsigned char *b = array + (b_idx - 1) * size;
73 for (i = 0; i < size; i++)
75 unsigned char t = a[i];
86do_compare (
unsigned char *array,
size_t a_idx,
size_t b_idx,
size_t size,
87 int (*compare) (
const void *,
const void *,
void *aux),
90 return compare (array + (a_idx - 1) * size, array + (b_idx - 1) * size, aux);
97heapify (
unsigned char *array,
size_t i,
size_t cnt,
size_t size,
98 int (*compare) (
const void *,
const void *,
void *aux),
106 size_t right = 2 * i + 1;
108 if (left <= cnt &&
do_compare (array, left, max, size, compare, aux) > 0)
111 &&
do_compare (array, right, max, size, compare, aux) > 0)
132sort (
void *array,
size_t cnt,
size_t size,
133 int (*compare) (
const void *,
const void *,
void *aux),
143 for (i = cnt / 2; i > 0; i--)
144 heapify (array, i, cnt, size, compare, aux);
147 for (i = cnt; i > 1; i--)
150 heapify (array, 1, i - 1, size, compare, aux);
166bsearch (
const void *key,
const void *array,
size_t cnt,
167 size_t size,
int (*compare) (
const void *,
const void *))
186 int (*compare) (
const void *,
const void *,
void *aux),
189 const unsigned char *first = array;
190 const unsigned char *last = array + size * cnt;
194 size_t range = (last - first) / size;
195 const unsigned char *middle = first + (range / 2) * size;
196 int cmp = compare (key, middle, aux);
201 first = middle + size;
203 return (
void *) middle;
static int isspace(int c)
static int isdigit(int c)
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
static void heapify(unsigned char *array, size_t i, size_t cnt, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
"Float down" the element with 1-based index I in ARRAY of CNT elements of SIZE bytes each,...
void * bsearch(const void *key, const void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *))
Searches ARRAY, which contains CNT elements of SIZE bytes each, for the given KEY.
int atoi(const char *s)
Converts a string representation of a signed decimal integer in S into an ‘int’, which is returned.
static void do_swap(unsigned char *array, size_t a_idx, size_t b_idx, size_t size)
Swaps elements with 1-based indexes A_IDX and B_IDX in ARRAY with elements of SIZE bytes each.
void qsort(void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *))
Sorts ARRAY, which contains CNT elements of SIZE bytes each, using COMPARE.
void * binary_search(const void *key, const void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
Searches ARRAY, which contains CNT elements of SIZE bytes each, for the given KEY.
static int compare_thunk(const void *a, const void *b, void *aux)
Compares A and B by calling the AUX function.
void sort(void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
Sorts ARRAY, which contains CNT elements of SIZE bytes each, using COMPARE to compare elements,...
static int do_compare(unsigned char *array, size_t a_idx, size_t b_idx, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
Compares elements with 1-based indexes A_IDX and B_IDX in ARRAY with elements of SIZE bytes each,...
static uint8_t s[256]
RC4-based pseudo-random number generator (PRNG).