Skip to content

Commit fe6b6c6

Browse files
Audit public functions and properties that could be inlinable
1 parent d91723b commit fe6b6c6

File tree

8 files changed

+131
-17
lines changed

8 files changed

+131
-17
lines changed

Sources/Algorithms/Chain.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public struct Chain2<Base1: Sequence, Base2: Sequence>
1919
/// The second sequence in this chain.
2020
public let base2: Base2
2121

22+
@usableFromInline
2223
internal init(base1: Base1, base2: Base2) {
2324
self.base1 = base1
2425
self.base2 = base2
@@ -306,6 +307,7 @@ extension Chain2: Hashable where Base1: Hashable, Base2: Hashable {}
306307
/// then over the elements of `s2`.
307308
///
308309
/// - Complexity: O(1)
310+
@inlinable
309311
public func chain<S1, S2>(_ s1: S1, _ s2: S2) -> Chain2<S1, S2> {
310312
Chain2(base1: s1, base2: s2)
311313
}

Sources/Algorithms/Cycle.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
public struct Cycle<Base: Collection> {
1414
/// The collection to repeat.
1515
public let base: Base
16+
17+
@usableFromInline
18+
internal init(base: Base) {
19+
self.base = base
20+
}
1621
}
1722

1823
extension Cycle: Sequence {
@@ -43,6 +48,7 @@ extension Cycle: Sequence {
4348
}
4449
}
4550

51+
@inlinable
4652
public func makeIterator() -> Iterator {
4753
Iterator(base: base)
4854
}
@@ -80,6 +86,7 @@ extension Collection {
8086
/// forever.
8187
///
8288
/// - Complexity: O(1)
89+
@inlinable
8390
public func cycled() -> Cycle<Self> {
8491
Cycle(base: self)
8592
}
@@ -101,6 +108,7 @@ extension Collection {
101108
/// times.
102109
///
103110
/// - Complexity: O(1)
111+
@inlinable
104112
public func cycled(times: Int) -> FlattenSequence<Repeated<Self>> {
105113
repeatElement(self, count: times).joined()
106114
}

Sources/Algorithms/Intersperse.swift

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,45 @@
1212
/// A sequence that presents the elements of a base sequence of elements
1313
/// with a separator between each of those elements.
1414
public struct Intersperse<Base: Sequence> {
15-
let base: Base
16-
let separator: Base.Element
15+
@usableFromInline
16+
internal let base: Base
17+
18+
@usableFromInline
19+
internal let separator: Base.Element
20+
21+
@usableFromInline
22+
internal init(base: Base, separator: Base.Element) {
23+
self.base = base
24+
self.separator = separator
25+
}
1726
}
1827

1928
extension Intersperse: Sequence {
2029
/// The iterator for an `Intersperse` sequence.
2130
public struct Iterator: IteratorProtocol {
22-
var iterator: Base.Iterator
23-
let separator: Base.Element
24-
var state = State.start
25-
31+
@usableFromInline
32+
internal var iterator: Base.Iterator
33+
34+
@usableFromInline
35+
internal let separator: Base.Element
36+
37+
@usableFromInline
38+
internal var state = State.start
39+
40+
@usableFromInline
41+
internal init(iterator: Base.Iterator, separator: Base.Element) {
42+
self.iterator = iterator
43+
self.separator = separator
44+
}
45+
46+
@usableFromInline
2647
enum State {
2748
case start
2849
case element(Base.Element)
2950
case separator
3051
}
3152

53+
@inlinable
3254
public mutating func next() -> Base.Element? {
3355
// After the start, the state flips between element and separator. Before
3456
// returning a separator, a check is made for the next element as a
@@ -49,6 +71,7 @@ extension Intersperse: Sequence {
4971
}
5072
}
5173

74+
@inlinable
5275
public func makeIterator() -> Intersperse<Base>.Iterator {
5376
Iterator(iterator: base.makeIterator(), separator: separator)
5477
}
@@ -57,12 +80,16 @@ extension Intersperse: Sequence {
5780
extension Intersperse: Collection where Base: Collection {
5881
/// A position in an `Intersperse` collection.
5982
public struct Index: Comparable {
83+
@usableFromInline
6084
enum Representation: Equatable {
6185
case element(Base.Index)
6286
case separator(next: Base.Index)
6387
}
64-
let representation: Representation
88+
89+
@usableFromInline
90+
internal let representation: Representation
6591

92+
@inlinable
6693
public static func < (lhs: Index, rhs: Index) -> Bool {
6794
switch (lhs.representation, rhs.representation) {
6895
case let (.element(li), .element(ri)),
@@ -73,24 +100,29 @@ extension Intersperse: Collection where Base: Collection {
73100
return li <= ri
74101
}
75102
}
76-
103+
104+
@usableFromInline
77105
static func element(_ index: Base.Index) -> Self {
78106
Self(representation: .element(index))
79107
}
80108

109+
@usableFromInline
81110
static func separator(next: Base.Index) -> Self {
82111
Self(representation: .separator(next: next))
83112
}
84113
}
85114

115+
@inlinable
86116
public var startIndex: Index {
87117
base.startIndex == base.endIndex ? endIndex : .element(base.startIndex)
88118
}
89119

120+
@inlinable
90121
public var endIndex: Index {
91122
.separator(next: base.endIndex)
92123
}
93124

125+
@inlinable
94126
public func index(after i: Index) -> Index {
95127
precondition(i != endIndex, "Can't advance past endIndex")
96128
switch i.representation {
@@ -101,13 +133,15 @@ extension Intersperse: Collection where Base: Collection {
101133
}
102134
}
103135

136+
@inlinable
104137
public subscript(position: Index) -> Element {
105138
switch position.representation {
106139
case .element(let index): return base[index]
107140
case .separator: return separator
108141
}
109142
}
110-
143+
144+
@inlinable
111145
public func index(_ i: Index, offsetBy distance: Int) -> Index {
112146
switch (i.representation, distance.isMultiple(of: 2)) {
113147
case (let .element(index), true):
@@ -122,7 +156,7 @@ extension Intersperse: Collection where Base: Collection {
122156
}
123157

124158
// TODO: Implement index(_:offsetBy:limitedBy:)
125-
159+
@inlinable
126160
public func distance(from start: Index, to end: Index) -> Int {
127161
switch (start.representation, end.representation) {
128162
case let (.element(element), .separator(next: separator)):
@@ -139,6 +173,7 @@ extension Intersperse: Collection where Base: Collection {
139173
extension Intersperse: BidirectionalCollection
140174
where Base: BidirectionalCollection
141175
{
176+
@inlinable
142177
public func index(before i: Index) -> Index {
143178
precondition(i != startIndex, "Can't move before startIndex")
144179
switch i.representation {
@@ -189,6 +224,7 @@ extension Sequence {
189224
/// - Returns: The interspersed sequence of elements.
190225
///
191226
/// - Complexity: O(1)
227+
@inlinable
192228
public func interspersed(with separator: Element) -> Intersperse<Self> {
193229
Intersperse(base: self, separator: separator)
194230
}

Sources/Algorithms/Permutations.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ extension Permutations: Sequence {
186186
}
187187
}
188188

189+
@inlinable
189190
public func makeIterator() -> Iterator {
190191
Iterator(self)
191192
}

Sources/Algorithms/Product.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public struct Product2<Base1: Sequence, Base2: Collection> {
1616
/// The inner sequence in the product.
1717
public let base2: Base2
1818

19+
@usableFromInline
1920
internal init(_ base1: Base1, _ base2: Base2) {
2021
self.base1 = base1
2122
self.base2 = base2
@@ -470,6 +471,7 @@ extension Product2: Hashable where Base1: Hashable, Base2: Hashable {}
470471
/// - s2: The second sequence to iterate over.
471472
///
472473
/// - Complexity: O(1)
474+
@inlinable
473475
public func product<Base1: Sequence, Base2: Collection>(
474476
_ s1: Base1, _ s2: Base2
475477
) -> Product2<Base1, Base2> {

Sources/Algorithms/Stride.swift

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,21 @@ extension Sequence {
2727
/// - Parameter step: The amount to step with each iteration.
2828
/// - Returns: Returns a sequence or collection for stepping through the
2929
/// elements by the specified amount.
30+
@inlinable
3031
public func striding(by step: Int) -> Stride<Self> {
3132
Stride(base: self, stride: step)
3233
}
3334
}
3435

3536
/// A wrapper that strides over a base sequence or collection.
3637
public struct Stride<Base: Sequence> {
38+
@usableFromInline
3739
internal let base: Base
40+
41+
@usableFromInline
3842
internal let stride: Int
3943

44+
@usableFromInline
4045
internal init(base: Base, stride: Int) {
4146
precondition(stride > 0, "striding must be greater than zero")
4247
self.base = base
@@ -45,6 +50,7 @@ public struct Stride<Base: Sequence> {
4550
}
4651

4752
extension Stride {
53+
@inlinable
4854
public func striding(by step: Int) -> Self {
4955
Stride(base: base, stride: stride * step)
5056
}
@@ -53,10 +59,22 @@ extension Stride {
5359
extension Stride: Sequence {
5460
/// An iterator over a `Stride` sequence.
5561
public struct Iterator: IteratorProtocol {
62+
@usableFromInline
5663
internal var iterator: Base.Iterator
64+
65+
@usableFromInline
5766
internal let stride: Int
67+
68+
@usableFromInline
5869
internal var striding: Bool = false
5970

71+
@usableFromInline
72+
internal init(iterator: Base.Iterator, stride: Int) {
73+
self.iterator = iterator
74+
self.stride = stride
75+
}
76+
77+
@inlinable
6078
public mutating func next() -> Base.Element? {
6179
guard striding else {
6280
striding = true
@@ -69,6 +87,7 @@ extension Stride: Sequence {
6987
}
7088
}
7189

90+
@inlinable
7291
public func makeIterator() -> Stride<Base>.Iterator {
7392
Iterator(iterator: base.makeIterator(), stride: stride)
7493
}
@@ -77,34 +96,42 @@ extension Stride: Sequence {
7796
extension Stride: Collection where Base: Collection {
7897
/// A position in a `Stride` collection.
7998
public struct Index: Comparable {
99+
@usableFromInline
80100
internal let base: Base.Index
81101

102+
@usableFromInline
82103
internal init(_ base: Base.Index) {
83104
self.base = base
84105
}
85106

107+
@inlinable
86108
public static func < (lhs: Index, rhs: Index) -> Bool {
87109
lhs.base < rhs.base
88110
}
89111
}
90112

113+
@inlinable
91114
public var startIndex: Index {
92115
Index(base.startIndex)
93116
}
94117

118+
@inlinable
95119
public var endIndex: Index {
96120
Index(base.endIndex)
97121
}
98122

123+
@inlinable
99124
public subscript(i: Index) -> Base.Element {
100125
base[i.base]
101126
}
102127

128+
@inlinable
103129
public func index(after i: Index) -> Index {
104130
precondition(i.base < base.endIndex, "Advancing past end index")
105131
return index(i, offsetBy: 1)
106132
}
107133

134+
@inlinable
108135
public func index(
109136
_ i: Index,
110137
offsetBy n: Int,
@@ -118,7 +145,8 @@ extension Stride: Collection where Base: Collection {
118145
: offsetBackward(i, offsetBy: -n, limitedBy: limit)
119146
}
120147

121-
private func offsetForward(
148+
@usableFromInline
149+
internal func offsetForward(
122150
_ i: Index,
123151
offsetBy n: Int,
124152
limitedBy limit: Index
@@ -147,7 +175,8 @@ extension Stride: Collection where Base: Collection {
147175
}
148176
}
149177

150-
private func offsetBackward(
178+
@usableFromInline
179+
internal func offsetBackward(
151180
_ i: Index,
152181
offsetBy n: Int,
153182
limitedBy limit: Index
@@ -162,15 +191,18 @@ extension Stride: Collection where Base: Collection {
162191
).map(Index.init)
163192
}
164193

194+
@inlinable
165195
public var count: Int {
166196
base.isEmpty ? 0 : (base.count - 1) / stride + 1
167197
}
168198

199+
@inlinable
169200
public func distance(from start: Index, to end: Index) -> Int {
170201
let distance = base.distance(from: start.base, to: end.base)
171202
return distance / stride + (distance % stride).signum()
172203
}
173204

205+
@inlinable
174206
public func index(_ i: Index, offsetBy distance: Int) -> Index {
175207
precondition(distance <= 0 || i.base < base.endIndex, "Advancing past end index")
176208
precondition(distance >= 0 || i.base > base.startIndex, "Incrementing past start index")
@@ -183,7 +215,8 @@ extension Stride: Collection where Base: Collection {
183215

184216
extension Stride: BidirectionalCollection
185217
where Base: RandomAccessCollection {
186-
218+
219+
@inlinable
187220
public func index(before i: Index) -> Index {
188221
precondition(i.base > base.startIndex, "Incrementing past start index")
189222
return index(i, offsetBy: -1)
@@ -193,12 +226,14 @@ extension Stride: BidirectionalCollection
193226
extension Stride: RandomAccessCollection where Base: RandomAccessCollection {}
194227

195228
extension Stride: Equatable where Base.Element: Equatable {
229+
@inlinable
196230
public static func == (lhs: Stride, rhs: Stride) -> Bool {
197231
lhs.elementsEqual(rhs, by: ==)
198232
}
199233
}
200234

201235
extension Stride: Hashable where Base.Element: Hashable {
236+
@inlinable
202237
public func hash(into hasher: inout Hasher) {
203238
hasher.combine(stride)
204239
for element in self {

0 commit comments

Comments
 (0)