Commit 0db3339 1 parent d089e22 commit 0db3339 Copy full SHA for 0db3339
File tree 1 file changed +14
-4
lines changed
1 file changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -81,10 +81,20 @@ impl<I: Interval> IntervalSet<I> {
81
81
82
82
/// Add a new interval to this set.
83
83
pub fn push ( & mut self , interval : I ) {
84
- // TODO: This could be faster. e.g., Push the interval such that
85
- // it preserves canonicalization.
86
- self . ranges . push ( interval) ;
87
- self . canonicalize ( ) ;
84
+ // Use a binary search to try to find the approximate place this
85
+ // interval should go
86
+ let point = match self . ranges . binary_search ( & interval) {
87
+ // We lucked out, this interval already exists in the set.
88
+ Ok ( _) => return ,
89
+ Err ( point) => point,
90
+ } ;
91
+
92
+ // TODO: A more efficient implementation is possible here, one which
93
+ // avoids the unconditional insert and searches only the range covered
94
+ // by `interval` when performing the union.
95
+ self . ranges . insert ( point, interval) ;
96
+ union_sorted ( & mut self . ranges ) ;
97
+
88
98
// We don't know whether the new interval added here is considered
89
99
// case folded, so we conservatively assume that the entire set is
90
100
// no longer case folded if it was previously.
You can’t perform that action at this time.
0 commit comments