@@ -15,31 +15,31 @@ mod tests;
15
15
#[ derive( Debug , Clone , PartialEq , Eq , Hash , Encodable , Decodable ) ]
16
16
pub struct IntervalSet < I > {
17
17
// Start, end
18
- map : SmallVec < [ ( u32 , u32 ) ; 4 ] > ,
18
+ map : SmallVec < [ ( I , I ) ; 4 ] > ,
19
19
domain : usize ,
20
20
_data : PhantomData < I > ,
21
21
}
22
22
23
23
#[ inline]
24
- fn inclusive_start < T : Idx > ( range : impl RangeBounds < T > ) -> u32 {
24
+ fn inclusive_start < T : Idx > ( range : impl RangeBounds < T > ) -> T {
25
25
match range. start_bound ( ) {
26
- Bound :: Included ( start) => start. index ( ) as u32 ,
27
- Bound :: Excluded ( start) => start. index ( ) as u32 + 1 ,
28
- Bound :: Unbounded => 0 ,
26
+ Bound :: Included ( start) => * start,
27
+ Bound :: Excluded ( start) => T :: new ( start. index ( ) + 1 ) ,
28
+ Bound :: Unbounded => T :: new ( 0 ) ,
29
29
}
30
30
}
31
31
32
32
#[ inline]
33
- fn inclusive_end < T : Idx > ( domain : usize , range : impl RangeBounds < T > ) -> Option < u32 > {
33
+ fn inclusive_end < T : Idx > ( domain : usize , range : impl RangeBounds < T > ) -> Option < T > {
34
34
let end = match range. end_bound ( ) {
35
- Bound :: Included ( end) => end. index ( ) as u32 ,
36
- Bound :: Excluded ( end) => end. index ( ) . checked_sub ( 1 ) ? as u32 ,
37
- Bound :: Unbounded => domain. checked_sub ( 1 ) ? as u32 ,
35
+ Bound :: Included ( end) => * end,
36
+ Bound :: Excluded ( end) => T :: new ( end. index ( ) . checked_sub ( 1 ) ?) ,
37
+ Bound :: Unbounded => T :: new ( domain. checked_sub ( 1 ) ?) ,
38
38
} ;
39
39
Some ( end)
40
40
}
41
41
42
- impl < I : Idx > IntervalSet < I > {
42
+ impl < I : Ord + Idx > IntervalSet < I > {
43
43
pub fn new ( domain : usize ) -> IntervalSet < I > {
44
44
IntervalSet { map : SmallVec :: new ( ) , domain, _data : PhantomData }
45
45
}
@@ -71,7 +71,7 @@ impl<I: Idx> IntervalSet<I> {
71
71
where
72
72
I : Step ,
73
73
{
74
- self . map . iter ( ) . map ( |& ( start, end) | I :: new ( start as usize ) ..I :: new ( end as usize + 1 ) )
74
+ self . map . iter ( ) . map ( |& ( start, end) | start..I :: new ( end. index ( ) + 1 ) )
75
75
}
76
76
77
77
/// Returns true if we increased the number of elements present.
@@ -100,10 +100,10 @@ impl<I: Idx> IntervalSet<I> {
100
100
// if r.0 == end + 1, then we're actually adjacent, so we want to
101
101
// continue to the next range. We're looking here for the first
102
102
// range which starts *non-adjacently* to our end.
103
- let next = self . map . partition_point ( |r| r. 0 <= end + 1 ) ;
103
+ let next = self . map . partition_point ( |r| r. 0 . index ( ) <= end. index ( ) + 1 ) ;
104
104
if let Some ( last) = next. checked_sub ( 1 ) {
105
105
let ( prev_start, prev_end) = & mut self . map [ last] ;
106
- if * prev_end + 1 >= start {
106
+ if prev_end. index ( ) + 1 >= start. index ( ) {
107
107
// If the start for the inserted range is adjacent to the
108
108
// end of the previous, we can extend the previous range.
109
109
if start < * prev_start {
@@ -168,15 +168,11 @@ impl<I: Idx> IntervalSet<I> {
168
168
let ( prev_start, prev_end) = self . map . remove ( idx) ;
169
169
// The range we're looking at contains the range we're removing completely.
170
170
assert ! ( prev_start <= start && end <= prev_end) ;
171
- self . insert_range ( I :: new ( prev_start as usize ) ..I :: new ( start as usize ) ) ;
172
- self . insert_range ( (
173
- Bound :: Excluded ( I :: new ( end as usize ) ) ,
174
- Bound :: Included ( I :: new ( prev_end as usize ) ) ,
175
- ) ) ;
171
+ self . insert_range ( prev_start..start) ;
172
+ self . insert_range ( ( Bound :: Excluded ( end) , Bound :: Included ( prev_end) ) ) ;
176
173
}
177
174
178
175
pub fn contains ( & self , needle : I ) -> bool {
179
- let needle = needle. index ( ) as u32 ;
180
176
let Some ( last) = self . map . partition_point ( |r| r. 0 <= needle) . checked_sub ( 1 ) else {
181
177
// All ranges in the map start after the new range's end
182
178
return false ;
@@ -209,7 +205,7 @@ impl<I: Idx> IntervalSet<I> {
209
205
return None ;
210
206
}
211
207
let range = self . map . get ( self . map . partition_point ( |r| r. 1 < start) ) ?;
212
- if range. 0 > end { None } else { Some ( I :: new ( std:: cmp:: max ( range. 0 , start) as usize ) ) }
208
+ if range. 0 > end { None } else { Some ( std:: cmp:: max ( range. 0 , start) ) }
213
209
}
214
210
215
211
/// Returns the minimum (first) element **not** present in the set from `range`.
@@ -223,13 +219,13 @@ impl<I: Idx> IntervalSet<I> {
223
219
return None ;
224
220
}
225
221
let Some ( range) = self . map . get ( self . map . partition_point ( |r| r. 1 < start) ) else {
226
- return Some ( I :: new ( start as usize ) ) ;
222
+ return Some ( start) ;
227
223
} ;
228
224
if start < range. 0 {
229
- return Some ( I :: new ( start as usize ) ) ;
230
- } else if range. 1 as usize + 1 < self . domain {
231
- if range. 1 + 1 <= end {
232
- return Some ( I :: new ( range. 1 as usize + 1 ) ) ;
225
+ return Some ( start) ;
226
+ } else if range. 1 . index ( ) + 1 < self . domain {
227
+ if range. 1 . index ( ) + 1 <= end. index ( ) {
228
+ return Some ( I :: new ( range. 1 . index ( ) + 1 ) ) ;
233
229
}
234
230
}
235
231
@@ -251,12 +247,12 @@ impl<I: Idx> IntervalSet<I> {
251
247
return None ;
252
248
} ;
253
249
let ( _, prev_end) = & self . map [ last] ;
254
- if start <= * prev_end { Some ( I :: new ( std:: cmp:: min ( * prev_end, end) as usize ) ) } else { None }
250
+ if start <= * prev_end { Some ( std:: cmp:: min ( * prev_end, end) ) } else { None }
255
251
}
256
252
257
253
pub fn insert_all ( & mut self ) {
258
254
self . clear ( ) ;
259
- self . map . push ( ( 0 , self . domain . try_into ( ) . unwrap ( ) ) ) ;
255
+ self . map . push ( ( I :: new ( 0 ) , I :: new ( self . domain ) ) ) ;
260
256
}
261
257
262
258
pub fn union ( & mut self , other : & IntervalSet < I > ) -> bool
@@ -287,7 +283,7 @@ where
287
283
column_size : usize ,
288
284
}
289
285
290
- impl < R : Idx , C : Step + Idx > SparseIntervalMatrix < R , C > {
286
+ impl < R : Idx , C : Ord + Step + Idx > SparseIntervalMatrix < R , C > {
291
287
pub fn new ( column_size : usize ) -> SparseIntervalMatrix < R , C > {
292
288
SparseIntervalMatrix { rows : IndexVec :: new ( ) , column_size }
293
289
}
0 commit comments