@@ -887,8 +887,16 @@ impl<V: Ord> FromIterator<(Bound<V>, Bound<V>)> for Ranges<V> {
887887 fn from_iter < T : IntoIterator < Item = ( Bound < V > , Bound < V > ) > > ( iter : T ) -> Self {
888888 // We have three constraints we need to fulfil:
889889 // 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
892900
893901 // For this implementation, we choose to only build a single smallvec and insert or remove
894902 // 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> {
10391047 // following: |------|
10401048 //
10411049 // final: |------| |------| |------|
1050+
1051+ // This line is O(n), which makes the algorithm O(n²), but it should be good
1052+ // enough for now.
10421053 segments. insert ( insertion_point, segment) ;
10431054 }
10441055 }
0 commit comments