-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
interval_add.cc
69 lines (58 loc) · 2.19 KB
/
interval_add.cc
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
#include <algorithm>
#include <vector>
#include "test_framework/fmt_print.h"
#include "test_framework/generic_test.h"
#include "test_framework/serialization_traits.h"
using std::max;
using std::min;
using std::vector;
struct Interval {
int left, right;
};
vector<Interval> AddInterval(const vector<Interval>& disjoint_intervals,
Interval new_interval) {
size_t i = 0;
vector<Interval> result;
// Processes intervals in disjoint_intervals which come before new_interval.
while (i < size(disjoint_intervals) &&
new_interval.left > disjoint_intervals[i].right) {
result.emplace_back(disjoint_intervals[i++]);
}
// Processes intervals in disjoint_intervals which overlap with
// new_interval.
while (i < size(disjoint_intervals) &&
new_interval.right >= disjoint_intervals[i].left) {
// If [a, b] and [c, d] overlap, their union is [min(a, c),max(b, d)].
new_interval = {min(new_interval.left, disjoint_intervals[i].left),
max(new_interval.right, disjoint_intervals[i].right)};
++i;
}
result.emplace_back(new_interval);
// Processes intervals in disjoint_intervals which come after new_interval.
result.insert(end(result), begin(disjoint_intervals) + i,
end(disjoint_intervals));
return result;
}
namespace test_framework {
template <>
struct SerializationTrait<Interval> : UserSerTrait<Interval, int, int> {
static std::vector<std::string> GetMetricNames(const std::string& arg_name) {
return {FmtStr("length({})", arg_name)};
}
static std::vector<int> GetMetrics(const Interval& x) {
return {x.right - x.left};
}
};
} // namespace test_framework
bool operator==(const Interval& a, const Interval& b) {
return a.left == b.left && a.right == b.right;
}
std::ostream& operator<<(std::ostream& out, const Interval& i) {
return PrintTo(out, std::make_tuple(i.left, i.right));
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"disjoint_intervals", "new_interval"};
return GenericTestMain(args, "interval_add.cc", "interval_add.tsv",
&AddInterval, DefaultComparator{}, param_names);
}