11use std:: iter:: Step ;
22use std:: marker:: PhantomData ;
3- use std:: ops:: { Bound , Range , RangeBounds } ;
3+ use std:: ops:: { Bound , RangeBounds } ;
4+ use std:: range:: RangeInclusive ;
45
56use smallvec:: SmallVec ;
67
@@ -59,11 +60,14 @@ impl<I: Idx> IntervalSet<I> {
5960 }
6061
6162 /// Iterates through intervals stored in the set, in order.
62- pub fn iter_intervals ( & self ) -> impl Iterator < Item = std :: ops :: Range < I > >
63+ pub fn iter_intervals ( & self ) -> impl Iterator < Item = RangeInclusive < I > >
6364 where
6465 I : Step ,
6566 {
66- self . map . iter ( ) . map ( |& ( start, end) | I :: new ( start as usize ) ..I :: new ( end as usize + 1 ) )
67+ self . map . iter ( ) . map ( |& ( start, end) | RangeInclusive {
68+ start : I :: new ( start as usize ) ,
69+ last : I :: new ( end as usize ) ,
70+ } )
6771 }
6872
6973 /// Returns true if we increased the number of elements present.
@@ -164,6 +168,35 @@ impl<I: Idx> IntervalSet<I> {
164168 ) ;
165169 }
166170
171+ /// Specialized version of `insert` when we know that the inserted range is
172+ /// *after* any contained.
173+ pub fn append_range ( & mut self , range : impl RangeBounds < I > + Clone ) {
174+ let start = inclusive_start ( range. clone ( ) ) ;
175+ let Some ( end) = inclusive_end ( self . domain , range) else {
176+ // empty range
177+ return ;
178+ } ;
179+ if start > end {
180+ return ;
181+ }
182+
183+ if let Some ( ( _, last_end) ) = self . map . last_mut ( ) {
184+ assert ! ( * last_end < start) ;
185+ if start == * last_end + 1 {
186+ * last_end = end;
187+ } else {
188+ self . map . push ( ( start, end) ) ;
189+ }
190+ } else {
191+ self . map . push ( ( start, end) ) ;
192+ }
193+
194+ debug_assert ! (
195+ self . check_invariants( ) ,
196+ "wrong intervals after append {start:?}..={end:?} to {self:?}"
197+ ) ;
198+ }
199+
167200 pub fn contains ( & self , needle : I ) -> bool {
168201 let needle = needle. index ( ) as u32 ;
169202 let Some ( last) = self . map . partition_point ( |r| r. 0 <= needle) . checked_sub ( 1 ) else {
@@ -180,11 +213,12 @@ impl<I: Idx> IntervalSet<I> {
180213 {
181214 let mut sup_iter = self . iter_intervals ( ) ;
182215 let mut current = None ;
183- let contains = |sup : Range < I > , sub : Range < I > , current : & mut Option < Range < I > > | {
184- if sup. end < sub. start {
185- // if `sup.end == sub.start`, the next sup doesn't contain `sub.start`
216+ let contains = |sup : RangeInclusive < I > ,
217+ sub : RangeInclusive < I > ,
218+ current : & mut Option < RangeInclusive < I > > | {
219+ if sup. last < sub. start {
186220 None // continue to the next sup
187- } else if sup. end >= sub. end && sup. start <= sub. start {
221+ } else if sup. last >= sub. last && sup. start <= sub. start {
188222 * current = Some ( sup) ; // save the current sup
189223 Some ( true )
190224 } else {
@@ -194,8 +228,8 @@ impl<I: Idx> IntervalSet<I> {
194228 other. iter_intervals ( ) . all ( |sub| {
195229 current
196230 . take ( )
197- . and_then ( |sup| contains ( sup, sub. clone ( ) , & mut current) )
198- . or_else ( || sup_iter. find_map ( |sup| contains ( sup, sub. clone ( ) , & mut current) ) )
231+ . and_then ( |sup| contains ( sup, sub, & mut current) )
232+ . or_else ( || sup_iter. find_map ( |sup| contains ( sup, sub, & mut current) ) )
199233 . unwrap_or ( false )
200234 } )
201235 }
@@ -212,11 +246,11 @@ impl<I: Idx> IntervalSet<I> {
212246 let mut other_current = other_iter. next ( ) ?;
213247
214248 loop {
215- if self_current. end <= other_current. start {
249+ if self_current. last < other_current. start {
216250 self_current = self_iter. next ( ) ?;
217251 continue ;
218252 }
219- if other_current. end <= self_current. start {
253+ if other_current. last < self_current. start {
220254 other_current = other_iter. next ( ) ?;
221255 continue ;
222256 }
@@ -340,6 +374,12 @@ impl<R: Idx, C: Step + Idx> SparseIntervalMatrix<R, C> {
340374 self . rows . get ( row)
341375 }
342376
377+ pub fn clear_row ( & mut self , row : R ) {
378+ if let Some ( row) = self . rows . get_mut ( row) {
379+ row. clear ( ) ;
380+ }
381+ }
382+
343383 fn ensure_row ( & mut self , row : R ) -> & mut IntervalSet < C > {
344384 self . rows . ensure_contains_elem ( row, || IntervalSet :: new ( self . column_size ) )
345385 }
@@ -363,6 +403,16 @@ impl<R: Idx, C: Step + Idx> SparseIntervalMatrix<R, C> {
363403 write_row. union ( read_row)
364404 }
365405
406+ pub fn disjoint_rows ( & self , a : R , b : R ) -> bool
407+ where
408+ C : Step ,
409+ {
410+ let ( Some ( a) , Some ( b) ) = ( self . rows . get ( a) , self . rows . get ( b) ) else {
411+ return true ;
412+ } ;
413+ a. disjoint ( b)
414+ }
415+
366416 pub fn insert_all_into_row ( & mut self , row : R ) {
367417 self . ensure_row ( row) . insert_all ( ) ;
368418 }
@@ -379,6 +429,10 @@ impl<R: Idx, C: Step + Idx> SparseIntervalMatrix<R, C> {
379429 self . ensure_row ( row) . append ( point)
380430 }
381431
432+ pub fn append_range ( & mut self , row : R , range : impl RangeBounds < C > + Clone ) {
433+ self . ensure_row ( row) . append_range ( range)
434+ }
435+
382436 pub fn contains ( & self , row : R , point : C ) -> bool {
383437 self . row ( row) . is_some_and ( |r| r. contains ( point) )
384438 }
0 commit comments