Skip to content

Commit cd57a69

Browse files
committed
Review
1 parent 292f3d0 commit cd57a69

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

version-ranges/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,16 @@ impl<V: Ord> FromIterator<(Bound<V>, Bound<V>)> for Ranges<V> {
834834
fn from_iter<T: IntoIterator<Item = (Bound<V>, Bound<V>)>>(iter: T) -> Self {
835835
// We have three constraints we need to fulfil:
836836
// 1. The segments are sorted, from lowest to highest (through `Ord`): By sorting.
837-
// 2. Each segment contains at least one version (start < end): By `union`.
838-
// 3. There is at least one version between two segments: By `union`.
837+
// 2. Each segment contains at least one version (start < end): By skipping invalid
838+
// segments.
839+
// 3. There is at least one version between two segments: By merging overlapping elements.
840+
//
841+
// Technically, the implementation has a O(n²) worst case complexity since we're inserting
842+
// and removing. This has two motivations: One is that we don't have any performance
843+
// critical usages of this method as of this writing, so we have no real world benchmark.
844+
// The other is that we get the elements from an iterator, so to avoid moving elements
845+
// around we would first need to build a different, sorted collection with extra
846+
// allocation(s), before we could build our real segments. --Konsti
839847

840848
// For this implementation, we choose to only build a single smallvec and insert or remove
841849
// in it, instead of e.g. collecting the segments into a sorted datastructure first and then
@@ -986,6 +994,9 @@ impl<V: Ord> FromIterator<(Bound<V>, Bound<V>)> for Ranges<V> {
986994
// following: |------|
987995
//
988996
// final: |------| |------| |------|
997+
998+
// This line is O(n), which makes the algorithm O(n²), but it should be good
999+
// enough for now.
9891000
segments.insert(insertion_point, segment);
9901001
}
9911002
}

0 commit comments

Comments
 (0)