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
|
//===--- LazyCollection.swift ---------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
public protocol LazyCollectionProtocol: Collection, LazySequenceProtocol
where Elements: Collection {}
extension LazyCollectionProtocol {
// Lazy things are already lazy
@inlinable // protocol-only
public var lazy: LazyCollection<Elements> {
return elements.lazy
}
}
extension LazyCollectionProtocol where Elements: LazyCollectionProtocol {
// Lazy things are already lazy
@inlinable // protocol-only
public var lazy: Elements {
return elements
}
}
/// 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: `LazySequenceProtocol`, `LazyCollection`
public typealias LazyCollection<T: Collection> = LazySequence<T>
extension LazyCollection: Collection {
/// 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
public typealias Indices = Base.Indices
public typealias SubSequence = Slice<LazySequence>
/// The position of the first element in a non-empty collection.
///
/// In an empty collection, `startIndex == endIndex`.
@inlinable
public var startIndex: Index { return _base.startIndex }
/// The collection's "past the end" position---that is, the position one
/// greater than the last valid subscript argument.
///
/// `endIndex` is always reachable from `startIndex` by zero or more
/// applications of `index(after:)`.
@inlinable
public var endIndex: Index { return _base.endIndex }
@inlinable
public var indices: Indices { return _base.indices }
// TODO: swift-3-indexing-model - add docs
@inlinable
public func index(after i: Index) -> Index {
return _base.index(after: i)
}
/// Accesses the element at `position`.
///
/// - Precondition: `position` is a valid position in `self` and
/// `position != endIndex`.
@inlinable
public subscript(position: Index) -> Element {
return _base[position]
}
/// A Boolean value indicating whether the collection is empty.
@inlinable
public var isEmpty: Bool {
return _base.isEmpty
}
/// Returns the number of elements.
///
/// To check whether a collection is empty, use its `isEmpty` property
/// instead of comparing `count` to zero. Unless the collection guarantees
/// random-access performance, calculating `count` can be an O(*n*)
/// operation.
///
/// - Complexity: O(1) if `Self` conforms to `RandomAccessCollection`;
/// O(*n*) otherwise.
@inlinable
public var count: Int {
return _base.count
}
// The following requirement enables dispatching for firstIndex(of:) and
// lastIndex(of:) when the element type is Equatable.
/// Returns `Optional(Optional(index))` if an element was found;
/// `Optional(nil)` if the element doesn't exist in the collection;
/// `nil` if a search was not performed.
///
/// - Complexity: Better than O(*n*)
@inlinable
public func _customIndexOfEquatableElement(
_ element: Element
) -> Index?? {
return _base._customIndexOfEquatableElement(element)
}
/// Returns `Optional(Optional(index))` if an element was found;
/// `Optional(nil)` if the element doesn't exist in the collection;
/// `nil` if a search was not performed.
///
/// - Complexity: Better than O(*n*)
@inlinable
public func _customLastIndexOfEquatableElement(
_ element: Element
) -> Index?? {
return _base._customLastIndexOfEquatableElement(element)
}
// TODO: swift-3-indexing-model - add docs
@inlinable
public func index(_ i: Index, offsetBy n: Int) -> Index {
return _base.index(i, offsetBy: n)
}
// TODO: swift-3-indexing-model - add docs
@inlinable
public func index(
_ i: Index, offsetBy n: Int, limitedBy limit: Index
) -> Index? {
return _base.index(i, offsetBy: n, limitedBy: limit)
}
// TODO: swift-3-indexing-model - add docs
@inlinable
public func distance(from start: Index, to end: Index) -> Int {
return _base.distance(from:start, to: end)
}
}
extension LazyCollection: LazyCollectionProtocol { }
extension LazyCollection: BidirectionalCollection
where Base: BidirectionalCollection {
@inlinable
public func index(before i: Index) -> Index {
return _base.index(before: i)
}
}
extension LazyCollection: RandomAccessCollection
where Base: RandomAccessCollection {}
extension Slice: LazySequenceProtocol where Base: LazySequenceProtocol { }
extension ReversedCollection: LazySequenceProtocol where Base: LazySequenceProtocol { }
|