|
| 1 | +////===--- _EitherSequence.swift - A sequence type-erasing two sequences -----===// |
| 2 | +//// |
| 3 | +//// This source file is part of the Swift.org open source project |
| 4 | +//// |
| 5 | +//// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +//// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +//// |
| 8 | +//// See https://swift.org/LICENSE.txt for license information |
| 9 | +//// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +//// |
| 11 | +////===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// Not public stdlib API, currently used in Mirror.children implementation. |
| 14 | + |
| 15 | +internal enum _Either<Left, Right> { |
| 16 | + case left(Left), right(Right) |
| 17 | +} |
| 18 | + |
| 19 | +extension _Either { |
| 20 | + internal init(_ left: Left, or other: Right.Type) { self = .left(left) } |
| 21 | + internal init(_ left: Left) { self = .left(left) } |
| 22 | + internal init(_ right: Right) { self = .right(right) } |
| 23 | +} |
| 24 | + |
| 25 | +extension _Either: Equatable where Left: Equatable, Right: Equatable { |
| 26 | + internal static func == (lhs: Self, rhs: Self) -> Bool { |
| 27 | + switch (lhs, rhs) { |
| 28 | + case let (.left(l), .left(r)): return l == r |
| 29 | + case let (.right(l), .right(r)): return l == r |
| 30 | + case (.left, .right), (.right, .left): return false |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +extension _Either: Comparable where Left: Comparable, Right: Comparable { |
| 36 | + internal static func < (lhs: Self, rhs: Self) -> Bool { |
| 37 | + switch (lhs, rhs) { |
| 38 | + case let (.left(l), .left(r)): return l < r |
| 39 | + case let (.right(l), .right(r)): return l < r |
| 40 | + case (.left, .right): return true |
| 41 | + case (.right, .left): return false |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// A sequence that type erases two sequences. A lighter-weight alternative to |
| 47 | +/// AnySequence when more can be statically known, and which is more easily |
| 48 | +/// specialized. |
| 49 | +/// |
| 50 | +/// If you only know about one of the types, the second one can be |
| 51 | +/// AnySequence, giving you a fast path for the known one. |
| 52 | +/// |
| 53 | +/// If you have 3+ types to erase, you can nest them. |
| 54 | +typealias _EitherSequence<L: Sequence, R: Sequence> = |
| 55 | + _Either<L,R> where L.Element == R.Element |
| 56 | + |
| 57 | +extension _EitherSequence { |
| 58 | + internal struct Iterator { |
| 59 | + var left: Left.Iterator? |
| 60 | + var right: Right.Iterator? |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +extension _Either.Iterator: IteratorProtocol { |
| 65 | + internal typealias Element = Left.Element |
| 66 | + |
| 67 | + internal mutating func next() -> Element? { |
| 68 | + left?.next() ?? right?.next() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +extension _EitherSequence: Sequence { |
| 73 | + internal typealias Element = Left.Element |
| 74 | + |
| 75 | + internal func makeIterator() -> Iterator { |
| 76 | + switch self { |
| 77 | + case let .left(l): |
| 78 | + return Iterator(left: l.makeIterator(), right: nil) |
| 79 | + case let .right(r): |
| 80 | + return Iterator(left: nil, right: r.makeIterator()) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +internal typealias _EitherCollection< |
| 86 | + T: Collection, U: Collection |
| 87 | +> = _EitherSequence<T,U> where T.Element == U.Element |
| 88 | + |
| 89 | +extension _EitherCollection: Collection { |
| 90 | + internal typealias Index = _Either<Left.Index, Right.Index> |
| 91 | + |
| 92 | + internal var startIndex: Index { |
| 93 | + switch self { |
| 94 | + case let .left(s): return .left(s.startIndex) |
| 95 | + case let .right(s): return .right(s.startIndex) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + internal var endIndex: Index { |
| 100 | + switch self { |
| 101 | + case let .left(s): return .left(s.endIndex) |
| 102 | + case let .right(s): return .right(s.endIndex) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + internal subscript(position: Index) -> Element { |
| 107 | + switch (self,position) { |
| 108 | + case let (.left(s),.left(i)): return s[i] |
| 109 | + case let (.right(s),.right(i)): return s[i] |
| 110 | + default: fatalError("_EitherCollecton: Sequence used with other index type") |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + internal func index(after i: Index) -> Index { |
| 115 | + switch (self,i) { |
| 116 | + case let (.left(s),.left(i)): return .left(s.index(after: i)) |
| 117 | + case let (.right(s),.right(i)): return .right(s.index(after: i)) |
| 118 | + default: fatalError("_EitherCollecton: wrong type of index used") |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + internal func index( |
| 123 | + _ i: Index, |
| 124 | + offsetBy distance: Int, |
| 125 | + limitedBy limit: Index |
| 126 | + ) -> Index? { |
| 127 | + switch (self,i,limit) { |
| 128 | + case let (.left(s),.left(i),.left(limit)): |
| 129 | + return s.index(i, offsetBy: distance, limitedBy: limit).map { .left($0) } |
| 130 | + case let (.right(s),.right(i),.right(limit)): |
| 131 | + return s.index(i, offsetBy: distance, limitedBy: limit).map { .right($0) } |
| 132 | + default: fatalError("_EitherCollecton: wrong type of index used") |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + internal func index(_ i: Index, offsetBy distance: Int) -> Index { |
| 137 | + switch (self,i) { |
| 138 | + case let (.left(s),.left(i)): return .left(s.index(i, offsetBy: distance)) |
| 139 | + case let (.right(s),.right(i)): return .right(s.index(i, offsetBy: distance)) |
| 140 | + default: fatalError("_EitherCollecton: wrong type of index used") |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + internal func distance(from start: Index, to end: Index) -> Int { |
| 145 | + switch (self,start,end) { |
| 146 | + case let (.left(s),.left(i),.left(j)): |
| 147 | + return s.distance(from: i, to: j) |
| 148 | + case let (.right(s),.right(i),.right(j)): |
| 149 | + return s.distance(from: i, to: j) |
| 150 | + default: fatalError("_EitherCollecton: wrong type of index used") |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +internal typealias _EitherBidirectionalCollection< |
| 156 | + L: BidirectionalCollection, R: BidirectionalCollection |
| 157 | +> = _Either<L,R> where L.Element == R.Element |
| 158 | + |
| 159 | +extension _EitherBidirectionalCollection: BidirectionalCollection { |
| 160 | + internal func index(before i: Index) -> Index { |
| 161 | + switch (self,i) { |
| 162 | + case let (.left(s),.left(i)): return .left(s.index(before: i)) |
| 163 | + case let (.right(s),.right(i)): return .right(s.index(before: i)) |
| 164 | + default: fatalError("_EitherCollecton: wrong type of index used") |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +internal typealias _EitherRandomAccessCollection< |
| 170 | + L: RandomAccessCollection, R: RandomAccessCollection |
| 171 | +> = _Either<L,R> where L.Element == R.Element |
| 172 | + |
| 173 | +extension _EitherRandomAccessCollection: RandomAccessCollection { } |
| 174 | + |
| 175 | +extension _Either { |
| 176 | + init<T, C: Collection>( |
| 177 | + _ collection: C |
| 178 | + ) where Right == AnyCollection<T>, C.Element == T { |
| 179 | + self = .right(AnyCollection(collection)) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +extension AnyCollection { |
| 184 | + init<L: Collection,R: Collection>( |
| 185 | + _ other: _Either<L,R> |
| 186 | + ) where L.Element == Element, R.Element == Element { |
| 187 | + // Strip away the Either and put the actual collection into the existential, |
| 188 | + // trying to use the custom initializer from another AnyCollection. |
| 189 | + switch other { |
| 190 | + case let .left(l as Self): self = .init(l) |
| 191 | + case let .right(r as Self): self = .init(r) |
| 192 | + case let .left(l): self = .init(l) |
| 193 | + case let .right(r): self = .init(r) |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | + |
0 commit comments