-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterval.h
66 lines (48 loc) · 2.12 KB
/
interval.h
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
// Data structures for representing ranges of numbers (doubles).
#pragma once
#include <compare>
#include <set>
#include <string>
namespace pivid {
// A half-open interval [begin, end) on the number line.
struct Interval {
double begin = {}, end = {};
bool empty() const { return begin >= end; }
bool contains(double t) const { return begin <= t && t < end; }
auto operator<=>(Interval const& o) const { return begin <=> o.begin; }
bool operator==(Interval const& o) const = default;
};
// A set of non-overlapping Intervals across the number line.
class IntervalSet {
public:
using iterator = std::set<Interval>::const_iterator;
// Adds an Interval, merging as necessary for overlaps and adjacencies.
iterator insert(Interval);
// Adds every Interval in another IntervalSet.
void insert(IntervalSet const& s) { for (auto r : s) insert(r); }
// Removes an Interval, truncating or splitting intervals as necessary.
iterator erase(Interval);
// Removes every Interval in another IntervalSet.
void erase(IntervalSet const& s) { for (auto r : s) erase(r); }
// STL standard accessors for the Intervals in the set,
// which are always non-overlapping, non-abutting and in sorted order.
iterator begin() const { return ranges.begin(); }
iterator end() const { return ranges.end(); }
bool empty() const { return ranges.empty(); }
int count() const { return ranges.size(); }
// Returns the range of Intervals that overlap a given interval.
iterator overlap_begin(double t) const;
iterator overlap_end(double t) const { return ranges.lower_bound({t, {}}); }
// Returns true if an interval in this set contains the given point.
bool contains(double) const;
// Returns the narrowest Interval that covers every Interval in the set.
Interval bounds() const;
auto operator<=>(IntervalSet const& o) const = default;
bool operator==(IntervalSet const& o) const = default;
private:
std::set<Interval> ranges;
};
// Debugging descriptions of structures.
std::string debug(Interval);
std::string debug(IntervalSet const&);
} // namespace pivid