whitespace

This commit is contained in:
Nikolaj
2021-09-29 15:23:58 +02:00
parent a211face12
commit 90ddd7f780
10 changed files with 302 additions and 302 deletions

View File

@ -8,57 +8,57 @@
#include "timing.h" #include "timing.h"
int coord_query_loop(int argc, char** argv, mk_index_fn mk_index, free_index_fn free_index, lookup_fn lookup) { int coord_query_loop(int argc, char** argv, mk_index_fn mk_index, free_index_fn free_index, lookup_fn lookup) {
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: %s FILE\n", argv[0]); fprintf(stderr, "Usage: %s FILE\n", argv[0]);
exit(1); exit(1);
}
uint64_t start, runtime;
int n;
start = microseconds();
struct record *rs = read_records(argv[1], &n);
runtime = microseconds()-start;
if (rs) {
printf("Reading records: %dms\n", (int)runtime/1000);
start = microseconds();
void *index = mk_index(rs, n);
runtime = microseconds()-start;
printf("Building index: %dms\n", (int)runtime/1000);
char *line = NULL;
size_t line_len;
uint64_t runtime_sum = 0;
while (getline(&line, &line_len, stdin) != -1) {
double lon, lat;
sscanf(line, "%lf %lf", &lon, &lat);
start = microseconds();
const struct record *r = lookup(index, lon, lat);
runtime = microseconds()-start;
if (r) {
printf("(%f,%f): %s (%f,%f)\n", lon, lat, r->name, r->lon, r->lat);
} else {
printf("(%f,%f): not found\n", lon, lat);
}
printf("Query time: %dus\n", (int)runtime);
runtime_sum += runtime;
} }
printf("Total query runtime: %dus\n", (int)runtime_sum); uint64_t start, runtime;
int n;
free(line); start = microseconds();
free_index(index); struct record *rs = read_records(argv[1], &n);
free_records(rs, n); runtime = microseconds()-start;
return 0;
} else { if (rs) {
fprintf(stderr, "Failed to read input from %s (errno: %s)\n", printf("Reading records: %dms\n", (int)runtime/1000);
argv[1], strerror(errno));
return 1; start = microseconds();
} void *index = mk_index(rs, n);
runtime = microseconds()-start;
printf("Building index: %dms\n", (int)runtime/1000);
char *line = NULL;
size_t line_len;
uint64_t runtime_sum = 0;
while (getline(&line, &line_len, stdin) != -1) {
double lon, lat;
sscanf(line, "%lf %lf", &lon, &lat);
start = microseconds();
const struct record *r = lookup(index, lon, lat);
runtime = microseconds()-start;
if (r) {
printf("(%f,%f): %s (%f,%f)\n", lon, lat, r->name, r->lon, r->lat);
} else {
printf("(%f,%f): not found\n", lon, lat);
}
printf("Query time: %dus\n", (int)runtime);
runtime_sum += runtime;
}
printf("Total query runtime: %dus\n", (int)runtime_sum);
free(line);
free_index(index);
free_records(rs, n);
return 0;
} else {
fprintf(stderr, "Failed to read input from %s (errno: %s)\n",
argv[1], strerror(errno));
return 1;
}
} }

View File

@ -1,4 +1,4 @@
// Similar to id_query.h. See the comments there. // Similar to id_query.h. See the comments there.
#ifndef COORD_QUERY_LOOP_H #ifndef COORD_QUERY_LOOP_H
#define COORD_QUERY_LOOP_H #define COORD_QUERY_LOOP_H

View File

@ -10,28 +10,28 @@
#include "coord_query.h" #include "coord_query.h"
struct naive_data { struct naive_data {
struct record *rs; struct record *rs;
int n; int n;
}; };
struct naive_data* mk_naive(struct record* rs, int n) { struct naive_data* mk_naive(struct record* rs, int n) {
assert(0); assert(0);
// TODO // TODO
} }
void free_naive(struct naive_data* data) { void free_naive(struct naive_data* data) {
assert(0); assert(0);
// TODO // TODO
} }
const struct record* lookup_naive(struct naive_data *data, double lon, double lat) { const struct record* lookup_naive(struct naive_data *data, double lon, double lat) {
assert(0); assert(0);
// TODO // TODO
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
return coord_query_loop(argc, argv, return coord_query_loop(argc, argv,
(mk_index_fn)mk_naive, (mk_index_fn)mk_naive,
(free_index_fn)free_naive, (free_index_fn)free_naive,
(lookup_fn)lookup_naive); (lookup_fn)lookup_naive);
} }

View File

@ -8,56 +8,56 @@
#include "timing.h" #include "timing.h"
int id_query_loop(int argc, char** argv, mk_index_fn mk_index, free_index_fn free_index, lookup_fn lookup) { int id_query_loop(int argc, char** argv, mk_index_fn mk_index, free_index_fn free_index, lookup_fn lookup) {
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: %s FILE\n", argv[0]); fprintf(stderr, "Usage: %s FILE\n", argv[0]);
exit(1); exit(1);
}
uint64_t start, runtime;
int n;
start = microseconds();
struct record *rs = read_records(argv[1], &n);
runtime = microseconds()-start;
if (rs) {
printf("Reading records: %dms\n", (int)runtime/1000);
start = microseconds();
void *index = mk_index(rs, n);
runtime = microseconds()-start;
printf("Building index: %dms\n", (int)runtime/1000);
char *line = NULL;
size_t line_len;
uint64_t runtime_sum = 0;
while (getline(&line, &line_len, stdin) != -1) {
int64_t needle = atol(line);
start = microseconds();
const struct record *r = lookup(index, needle);
runtime = microseconds()-start;
if (r) {
printf("%ld: %s %f %f\n", (long)needle, r->name, r->lon, r->lat);
} else {
printf("%ld: not found\n", (long)needle);
}
printf("Query time: %dus\n", (int)runtime);
runtime_sum += runtime;
} }
printf("Total query runtime: %dus\n", (int)runtime_sum); uint64_t start, runtime;
int n;
free(line); start = microseconds();
free_index(index); struct record *rs = read_records(argv[1], &n);
free_records(rs, n); runtime = microseconds()-start;
return 0;
} else { if (rs) {
fprintf(stderr, "Failed to read input from %s (errno: %s)\n", printf("Reading records: %dms\n", (int)runtime/1000);
argv[1], strerror(errno));
return 1; start = microseconds();
} void *index = mk_index(rs, n);
runtime = microseconds()-start;
printf("Building index: %dms\n", (int)runtime/1000);
char *line = NULL;
size_t line_len;
uint64_t runtime_sum = 0;
while (getline(&line, &line_len, stdin) != -1) {
int64_t needle = atol(line);
start = microseconds();
const struct record *r = lookup(index, needle);
runtime = microseconds()-start;
if (r) {
printf("%ld: %s %f %f\n", (long)needle, r->name, r->lon, r->lat);
} else {
printf("%ld: not found\n", (long)needle);
}
printf("Query time: %dus\n", (int)runtime);
runtime_sum += runtime;
}
printf("Total query runtime: %dus\n", (int)runtime_sum);
free(line);
free_index(index);
free_records(rs, n);
return 0;
} else {
fprintf(stderr, "Failed to read input from %s (errno: %s)\n",
argv[1], strerror(errno));
return 1;
}
} }

View File

@ -1,12 +1,12 @@
// This file (along with its implementation id_query.c) abstracts out // This file (along with its implementation id_query.c) abstracts out
// the user-facing part of the query programs. It implements the // the user-facing part of the query programs. It implements the
// following algorithm: // following algorithm:
// //
// Records <- Read Dataset // Records <- Read Dataset
// Index <- Produce Index From Records // Index <- Produce Index From Records
// While Program Is Running: // While Program Is Running:
// Read Query From User // Read Query From User
// Lookup Query In Index // Lookup Query In Index
// Free Index // Free Index
// //
// Where the specifics of "Produce Index From Records", "Lookup Query // Where the specifics of "Produce Index From Records", "Lookup Query

View File

@ -10,28 +10,28 @@
#include "id_query.h" #include "id_query.h"
struct naive_data { struct naive_data {
struct record *rs; struct record *rs;
int n; int n;
}; };
struct naive_data* mk_naive(struct record* rs, int n) { struct naive_data* mk_naive(struct record* rs, int n) {
// TODO // TODO
assert(0); assert(0);
} }
void free_naive(struct naive_data* data) { void free_naive(struct naive_data* data) {
// TODO // TODO
assert(0); assert(0);
} }
const struct record* lookup_naive(struct naive_data *data, int64_t needle) { const struct record* lookup_naive(struct naive_data *data, int64_t needle) {
// TODO // TODO
assert(0); assert(0);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
return id_query_loop(argc, argv, return id_query_loop(argc, argv,
(mk_index_fn)mk_naive, (mk_index_fn)mk_naive,
(free_index_fn)free_naive, (free_index_fn)free_naive,
(lookup_fn)lookup_naive); (lookup_fn)lookup_naive);
} }

View File

@ -4,22 +4,22 @@
#include "record.h" #include "record.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: %s FILE\n", argv[1]); fprintf(stderr, "Usage: %s FILE\n", argv[1]);
return 1; return 1;
} }
int n; int n;
struct record* rs = read_records(argv[1], &n); struct record* rs = read_records(argv[1], &n);
if (!rs) { if (!rs) {
fprintf(stderr, "Failed to read records from %s\n", argv[1]); fprintf(stderr, "Failed to read records from %s\n", argv[1]);
return 1; return 1;
} }
while (1) { while (1) {
if (printf("%ld\n", (long)rs[rand() % n].osm_id) == 0) { if (printf("%ld\n", (long)rs[rand() % n].osm_id) == 0) {
break; break;
}
} }
}
} }

View File

@ -7,168 +7,168 @@
// Sanity check to make sure we are reading the right kind of file. // Sanity check to make sure we are reading the right kind of file.
int input_looks_ok(FILE *f) { int input_looks_ok(FILE *f) {
char *line = NULL; char *line = NULL;
size_t n; size_t n;
if (getline(&line, &n, f) == -1) { if (getline(&line, &n, f) == -1) {
return -1; return -1;
} }
int ret; int ret;
if (strcmp(line, "name alternative_names osm_type osm_id class type lon lat place_rank importance street city county state country country_code display_name west south east north wikidata wikipedia housenumbers\n") == 0) { if (strcmp(line, "name alternative_names osm_type osm_id class type lon lat place_rank importance street city county state country country_code display_name west south east north wikidata wikipedia housenumbers\n") == 0) {
ret = 1; ret = 1;
} else { } else {
ret = 0; ret = 0;
} }
free(line); free(line);
return ret; return ret;
} }
// Read a single record from an open file. This is pretty tedious, as // Read a single record from an open file. This is pretty tedious, as
// we handle each field explicitly. // we handle each field explicitly.
int read_record(struct record *r, FILE *f) { int read_record(struct record *r, FILE *f) {
char *line = NULL; char *line = NULL;
size_t n; size_t n;
if (getline(&line, &n, f) == -1) { if (getline(&line, &n, f) == -1) {
free(line); free(line);
return -1; return -1;
} }
r->line = line; r->line = line;
char* start = line; char* start = line;
char* end; char* end;
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->name = start; *end = 0; start = end+1; r->name = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->alternative_names = start; *end = 0; start = end+1; r->alternative_names = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->osm_type = start; *end = 0; start = end+1; r->osm_type = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->osm_id = atol(start); *end = 0; start = end+1; r->osm_id = atol(start); *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->class = start; *end = 0; start = end+1; r->class = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->type = start; *end = 0; start = end+1; r->type = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->lon = atof(start); *end = 0; start = end+1; r->lon = atof(start); *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->lat = atof(start); *end = 0; start = end+1; r->lat = atof(start); *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->place_rank = atoi(start); *end = 0; start = end+1; r->place_rank = atoi(start); *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->importance = atof(start); *end = 0; start = end+1; r->importance = atof(start); *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->street = start; *end = 0; start = end+1; r->street = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->city = start; *end = 0; start = end+1; r->city = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->county = start; *end = 0; start = end+1; r->county = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->state = start; *end = 0; start = end+1; r->state = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->country = start; *end = 0; start = end+1; r->country = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->country_code = start; *end = 0; start = end+1; r->country_code = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->display_name = start; *end = 0; start = end+1; r->display_name = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->west = atof(start); *end = 0; start = end+1; r->west = atof(start); *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->west = atof(start); *end = 0; start = end+1; r->west = atof(start); *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->east = atof(start); *end = 0; start = end+1; r->east = atof(start); *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->north = atof(start); *end = 0; start = end+1; r->north = atof(start); *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->wikidata = start; *end = 0; start = end+1; r->wikidata = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->wikipedia = start; *end = 0; start = end+1; r->wikipedia = start; *end = 0; start = end+1;
} }
if ((end = strstr(start, "\t"))) { if ((end = strstr(start, "\t"))) {
r->housenumbers = start; *end = 0; start = end+1; r->housenumbers = start; *end = 0; start = end+1;
} }
return 0; return 0;
} }
struct record* read_records(const char *filename, int *n) { struct record* read_records(const char *filename, int *n) {
FILE *f = fopen(filename, "r"); FILE *f = fopen(filename, "r");
*n = 0; *n = 0;
if (f == NULL) { if (f == NULL) {
return NULL; return NULL;
}
if (!input_looks_ok(f)) {
return NULL;
}
int capacity = 100;
int i = 0;
struct record *rs = malloc(capacity * sizeof(struct record));
while (read_record(&rs[i], f) == 0) {
i++;
if (i == capacity) {
capacity *= 2;
rs = realloc(rs, capacity * sizeof(struct record));
} }
}
*n = i; if (!input_looks_ok(f)) {
fclose(f); return NULL;
return rs; }
int capacity = 100;
int i = 0;
struct record *rs = malloc(capacity * sizeof(struct record));
while (read_record(&rs[i], f) == 0) {
i++;
if (i == capacity) {
capacity *= 2;
rs = realloc(rs, capacity * sizeof(struct record));
}
}
*n = i;
fclose(f);
return rs;
} }
void free_records(struct record *rs, int n) { void free_records(struct record *rs, int n) {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
free(rs[i].line); free(rs[i].line);
} }
free(rs); free(rs);
} }

View File

@ -4,53 +4,53 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
// An OpenStreetMap place record. All the 'const char*' strings are // An OpenStreetMap place record. All the 'const char*' strings are
// pointers into the string stored in the 'line' field. This string // pointers into the string stored in the 'line' field. This string
// is "owned" by the record, meaning that it is freed exactly when the // is "owned" by the record, meaning that it is freed exactly when the
// record itself is freed. // record itself is freed.
// //
// You don't need to worry about the meaning of these fields. The // You don't need to worry about the meaning of these fields. The
// ones that matter are osm_id, lon, lat, and name. // ones that matter are osm_id, lon, lat, and name.
struct record { struct record {
const char *name; const char *name;
const char *alternative_names; const char *alternative_names;
const char *osm_type; const char *osm_type;
int64_t osm_id; int64_t osm_id;
const char *class; const char *class;
const char *type; const char *type;
double lon; double lon;
double lat; double lat;
int place_rank; int place_rank;
double importance; double importance;
const char *street; const char *street;
const char *city; const char *city;
const char *county; const char *county;
const char *state; const char *state;
const char *country; const char *country;
const char *country_code; const char *country_code;
const char *display_name; const char *display_name;
double west; double west;
double south; double south;
double east; double east;
double north; double north;
const char *wikidata; const char *wikidata;
const char *wikipedia; const char *wikipedia;
const char *housenumbers; const char *housenumbers;
// Not a real field - all the other char* elements are pointers into // Not a real field - all the other char* elements are pointers into
// this memory, which we can pass to free(). // this memory, which we can pass to free().
char *line; char *line;
}; };
// Read an OpenStreetMap place names dataset from a given file. On // Read an OpenStreetMap place names dataset from a given file. On
// success, returns a pointer to the array of records read, and sets // success, returns a pointer to the array of records read, and sets
// *n to the number of records. Returns NULL on failure. // *n to the number of records. Returns NULL on failure.
// //
// Expects lines of form: // Expects lines of form:
// Index,Date,Open,High,Low,Close,AdjustedClose,Volume // Index,Date,Open,High,Low,Close,AdjustedClose,Volume
struct record* read_records(const char *filename, int *n); struct record* read_records(const char *filename, int *n);
// Free records returned by read_records(). The 'n' argument must // Free records returned by read_records(). The 'n' argument must
// correspond to the number of records, as produced by read_records(). // correspond to the number of records, as produced by read_records().
void free_records(struct record *r, int n); void free_records(struct record *r, int n);

View File

@ -4,9 +4,9 @@
#include <sys/time.h> #include <sys/time.h>
static uint64_t microseconds() { static uint64_t microseconds() {
static struct timeval t; static struct timeval t;
gettimeofday(&t, NULL); gettimeofday(&t, NULL);
return ((uint64_t)t.tv_sec*1000000)+t.tv_usec; return ((uint64_t)t.tv_sec*1000000)+t.tv_usec;
} }
#endif #endif