Skip to content

Commit 914e626

Browse files
Audit public functions and properties that could be inlinable (#83)
1 parent d91723b commit 914e626

File tree

9 files changed

+129
-17
lines changed

9 files changed

+129
-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/Chunked.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ extension ChunkedByCount: Equatable where Base: Equatable {}
529529
// only in terms of `base`. Since the computed index is based on it,
530530
// it should not make a difference here.
531531
extension ChunkedByCount: Hashable where Base: Hashable {
532+
@inlinable
532533
public func hash(into hasher: inout Hasher) {
533534
hasher.combine(base)
534535
}

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> {

0 commit comments

Comments
 (0)