Skip to content

Commit a009ae2

Browse files
committed
SR-7266: Avoid nesting of ReversedCollection
Applying reversed() two times should return identity.
1 parent 8353b4c commit a009ae2

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

stdlib/public/core/Reverse.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ extension ReversedCollection: BidirectionalCollection {
253253

254254
extension ReversedCollection: RandomAccessCollection where Base: RandomAccessCollection { }
255255

256+
extension ReversedCollection {
257+
/// This is optimization to return identity of doubly reversed collection
258+
/// For example [1,2].reversed().reversed() => [1,2]
259+
///
260+
/// Returns a view presenting the elements of the collection in reverse
261+
/// order.
262+
///
263+
/// - Complexity: O(1)
264+
public func reversed() -> Base {
265+
return _base
266+
}
267+
}
268+
256269
extension BidirectionalCollection {
257270
/// Returns a view presenting the elements of the collection in reverse
258271
/// order.

0 commit comments

Comments
 (0)