#include #include #include #include #include #include #include #include #include "record.h" #include "id_query.h" struct index_record { int64_t osm_id; const struct record* record; }; struct indexed_data { struct index_record* irs; int n; }; int comp_func(const void* a, const void* b) { return ( ((struct index_record*)a)->osm_id - ((struct index_record*)b)->osm_id ); } struct indexed_data* mk_indexed(struct record* rs, int n) { struct index_record irs[n]; for (int i = 0 ; i < n ; i++) { struct record* rec = &rs[i]; struct index_record irec = { .osm_id = rec->osm_id, .record = rec }; irs[i] = irec; } struct indexed_data* data = malloc(sizeof(struct indexed_data*)); qsort(&irs, n, sizeof(struct index_record), comp_func); data->irs = irs; data->n = n; return data; } void free_indexed(struct indexed_data* data) { free(data); } const struct record* lookup_indexed(struct indexed_data *data, int64_t needle) { int bottom = 0; int top = data->n; while (top >= bottom) { int mid = bottom + ((top - bottom) / 2); if ((&data->irs[mid])->osm_id < needle) { bottom = mid + 1; } else if ((&data->irs[mid])->osm_id > needle) { top = mid - 1; } else if ((&data->irs[mid])->osm_id == needle) { return (&data->irs[mid])->record; } } return NULL; } int main(int argc, char** argv) { return id_query_loop(argc, argv, (mk_index_fn)mk_indexed, (free_index_fn)free_indexed, (lookup_fn)lookup_indexed); }