File tree Expand file tree Collapse file tree 1 file changed +11
-7
lines changed Expand file tree Collapse file tree 1 file changed +11
-7
lines changed Original file line number Diff line number Diff line change @@ -891,7 +891,7 @@ extension Sequence {
891
891
// Put incoming elements into a ring buffer to save space. Once all
892
892
// elements are consumed, reorder the ring buffer and return it.
893
893
// This saves memory for sequences particularly longer than `maxLength`.
894
- var ringBuffer = ContiguousArray < Element > ( )
894
+ var ringBuffer : [ Element ] = [ ]
895
895
ringBuffer. reserveCapacity ( Swift . min ( maxLength, underestimatedCount) )
896
896
897
897
var i = 0
@@ -901,16 +901,20 @@ extension Sequence {
901
901
ringBuffer. append ( element)
902
902
} else {
903
903
ringBuffer [ i] = element
904
- i = ( i + 1 ) % maxLength
904
+ i += 1
905
+ i %= maxLength
905
906
}
906
907
}
907
908
908
- if i != ringBuffer. startIndex { // Rotate the array in-place
909
- ringBuffer [ 0 ..< i] . reverse ( )
910
- ringBuffer [ i..< ringBuffer. endIndex] . reverse ( )
911
- ringBuffer [ 0 ..< ringBuffer. endIndex] . reverse ( )
909
+ if i != ringBuffer. startIndex {
910
+ var rotated : [ Element ] = [ ]
911
+ rotated. reserveCapacity ( ringBuffer. count)
912
+ rotated += ringBuffer [ i..< ringBuffer. endIndex]
913
+ rotated += ringBuffer [ 0 ..< i]
914
+ return rotated
915
+ } else {
916
+ return ringBuffer
912
917
}
913
- return Array ( ringBuffer)
914
918
}
915
919
916
920
/// Returns a sequence containing all but the given number of initial
You can’t perform that action at this time.
0 commit comments