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
use crate::IntSpan;
use std::collections::BTreeMap;
#[derive(Default, Clone)]
pub struct Coverage {
max: i32,
tiers: BTreeMap<i32, IntSpan>,
}
impl Coverage {
pub fn max(&self) -> &i32 {
&self.max
}
pub fn tiers(&self) -> &BTreeMap<i32, IntSpan> {
&self.tiers
}
pub fn new(max: i32) -> Self {
Self::new_len(max, 1_000_000_000)
}
pub fn new_len(max: i32, len: i32) -> Self {
let mut tiers: BTreeMap<i32, IntSpan> = BTreeMap::new();
tiers.insert(-1, IntSpan::from_pair(1, len));
tiers.insert(0, IntSpan::from_pair(1, len));
for i in 1..=max {
tiers.insert(i, IntSpan::new());
}
Self { max, tiers }
}
fn begin_end(begin: i32, end: i32) -> (i32, i32) {
let mut tup = (begin.min(end), begin.max(end));
if tup.0 == 0 {
tup.0 = 1;
}
tup
}
pub fn bump(&mut self, begin: i32, end: i32) {
let tup = Self::begin_end(begin, end);
let mut intspan = IntSpan::from_pair(tup.0, tup.1);
if self
.tiers
.get(&-1)
.unwrap()
.equals(self.tiers.get(&self.max).unwrap())
{
return;
}
self.tiers.entry(0).and_modify(|e| e.subtract(&intspan));
for i in 1..=self.max {
let intersect = self.tiers.get(&i).unwrap().intersect(&intspan);
self.tiers.entry(i).and_modify(|e| e.merge(&intspan));
if i + 1 > self.max {
break;
}
intspan = intersect.copy();
}
}
pub fn max_tier(&self) -> IntSpan {
self.tiers().get(self.max()).unwrap().copy()
}
pub fn uniq_tiers(&self) -> BTreeMap<i32, IntSpan> {
let mut tiers = self.tiers.clone();
for i in 1..self.max {
let intspan_next = tiers[&(i + 1)].copy();
tiers.entry(i).and_modify(|e| e.subtract(&intspan_next));
}
tiers
}
}