summaryrefslogtreecommitdiff
path: root/src/haversine_processor/haversine_processor.cpp
blob: ffb3a2875e99db0f7c894fdc77141201403fa132 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "../shared_libraries/lr/lr.h"
#define STB_SPRINTF_IMPLEMENTATION
#include "libs/stb_sprintf.h"

#include <math.h>
#include <errno.h>

#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>

//~ Types
struct str8
{
    u8 *Data;
    umm Size;
};
#define S8Lit(String) (str8){.Data = (u8 *)(String), .Size = (sizeof((String)) - 1)}

//~ Globals
global_variable u8 LogBuffer[Kilobytes(64)];

//~ Functions

//- Haversine 
static f64 Square(f64 A)
{
    f64 Result = (A*A);
    return Result;
}

static f64 RadiansFromDegrees(f64 Degrees)
{
    f64 Result = 0.01745329251994329577 * Degrees;
    return Result;
}

// NOTE(casey): EarthRadius is generally expected to be 6372.8
static f64 ReferenceHaversine(f64 X0, f64 Y0, f64 X1, f64 Y1, f64 EarthRadius)
{
    /* NOTE(casey): This is not meant to be a "good" way to calculate the Haversine distance.
       Instead, it attempts to follow, as closely as possible, the formula used in the real-world
       question on which these homework exercises are loosely based.
    */
    
    f64 lat1 = Y0;
    f64 lat2 = Y1;
    f64 lon1 = X0;
    f64 lon2 = X1;
    
    f64 dLat = RadiansFromDegrees(lat2 - lat1);
    f64 dLon = RadiansFromDegrees(lon2 - lon1);
    lat1 = RadiansFromDegrees(lat1);
    lat2 = RadiansFromDegrees(lat2);
    
    f64 a = Square(sin(dLat/2.0)) + cos(lat1)*cos(lat2)*Square(sin(dLon/2));
    f64 c = 2.0*asin(sqrt(a));
    
    f64 Result = EarthRadius * c;
    
    return Result;
}

//- Debug utilities 
void AssertErrnoNotEquals(smm Result, smm ErrorValue)
{
    if(Result == ErrorValue)
    {
        int Errno = errno;
        Assert(0);
    }
}

void AssertErrnoEquals(smm Result, smm ErrorValue)
{
    if(Result != ErrorValue)
    {
        int Errno = errno;
        Assert(0);
    }
}

void LogFormat(char *Format, ...)
{
    va_list Args;
    va_start(Args, Format);
    
    int Length = stbsp_vsprintf((char *)LogBuffer, Format, Args);
    
    smm BytesWritten = write(STDOUT_FILENO, LogBuffer, Length);
    AssertErrnoEquals(BytesWritten, Length);
}


str8 ReadEntireFileIntoMemory(char *FileName)
{
    str8 Result = {};
    
    int File = open(FileName, O_RDONLY);
    
    struct stat StatBuffer = {};
    int Error = fstat(File, &StatBuffer);
    AssertErrnoNotEquals(Error, -1);
    
    Result.Size = StatBuffer.st_size;
    Result.Data = (u8 *)mmap(0, Result.Size, PROT_READ, MAP_PRIVATE, File, 0);
    AssertErrnoNotEquals((smm)Result.Data, (smm)MAP_FAILED);
    
    return Result;
}

//- Parsing utilities 

b32 IsWhiteSpace(u8 Char)
{
    b32 Result = (Char == ' ' || Char == '\t' || Char == '\n');
    return Result;
}

void ConsumeWhiteSpacePastChar(umm Size, u8 *In, umm *Start, u8 Char)
{
    umm At = *Start;
    
    while(At < Size && IsWhiteSpace(In[At])) At += 1;
    
    if(At >= Size || In[At] != Char)
    {
        Assert(0 && "Expected Char");
    }
    At += 1;
    
    *Start = At;
}

void ConsumePastJsonString(umm Size, u8 *In, umm *Start, str8 String)
{
    umm At = *Start;
    
    ConsumeWhiteSpacePastChar(Size, In, &At, '"');
    
    if(At + String.Size < Size)
    {
        b32 Match = true;
        for(umm MatchIndex = 0;
            MatchIndex < String.Size;
            MatchIndex += 1)
        {
            if(In[At + MatchIndex] != String.Data[MatchIndex])
            {
                Match = false;
                break;
            }
        }
        
        if(Match)
        {
            At += String.Size;
            
            if(In[At] == '"')
            {
                At += 1;
            }
            else
            {
                Assert(In[At] == '"');
            }
        }
        else
        {
            Assert(Match && "Expected String");
        }
        
    }
    else
    {
        Assert(0 && "Expected '\"'");
    }
    
    *Start = At;
}

struct parse_number_result
{
    f64 Value;
    umm At;
};

void ParseFloatNumber(umm Size, u8 *In, umm *Start, f64 *Value)
{
    umm At = *Start;
    
    b32 Negative = false;
    f64 Integer = 0;
    
    if(In[At] == '-')
    {
        At += 1;
        Negative = true;
    }
    
    while(At < Size && (In[At] >= '0' && In[At] <= '9')) 
    {
        Integer = 10*Integer + (In[At] - '0');
        At += 1;
    }
    Assert(In[At] == '.');
    At += 1;
    
    *Value = Integer;
    
    f64 Divider = 10;
    while(At < Size && (In[At] >= '0' && In[At] <= '9'))
    {
        f64 Digit = (f64)(In[At] - '0');
        
        *Value += (Digit / Divider);
        Divider *= 10;
        
        At += 1;
    }
    
    if(Negative)
    {
        *Value = -*Value;
    }
    
    *Start = At;
}

void ConsumeHaversineJsonNumber(umm Size, u8 *In, umm *Start, str8 Name, f64 *Value, b32 NumberLeft)
{
    umm At = *Start;
    
    ConsumePastJsonString(Size, In, &At, Name);
    ConsumeWhiteSpacePastChar(Size, In, &At, ':');
    while(At < Size && IsWhiteSpace(In[At])) At += 1;
    ParseFloatNumber(Size, In, &At, Value);
    
    if(NumberLeft)
    {
        ConsumeWhiteSpacePastChar(Size, In, &At, ',');
    }
    
    *Start = At;
}

//-

int main(int ArgsCount, char *Args[])
{
    str8 Answers = {};
    umm AnswerIndex = 0;
    str8 Json = {};
    
    if(ArgsCount >= 2)
    {
        Json = ReadEntireFileIntoMemory(Args[1]);
    }
    else
    {
        LogFormat("usage: %s <file.json> [answer.f64]\n", Args[0]);
    }
    
    if(ArgsCount >= 3)
    {
        Answers = ReadEntireFileIntoMemory(Args[2]);
    }
    
    if(Json.Size)
    {
        f64 X0 = 0.0;
        f64 X1 = 0.0;
        f64 Y0 = 0.0;
        f64 Y1 = 0.0;
        
        // Json Parsing
        u8 *In = Json.Data;
        
        umm At = 0;
        ConsumeWhiteSpacePastChar(Json.Size, In, &At, '{');
        ConsumePastJsonString(Json.Size, In, &At, S8Lit("pairs"));
        ConsumeWhiteSpacePastChar(Json.Size, In, &At, ':');
        ConsumeWhiteSpacePastChar(Json.Size, In, &At, '[');
        
        f64 TotalSum = 0;
        umm PairCount = 0;
        b32 PairsRemaining = true;
        
        // One pair
        while(PairsRemaining && (At < Json.Size))
        {                
            ConsumeWhiteSpacePastChar(Json.Size, In, &At, '{');
            
            ConsumeHaversineJsonNumber(Json.Size, In, &At, S8Lit("x0"), &X0, true);
            ConsumeHaversineJsonNumber(Json.Size, In, &At, S8Lit("y0"), &Y0, true);
            ConsumeHaversineJsonNumber(Json.Size, In, &At, S8Lit("x1"), &X1, true);
            ConsumeHaversineJsonNumber(Json.Size, In, &At, S8Lit("y1"), &Y1, false);
            
            f64 Sum = ReferenceHaversine(X0, Y0, X1, Y1, 6372.8);
            TotalSum += Sum;
            
            if(Answers.Size)
            {
                // NOTE(luca): What to do here?
                //f64 AnswerSum = ((f64 *)Answers.Data)[AnswerIndex];
                
                AnswerIndex += 1;
            }
            
            ConsumeWhiteSpacePastChar(Json.Size, In, &At, '}');
            
            if(At < Json.Size && In[At] == ',')
            {
                At += 1;
            }
            
            while(At < Json.Size && IsWhiteSpace(In[At])) At += 1;
            PairsRemaining = (In[At] == '{' || At >= Json.Size);
            
            PairCount += 1;
        }
        
        ConsumeWhiteSpacePastChar(Json.Size, In, &At, ']');
        ConsumeWhiteSpacePastChar(Json.Size, In, &At, '}');
        
        f64 AverageSum = TotalSum / (f64)PairCount;
        LogFormat("Input size: %lu\n"
                  "Pair count: %lu\n"
                  "Haversine sum: %.16f\n"
                  , Json.Size, PairCount, AverageSum);
        
        if(Answers.Size)
        {
            f64 AnswerAverageSum = ((f64 *)Answers.Data)[AnswerIndex];
            AnswerIndex += 1;
            
            Assert((AnswerIndex * sizeof(f64)) == Answers.Size);
            
            LogFormat("\nValidation\n"
                      "Reference sum: %.16f\n"
                      "Difference: %.16f\n"
                      , AnswerAverageSum, AnswerAverageSum - AverageSum);
            
            
        }
        
    }
    else
    {
        LogFormat("Error: File not found.\n"
                  "usage: %s <file.json> [answer.f64]\n", Args[0]);
    }
    
    
    return 0;
}