|
| 1 | +//===--- Filter.swift - Lazily filter the elements of a Sequence ----------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +/// The `Generator` used by `FilterSequenceView` and |
| 14 | +/// `FilterCollectionView` |
| 15 | +struct FilterGenerator<Base: Generator> : Generator, Sequence { |
| 16 | + mutating func next() -> Base.Element? { |
| 17 | + var n: Base.Element? |
| 18 | + for/*ever*/;; { |
| 19 | + n = _base.next() |
| 20 | + if n ? _include(n!) : true { |
| 21 | + return n |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + func generate() -> FilterGenerator { |
| 27 | + return self |
| 28 | + } |
| 29 | + |
| 30 | + var _base: Base |
| 31 | + var _include: (Base.Element)->Bool |
| 32 | +} |
| 33 | + |
| 34 | +/// The lazy `Sequence` returned by `filter(c)` where `c` is a |
| 35 | +/// `Sequence` |
| 36 | +struct FilterSequenceView<Base: Sequence> : Sequence { |
| 37 | + func generate() -> FilterGenerator<Base.GeneratorType> { |
| 38 | + return FilterGenerator(_base: _base.generate(), _include: _include) |
| 39 | + } |
| 40 | + |
| 41 | + var _base: Base |
| 42 | + var _include: (Base.GeneratorType.Element)->Bool |
| 43 | +} |
| 44 | + |
| 45 | +/// The `IndexType` used for subscripting a `FilterCollectionView` |
| 46 | +struct FilterCollectionViewIndex<Base: Collection> : ForwardIndex { |
| 47 | + func succ() -> FilterCollectionViewIndex { |
| 48 | + for nextPos in _pos.succ().._end { |
| 49 | + if _include(_base[nextPos]) { |
| 50 | + return FilterCollectionViewIndex( |
| 51 | + _pos: nextPos, _end: _end, |
| 52 | + _base: _base, _include: _include) |
| 53 | + } |
| 54 | + } |
| 55 | + return FilterCollectionViewIndex( |
| 56 | + _pos: _end, _end: _end, _base: _base, _include: _include) |
| 57 | + } |
| 58 | + var _pos: Base.IndexType |
| 59 | + var _end: Base.IndexType |
| 60 | + var _base: Base |
| 61 | + var _include: (Base.GeneratorType.Element)->Bool |
| 62 | +} |
| 63 | + |
| 64 | +func == <Base: Collection>( |
| 65 | + lhs: FilterCollectionViewIndex<Base>, |
| 66 | + rhs: FilterCollectionViewIndex<Base> |
| 67 | +) -> Bool { |
| 68 | + return lhs._pos == rhs._pos |
| 69 | +} |
| 70 | + |
| 71 | +/// The lazy `Collection` returned by `filter(c)` where `c` is a |
| 72 | +/// `Collection` |
| 73 | +struct FilterCollectionView<Base: Collection> : Collection { |
| 74 | + |
| 75 | + typealias IndexType = FilterCollectionViewIndex<Base> |
| 76 | + var startIndex: IndexType { |
| 77 | + var first = _base.startIndex |
| 78 | + while first != _base.endIndex { |
| 79 | + if _include(_base[first]) { |
| 80 | + break |
| 81 | + } |
| 82 | + ++first |
| 83 | + } |
| 84 | + return FilterCollectionViewIndex( |
| 85 | + _pos: first, _end: _base.endIndex, _base: _base, _include: _include) |
| 86 | + } |
| 87 | + |
| 88 | + var endIndex: IndexType { |
| 89 | + return FilterCollectionViewIndex( |
| 90 | + _pos: _base.endIndex, _end: _base.endIndex, |
| 91 | + _base: _base, _include: _include) |
| 92 | + } |
| 93 | + |
| 94 | + subscript(index: IndexType) -> Base.GeneratorType.Element { |
| 95 | + return _base[index._pos] |
| 96 | + } |
| 97 | + |
| 98 | + func generate() -> FilterGenerator<Base.GeneratorType> { |
| 99 | + return FilterGenerator(_base: _base.generate(), _include: _include) |
| 100 | + } |
| 101 | + |
| 102 | + var _base: Base |
| 103 | + var _include: (Base.GeneratorType.Element)->Bool |
| 104 | +} |
| 105 | + |
| 106 | +/// Return a lazy Sequence containing the elements `x` of `source` for |
| 107 | +/// which `includeElement(x)` is `true` |
| 108 | +func filter<S:Sequence>( |
| 109 | + source: S, includeElement: (S.GeneratorType.Element)->Bool |
| 110 | +) -> FilterSequenceView<S> { |
| 111 | + return FilterSequenceView(_base: source, _include: includeElement) |
| 112 | +} |
| 113 | + |
| 114 | +/// Return a lazy Collection containing the elements `x` of `source` for |
| 115 | +/// which `includeElement(x)` is `true` |
| 116 | +func filter<C:Collection>( |
| 117 | + source: C, includeElement: (C.GeneratorType.Element)->Bool |
| 118 | +) -> FilterCollectionView<C> { |
| 119 | + return FilterCollectionView(_base: source, _include: includeElement) |
| 120 | +} |
0 commit comments