Files
swift-mirror/stdlib/public/core/LazyCollection.swift.gyb
Dave Abrahams 9f59c9d52d [stdlib] More lazy doc comment updates
Swift SVN r30359
2015-07-18 02:58:34 +00:00

242 lines
7.1 KiB
Swift

//===--- 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
/// `LazyColectionType`s.
///
/// - See Also: `LazySequenceType`, `LazyCollection`
public protocol _prext_LazyCollectionType
: CollectionType, _prext_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 _prext_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 _prext_LazyCollection<Base_ : CollectionType>
: _prext_LazyCollectionType {
/// The type of the underlying collection
public typealias Base = Base_
public typealias Index = Base.Index
/// Construct an instance with `base` as its underlying Collection
/// instance.
public init(_ base: Base) {
self._base = base
}
public typealias Elements = Base
public var elements: Base { return _base }
internal var _base: Base
}
extension _prext_LazyCollection : SequenceType {
public func generate() -> Base.Generator { return _base.generate() }
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 _prext_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>) -> _prext_LazyCollection<Slice<Base>> {
return Slice(base: _base, bounds: bounds)._prext_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
}
}
/// Augment `self` with lazy methods such as `map`, `filter`, etc.
extension CollectionType {
public var _prext_lazy: _prext_LazyCollection<Self> {
return _prext_LazyCollection(self)
}
}
extension _prext_LazyCollectionType {
public var _prext_lazy: Self { // Don't re-wrap already-lazy collections
return self
}
}
//===----------------------------------------------------------------------===//
// Implementations we are replacing
//===----------------------------------------------------------------------===//
%for traversal in [ 'Forward', 'Bidirectional', 'RandomAccess' ]:
% whereClause = 'where Base.Index : %sIndexType' % traversal
% Self = 'Lazy%sCollection' % traversal
/// A collection that forwards its implementation to an underlying
/// collection instance while exposing lazy computations as methods.
public struct ${Self}<Base : CollectionType ${whereClause}> : CollectionType {
@available(*, unavailable, renamed="Base")
public typealias S = Base
/// Construct an instance with `base` as its underlying collection
/// instance.
public init(_ base: Base) {
self._base = base
}
/// Return a *generator* over the elements of this *sequence*.
///
/// - Complexity: O(1).
public func generate() -> Base.Generator {
return self._base.generate()
}
/// 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]
}
/// an Array, created on-demand, containing the elements of this
/// lazy CollectionType.
public var array: [Base.Generator.Element] {
return Array(_base)
}
public func underestimateCount() -> Int {
return _base.underestimateCount()
}
var _base: Base
}
/// Augment `s` with lazy methods such as `map`, `filter`, etc.
public func lazy<Base : CollectionType ${whereClause}>(s: Base)
-> ${Self}<Base> {
return ${Self}(s)
}
%end
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End: