]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_index/src/interval.rs
Auto merge of #96892 - oli-obk:🐌_obligation_cause_code_🐌, r=estebank
[rust.git] / compiler / rustc_index / src / interval.rs
1 use std::iter::Step;
2 use std::marker::PhantomData;
3 use std::ops::Bound;
4 use std::ops::RangeBounds;
5
6 use crate::vec::Idx;
7 use crate::vec::IndexVec;
8 use smallvec::SmallVec;
9
10 #[cfg(test)]
11 mod tests;
12
13 /// Stores a set of intervals on the indices.
14 #[derive(Debug, Clone)]
15 pub struct IntervalSet<I> {
16     // Start, end
17     map: SmallVec<[(u32, u32); 4]>,
18     domain: usize,
19     _data: PhantomData<I>,
20 }
21
22 #[inline]
23 fn inclusive_start<T: Idx>(range: impl RangeBounds<T>) -> u32 {
24     match range.start_bound() {
25         Bound::Included(start) => start.index() as u32,
26         Bound::Excluded(start) => start.index() as u32 + 1,
27         Bound::Unbounded => 0,
28     }
29 }
30
31 #[inline]
32 fn inclusive_end<T: Idx>(domain: usize, range: impl RangeBounds<T>) -> Option<u32> {
33     let end = match range.end_bound() {
34         Bound::Included(end) => end.index() as u32,
35         Bound::Excluded(end) => end.index().checked_sub(1)? as u32,
36         Bound::Unbounded => domain.checked_sub(1)? as u32,
37     };
38     Some(end)
39 }
40
41 impl<I: Idx> IntervalSet<I> {
42     pub fn new(domain: usize) -> IntervalSet<I> {
43         IntervalSet { map: SmallVec::new(), domain, _data: PhantomData }
44     }
45
46     pub fn clear(&mut self) {
47         self.map.clear();
48     }
49
50     pub fn iter(&self) -> impl Iterator<Item = I> + '_
51     where
52         I: Step,
53     {
54         self.iter_intervals().flatten()
55     }
56
57     /// Iterates through intervals stored in the set, in order.
58     pub fn iter_intervals(&self) -> impl Iterator<Item = std::ops::Range<I>> + '_
59     where
60         I: Step,
61     {
62         self.map.iter().map(|&(start, end)| I::new(start as usize)..I::new(end as usize + 1))
63     }
64
65     /// Returns true if we increased the number of elements present.
66     pub fn insert(&mut self, point: I) -> bool {
67         self.insert_range(point..=point)
68     }
69
70     /// Returns true if we increased the number of elements present.
71     pub fn insert_range(&mut self, range: impl RangeBounds<I> + Clone) -> bool {
72         let start = inclusive_start(range.clone());
73         let Some(end) = inclusive_end(self.domain, range) else {
74             // empty range
75             return false;
76         };
77         if start > end {
78             return false;
79         }
80
81         // This condition looks a bit weird, but actually makes sense.
82         //
83         // if r.0 == end + 1, then we're actually adjacent, so we want to
84         // continue to the next range. We're looking here for the first
85         // range which starts *non-adjacently* to our end.
86         let next = self.map.partition_point(|r| r.0 <= end + 1);
87         if let Some(right) = next.checked_sub(1) {
88             let (prev_start, prev_end) = self.map[right];
89             if prev_end + 1 >= start {
90                 // If the start for the inserted range is adjacent to the
91                 // end of the previous, we can extend the previous range.
92                 if start < prev_start {
93                     // The first range which ends *non-adjacently* to our start.
94                     // And we can ensure that left <= right.
95                     let left = self.map.partition_point(|l| l.1 + 1 < start);
96                     let min = std::cmp::min(self.map[left].0, start);
97                     let max = std::cmp::max(prev_end, end);
98                     self.map[right] = (min, max);
99                     if left != right {
100                         self.map.drain(left..right);
101                     }
102                     return true;
103                 } else {
104                     // We overlap with the previous range, increase it to
105                     // include us.
106                     //
107                     // Make sure we're actually going to *increase* it though --
108                     // it may be that end is just inside the previously existing
109                     // set.
110                     return if end > prev_end {
111                         self.map[right].1 = end;
112                         true
113                     } else {
114                         false
115                     };
116                 }
117             } else {
118                 // Otherwise, we don't overlap, so just insert
119                 self.map.insert(right + 1, (start, end));
120                 return true;
121             }
122         } else {
123             if self.map.is_empty() {
124                 // Quite common in practice, and expensive to call memcpy
125                 // with length zero.
126                 self.map.push((start, end));
127             } else {
128                 self.map.insert(next, (start, end));
129             }
130             return true;
131         }
132     }
133
134     pub fn contains(&self, needle: I) -> bool {
135         let needle = needle.index() as u32;
136         let Some(last) = self.map.partition_point(|r| r.0 <= needle).checked_sub(1) else {
137             // All ranges in the map start after the new range's end
138             return false;
139         };
140         let (_, prev_end) = &self.map[last];
141         needle <= *prev_end
142     }
143
144     pub fn superset(&self, other: &IntervalSet<I>) -> bool
145     where
146         I: Step,
147     {
148         // FIXME: Performance here is probably not great. We will be doing a lot
149         // of pointless tree traversals.
150         other.iter().all(|elem| self.contains(elem))
151     }
152
153     pub fn is_empty(&self) -> bool {
154         self.map.is_empty()
155     }
156
157     /// Returns the maximum (last) element present in the set from `range`.
158     pub fn last_set_in(&self, range: impl RangeBounds<I> + Clone) -> Option<I> {
159         let start = inclusive_start(range.clone());
160         let Some(end) = inclusive_end(self.domain, range) else {
161             // empty range
162             return None;
163         };
164         if start > end {
165             return None;
166         }
167         let Some(last) = self.map.partition_point(|r| r.0 <= end).checked_sub(1) else {
168             // All ranges in the map start after the new range's end
169             return None;
170         };
171         let (_, prev_end) = &self.map[last];
172         if start <= *prev_end { Some(I::new(std::cmp::min(*prev_end, end) as usize)) } else { None }
173     }
174
175     pub fn insert_all(&mut self) {
176         self.clear();
177         self.map.push((0, self.domain.try_into().unwrap()));
178     }
179
180     pub fn union(&mut self, other: &IntervalSet<I>) -> bool
181     where
182         I: Step,
183     {
184         assert_eq!(self.domain, other.domain);
185         let mut did_insert = false;
186         for range in other.iter_intervals() {
187             did_insert |= self.insert_range(range);
188         }
189         did_insert
190     }
191 }
192
193 /// This data structure optimizes for cases where the stored bits in each row
194 /// are expected to be highly contiguous (long ranges of 1s or 0s), in contrast
195 /// to BitMatrix and SparseBitMatrix which are optimized for
196 /// "random"/non-contiguous bits and cheap(er) point queries at the expense of
197 /// memory usage.
198 #[derive(Clone)]
199 pub struct SparseIntervalMatrix<R, C>
200 where
201     R: Idx,
202     C: Idx,
203 {
204     rows: IndexVec<R, IntervalSet<C>>,
205     column_size: usize,
206 }
207
208 impl<R: Idx, C: Step + Idx> SparseIntervalMatrix<R, C> {
209     pub fn new(column_size: usize) -> SparseIntervalMatrix<R, C> {
210         SparseIntervalMatrix { rows: IndexVec::new(), column_size }
211     }
212
213     pub fn rows(&self) -> impl Iterator<Item = R> {
214         self.rows.indices()
215     }
216
217     pub fn row(&self, row: R) -> Option<&IntervalSet<C>> {
218         self.rows.get(row)
219     }
220
221     fn ensure_row(&mut self, row: R) -> &mut IntervalSet<C> {
222         self.rows.ensure_contains_elem(row, || IntervalSet::new(self.column_size));
223         &mut self.rows[row]
224     }
225
226     pub fn union_row(&mut self, row: R, from: &IntervalSet<C>) -> bool
227     where
228         C: Step,
229     {
230         self.ensure_row(row).union(from)
231     }
232
233     pub fn union_rows(&mut self, read: R, write: R) -> bool
234     where
235         C: Step,
236     {
237         if read == write || self.rows.get(read).is_none() {
238             return false;
239         }
240         self.ensure_row(write);
241         let (read_row, write_row) = self.rows.pick2_mut(read, write);
242         write_row.union(read_row)
243     }
244
245     pub fn insert_all_into_row(&mut self, row: R) {
246         self.ensure_row(row).insert_all();
247     }
248
249     pub fn insert_range(&mut self, row: R, range: impl RangeBounds<C> + Clone) {
250         self.ensure_row(row).insert_range(range);
251     }
252
253     pub fn insert(&mut self, row: R, point: C) -> bool {
254         self.ensure_row(row).insert(point)
255     }
256
257     pub fn contains(&self, row: R, point: C) -> bool {
258         self.row(row).map_or(false, |r| r.contains(point))
259     }
260 }