Skip to content

Commit 8ca636d

Browse files
committed
Add inlinability annotations throughout
1 parent 115abe1 commit 8ca636d

File tree

11 files changed

+156
-21
lines changed

11 files changed

+156
-21
lines changed

Sources/Algorithms/Chain.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,25 @@ public struct Chain<Base1: Sequence, Base2: Sequence>
2828
extension Chain: Sequence {
2929
/// The iterator for a `Chain` sequence.
3030
public struct Iterator: IteratorProtocol {
31+
@usableFromInline
3132
internal var iterator1: Base1.Iterator
33+
34+
@usableFromInline
3235
internal var iterator2: Base2.Iterator
3336

37+
@usableFromInline
3438
internal init(_ concatenation: Chain) {
3539
iterator1 = concatenation.base1.makeIterator()
3640
iterator2 = concatenation.base2.makeIterator()
3741
}
3842

43+
@inlinable
3944
public mutating func next() -> Base1.Element? {
4045
return iterator1.next() ?? iterator2.next()
4146
}
4247
}
4348

49+
@inlinable
4450
public func makeIterator() -> Iterator {
4551
Iterator(self)
4652
}
@@ -54,23 +60,28 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
5460
// is not to be used as a value - iterating over indices should go straight
5561
// from the penultimate index of the first collection to the start of the
5662
// second.
63+
@usableFromInline
5764
internal enum Representation : Equatable {
5865
case first(Base1.Index)
5966
case second(Base2.Index)
6067
}
6168

69+
@usableFromInline
6270
internal let position: Representation
6371

6472
/// Creates a new index into the first underlying collection.
73+
@usableFromInline
6574
internal init(first i: Base1.Index) {
6675
position = .first(i)
6776
}
6877

6978
/// Creates a new index into the second underlying collection.
79+
@usableFromInline
7080
internal init(second i: Base2.Index) {
7181
position = .second(i)
7282
}
7383

84+
@inlinable
7485
public static func < (lhs: Index, rhs: Index) -> Bool {
7586
switch (lhs.position, rhs.position) {
7687
case (.first, .second):
@@ -87,20 +98,24 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
8798

8899
/// Converts an index of `Base1` to the corresponding `Index` by mapping
89100
/// `base1.endIndex` to `base2.startIndex`.
101+
@usableFromInline
90102
internal func convertIndex(_ i: Base1.Index) -> Index {
91103
i == base1.endIndex ? Index(second: base2.startIndex) : Index(first: i)
92104
}
93105

106+
@inlinable
94107
public var startIndex: Index {
95108
// if `base1` is empty, this will return `base2.startIndex` - if `base2` is
96109
// also empty, this will correctly equal `base2.endIndex`
97110
convertIndex(base1.startIndex)
98111
}
99112

113+
@inlinable
100114
public var endIndex: Index {
101115
return Index(second: base2.endIndex)
102116
}
103117

118+
@inlinable
104119
public subscript(i: Index) -> Base1.Element {
105120
switch i.position {
106121
case let .first(i):
@@ -110,6 +125,7 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
110125
}
111126
}
112127

128+
@inlinable
113129
public func index(after i: Index) -> Index {
114130
switch i.position {
115131
case let .first(i):
@@ -120,13 +136,15 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
120136
}
121137
}
122138

139+
@inlinable
123140
public func index(_ i: Index, offsetBy n: Int) -> Index {
124141
if n == 0 { return i }
125142
return n > 0
126143
? offsetForward(i, by: n, limitedBy: endIndex)!
127144
: offsetBackward(i, by: -n, limitedBy: startIndex)!
128145
}
129146

147+
@inlinable
130148
public func index(
131149
_ i: Index,
132150
offsetBy n: Int,
@@ -138,7 +156,8 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
138156
: offsetBackward(i, by: -n, limitedBy: limit)
139157
}
140158

141-
private func offsetForward(
159+
@usableFromInline
160+
internal func offsetForward(
142161
_ i: Index, by n: Int, limitedBy limit: Index
143162
) -> Index? {
144163
switch (i.position, limit.position) {
@@ -177,7 +196,8 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
177196
}
178197
}
179198

180-
private func offsetBackward(
199+
@usableFromInline
200+
internal func offsetBackward(
181201
_ i: Index, by n: Int, limitedBy limit: Index
182202
) -> Index? {
183203
switch (i.position, limit.position) {
@@ -216,6 +236,7 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
216236
}
217237
}
218238

239+
@inlinable
219240
public func distance(from start: Index, to end: Index) -> Int {
220241
switch (start.position, end.position) {
221242
case let (.first(i), .first(j)):
@@ -235,6 +256,7 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
235256
extension Chain: BidirectionalCollection
236257
where Base1: BidirectionalCollection, Base2: BidirectionalCollection
237258
{
259+
@inlinable
238260
public func index(before i: Index) -> Index {
239261
assert(i != startIndex, "Can't advance before startIndex")
240262
switch i.position {

Sources/Algorithms/Chunked.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,38 @@ public struct LazyChunked<Base: Collection> {
1414
public let base: Base
1515

1616
/// The projection function.
17+
@usableFromInline
1718
internal var belongInSameGroup: (Base.Element, Base.Element) -> Bool
19+
20+
@usableFromInline
21+
internal init(base: Base, belongInSameGroup: @escaping (Base.Element, Base.Element) -> Bool) {
22+
self.base = base
23+
self.belongInSameGroup = belongInSameGroup
24+
}
1825
}
1926

2027
extension LazyChunked: LazyCollectionProtocol {
2128
/// A position in a chunked collection.
2229
public struct Index: Comparable {
2330
/// The lower bound of the chunk at this position.
31+
@usableFromInline
2432
internal var lowerBound: Base.Index
2533

2634
/// The upper bound of the chunk at this position.
2735
///
2836
/// `upperBound` is optional so that computing `startIndex` can be an O(1)
2937
/// operation. When `upperBound` is `nil`, the actual upper bound is found
3038
/// when subscripting or calling `index(after:)`.
39+
@usableFromInline
3140
internal var upperBound: Base.Index?
3241

42+
@usableFromInline
43+
internal init(lowerBound: Base.Index, upperBound: Base.Index? = nil) {
44+
self.lowerBound = lowerBound
45+
self.upperBound = upperBound
46+
}
47+
48+
@inlinable
3349
public static func == (lhs: Index, rhs: Index) -> Bool {
3450
// Only use the lower bound to test for equality, since sometimes the
3551
// `startIndex` will have an upper bound of `nil` and sometimes it won't,
@@ -41,6 +57,7 @@ extension LazyChunked: LazyCollectionProtocol {
4157
lhs.lowerBound == rhs.lowerBound
4258
}
4359

60+
@inlinable
4461
public static func < (lhs: Index, rhs: Index) -> Bool {
4562
// Only use the lower bound to test for ordering, as above.
4663
lhs.lowerBound < rhs.lowerBound
@@ -49,26 +66,31 @@ extension LazyChunked: LazyCollectionProtocol {
4966

5067
/// Returns the index in the base collection for the first element that
5168
/// doesn't match the current chunk.
69+
@usableFromInline
5270
internal func endOfChunk(from i: Base.Index) -> Base.Index {
5371
guard i != base.endIndex else { return base.endIndex }
5472
return base[base.index(after: i)...]
5573
.firstIndex(where: { !belongInSameGroup($0, base[i]) }) ?? base.endIndex
5674
}
5775

76+
@inlinable
5877
public var startIndex: Index {
5978
Index(lowerBound: base.startIndex)
6079
}
6180

81+
@inlinable
6282
public var endIndex: Index {
6383
Index(lowerBound: base.endIndex, upperBound: base.endIndex)
6484
}
6585

86+
@inlinable
6687
public func index(after i: Index) -> Index {
6788
let upperBound = i.upperBound ?? endOfChunk(from: i.lowerBound)
6889
let end = endOfChunk(from: upperBound)
6990
return Index(lowerBound: upperBound, upperBound: end)
7091
}
7192

93+
@inlinable
7294
public subscript(position: Index) -> Base.SubSequence {
7395
let upperBound = position.upperBound
7496
?? endOfChunk(from: position.lowerBound)
@@ -83,6 +105,7 @@ extension LazyChunked: BidirectionalCollection
83105
{
84106
/// Returns the index in the base collection for the element that starts
85107
/// the chunk ending at the given index.
108+
@usableFromInline
86109
internal func startOfChunk(endingAt end: Base.Index) -> Base.Index {
87110
// Get the projected value of the last element in the range ending at `end`.
88111
let lastOfPreviousChunk = base[base.index(before: end)]
@@ -102,6 +125,7 @@ extension LazyChunked: BidirectionalCollection
102125
}
103126
}
104127

128+
@inlinable
105129
public func index(before i: Index) -> Index {
106130
let start = startOfChunk(endingAt: i.lowerBound)
107131
return Index(lowerBound: start, upperBound: i.lowerBound)
@@ -119,6 +143,7 @@ extension LazyCollectionProtocol {
119143
/// - Complexity: O(1). When iterating over the resulting collection,
120144
/// accessing each successive chunk has a complexity of O(*m*), where *m*
121145
/// is the length of the chunk.
146+
@inlinable
122147
public func chunked(
123148
by belongInSameGroup: @escaping (Element, Element) -> Bool
124149
) -> LazyChunked<Elements> {
@@ -131,6 +156,7 @@ extension LazyCollectionProtocol {
131156
/// - Complexity: O(1). When iterating over the resulting collection,
132157
/// accessing each successive chunk has a complexity of O(*m*), where *m*
133158
/// is the length of the chunk.
159+
@inlinable
134160
public func chunked<Subject: Equatable>(
135161
on projection: @escaping (Element) -> Subject
136162
) -> LazyChunked<Elements> {
@@ -149,6 +175,7 @@ extension Collection {
149175
/// the given predicate.
150176
///
151177
/// - Complexity: O(*n*), where *n* is the length of this collection.
178+
@inlinable
152179
public func chunked(
153180
by belongInSameGroup: (Element, Element) -> Bool
154181
) -> [SubSequence] {
@@ -179,6 +206,7 @@ extension Collection {
179206
/// grouping elements that project to the same value.
180207
///
181208
/// - Complexity: O(*n*), where *n* is the length of this collection.
209+
@inlinable
182210
public func chunked<Subject: Equatable>(
183211
on projection: (Element) -> Subject
184212
) -> [SubSequence] {

Sources/Algorithms/Combinations.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
public struct Combinations<Base: Collection> {
1313
/// The collection to iterate over for combinations.
1414
public let base: Base
15+
16+
@usableFromInline
1517
internal var k: Int
1618

19+
@usableFromInline
1720
internal init(_ base: Base, k: Int) {
1821
self.base = base
1922
self.k = base.count < k ? -1 : k
2023
}
2124

25+
@inlinable
2226
public var count: Int {
2327
func binomial(n: Int, k: Int) -> Int {
2428
switch k {
@@ -37,8 +41,13 @@ public struct Combinations<Base: Collection> {
3741

3842
extension Combinations: Sequence {
3943
public struct Iterator: IteratorProtocol {
44+
@usableFromInline
4045
internal let base: Base
46+
47+
@usableFromInline
4148
internal var indexes: [Base.Index]
49+
50+
@usableFromInline
4251
internal var finished: Bool
4352

4453
internal init(_ combinations: Combinations) {
@@ -67,6 +76,7 @@ extension Combinations: Sequence {
6776
/// [2, 3, 4] *
6877
/// // Can't advance without needing to go past `base.endIndex`,
6978
/// // so the iteration is finished.
79+
@usableFromInline
7080
internal mutating func advance() {
7181
guard !indexes.isEmpty else {
7282
// Initial state for combinations of 0 elements is an empty array with
@@ -99,6 +109,7 @@ extension Combinations: Sequence {
99109
}
100110
}
101111

112+
@inlinable
102113
public mutating func next() -> [Base.Element]? {
103114
if finished { return nil }
104115
defer { advance() }
@@ -146,6 +157,7 @@ extension Collection {
146157
/// - Parameter k: The number of elements to include in each combination.
147158
///
148159
/// - Complexity: O(1)
160+
@inlinable
149161
public func combinations(ofCount k: Int) -> Combinations<Self> {
150162
assert(k >= 0, "Can't have combinations with a negative number of elements.")
151163
return Combinations(self, k: k)

Sources/Algorithms/Cycle.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@ public struct Cycle<Base: Collection> {
1818
extension Cycle: Sequence {
1919
/// The iterator for a `Cycle` sequence.
2020
public struct Iterator: IteratorProtocol {
21+
@usableFromInline
2122
let base: Base
23+
24+
@usableFromInline
2225
var current: Base.Index
2326

27+
@usableFromInline
28+
internal init(base: Base) {
29+
self.base = base
30+
self.current = base.startIndex
31+
}
32+
33+
@inlinable
2434
public mutating func next() -> Base.Element? {
2535
guard !base.isEmpty else { return nil }
2636

@@ -34,7 +44,7 @@ extension Cycle: Sequence {
3444
}
3545

3646
public func makeIterator() -> Iterator {
37-
Iterator(base: base, current: base.startIndex)
47+
Iterator(base: base)
3848
}
3949
}
4050

0 commit comments

Comments
 (0)