Skip to content

Commit c769a06

Browse files
Audit public functions and properties that could be inlinable and base properties on lazy collection that could be internal
1 parent 63311ae commit c769a06

File tree

10 files changed

+150
-27
lines changed

10 files changed

+150
-27
lines changed

Sources/Algorithms/Chain.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ public struct Chain2<Base1: Sequence, Base2: Sequence>
1414
where Base1.Element == Base2.Element
1515
{
1616
/// The first sequence in this chain.
17-
public let base1: Base1
17+
@usableFromInline
18+
internal let base1: Base1
1819

1920
/// The second sequence in this chain.
20-
public let base2: Base2
21+
@usableFromInline
22+
internal let base2: Base2
2123

24+
@usableFromInline
2225
internal init(base1: Base1, base2: Base2) {
2326
self.base1 = base1
2427
self.base2 = base2

Sources/Algorithms/Combinations.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
/// A collection wrapper that generates combinations of a base collection.
1313
public struct Combinations<Base: Collection> {
1414
/// The collection to iterate over for combinations.
15-
public let base: Base
15+
@usableFromInline
16+
internal let base: Base
1617

1718
@usableFromInline
1819
internal let baseCount: Int

Sources/Algorithms/Cycle.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
/// A collection wrapper that repeats the elements of a base collection.
1313
public struct Cycle<Base: Collection> {
1414
/// The collection to repeat.
15-
public let base: Base
15+
@usableFromInline
16+
internal let base: Base
17+
18+
@usableFromInline
19+
internal init(base: Base) {
20+
self.base = base
21+
}
1622
}
1723

1824
extension Cycle: Sequence {
@@ -43,6 +49,7 @@ extension Cycle: Sequence {
4349
}
4450
}
4551

52+
@inlinable
4653
public func makeIterator() -> Iterator {
4754
Iterator(base: base)
4855
}
@@ -80,6 +87,7 @@ extension Collection {
8087
/// forever.
8188
///
8289
/// - Complexity: O(1)
90+
@inlinable
8391
public func cycled() -> Cycle<Self> {
8492
Cycle(base: self)
8593
}
@@ -101,6 +109,7 @@ extension Collection {
101109
/// times.
102110
///
103111
/// - Complexity: O(1)
112+
@inlinable
104113
public func cycled(times: Int) -> FlattenSequence<Repeated<Self>> {
105114
repeatElement(self, count: times).joined()
106115
}

Sources/Algorithms/Indexed.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public struct Indexed<Base: Collection> {
1616
public typealias Element = (index: Base.Index, element: Base.Element)
1717

1818
/// The base collection.
19-
public let base: Base
19+
@usableFromInline
20+
internal let base: Base
2021

2122
@usableFromInline
2223
internal init(base: Base) {

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
/// A sequence of all the permutations of a collection's elements.
1313
public struct Permutations<Base: Collection> {
1414
/// The base collection to iterate over for permutations.
15-
public let base: Base
15+
@usableFromInline
16+
internal let base: Base
1617

1718
@usableFromInline
1819
internal let baseCount: Int
@@ -186,6 +187,7 @@ extension Permutations: Sequence {
186187
}
187188
}
188189

190+
@inlinable
189191
public func makeIterator() -> Iterator {
190192
Iterator(self)
191193
}

Sources/Algorithms/Product.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
/// A sequence that represents the product of two sequences' elements.
1313
public struct Product2<Base1: Sequence, Base2: Collection> {
1414
/// The outer sequence in the product.
15-
public let base1: Base1
15+
@usableFromInline
16+
internal let base1: Base1
1617
/// The inner sequence in the product.
17-
public let base2: Base2
18+
@usableFromInline
19+
internal let base2: Base2
1820

21+
@usableFromInline
1922
internal init(_ base1: Base1, _ base2: Base2) {
2023
self.base1 = base1
2124
self.base2 = base2

0 commit comments

Comments
 (0)