@@ -35,6 +35,17 @@ extension MutableCollection where Self: BidirectionalCollection {
35
35
}
36
36
}
37
37
38
+ /// A collection that presents the elements of its base collection
39
+ /// in reverse order.
40
+ ///
41
+ /// - See also: `ReversedCollection` below.
42
+ public protocol ReversedCollectionProtocol : BidirectionalCollection {
43
+ /// A `Collection` that can contain the same elements as this one,
44
+ /// just in reverse order.
45
+ associatedtype Elements : Collection where Elements. Iterator. Element == Iterator . Element
46
+ var elements : Elements { get }
47
+ }
48
+
38
49
/// A collection that presents the elements of its base collection
39
50
/// in reverse order.
40
51
///
@@ -84,7 +95,16 @@ extension ReversedCollection {
84
95
}
85
96
}
86
97
}
87
-
98
+
99
+ extension ReversedCollection : ReversedCollectionProtocol {
100
+ /// The type of the underlying collection.
101
+ public typealias Elements = Base
102
+
103
+ /// The underlying collection.
104
+ @inlinable
105
+ public var elements : Elements { return _base }
106
+ }
107
+
88
108
extension ReversedCollection . Iterator : IteratorProtocol , Sequence {
89
109
public typealias Element = Base . Element
90
110
@@ -254,13 +274,8 @@ extension ReversedCollection: BidirectionalCollection {
254
274
extension ReversedCollection : RandomAccessCollection where Base: RandomAccessCollection { }
255
275
256
276
extension ReversedCollection {
257
- /// This is optimization to return identity of doubly reversed collection
277
+ /// This is optimization to return original collection of doubly reversed collection
258
278
/// 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
279
public func reversed( ) -> Base {
265
280
return _base
266
281
}
@@ -298,6 +313,14 @@ extension BidirectionalCollection {
298
313
}
299
314
}
300
315
316
+ extension LazyCollection where Base: ReversedCollectionProtocol {
317
+ /// This is optimization to return original collection of doubly reversed lazy collection
318
+ /// For example [1,2].lazy.reversed().reversed() => [1,2].lazy
319
+ public func reversed( ) -> LazyCollection < Elements . Elements > {
320
+ return elements. elements. lazy
321
+ }
322
+ }
323
+
301
324
extension LazyCollectionProtocol
302
325
where
303
326
Self: BidirectionalCollection ,
0 commit comments