diff --git a/A1/coord_query.c b/A1/coord_query.c index 465904c..9d365bb 100644 --- a/A1/coord_query.c +++ b/A1/coord_query.c @@ -8,57 +8,57 @@ #include "timing.h" int coord_query_loop(int argc, char** argv, mk_index_fn mk_index, free_index_fn free_index, lookup_fn lookup) { - if (argc != 2) { - fprintf(stderr, "Usage: %s FILE\n", argv[0]); - 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; + if (argc != 2) { + fprintf(stderr, "Usage: %s FILE\n", argv[0]); + exit(1); } - printf("Total query runtime: %dus\n", (int)runtime_sum); + uint64_t start, runtime; + int n; - 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; - } + 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); + + 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; + } } diff --git a/A1/coord_query.h b/A1/coord_query.h index b7ab80e..9be4e9a 100644 --- a/A1/coord_query.h +++ b/A1/coord_query.h @@ -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 #define COORD_QUERY_LOOP_H diff --git a/A1/coord_query_naive.c b/A1/coord_query_naive.c index dc5ed4f..9d27882 100644 --- a/A1/coord_query_naive.c +++ b/A1/coord_query_naive.c @@ -10,28 +10,28 @@ #include "coord_query.h" struct naive_data { - struct record *rs; - int n; + struct record *rs; + int n; }; struct naive_data* mk_naive(struct record* rs, int n) { - assert(0); - // TODO + assert(0); + // TODO } void free_naive(struct naive_data* data) { - assert(0); - // TODO + assert(0); + // TODO } const struct record* lookup_naive(struct naive_data *data, double lon, double lat) { - assert(0); - // TODO + assert(0); + // TODO } int main(int argc, char** argv) { - return coord_query_loop(argc, argv, - (mk_index_fn)mk_naive, - (free_index_fn)free_naive, - (lookup_fn)lookup_naive); + return coord_query_loop(argc, argv, + (mk_index_fn)mk_naive, + (free_index_fn)free_naive, + (lookup_fn)lookup_naive); } diff --git a/A1/id_query.c b/A1/id_query.c index 2ca3ca8..cd103fa 100644 --- a/A1/id_query.c +++ b/A1/id_query.c @@ -8,56 +8,56 @@ #include "timing.h" int id_query_loop(int argc, char** argv, mk_index_fn mk_index, free_index_fn free_index, lookup_fn lookup) { - if (argc != 2) { - fprintf(stderr, "Usage: %s FILE\n", argv[0]); - 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; + if (argc != 2) { + fprintf(stderr, "Usage: %s FILE\n", argv[0]); + exit(1); } - printf("Total query runtime: %dus\n", (int)runtime_sum); + uint64_t start, runtime; + int n; - 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; - } + 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); + + 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; + } } diff --git a/A1/id_query.h b/A1/id_query.h index 32f2048..562cbcb 100644 --- a/A1/id_query.h +++ b/A1/id_query.h @@ -1,12 +1,12 @@ // 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: // // Records <- Read Dataset // Index <- Produce Index From Records // While Program Is Running: -// Read Query From User -// Lookup Query In Index +// Read Query From User +// Lookup Query In Index // Free Index // // Where the specifics of "Produce Index From Records", "Lookup Query diff --git a/A1/id_query_naive.c b/A1/id_query_naive.c index 3731396..7653f16 100644 --- a/A1/id_query_naive.c +++ b/A1/id_query_naive.c @@ -10,28 +10,28 @@ #include "id_query.h" struct naive_data { - struct record *rs; - int n; + struct record *rs; + int n; }; struct naive_data* mk_naive(struct record* rs, int n) { - // TODO - assert(0); + // TODO + assert(0); } void free_naive(struct naive_data* data) { - // TODO - assert(0); + // TODO + assert(0); } const struct record* lookup_naive(struct naive_data *data, int64_t needle) { - // TODO - assert(0); + // TODO + assert(0); } int main(int argc, char** argv) { - return id_query_loop(argc, argv, - (mk_index_fn)mk_naive, - (free_index_fn)free_naive, - (lookup_fn)lookup_naive); + return id_query_loop(argc, argv, + (mk_index_fn)mk_naive, + (free_index_fn)free_naive, + (lookup_fn)lookup_naive); } diff --git a/A1/random_ids.c b/A1/random_ids.c index b43f22e..1fbb98f 100644 --- a/A1/random_ids.c +++ b/A1/random_ids.c @@ -4,22 +4,22 @@ #include "record.h" int main(int argc, char** argv) { - if (argc != 2) { - fprintf(stderr, "Usage: %s FILE\n", argv[1]); - return 1; - } - - int n; - struct record* rs = read_records(argv[1], &n); - - if (!rs) { - fprintf(stderr, "Failed to read records from %s\n", argv[1]); - return 1; - } - - while (1) { - if (printf("%ld\n", (long)rs[rand() % n].osm_id) == 0) { - break; + if (argc != 2) { + fprintf(stderr, "Usage: %s FILE\n", argv[1]); + return 1; + } + + int n; + struct record* rs = read_records(argv[1], &n); + + if (!rs) { + fprintf(stderr, "Failed to read records from %s\n", argv[1]); + return 1; + } + + while (1) { + if (printf("%ld\n", (long)rs[rand() % n].osm_id) == 0) { + break; + } } - } } diff --git a/A1/record.c b/A1/record.c index df12aa2..9e68c7a 100644 --- a/A1/record.c +++ b/A1/record.c @@ -7,168 +7,168 @@ // Sanity check to make sure we are reading the right kind of file. int input_looks_ok(FILE *f) { - char *line = NULL; - size_t n; - if (getline(&line, &n, f) == -1) { - return -1; - } + char *line = NULL; + size_t n; + if (getline(&line, &n, f) == -1) { + return -1; + } - 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) { - ret = 1; - } else { - ret = 0; - } + 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) { + ret = 1; + } else { + ret = 0; + } - free(line); - return ret; + free(line); + 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. int read_record(struct record *r, FILE *f) { - char *line = NULL; - size_t n; - if (getline(&line, &n, f) == -1) { - free(line); - return -1; - } + char *line = NULL; + size_t n; + if (getline(&line, &n, f) == -1) { + free(line); + return -1; + } - r->line = line; + r->line = line; - char* start = line; - char* end; + char* start = line; + char* end; - if ((end = strstr(start, "\t"))) { - r->name = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->name = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->alternative_names = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->alternative_names = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->osm_type = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->osm_type = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->osm_id = atol(start); *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->osm_id = atol(start); *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->class = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->class = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->type = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->type = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->lon = atof(start); *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->lon = atof(start); *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->lat = atof(start); *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->lat = atof(start); *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->place_rank = atoi(start); *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->place_rank = atoi(start); *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->importance = atof(start); *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->importance = atof(start); *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->street = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->street = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->city = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->city = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->county = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->county = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->state = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->state = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->country = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->country = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->country_code = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->country_code = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->display_name = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->display_name = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->west = atof(start); *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->west = atof(start); *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->west = atof(start); *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->west = atof(start); *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->east = atof(start); *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->east = atof(start); *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->north = atof(start); *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->north = atof(start); *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->wikidata = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->wikidata = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->wikipedia = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->wikipedia = start; *end = 0; start = end+1; + } - if ((end = strstr(start, "\t"))) { - r->housenumbers = start; *end = 0; start = end+1; - } + if ((end = strstr(start, "\t"))) { + r->housenumbers = start; *end = 0; start = end+1; + } - return 0; + return 0; } struct record* read_records(const char *filename, int *n) { - FILE *f = fopen(filename, "r"); - *n = 0; + FILE *f = fopen(filename, "r"); + *n = 0; - if (f == 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)); + if (f == NULL) { + return NULL; } - } - *n = i; - fclose(f); - return rs; + 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; + fclose(f); + return rs; } void free_records(struct record *rs, int n) { - for (int i = 0; i < n; i++) { - free(rs[i].line); - } - free(rs); + for (int i = 0; i < n; i++) { + free(rs[i].line); + } + free(rs); } diff --git a/A1/record.h b/A1/record.h index 05ee9b5..318c2d5 100644 --- a/A1/record.h +++ b/A1/record.h @@ -4,53 +4,53 @@ #include #include -// An OpenStreetMap place record. All the 'const char*' strings are -// pointers into the string stored in the 'line' field. This string +// An OpenStreetMap place record. All the 'const char*' strings are +// pointers into the string stored in the 'line' field. This string // is "owned" by the record, meaning that it is freed exactly when the // 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. struct record { - const char *name; - const char *alternative_names; - const char *osm_type; - int64_t osm_id; - const char *class; - const char *type; - double lon; - double lat; - int place_rank; - double importance; - const char *street; - const char *city; - const char *county; - const char *state; - const char *country; - const char *country_code; - const char *display_name; - double west; - double south; - double east; - double north; - const char *wikidata; - const char *wikipedia; - const char *housenumbers; + const char *name; + const char *alternative_names; + const char *osm_type; + int64_t osm_id; + const char *class; + const char *type; + double lon; + double lat; + int place_rank; + double importance; + const char *street; + const char *city; + const char *county; + const char *state; + const char *country; + const char *country_code; + const char *display_name; + double west; + double south; + double east; + double north; + const char *wikidata; + const char *wikipedia; + const char *housenumbers; - // Not a real field - all the other char* elements are pointers into - // this memory, which we can pass to free(). - char *line; + // Not a real field - all the other char* elements are pointers into + // this memory, which we can pass to free(). + 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 -// *n to the number of records. Returns NULL on failure. +// *n to the number of records. Returns NULL on failure. // // Expects lines of form: // Index,Date,Open,High,Low,Close,AdjustedClose,Volume 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(). void free_records(struct record *r, int n); diff --git a/A1/timing.h b/A1/timing.h index 9e63a2a..4d73e51 100644 --- a/A1/timing.h +++ b/A1/timing.h @@ -4,9 +4,9 @@ #include static uint64_t microseconds() { - static struct timeval t; - gettimeofday(&t, NULL); - return ((uint64_t)t.tv_sec*1000000)+t.tv_usec; + static struct timeval t; + gettimeofday(&t, NULL); + return ((uint64_t)t.tv_sec*1000000)+t.tv_usec; } #endif