Skip to content

Inline all the things! 🎨 #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Sources/Algorithms/Chain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct Chain2<Base1: Sequence, Base2: Sequence>
/// The second sequence in this chain.
public let base2: Base2

@usableFromInline
@inlinable
internal init(base1: Base1, base2: Base2) {
self.base1 = base1
self.base2 = base2
Expand All @@ -35,7 +35,7 @@ extension Chain2: Sequence {
@usableFromInline
internal var iterator2: Base2.Iterator

@usableFromInline
@inlinable
internal init(_ concatenation: Chain2) {
iterator1 = concatenation.base1.makeIterator()
iterator2 = concatenation.base2.makeIterator()
Expand Down Expand Up @@ -71,13 +71,13 @@ extension Chain2: Collection where Base1: Collection, Base2: Collection {
internal let position: Representation

/// Creates a new index into the first underlying collection.
@usableFromInline
@inlinable
internal init(first i: Base1.Index) {
position = .first(i)
}

/// Creates a new index into the second underlying collection.
@usableFromInline
@inlinable
internal init(second i: Base2.Index) {
position = .second(i)
}
Expand All @@ -99,7 +99,7 @@ extension Chain2: Collection where Base1: Collection, Base2: Collection {

/// Converts an index of `Base1` to the corresponding `Index` by mapping
/// `base1.endIndex` to `base2.startIndex`.
@usableFromInline
@inlinable
internal func convertIndex(_ i: Base1.Index) -> Index {
i == base1.endIndex ? Index(second: base2.startIndex) : Index(first: i)
}
Expand Down Expand Up @@ -157,7 +157,7 @@ extension Chain2: Collection where Base1: Collection, Base2: Collection {
: offsetBackward(i, by: -n, limitedBy: limit)
}

@usableFromInline
@inlinable
internal func offsetForward(
_ i: Index, by n: Int, limitedBy limit: Index
) -> Index? {
Expand Down Expand Up @@ -197,7 +197,7 @@ extension Chain2: Collection where Base1: Collection, Base2: Collection {
}
}

@usableFromInline
@inlinable
internal func offsetBackward(
_ i: Index, by n: Int, limitedBy limit: Index
) -> Index? {
Expand Down
25 changes: 13 additions & 12 deletions Sources/Algorithms/Chunked.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct LazyChunked<Base: Collection, Subject> {
@usableFromInline
internal var firstUpperBound: Base.Index

@usableFromInline
@inlinable
internal init(
base: Base,
projection: @escaping (Base.Element) -> Subject,
Expand All @@ -51,7 +51,7 @@ extension LazyChunked: LazyCollectionProtocol {
@usableFromInline
internal var baseRange: Range<Base.Index>

@usableFromInline
@inlinable
internal init(_ baseRange: Range<Base.Index>) {
self.baseRange = baseRange
}
Expand All @@ -72,7 +72,7 @@ extension LazyChunked: LazyCollectionProtocol {

/// Returns the index in the base collection of the end of the chunk starting
/// at the given index.
@usableFromInline
@inlinable
internal func endOfChunk(startingAt start: Base.Index) -> Base.Index {
let subject = projection(base[start])
return base[base.index(after: start)...]
Expand Down Expand Up @@ -112,7 +112,7 @@ extension LazyChunked: BidirectionalCollection
{
/// Returns the index in the base collection of the start of the chunk ending
/// at the given index.
@usableFromInline
@inlinable
internal func startOfChunk(endingAt end: Base.Index) -> Base.Index {
let indexBeforeEnd = base.index(before: end)

Expand Down Expand Up @@ -174,7 +174,7 @@ extension Collection {
/// predicate.
///
/// - Complexity: O(*n*), where *n* is the length of this collection.
@usableFromInline
@inlinable
internal func chunked<Subject>(
on projection: (Element) throws -> Subject,
by belongInSameGroup: (Subject, Subject) throws -> Bool
Expand Down Expand Up @@ -273,7 +273,7 @@ extension ChunkedByCount: Collection {
@usableFromInline
internal let baseRange: Range<Base.Index>

@usableFromInline
@inlinable
internal init(_baseRange: Range<Base.Index>) {
self.baseRange = _baseRange
}
Expand All @@ -290,6 +290,7 @@ extension ChunkedByCount: Collection {
}

/// - Complexity: O(1)
@inlinable
public subscript(i: Index) -> Element {
precondition(i < endIndex, "Index out of range")
return base[i.baseRange]
Expand Down Expand Up @@ -392,7 +393,7 @@ extension ChunkedByCount {
return index
}

@usableFromInline
@inlinable
internal func offsetForward(
_ i: Index, offsetBy distance: Int, limit: Index? = nil
) -> Index? {
Expand All @@ -406,8 +407,8 @@ extension ChunkedByCount {
}

// Convenience to compute offset backward base distance.
@inline(__always)
private func computeOffsetBackwardBaseDistance(
@inlinable
internal func computeOffsetBackwardBaseDistance(
_ i: Index, _ distance: Int
) -> Int {
if i == endIndex {
Expand All @@ -423,7 +424,7 @@ extension ChunkedByCount {
return distance * chunkCount
}

@usableFromInline
@inlinable
internal func offsetBackward(
_ i: Index, offsetBy distance: Int, limit: Index? = nil
) -> Index? {
Expand All @@ -438,8 +439,8 @@ extension ChunkedByCount {
}

// Helper to compute index(offsetBy:) index.
@inline(__always)
private func makeOffsetIndex(
@inlinable
internal func makeOffsetIndex(
from i: Index, baseBound: Base.Index, distance: Int, baseDistance: Int,
limit: Index?, by limitFn: (Base.Index, Base.Index) -> Bool
) -> Index? {
Expand Down
9 changes: 5 additions & 4 deletions Sources/Algorithms/Combinations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct Combinations<Base: Collection> {
/// - Parameters:
/// - base: The collection to iterate over for combinations.
/// - k: The expected size of each combination.
@usableFromInline
@inlinable
internal init(_ base: Base, k: Int) {
self.init(base, kRange: k...k)
}
Expand All @@ -37,7 +37,7 @@ public struct Combinations<Base: Collection> {
/// - Parameters:
/// - base: The collection to iterate over for combinations.
/// - kRange: The range of accepted sizes of combinations.
@usableFromInline
@inlinable
internal init<R: RangeExpression>(
_ base: Base, kRange: R
) where R.Bound == Int {
Expand Down Expand Up @@ -89,14 +89,15 @@ extension Combinations: Sequence {
internal var kRange: Range<Int>

/// Whether or not iteration is finished (`kRange` is empty)
@usableFromInline
@inlinable
internal var isFinished: Bool {
return kRange.isEmpty
}

@usableFromInline
internal var indexes: [Base.Index]

@inlinable
internal init(_ combinations: Combinations) {
self.base = combinations.base
self.kRange = combinations.kRange ?? 0..<0
Expand All @@ -121,7 +122,7 @@ extension Combinations: Sequence {
/// [2, 3, 4] *
/// // Can't advance without needing to go past `base.endIndex`,
/// // so the iteration is finished.
@usableFromInline
@inlinable
internal mutating func advance() {
/// Advances `kRange` by incrementing its `lowerBound` until the range is
/// empty, when iteration is finished.
Expand Down
4 changes: 2 additions & 2 deletions Sources/Algorithms/Cycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public struct Cycle<Base: Collection> {
/// The collection to repeat.
public let base: Base

@usableFromInline
@inlinable
internal init(base: Base) {
self.base = base
}
Expand All @@ -29,7 +29,7 @@ extension Cycle: Sequence {
@usableFromInline
var current: Base.Index

@usableFromInline
@inlinable
internal init(base: Base) {
self.base = base
self.current = base.startIndex
Expand Down
2 changes: 1 addition & 1 deletion Sources/Algorithms/Indexed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct Indexed<Base: Collection> {
/// The base collection.
public let base: Base

@usableFromInline
@inlinable
internal init(base: Base) {
self.base = base
}
Expand Down
13 changes: 9 additions & 4 deletions Sources/Algorithms/Intersperse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct Intersperse<Base: Sequence> {
@usableFromInline
internal let separator: Base.Element

@usableFromInline
@inlinable
internal init(base: Base, separator: Base.Element) {
self.base = base
self.separator = separator
Expand All @@ -37,7 +37,7 @@ extension Intersperse: Sequence {
@usableFromInline
internal var state = State.start

@usableFromInline
@inlinable
internal init(iterator: Base.Iterator, separator: Base.Element) {
self.iterator = iterator
self.separator = separator
Expand Down Expand Up @@ -89,6 +89,11 @@ extension Intersperse: Collection where Base: Collection {
@usableFromInline
internal let representation: Representation

@inlinable
init(representation: Representation) {
self.representation = representation
}

@inlinable
public static func < (lhs: Index, rhs: Index) -> Bool {
switch (lhs.representation, rhs.representation) {
Expand All @@ -101,12 +106,12 @@ extension Intersperse: Collection where Base: Collection {
}
}

@usableFromInline
@inlinable
static func element(_ index: Base.Index) -> Self {
Self(representation: .element(index))
}

@usableFromInline
@inlinable
static func separator(next: Base.Index) -> Self {
Self(representation: .separator(next: next))
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/Algorithms/LazySplit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public struct LazySplitSequence<Base: Sequence> {
@usableFromInline
internal let omittingEmptySubsequences: Bool

@usableFromInline
@inlinable
internal init(
base: Base,
isSeparator: @escaping (Base.Element) -> Bool,
Expand Down Expand Up @@ -72,7 +72,7 @@ extension LazySplitSequence {
@usableFromInline
internal var sequenceLength = 0

@usableFromInline
@inlinable
internal init(
base: Base.Iterator,
whereSeparator: @escaping (Base.Element) -> Bool,
Expand Down Expand Up @@ -360,7 +360,7 @@ public struct LazySplitCollection<Base: Collection> {
@usableFromInline
internal var _startIndex: Index

@usableFromInline
@inlinable
internal init(
base: Base,
isSeparator: @escaping (Base.Element) -> Bool,
Expand Down Expand Up @@ -415,7 +415,7 @@ extension LazySplitCollection: LazyCollectionProtocol {
@usableFromInline
internal let splitCount: Int

@usableFromInline
@inlinable
internal init(
baseRange: Range<Base.Index>,
sequenceLength: Int,
Expand All @@ -441,7 +441,7 @@ extension LazySplitCollection: LazyCollectionProtocol {

/// Returns the index of the subsequence starting at or after the given base
/// collection index.
@usableFromInline
@inlinable
internal func indexForSubsequence(
atOrAfter lowerBound: Base.Index,
sequenceLength: Int,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Algorithms/Partition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension MutableCollection {
/// - Complexity: O(*n* log *n*), where *n* is the number of elements.
/// - Precondition:
/// `n == distance(from: range.lowerBound, to: range.upperBound)`
@usableFromInline
@inlinable
internal mutating func stablePartition(
count n: Int,
subrange: Range<Index>,
Expand Down
10 changes: 5 additions & 5 deletions Sources/Algorithms/Permutations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct Permutations<Base: Collection> {
/// - base: The collection to iterate over for permutations
/// - k: The expected size of each permutation, or `nil` (default) to
/// iterate over all permutations of the same size as the base collection.
@usableFromInline
@inlinable
internal init(_ base: Base, k: Int? = nil) {
let kRange: ClosedRange<Int>?
if let countToChoose = k {
Expand All @@ -45,7 +45,7 @@ public struct Permutations<Base: Collection> {
/// - base: The collection to iterate over for permutations.
/// - kRange: The range of accepted sizes of permutations, or `nil` to
/// iterate over all permutations of the same size as the base collection.
@usableFromInline
@inlinable
internal init<R: RangeExpression>(
_ base: Base, kRange: R?
) where R.Bound == Int {
Expand Down Expand Up @@ -84,15 +84,15 @@ extension Permutations: Sequence {
internal var kRange: Range<Int>

/// Whether or not iteration is finished (`kRange` is empty)
@usableFromInline
@inlinable
internal var isFinished: Bool {
return kRange.isEmpty
}

@usableFromInline
internal var indexes: [Base.Index]

@usableFromInline
@inlinable
internal init(_ permutations: Permutations) {
self.base = permutations.base
self.baseCount = permutations.baseCount
Expand All @@ -111,7 +111,7 @@ extension Permutations: Sequence {
/// is in ascending order.
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@usableFromInline
@inlinable
internal mutating func nextState() -> Bool {
let countToChoose = self.kRange.lowerBound
let edge = countToChoose - 1
Expand Down
Loading