Skip to content

Commit 0db3339

Browse files
committed
Slightly improved IntervalSet::push
1 parent d089e22 commit 0db3339

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

regex-syntax/src/hir/interval.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,20 @@ impl<I: Interval> IntervalSet<I> {
8181

8282
/// Add a new interval to this set.
8383
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+
8898
// We don't know whether the new interval added here is considered
8999
// case folded, so we conservatively assume that the entire set is
90100
// no longer case folded if it was previously.

0 commit comments

Comments
 (0)