Compare commits

...

4 Commits

6 changed files with 153 additions and 5 deletions

View File

@ -0,0 +1,12 @@
#ifndef PROC_ARGPARSER_H
#define PROC_ARGPARSER_H
#include "aliases.h"
struct ProcessorArgs {
const char *filepath;
};
ProcessorArgs parse_args(i32 argc, char *argv[]);
#endif // !PROC_ARGPARSER_H

View File

@ -23,7 +23,7 @@ i32 main(i32 argc, char *argv[]) {
if (fp) { if (fp) {
fwrite(&(arr.count), sizeof(arr.count), 1, fp); fwrite(&(arr.count), sizeof(arr.count), 1, fp);
i64 sum = 0; f64 sum = 0.0;
for (u64 i = 0; i < arr.count; ++i) { for (u64 i = 0; i < arr.count; ++i) {
f64 distance = haversine_of_degrees(arr.pairs[i], EARTH_RADIUS_KM); f64 distance = haversine_of_degrees(arr.pairs[i], EARTH_RADIUS_KM);
@ -31,7 +31,7 @@ i32 main(i32 argc, char *argv[]) {
sum += distance; sum += distance;
} }
printf("%ld\n", sum / arr.count); printf("\nAVERAGE DISTANCE: %f\n", sum / arr.count);
fclose(fp); fclose(fp);
} }

View File

@ -33,7 +33,7 @@ jentity_t *load_json(const char *filepath) {
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
char json[length + 1]; char *json = (char *)malloc(sizeof(char) * (length + 1));
memset(json, 0, length + 1); memset(json, 0, length + 1);
fread(json, sizeof(char), length, fp); fread(json, sizeof(char), length, fp);
@ -76,6 +76,7 @@ jentity_t *load_json(const char *filepath) {
parser_free(&parser); parser_free(&parser);
lexer_free(&lexer); lexer_free(&lexer);
free(json);
return root; return root;
} }
@ -93,7 +94,7 @@ void parser_init(parser_t **parser) {
(*parser)->root = NULL; (*parser)->root = NULL;
(*parser)->current = NULL; (*parser)->current = NULL;
(*parser)->value = (jval_t){}; (*parser)->value = (jval_t){0};
} }
void parser_free(parser_t **parser) { void parser_free(parser_t **parser) {

View File

@ -41,7 +41,9 @@ void write_pairs_to_json(const PairArray &arr, const char *filename) {
for (u64 i = 0; i < arr.count; ++i) { for (u64 i = 0; i < arr.count; ++i) {
PointPair pair = arr.pairs[i]; PointPair pair = arr.pairs[i];
fprintf(fp, "\t\t{\"x0\": %f, \"y0\": %f, \"x1\": %f, \"y1\": %f}%s\n", fprintf(fp,
"\t\t{\"x0\": %.16f, \"y0\": %.16f, \"x1\": %.16f, \"y1\": "
"%.16f}%s\n",
pair.p1.x, pair.p1.y, pair.p2.x, pair.p2.y, pair.p1.x, pair.p1.y, pair.p2.x, pair.p2.y,
i + 1 < arr.count ? "," : ""); i + 1 < arr.count ? "," : "");
} }

View File

@ -0,0 +1,89 @@
#include "haversine.h"
#include "point_types.h"
#include "processor/proc_argparser.h"
#include "json/dstring.h"
#include "json/json_entities.h"
#include "json/parser.h"
#include <aliases.h>
#include <assert.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
ProcessorArgs args = parse_args(argc, argv);
jentity_t *root = load_json(args.filepath);
assert(root->type == JENTITY_SINGLE && root->value.type == JVAL_COLLECTION);
jentity_t *pairs = root->value.collection->begin;
assert(pairs->type == JENTITY_PAIR &&
pairs->pair.value.type == JVAL_COLLECTION);
u64 pair_count = pairs->pair.value.collection->size;
PointPair *point_pairs = (PointPair *)malloc(sizeof(PointPair) * pair_count);
memset(point_pairs, 0, pair_count);
u64 index = 0;
for (jentity_t *pair = pairs->pair.value.collection->begin; pair != NULL;
pair = pair->next) {
assert(index < pair_count && pair->type == JENTITY_SINGLE &&
pair->value.type == JVAL_COLLECTION &&
pair->value.collection->size == 4);
jentity_t *x0 = pair->value.collection->begin;
jentity_t *y0 = x0->next;
jentity_t *x1 = y0->next;
jentity_t *y1 = x1->next;
PointPair p = {
{x0->pair.value.num_dbl, y0->pair.value.num_dbl},
{x1->pair.value.num_dbl, y1->pair.value.num_dbl},
};
point_pairs[index++] = p;
}
const char *filename = "count_and_distances";
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("Failed to open the %s file", filename);
} else {
// Skip the count
fseek(fp, sizeof(u64), SEEK_SET);
}
f64 sum = 0.0;
f64 distance = 0.0;
f64 saved_distance = 0.0;
for (u64 i = 0; i < pair_count; ++i) {
distance = haversine_of_degrees(point_pairs[i], EARTH_RADIUS_KM);
if (fp) {
fread(&saved_distance, sizeof(f64), 1, fp);
if (fabs(distance - saved_distance) > FLT_EPSILON) {
printf("%llu: %.16f does not equal %.16f\n", (unsigned long long)i,
distance, saved_distance);
}
}
sum += distance;
}
printf("\nAVERAGE DISTANCE: %f\n", sum / pair_count);
if (fp) {
fclose(fp);
}
free(point_pairs);
return 0;
}

View File

@ -0,0 +1,44 @@
#include "processor/proc_argparser.h"
#include "aliases.h"
#include <argp.h>
INTERNAL error_t argp_parser(i32 key, char *arg, argp_state *state);
INTERNAL argp parser = {};
ProcessorArgs parse_args(i32 argc, char *argv[]) {
ProcessorArgs args = {};
parser.options = {};
parser.parser = argp_parser;
parser.args_doc = "JSON_FILEPATH";
argp_parse(&parser, argc, argv, 0, 0, &args);
return args;
}
error_t argp_parser(i32 key, char *arg, argp_state *state) {
ProcessorArgs *args = (ProcessorArgs *)state->input;
switch (key) {
case ARGP_KEY_ARG:
if (state->arg_num >= 1) {
argp_usage(state);
}
args->filepath = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 1) {
argp_usage(state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}