forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse.swift
274 lines (235 loc) · 8.7 KB
/
Reverse.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//===--- Reverse.swift - Lazy sequence reversal ---------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
public protocol ReverseIndexType : BidirectionalIndexType {
typealias Base : BidirectionalIndexType
/// A type that can represent the number of steps between pairs of
/// `ReverseIndex` values where one value is reachable from the other.
typealias Distance: _SignedIntegerType = Base.Distance
/// The successor position in the underlying (un-reversed)
/// collection.
///
/// If `self` is `advance(c.reverse.startIndex, n)`, then:
/// - `self.base` is `advance(c.endIndex, -n)`.
/// - if `n` != `c.count`, then `c.reverse[self]` is
/// equivalent to `[self.base.predecessor()]`.
var base: Base { get }
init(_ base: Base)
}
extension BidirectionalIndexType where Self : ReverseIndexType {
/// Returns the next consecutive value after `self`.
///
/// - Requires: The next value is representable.
public func successor() -> Self {
return Self(base.predecessor())
}
/// Returns the previous consecutive value before `self`.
///
/// - Requires: The previous value is representable.
public func predecessor() -> Self {
return Self(base.successor())
}
}
/// A wrapper for a `BidirectionalIndexType` that reverses its
/// direction of traversal.
public struct ReverseIndex<Base: BidirectionalIndexType>
: BidirectionalIndexType, ReverseIndexType {
public typealias Distance = Base.Distance
public init(_ base: Base) { self.base = base }
/// The successor position in the underlying (un-reversed)
/// collection.
///
/// If `self` is `advance(c.reverse.startIndex, n)`, then:
/// - `self.base` is `advance(c.endIndex, -n)`.
/// - if `n` != `c.count`, then `c.reverse[self]` is
/// equivalent to `[self.base.predecessor()]`.
public let base: Base
@available(*, unavailable, renamed="Base")
public typealias I = Base
}
@warn_unused_result
public func == <Base> (
lhs: ReverseIndex<Base>, rhs: ReverseIndex<Base>
) -> Bool {
return lhs.base == rhs.base
}
/// A wrapper for a `RandomAccessIndexType` that reverses its
/// direction of traversal.
public struct ReverseRandomAccessIndex<Base: RandomAccessIndexType>
: RandomAccessIndexType, ReverseIndexType {
public typealias Distance = Base.Distance
public init(_ base: Base) { self.base = base }
/// The successor position in the underlying (un-reversed)
/// collection.
///
/// If `self` is `advance(c.reverse.startIndex, n)`, then:
/// - `self.base` is `advance(c.endIndex, -n)`.
/// - if `n` != `c.count`, then `c.reverse[self]` is
/// equivalent to `[self.base.predecessor()]`.
public let base: Base
public func distanceTo(other: ReverseRandomAccessIndex) -> Distance {
return other.base.distanceTo(base)
}
public func advancedBy(n: Distance) -> ReverseRandomAccessIndex {
return ReverseRandomAccessIndex(base.advancedBy(-n))
}
@available(*, unavailable, renamed="Base")
public typealias I = Base
}
public protocol _ReverseCollectionType : CollectionType {
typealias Index : ReverseIndexType
typealias Base : CollectionType
var _base: Base {get}
}
extension CollectionType
where Self : _ReverseCollectionType, Self.Base.Index : RandomAccessIndexType {
public var startIndex : ReverseRandomAccessIndex<Self.Base.Index> {
return ReverseRandomAccessIndex(_base.endIndex)
}
}
extension _ReverseCollectionType
where Self : CollectionType, Self.Index.Base == Self.Base.Index
{
public var startIndex : Index { return Self.Index(_base.endIndex) }
public var endIndex : Index { return Self.Index(_base.startIndex) }
public subscript(position: Index) -> Self.Base.Generator.Element {
return _base[position.base.predecessor()]
}
}
/// A Collection that presents the elements of its `Base` collection
/// in reverse order.
///
/// - Note: This type is the result of `x.reverse()` where `x` is a
/// collection having bidirectional indices.
///
/// The `reverse()` method is always lazy when applied to a collection
/// with bidirectional indices, but does not implicitly confer
/// laziness on algorithms applied to its result. In other words, for
/// ordinary collections `c` having bidirectional indices:
///
/// * `c.reverse()` does not create new storage
/// * `c.reverse().map(f)` maps eagerly and returns a new array
/// * `c.lazy.reverse().map(f)` maps lazily and returns a `LazyMapCollection`
///
/// - See also: `ReverseRandomAccessCollection`
public struct ReverseCollection<
Base : CollectionType where Base.Index : BidirectionalIndexType
> : CollectionType, _ReverseCollectionType {
/// Creates an instance that presents the elements of `base` in
/// reverse order.
///
/// - Complexity: O(1)
public init(_ base: Base) {
self._base = base
}
/// A type that represents a valid position in the collection.
///
/// Valid indices consist of the position of every element and a
/// "past the end" position that's not valid for use as a subscript.
public typealias Index = ReverseIndex<Base.Index>
/// A type that provides the *sequence*'s iteration interface and
/// encapsulates its iteration state.
public typealias Generator = IndexingGenerator<ReverseCollection>
public let _base: Base
@available(*, unavailable, renamed="Base")
public typealias T = Base
}
/// A Collection that presents the elements of its `Base` collection
/// in reverse order.
///
/// - Note: This type is the result of `x.reverse()` where `x` is a
/// collection having random access indices.
/// - See also: `ReverseCollection`
public struct ReverseRandomAccessCollection<
Base : CollectionType where Base.Index : RandomAccessIndexType
> : _ReverseCollectionType {
/// Creates an instance that presents the elements of `base` in
/// reverse order.
///
/// - Complexity: O(1)
public init(_ base: Base) {
self._base = base
}
/// A type that represents a valid position in the collection.
///
/// Valid indices consist of the position of every element and a
/// "past the end" position that's not valid for use as a subscript.
public typealias Index = ReverseRandomAccessIndex<Base.Index>
/// A type that provides the *sequence*'s iteration interface and
/// encapsulates its iteration state.
public typealias Generator = IndexingGenerator<
ReverseRandomAccessCollection
>
public let _base: Base
@available(*, unavailable, renamed="Base")
public typealias T = Base
}
extension CollectionType where Index : BidirectionalIndexType {
/// Return the elements of `self` in reverse order.
///
/// - Complexity: O(1)
@warn_unused_result
public func reverse() -> ReverseCollection<Self> {
return ReverseCollection(self)
}
}
extension CollectionType where Index : RandomAccessIndexType {
/// Return the elements of `self` in reverse order.
///
/// - Complexity: O(1)
@warn_unused_result
public func reverse() -> ReverseRandomAccessCollection<Self> {
return ReverseRandomAccessCollection(self)
}
}
extension LazyCollectionType
where Index : BidirectionalIndexType, Elements.Index : BidirectionalIndexType {
/// Return the elements of `self` in reverse order.
///
/// - Complexity: O(1)
@warn_unused_result
public func reverse() -> LazyCollection<
ReverseCollection<Elements>
> {
return ReverseCollection(elements).lazy
}
}
extension LazyCollectionType
where Index : RandomAccessIndexType, Elements.Index : RandomAccessIndexType {
/// Return the elements of `self` in reverse order.
///
/// - Complexity: O(1)
@warn_unused_result
public func reverse() -> LazyCollection<
ReverseRandomAccessCollection<Elements>
> {
return ReverseRandomAccessCollection(elements).lazy
}
}
/// Return an `Array` containing the elements of `source` in reverse
/// order.
@available(*, unavailable, message="call the 'reverse()' method on the collection")
public func reverse<C:CollectionType where C.Index: BidirectionalIndexType>(
source: C
) -> [C.Generator.Element] {
fatalError("unavailable function can't be called")
}
@available(*, unavailable, renamed="ReverseCollection")
public struct BidirectionalReverseView<
Base : CollectionType where Base.Index : BidirectionalIndexType
> {}
@available(*, unavailable, renamed="ReverseRandomAccessCollection")
public struct RandomAccessReverseView<
Base : CollectionType where Base.Index : RandomAccessIndexType
> {}
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End: