Skip to content

[0.0.3] Audit public functions and properties that could be inlinable #83

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 1 commit into from
Feb 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
2 changes: 2 additions & 0 deletions Sources/Algorithms/Chain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public struct Chain2<Base1: Sequence, Base2: Sequence>
/// The second sequence in this chain.
public let base2: Base2

@usableFromInline
internal init(base1: Base1, base2: Base2) {
self.base1 = base1
self.base2 = base2
Expand Down Expand Up @@ -306,6 +307,7 @@ extension Chain2: Hashable where Base1: Hashable, Base2: Hashable {}
/// then over the elements of `s2`.
///
/// - Complexity: O(1)
@inlinable
public func chain<S1, S2>(_ s1: S1, _ s2: S2) -> Chain2<S1, S2> {
Chain2(base1: s1, base2: s2)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Algorithms/Chunked.swift
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ extension ChunkedByCount: Equatable where Base: Equatable {}
// only in terms of `base`. Since the computed index is based on it,
// it should not make a difference here.
extension ChunkedByCount: Hashable where Base: Hashable {
@inlinable
public func hash(into hasher: inout Hasher) {
hasher.combine(base)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/Algorithms/Cycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
public struct Cycle<Base: Collection> {
/// The collection to repeat.
public let base: Base

@usableFromInline
internal init(base: Base) {
self.base = base
}
}

extension Cycle: Sequence {
Expand Down Expand Up @@ -43,6 +48,7 @@ extension Cycle: Sequence {
}
}

@inlinable
public func makeIterator() -> Iterator {
Iterator(base: base)
}
Expand Down Expand Up @@ -80,6 +86,7 @@ extension Collection {
/// forever.
///
/// - Complexity: O(1)
@inlinable
public func cycled() -> Cycle<Self> {
Cycle(base: self)
}
Expand All @@ -101,6 +108,7 @@ extension Collection {
/// times.
///
/// - Complexity: O(1)
@inlinable
public func cycled(times: Int) -> FlattenSequence<Repeated<Self>> {
repeatElement(self, count: times).joined()
}
Expand Down
56 changes: 46 additions & 10 deletions Sources/Algorithms/Intersperse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,45 @@
/// A sequence that presents the elements of a base sequence of elements
/// with a separator between each of those elements.
public struct Intersperse<Base: Sequence> {
let base: Base
let separator: Base.Element
@usableFromInline
internal let base: Base

@usableFromInline
internal let separator: Base.Element

@usableFromInline
internal init(base: Base, separator: Base.Element) {
self.base = base
self.separator = separator
}
}

extension Intersperse: Sequence {
/// The iterator for an `Intersperse` sequence.
public struct Iterator: IteratorProtocol {
var iterator: Base.Iterator
let separator: Base.Element
var state = State.start

@usableFromInline
internal var iterator: Base.Iterator

@usableFromInline
internal let separator: Base.Element

@usableFromInline
internal var state = State.start

@usableFromInline
internal init(iterator: Base.Iterator, separator: Base.Element) {
self.iterator = iterator
self.separator = separator
}

@usableFromInline
enum State {
case start
case element(Base.Element)
case separator
}

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

@inlinable
public func makeIterator() -> Intersperse<Base>.Iterator {
Iterator(iterator: base.makeIterator(), separator: separator)
}
Expand All @@ -57,12 +80,16 @@ extension Intersperse: Sequence {
extension Intersperse: Collection where Base: Collection {
/// A position in an `Intersperse` collection.
public struct Index: Comparable {
@usableFromInline
enum Representation: Equatable {
case element(Base.Index)
case separator(next: Base.Index)
}
let representation: Representation

@usableFromInline
internal let representation: Representation

@inlinable
public static func < (lhs: Index, rhs: Index) -> Bool {
switch (lhs.representation, rhs.representation) {
case let (.element(li), .element(ri)),
Expand All @@ -73,24 +100,29 @@ extension Intersperse: Collection where Base: Collection {
return li <= ri
}
}


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

@usableFromInline
static func separator(next: Base.Index) -> Self {
Self(representation: .separator(next: next))
}
}

@inlinable
public var startIndex: Index {
base.startIndex == base.endIndex ? endIndex : .element(base.startIndex)
}

@inlinable
public var endIndex: Index {
.separator(next: base.endIndex)
}

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

@inlinable
public subscript(position: Index) -> Element {
switch position.representation {
case .element(let index): return base[index]
case .separator: return separator
}
}


@inlinable
public func index(_ i: Index, offsetBy distance: Int) -> Index {
switch (i.representation, distance.isMultiple(of: 2)) {
case (let .element(index), true):
Expand All @@ -122,7 +156,7 @@ extension Intersperse: Collection where Base: Collection {
}

// TODO: Implement index(_:offsetBy:limitedBy:)

@inlinable
public func distance(from start: Index, to end: Index) -> Int {
switch (start.representation, end.representation) {
case let (.element(element), .separator(next: separator)):
Expand All @@ -139,6 +173,7 @@ extension Intersperse: Collection where Base: Collection {
extension Intersperse: BidirectionalCollection
where Base: BidirectionalCollection
{
@inlinable
public func index(before i: Index) -> Index {
precondition(i != startIndex, "Can't move before startIndex")
switch i.representation {
Expand Down Expand Up @@ -189,6 +224,7 @@ extension Sequence {
/// - Returns: The interspersed sequence of elements.
///
/// - Complexity: O(1)
@inlinable
public func interspersed(with separator: Element) -> Intersperse<Self> {
Intersperse(base: self, separator: separator)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Algorithms/Permutations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ extension Permutations: Sequence {
}
}

@inlinable
public func makeIterator() -> Iterator {
Iterator(self)
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Algorithms/Product.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public struct Product2<Base1: Sequence, Base2: Collection> {
/// The inner sequence in the product.
public let base2: Base2

@usableFromInline
internal init(_ base1: Base1, _ base2: Base2) {
self.base1 = base1
self.base2 = base2
Expand Down Expand Up @@ -470,6 +471,7 @@ extension Product2: Hashable where Base1: Hashable, Base2: Hashable {}
/// - s2: The second sequence to iterate over.
///
/// - Complexity: O(1)
@inlinable
public func product<Base1: Sequence, Base2: Collection>(
_ s1: Base1, _ s2: Base2
) -> Product2<Base1, Base2> {
Expand Down
Loading