summaryrefslogtreecommitdiffstats
path: root/dev/a3-earthquake/earthquake_database.cc
blob: 2c9a822f861f3b7908ae9cedcde9f9d590317bff (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
/** CSci-4611 Assignment 3:  Earthquake
 */

#include <fstream>
#include <algorithm>

#include "earthquake_database.h"


EarthquakeDatabase::EarthquakeDatabase(std::string filename) {
    
    std::ifstream in(filename.c_str());
    std::string line;
    while (getline(in, line)) {
        if (line.size() > 30) {
            Earthquake e(line);
            if (earthquakes.size() == 0) {
                min_mag_ = (float)e.magnitude();
                max_mag_ = (float)e.magnitude();
            }
            if (e.magnitude() > max_mag_) {
                max_mag_ = (float)e.magnitude();
            }
            if (e.magnitude() < min_mag_) {
                min_mag_ = (float)e.magnitude();
            }
            earthquakes.push_back(e);
        }
    }
}

Earthquake EarthquakeDatabase::earthquake(int index) {
    return earthquakes[index];
}

int EarthquakeDatabase::min_index() {
    return 250;
}

int EarthquakeDatabase::max_index() {
    return (int)earthquakes.size() - 1;
}

int EarthquakeDatabase::FindMostRecentQuake(Date d) {
    double targetSeconds = d.ToSeconds();
    int start = min_index();
    int end = max_index();
    while (start < end-1) {
        int half = (start + end) / 2;
        if (earthquakes[half].date().ToSeconds() > targetSeconds) {
            end = half - 1;
        }
        else {
            start = half;
        }
    }
    if (start == end) {
        return start;
    }
    else {
        if (earthquakes[end].date().ToSeconds() > targetSeconds) {
            return start;
        }
        else {
            return end;
        }
    }
}

float EarthquakeDatabase::min_magnitude() {
    return min_mag_;
}

float EarthquakeDatabase::max_magnitude() {
    return max_mag_;
}