2 * keyarray.c - routines to maintain a sorted array of keyids.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2004 Project Purple
17 bool array_find(struct keyarray *array, uint64_t key)
25 if (array->keys != NULL && array->count > 0) {
27 top = array->count - 1;
28 while ((top - bottom) > 1) {
29 curpos = (top + bottom) / 2;
30 if (key > array->keys[curpos]) {
36 found = (array->keys[top] == key);
42 bool array_add(struct keyarray *array, uint64_t key)
50 if (array->keys != NULL && array->count > 0) {
52 top = array->count - 1;
53 while ((top - bottom) > 1) {
54 curpos = (top + bottom) / 2;
55 if (key > array->keys[curpos]) {
61 found = (array->keys[top] == key);
63 if (key > array->keys[top]) {
71 if (array->size == 0) {
72 array->keys = malloc(16 * sizeof(uint64_t));
77 if (array->count >= array->size) {
79 array->keys = realloc(array->keys,
80 array->size * sizeof(uint64_t));
82 if (curpos < array->count) {
83 memmove(&array->keys[curpos+1],
86 (array->count - curpos));
88 array->keys[curpos] = key;
96 void array_free(struct keyarray *array)
102 array->count = array->size = 0;