-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEddingtonClass.cpp
59 lines (47 loc) · 1.37 KB
/
EddingtonClass.cpp
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
#include <vector>
#include "EddingtonClass.h"
Eddington::Eddington() {}
Eddington::Eddington(bool store_cumulative) {
if (store_cumulative) {
m_cumulative = std::vector<int>();
}
}
Eddington::Eddington(const std::vector<int> &rides, bool store_cumulative)
: Eddington::Eddington(store_cumulative) {
update(rides);
}
int Eddington::getNumberToNext() { return m_eddington_number + 1 - m_n_above; }
int Eddington::getNumberToTarget(int target) {
if (target == m_eddington_number + 1) {
// Early exit for trivial case.
return getNumberToNext();
}
int completed_rides = 0;
for (auto it = m_hashmap.begin(); it != m_hashmap.end(); ++it)
if (it->first >= target)
completed_rides += it->second;
return target - completed_rides;
}
void Eddington::update(int ride) {
if (ride > m_eddington_number) {
m_n_above++;
m_hashmap[ride]++;
if (m_n_above > m_eddington_number) {
m_n_above -= m_hashmap[++m_eddington_number];
m_hashmap.erase(m_eddington_number);
}
}
if (m_cumulative) {
m_cumulative->push_back(m_eddington_number);
}
}
void Eddington::update(const std::vector<int> &rides) {
for (auto ride : rides) update(ride);
}
void Eddington::update(std::istream& rides) {
double line;
while(rides >> line) update(static_cast<int>(line));
}
void Eddington::print() {
std::cout << m_eddington_number << std::endl;
}