forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLazyCollection.swift
204 lines (174 loc) · 6.25 KB
/
LazyCollection.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
//===--- LazyCollection.swift ---------------------------------*- 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
//
//===----------------------------------------------------------------------===//
/// A collection on which normally-eager operations such as `map` and
/// `filter` are implemented lazily.
///
/// Please see `LazySequenceType` for background; `LazyCollectionType`
/// is an analogous component, but for collections.
///
/// To add new lazy collection operations, extend this protocol with
/// methods that return lazy wrappers that are themselves
/// `LazyCollectionType`s.
///
/// - See Also: `LazySequenceType`, `LazyCollection`
public protocol LazyCollectionType
: CollectionType, LazySequenceType {
/// A `CollectionType` that can contain the same elements as this one,
/// possibly with a simpler type.
///
/// - See also: `elements`
typealias Elements: CollectionType = Self
}
/// When there's no special associated `Elements` type, the `elements`
/// property is provided.
extension LazyCollectionType where Elements == Self {
/// Identical to `self`.
public var elements: Self { return self }
}
/// A collection containing the same elements as a `Base` collection,
/// but on which some operations such as `map` and `filter` are
/// implemented lazily.
///
/// - See also: `LazySequenceType`, `LazyCollection`
public struct LazyCollection<Base : CollectionType>
: LazyCollectionType {
/// The type of the underlying collection
public typealias Elements = Base
/// The underlying collection
public var elements: Elements { return _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 = Base.Index
/// Construct an instance with `base` as its underlying Collection
/// instance.
public init(_ base: Base) {
self._base = base
}
internal var _base: Base
}
/// Forward implementations to the base collection, to pick up any
/// optimizations it might implement.
extension LazyCollection : SequenceType {
/// Return a *generator* over the elements of this *sequence*.
///
/// - Complexity: O(1).
public func generate() -> Base.Generator { return _base.generate() }
/// Return a value less than or equal to the number of elements in
/// `self`, **nondestructively**.
///
/// - Complexity: O(N).
public func underestimateCount() -> Int { return _base.underestimateCount() }
public func _copyToNativeArrayBuffer()
-> _ContiguousArrayBuffer<Base.Generator.Element> {
return _base._copyToNativeArrayBuffer()
}
public func _initializeTo(
ptr: UnsafeMutablePointer<Base.Generator.Element>
) -> UnsafeMutablePointer<Base.Generator.Element> {
return _base._initializeTo(ptr)
}
public func _customContainsEquatableElement(
element: Base.Generator.Element
) -> Bool? {
return _base._customContainsEquatableElement(element)
}
}
extension LazyCollection : CollectionType {
/// The position of the first element in a non-empty collection.
///
/// In an empty collection, `startIndex == endIndex`.
public var startIndex: Base.Index {
return _base.startIndex
}
/// The collection's "past the end" position.
///
/// `endIndex` is not a valid argument to `subscript`, and is always
/// reachable from `startIndex` by zero or more applications of
/// `successor()`.
public var endIndex: Base.Index {
return _base.endIndex
}
/// Access the element at `position`.
///
/// - Requires: `position` is a valid position in `self` and
/// `position != endIndex`.
public subscript(position: Base.Index) -> Base.Generator.Element {
return _base[position]
}
/// Returns a collection representing a contiguous sub-range of
/// `self`'s elements.
///
/// - Complexity: O(1)
public subscript(bounds: Range<Index>) -> LazyCollection<Slice<Base>> {
return Slice(base: _base, bounds: bounds).lazy
}
/// Returns `true` iff `self` is empty.
public var isEmpty: Bool {
return _base.isEmpty
}
/// Returns the number of elements.
///
/// - Complexity: O(1) if `Index` conforms to `RandomAccessIndexType`;
/// O(N) otherwise.
public var count: Index.Distance {
return _base.count
}
// The following requirement enables dispatching for indexOf when
// the element type is Equatable.
/// Returns `Optional(Optional(index))` if an element was found;
/// `nil` otherwise.
///
/// - Complexity: O(N).
public func _customIndexOfEquatableElement(
element: Base.Generator.Element
) -> Index?? {
return _base._customIndexOfEquatableElement(element)
}
/// Returns the first element of `self`, or `nil` if `self` is empty.
public var first: Base.Generator.Element? {
return _base.first
}
@available(*, unavailable, renamed="Base")
public typealias S = Void
}
/// Augment `self` with lazy methods such as `map`, `filter`, etc.
extension CollectionType {
/// A collection with contents identical to `self`, but on which
/// normally-eager operations such as `map` and `filter` are
/// implemented lazily.
///
/// - See Also: `LazySequenceType`, `LazyCollectionType`.
public var lazy: LazyCollection<Self> {
return LazyCollection(self)
}
}
extension LazyCollectionType {
/// Identical to `self`.
public var lazy: Self { // Don't re-wrap already-lazy collections
return self
}
}
@available(*, unavailable, message="Please use the collection's '.lazy' property")
public func lazy<Base : CollectionType>(s: Base) -> LazyCollection<Base> {
fatalError("unavailable")
}
@available(*, unavailable, renamed="LazyCollection")
public struct LazyForwardCollection<T> {}
@available(*, unavailable, renamed="LazyCollection")
public struct LazyBidirectionalCollection<T> {}
@available(*, unavailable, renamed="LazyCollection")
public struct LazyRandomAccessCollection<T> {}
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End: