@@ -893,10 +893,9 @@ extension Sequence {
893
893
894
894
// FIXME: <rdar://problem/21885650> Create reusable RingBuffer<T>
895
895
// Put incoming elements into a ring buffer to save space. Once all
896
- // elements are consumed, reorder the ring buffer into an `Array`
897
- // and return it. This saves memory for sequences particularly longer
898
- // than `maxLength`.
899
- var ringBuffer : [ Element ] = [ ]
896
+ // elements are consumed, reorder the ring buffer into a copy and return it.
897
+ // This saves memory for sequences particularly longer than `maxLength`.
898
+ var ringBuffer = ContiguousArray < Element > ( )
900
899
ringBuffer. reserveCapacity ( Swift . min ( maxLength, underestimatedCount) )
901
900
902
901
var i = 0
@@ -911,13 +910,13 @@ extension Sequence {
911
910
}
912
911
913
912
if i != ringBuffer. startIndex {
914
- var rotated : [ Element ] = [ ]
913
+ var rotated = ContiguousArray < Element > ( )
915
914
rotated. reserveCapacity ( ringBuffer. count)
916
915
rotated += ringBuffer [ i..< ringBuffer. endIndex]
917
916
rotated += ringBuffer [ 0 ..< i]
918
- return rotated
919
- } else {
920
- return ringBuffer
917
+ return Array ( rotated)
918
+ } else {
919
+ return Array ( ringBuffer)
921
920
}
922
921
}
923
922
@@ -975,8 +974,8 @@ extension Sequence {
975
974
// holding tank into the result, an `Array`. This saves
976
975
// `k` * sizeof(Element) of memory, because slices keep the entire
977
976
// memory of an `Array` alive.
978
- var result : [ Element ] = [ ]
979
- var ringBuffer : [ Element ] = [ ]
977
+ var result = ContiguousArray < Element > ( )
978
+ var ringBuffer = ContiguousArray < Element > ( )
980
979
var i = ringBuffer. startIndex
981
980
982
981
for element in self {
@@ -988,7 +987,7 @@ extension Sequence {
988
987
i = ( i + 1 ) % k
989
988
}
990
989
}
991
- return result
990
+ return Array ( result)
992
991
}
993
992
994
993
/// Returns a sequence by skipping the initial, consecutive elements that
@@ -1070,15 +1069,15 @@ extension Sequence {
1070
1069
public __consuming func prefix(
1071
1070
while predicate: ( Element ) throws -> Bool
1072
1071
) rethrows -> [ Element ] {
1073
- var result : [ Element ] = [ ]
1072
+ var result = ContiguousArray < Element > ( )
1074
1073
1075
1074
for element in self {
1076
1075
guard try predicate ( element) else {
1077
1076
break
1078
1077
}
1079
1078
result. append ( element)
1080
1079
}
1081
- return result
1080
+ return Array ( result)
1082
1081
}
1083
1082
}
1084
1083
0 commit comments