int findKeyValue ( const KeyValue *values, unsigned int count, const KeyValue *target, unsigned int *position ) { int first = 0; int last = count - 1; while (first <= last) { int current = (first + last) / 2; const KeyValue *value = &values[current]; int relation = compareKeyValues(target, value); if (!relation) { *position = current; return 1; } if (relation < 0) { last = current - 1; } else { first = current + 1; } } *position = first; return 0; }
static const KeyNameEntry * findKeyNameEntry (ListGenerationData *lgd, const KeyValue *value) { const KeyNameEntry *const *array = lgd->keyTable->keyNameTable; unsigned int count = lgd->keyTable->keyNameCount; const KeyNameEntry *const *kne = bsearch(value, array, count, sizeof(*array), searchKeyNameEntry); if (!kne) return NULL; while (kne > array) { if (compareKeyValues(value, &(*--kne)->value) != 0) { kne += 1; break; } } return *kne; }
static int searchKeyNameEntry (const void *target, const void *element) { const KeyValue *value = target; const KeyNameEntry *const *kne = element; return compareKeyValues(value, &(*kne)->value); }