@@ -887,8 +887,16 @@ impl<V: Ord> FromIterator<(Bound<V>, Bound<V>)> for Ranges<V> {
887
887
fn from_iter < T : IntoIterator < Item = ( Bound < V > , Bound < V > ) > > ( iter : T ) -> Self {
888
888
// We have three constraints we need to fulfil:
889
889
// 1. The segments are sorted, from lowest to highest (through `Ord`): By sorting.
890
- // 2. Each segment contains at least one version (start < end): By `union`.
891
- // 3. There is at least one version between two segments: By `union`.
890
+ // 2. Each segment contains at least one version (start < end): By skipping invalid
891
+ // segments.
892
+ // 3. There is at least one version between two segments: By merging overlapping elements.
893
+ //
894
+ // Technically, the implementation has a O(n²) worst case complexity since we're inserting
895
+ // and removing. This has two motivations: One is that we don't have any performance
896
+ // critical usages of this method as of this writing, so we have no real world benchmark.
897
+ // The other is that we get the elements from an iterator, so to avoid moving elements
898
+ // around we would first need to build a different, sorted collection with extra
899
+ // allocation(s), before we could build our real segments. --Konsti
892
900
893
901
// For this implementation, we choose to only build a single smallvec and insert or remove
894
902
// in it, instead of e.g. collecting the segments into a sorted datastructure first and then
@@ -1039,6 +1047,9 @@ impl<V: Ord> FromIterator<(Bound<V>, Bound<V>)> for Ranges<V> {
1039
1047
// following: |------|
1040
1048
//
1041
1049
// final: |------| |------| |------|
1050
+
1051
+ // This line is O(n), which makes the algorithm O(n²), but it should be good
1052
+ // enough for now.
1042
1053
segments. insert ( insertion_point, segment) ;
1043
1054
}
1044
1055
}
0 commit comments