-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathCompacted.swift
184 lines (157 loc) · 4.82 KB
/
Compacted.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Algorithms open source project
//
// Copyright (c) 2021 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
//
//===----------------------------------------------------------------------===//
/// A `Sequence` that iterates over every non-nil element from the original
/// `Sequence`.
public struct CompactedSequence<Base: Sequence, Element>: Sequence
where Base.Element == Element? {
@usableFromInline
let base: Base
@inlinable
init(base: Base) {
self.base = base
}
public struct Iterator: IteratorProtocol {
@usableFromInline
var base: Base.Iterator
@inlinable
init(base: Base.Iterator) {
self.base = base
}
@inlinable
public mutating func next() -> Element? {
while let wrapped = base.next() {
guard let some = wrapped else { continue }
return some
}
return nil
}
}
@inlinable
public func makeIterator() -> Iterator {
Iterator(base: base.makeIterator())
}
}
extension Sequence {
/// Returns a new `Sequence` that iterates over every non-nil element from the
/// original `Sequence`.
///
/// Produces the same result as `c.compactMap { $0 }`.
///
/// let c = [1, nil, 2, 3, nil]
/// for num in c.compacted() {
/// print(num)
/// }
/// // 1
/// // 2
/// // 3
///
/// - Returns: A `Sequence` where the element is the unwrapped original
/// element and iterates over every non-nil element from the original
/// `Sequence`.
///
/// Complexity: O(1)
@inlinable
public func compacted<Unwrapped>() -> CompactedSequence<Self, Unwrapped>
where Element == Unwrapped? {
CompactedSequence(base: self)
}
}
/// A `Collection` that iterates over every non-nil element from the original
/// `Collection`.
public struct CompactedCollection<Base: Collection, Element>: Collection
where Base.Element == Element? {
@usableFromInline
let base: Base
@inlinable
init(base: Base) {
self.base = base
let idx = base.firstIndex(where: { $0 != nil }) ?? base.endIndex
self.startIndex = Index(base: idx)
}
public struct Index {
@usableFromInline
let base: Base.Index
@inlinable
init(base: Base.Index) {
self.base = base
}
}
public var startIndex: Index
@inlinable
public var endIndex: Index { Index(base: base.endIndex) }
@inlinable
public subscript(position: Index) -> Element {
base[position.base]!
}
@inlinable
public func index(after i: Index) -> Index {
precondition(i != endIndex, "Index out of bounds")
let baseIdx = base.index(after: i.base)
guard let idx = base[baseIdx...].firstIndex(where: { $0 != nil })
else { return endIndex }
return Index(base: idx)
}
}
extension CompactedCollection: BidirectionalCollection
where Base: BidirectionalCollection {
@inlinable
public func index(before i: Index) -> Index {
precondition(i != startIndex, "Index out of bounds")
guard let idx =
base[startIndex.base..<i.base]
.lastIndex(where: { $0 != nil })
else { fatalError("Index out of bounds") }
return Index(base: idx)
}
}
extension CompactedCollection.Index: Comparable {
@inlinable
public static func < (lhs: CompactedCollection.Index,
rhs: CompactedCollection.Index) -> Bool {
lhs.base < rhs.base
}
}
extension CompactedCollection.Index: Hashable
where Base.Index: Hashable {}
extension Collection {
/// Returns a new `Collection` that iterates over every non-nil element from
/// the original `Collection`.
///
/// Produces the same result as `c.compactMap { $0 }`.
///
/// let c = [1, nil, 2, 3, nil]
/// for num in c.compacted() {
/// print(num)
/// }
/// // 1
/// // 2
/// // 3
///
/// - Returns: A `Collection` where the element is the unwrapped original
/// element and iterates over every non-nil element from the original
/// `Collection`.
///
/// Complexity: O(*n*) where *n* is the number of elements in the
/// original `Collection`.
@inlinable
public func compacted<Unwrapped>() -> CompactedCollection<Self, Unwrapped>
where Element == Unwrapped?
{
CompactedCollection(base: self)
}
}
//===----------------------------------------------------------------------===//
// Protocol Conformances
//===----------------------------------------------------------------------===//
extension CompactedSequence: LazySequenceProtocol
where Base: LazySequenceProtocol {}
extension CompactedCollection: LazySequenceProtocol, LazyCollectionProtocol
where Base: LazySequenceProtocol {}