mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-26 04:27:48 +00:00
Add inlinability annotations throughout
This commit is contained in:
1 parent
115abe1f69
commit
8ca636d702
11 files changed
+156
-21
No files matched your search
@@ -28,19 +28,25 @@ public struct Chain<Base1: Sequence, Base2: Sequence>
|
||||
extension Chain: Sequence {
|
||||
/// The iterator for a `Chain` sequence.
|
||||
public struct Iterator: IteratorProtocol {
|
||||
@usableFromInline
|
||||
internal var iterator1: Base1.Iterator
|
||||
|
||||
@usableFromInline
|
||||
internal var iterator2: Base2.Iterator
|
||||
|
||||
@usableFromInline
|
||||
internal init(_ concatenation: Chain) {
|
||||
iterator1 = concatenation.base1.makeIterator()
|
||||
iterator2 = concatenation.base2.makeIterator()
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func next() -> Base1.Element? {
|
||||
return iterator1.next() ?? iterator2.next()
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func makeIterator() -> Iterator {
|
||||
Iterator(self)
|
||||
}
|
||||
@@ -54,23 +60,28 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
|
||||
// is not to be used as a value - iterating over indices should go straight
|
||||
// from the penultimate index of the first collection to the start of the
|
||||
// second.
|
||||
@usableFromInline
|
||||
internal enum Representation : Equatable {
|
||||
case first(Base1.Index)
|
||||
case second(Base2.Index)
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal let position: Representation
|
||||
|
||||
/// Creates a new index into the first underlying collection.
|
||||
@usableFromInline
|
||||
internal init(first i: Base1.Index) {
|
||||
position = .first(i)
|
||||
}
|
||||
|
||||
/// Creates a new index into the second underlying collection.
|
||||
@usableFromInline
|
||||
internal init(second i: Base2.Index) {
|
||||
position = .second(i)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func < (lhs: Index, rhs: Index) -> Bool {
|
||||
switch (lhs.position, rhs.position) {
|
||||
case (.first, .second):
|
||||
@@ -87,20 +98,24 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
|
||||
|
||||
/// Converts an index of `Base1` to the corresponding `Index` by mapping
|
||||
/// `base1.endIndex` to `base2.startIndex`.
|
||||
@usableFromInline
|
||||
internal func convertIndex(_ i: Base1.Index) -> Index {
|
||||
i == base1.endIndex ? Index(second: base2.startIndex) : Index(first: i)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var startIndex: Index {
|
||||
// if `base1` is empty, this will return `base2.startIndex` - if `base2` is
|
||||
// also empty, this will correctly equal `base2.endIndex`
|
||||
convertIndex(base1.startIndex)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var endIndex: Index {
|
||||
return Index(second: base2.endIndex)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public subscript(i: Index) -> Base1.Element {
|
||||
switch i.position {
|
||||
case let .first(i):
|
||||
@@ -110,6 +125,7 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(after i: Index) -> Index {
|
||||
switch i.position {
|
||||
case let .first(i):
|
||||
@@ -120,6 +136,7 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(_ i: Index, offsetBy n: Int) -> Index {
|
||||
if n == 0 { return i }
|
||||
return n > 0
|
||||
@@ -127,6 +144,7 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
|
||||
: offsetBackward(i, by: -n, limitedBy: startIndex)!
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(
|
||||
_ i: Index,
|
||||
offsetBy n: Int,
|
||||
@@ -138,7 +156,8 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
|
||||
: offsetBackward(i, by: -n, limitedBy: limit)
|
||||
}
|
||||
|
||||
private func offsetForward(
|
||||
@usableFromInline
|
||||
internal func offsetForward(
|
||||
_ i: Index, by n: Int, limitedBy limit: Index
|
||||
) -> Index? {
|
||||
switch (i.position, limit.position) {
|
||||
@@ -177,7 +196,8 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
|
||||
}
|
||||
}
|
||||
|
||||
private func offsetBackward(
|
||||
@usableFromInline
|
||||
internal func offsetBackward(
|
||||
_ i: Index, by n: Int, limitedBy limit: Index
|
||||
) -> Index? {
|
||||
switch (i.position, limit.position) {
|
||||
@@ -216,6 +236,7 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func distance(from start: Index, to end: Index) -> Int {
|
||||
switch (start.position, end.position) {
|
||||
case let (.first(i), .first(j)):
|
||||
@@ -235,6 +256,7 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
|
||||
extension Chain: BidirectionalCollection
|
||||
where Base1: BidirectionalCollection, Base2: BidirectionalCollection
|
||||
{
|
||||
@inlinable
|
||||
public func index(before i: Index) -> Index {
|
||||
assert(i != startIndex, "Can't advance before startIndex")
|
||||
switch i.position {
|
||||
|
||||
@@ -14,13 +14,21 @@ public struct LazyChunked<Base: Collection> {
|
||||
public let base: Base
|
||||
|
||||
/// The projection function.
|
||||
@usableFromInline
|
||||
internal var belongInSameGroup: (Base.Element, Base.Element) -> Bool
|
||||
|
||||
@usableFromInline
|
||||
internal init(base: Base, belongInSameGroup: @escaping (Base.Element, Base.Element) -> Bool) {
|
||||
self.base = base
|
||||
self.belongInSameGroup = belongInSameGroup
|
||||
}
|
||||
}
|
||||
|
||||
extension LazyChunked: LazyCollectionProtocol {
|
||||
/// A position in a chunked collection.
|
||||
public struct Index: Comparable {
|
||||
/// The lower bound of the chunk at this position.
|
||||
@usableFromInline
|
||||
internal var lowerBound: Base.Index
|
||||
|
||||
/// The upper bound of the chunk at this position.
|
||||
@@ -28,8 +36,16 @@ extension LazyChunked: LazyCollectionProtocol {
|
||||
/// `upperBound` is optional so that computing `startIndex` can be an O(1)
|
||||
/// operation. When `upperBound` is `nil`, the actual upper bound is found
|
||||
/// when subscripting or calling `index(after:)`.
|
||||
@usableFromInline
|
||||
internal var upperBound: Base.Index?
|
||||
|
||||
@usableFromInline
|
||||
internal init(lowerBound: Base.Index, upperBound: Base.Index? = nil) {
|
||||
self.lowerBound = lowerBound
|
||||
self.upperBound = upperBound
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func == (lhs: Index, rhs: Index) -> Bool {
|
||||
// Only use the lower bound to test for equality, since sometimes the
|
||||
// `startIndex` will have an upper bound of `nil` and sometimes it won't,
|
||||
@@ -41,6 +57,7 @@ extension LazyChunked: LazyCollectionProtocol {
|
||||
lhs.lowerBound == rhs.lowerBound
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func < (lhs: Index, rhs: Index) -> Bool {
|
||||
// Only use the lower bound to test for ordering, as above.
|
||||
lhs.lowerBound < rhs.lowerBound
|
||||
@@ -49,26 +66,31 @@ extension LazyChunked: LazyCollectionProtocol {
|
||||
|
||||
/// Returns the index in the base collection for the first element that
|
||||
/// doesn't match the current chunk.
|
||||
@usableFromInline
|
||||
internal func endOfChunk(from i: Base.Index) -> Base.Index {
|
||||
guard i != base.endIndex else { return base.endIndex }
|
||||
return base[base.index(after: i)...]
|
||||
.firstIndex(where: { !belongInSameGroup($0, base[i]) }) ?? base.endIndex
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var startIndex: Index {
|
||||
Index(lowerBound: base.startIndex)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var endIndex: Index {
|
||||
Index(lowerBound: base.endIndex, upperBound: base.endIndex)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(after i: Index) -> Index {
|
||||
let upperBound = i.upperBound ?? endOfChunk(from: i.lowerBound)
|
||||
let end = endOfChunk(from: upperBound)
|
||||
return Index(lowerBound: upperBound, upperBound: end)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public subscript(position: Index) -> Base.SubSequence {
|
||||
let upperBound = position.upperBound
|
||||
?? endOfChunk(from: position.lowerBound)
|
||||
@@ -83,6 +105,7 @@ extension LazyChunked: BidirectionalCollection
|
||||
{
|
||||
/// Returns the index in the base collection for the element that starts
|
||||
/// the chunk ending at the given index.
|
||||
@usableFromInline
|
||||
internal func startOfChunk(endingAt end: Base.Index) -> Base.Index {
|
||||
// Get the projected value of the last element in the range ending at `end`.
|
||||
let lastOfPreviousChunk = base[base.index(before: end)]
|
||||
@@ -102,6 +125,7 @@ extension LazyChunked: BidirectionalCollection
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(before i: Index) -> Index {
|
||||
let start = startOfChunk(endingAt: i.lowerBound)
|
||||
return Index(lowerBound: start, upperBound: i.lowerBound)
|
||||
@@ -119,6 +143,7 @@ extension LazyCollectionProtocol {
|
||||
/// - Complexity: O(1). When iterating over the resulting collection,
|
||||
/// accessing each successive chunk has a complexity of O(*m*), where *m*
|
||||
/// is the length of the chunk.
|
||||
@inlinable
|
||||
public func chunked(
|
||||
by belongInSameGroup: @escaping (Element, Element) -> Bool
|
||||
) -> LazyChunked<Elements> {
|
||||
@@ -131,6 +156,7 @@ extension LazyCollectionProtocol {
|
||||
/// - Complexity: O(1). When iterating over the resulting collection,
|
||||
/// accessing each successive chunk has a complexity of O(*m*), where *m*
|
||||
/// is the length of the chunk.
|
||||
@inlinable
|
||||
public func chunked<Subject: Equatable>(
|
||||
on projection: @escaping (Element) -> Subject
|
||||
) -> LazyChunked<Elements> {
|
||||
@@ -149,6 +175,7 @@ extension Collection {
|
||||
/// the given predicate.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of this collection.
|
||||
@inlinable
|
||||
public func chunked(
|
||||
by belongInSameGroup: (Element, Element) -> Bool
|
||||
) -> [SubSequence] {
|
||||
@@ -179,6 +206,7 @@ extension Collection {
|
||||
/// grouping elements that project to the same value.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of this collection.
|
||||
@inlinable
|
||||
public func chunked<Subject: Equatable>(
|
||||
on projection: (Element) -> Subject
|
||||
) -> [SubSequence] {
|
||||
|
||||
@@ -12,13 +12,17 @@
|
||||
public struct Combinations<Base: Collection> {
|
||||
/// The collection to iterate over for combinations.
|
||||
public let base: Base
|
||||
|
||||
@usableFromInline
|
||||
internal var k: Int
|
||||
|
||||
@usableFromInline
|
||||
internal init(_ base: Base, k: Int) {
|
||||
self.base = base
|
||||
self.k = base.count < k ? -1 : k
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var count: Int {
|
||||
func binomial(n: Int, k: Int) -> Int {
|
||||
switch k {
|
||||
@@ -37,8 +41,13 @@ public struct Combinations<Base: Collection> {
|
||||
|
||||
extension Combinations: Sequence {
|
||||
public struct Iterator: IteratorProtocol {
|
||||
@usableFromInline
|
||||
internal let base: Base
|
||||
|
||||
@usableFromInline
|
||||
internal var indexes: [Base.Index]
|
||||
|
||||
@usableFromInline
|
||||
internal var finished: Bool
|
||||
|
||||
internal init(_ combinations: Combinations) {
|
||||
@@ -67,6 +76,7 @@ extension Combinations: Sequence {
|
||||
/// [2, 3, 4] *
|
||||
/// // Can't advance without needing to go past `base.endIndex`,
|
||||
/// // so the iteration is finished.
|
||||
@usableFromInline
|
||||
internal mutating func advance() {
|
||||
guard !indexes.isEmpty else {
|
||||
// Initial state for combinations of 0 elements is an empty array with
|
||||
@@ -99,6 +109,7 @@ extension Combinations: Sequence {
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func next() -> [Base.Element]? {
|
||||
if finished { return nil }
|
||||
defer { advance() }
|
||||
@@ -146,6 +157,7 @@ extension Collection {
|
||||
/// - Parameter k: The number of elements to include in each combination.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable
|
||||
public func combinations(ofCount k: Int) -> Combinations<Self> {
|
||||
assert(k >= 0, "Can't have combinations with a negative number of elements.")
|
||||
return Combinations(self, k: k)
|
||||
|
||||
@@ -18,9 +18,19 @@ public struct Cycle<Base: Collection> {
|
||||
extension Cycle: Sequence {
|
||||
/// The iterator for a `Cycle` sequence.
|
||||
public struct Iterator: IteratorProtocol {
|
||||
@usableFromInline
|
||||
let base: Base
|
||||
|
||||
@usableFromInline
|
||||
var current: Base.Index
|
||||
|
||||
@usableFromInline
|
||||
internal init(base: Base) {
|
||||
self.base = base
|
||||
self.current = base.startIndex
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func next() -> Base.Element? {
|
||||
guard !base.isEmpty else { return nil }
|
||||
|
||||
@@ -34,7 +44,7 @@ extension Cycle: Sequence {
|
||||
}
|
||||
|
||||
public func makeIterator() -> Iterator {
|
||||
Iterator(base: base, current: base.startIndex)
|
||||
Iterator(base: base)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,39 +17,52 @@ public struct Indexed<Base: Collection> {
|
||||
|
||||
/// The base collection.
|
||||
public let base: Base
|
||||
|
||||
@usableFromInline
|
||||
internal init(base: Base) {
|
||||
self.base = base
|
||||
}
|
||||
}
|
||||
|
||||
extension Indexed: Collection {
|
||||
@inlinable
|
||||
public var startIndex: Base.Index {
|
||||
base.startIndex
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var endIndex: Base.Index {
|
||||
base.endIndex
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public subscript(position: Base.Index) -> Element {
|
||||
(index: position, element: base[position])
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(after i: Base.Index) -> Base.Index {
|
||||
base.index(after: i)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(_ i: Base.Index, offsetBy distance: Int) -> Base.Index {
|
||||
base.index(i, offsetBy: distance)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(_ i: Base.Index, offsetBy distance: Int, limitedBy limit: Base.Index) -> Base.Index? {
|
||||
base.index(i, offsetBy: distance, limitedBy: limit)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func distance(from start: Base.Index, to end: Base.Index) -> Int {
|
||||
base.distance(from: start, to: end)
|
||||
}
|
||||
}
|
||||
|
||||
extension Indexed: BidirectionalCollection where Base: BidirectionalCollection {
|
||||
@inlinable
|
||||
public func index(before i: Base.Index) -> Base.Index {
|
||||
base.index(before: i)
|
||||
}
|
||||
@@ -75,6 +88,7 @@ extension Collection {
|
||||
/// }
|
||||
///
|
||||
/// Returns: A collection of paired indices and elements of this collection.
|
||||
@inlinable
|
||||
public func indexed() -> Indexed<Self> {
|
||||
Indexed(base: self)
|
||||
}
|
||||
|
||||
@@ -21,6 +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
|
||||
internal mutating func stablePartition(
|
||||
count n: Int,
|
||||
subrange: Range<Index>,
|
||||
@@ -56,12 +57,13 @@ extension MutableCollection {
|
||||
/// all elements not satisfying it.
|
||||
///
|
||||
/// - Complexity: O(*n* log *n*), where *n* is the length of this collection.
|
||||
@inlinable
|
||||
public mutating func stablePartition(
|
||||
subrange: Range<Index>,
|
||||
by belongsInSecondPartition: (Element) throws-> Bool
|
||||
) rethrows -> Index {
|
||||
try stablePartition(
|
||||
count: self[subrange].count,
|
||||
count: distance(from: subrange.lowerBound, to: subrange.upperBound),
|
||||
subrange: subrange,
|
||||
by: belongsInSecondPartition)
|
||||
}
|
||||
@@ -75,6 +77,7 @@ extension MutableCollection {
|
||||
/// all elements not satisfying it.
|
||||
///
|
||||
/// - Complexity: O(*n* log *n*), where *n* is the length of this collection.
|
||||
@inlinable
|
||||
public mutating func stablePartition(
|
||||
by belongsInSecondPartition: (Element) throws-> Bool
|
||||
) rethrows -> Index {
|
||||
@@ -93,6 +96,7 @@ extension MutableCollection {
|
||||
/// collection, returning the start position of the resulting suffix.
|
||||
///
|
||||
/// - Complexity: O(*n*) where n is the length of the collection.
|
||||
@inlinable
|
||||
public mutating func partition(
|
||||
subrange: Range<Index>,
|
||||
by belongsInSecondPartition: (Element) throws -> Bool
|
||||
@@ -120,6 +124,7 @@ extension MutableCollection where Self: BidirectionalCollection {
|
||||
/// collection, returning the start position of the resulting suffix.
|
||||
///
|
||||
/// - Complexity: O(*n*) where n is the length of the collection.
|
||||
@inlinable
|
||||
public mutating func partition(
|
||||
subrange: Range<Index>,
|
||||
by belongsInSecondPartition: (Element) throws -> Bool
|
||||
@@ -178,6 +183,7 @@ extension Collection {
|
||||
///
|
||||
/// - Complexity: O(log *n*), where *n* is the length of this collection if
|
||||
/// the collection conforms to `RandomAccessCollection`, otherwise O(*n*).
|
||||
@inlinable
|
||||
public func partitioningIndex(
|
||||
where belongsInSecondPartition: (Element) throws -> Bool
|
||||
) rethrows -> Index {
|
||||
|
||||
@@ -33,16 +33,22 @@ public struct Permutations<Base: Collection> {
|
||||
|
||||
extension Permutations: Sequence {
|
||||
public struct Iterator: IteratorProtocol {
|
||||
@usableFromInline
|
||||
internal var base: Base
|
||||
@usableFromInline
|
||||
internal var indexes: [Base.Index]
|
||||
@usableFromInline
|
||||
internal var hasMorePermutations: Bool
|
||||
@usableFromInline
|
||||
internal var countToChoose: Int = 0
|
||||
|
||||
/// `true` if we're generating permutations of the full collection.
|
||||
@usableFromInline
|
||||
internal var permutesFullCollection: Bool {
|
||||
countToChoose == indexes.count
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal init(_ base: Base) {
|
||||
self.base = base
|
||||
self.indexes = Array(base.indices)
|
||||
@@ -50,6 +56,7 @@ extension Permutations: Sequence {
|
||||
self.hasMorePermutations = true
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal init(_ base: Base, count: Int) {
|
||||
self.base = base
|
||||
self.countToChoose = count
|
||||
@@ -72,6 +79,7 @@ extension Permutations: Sequence {
|
||||
/// is in ascending order.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of the collection.
|
||||
@usableFromInline
|
||||
internal mutating func nextState() -> Bool {
|
||||
let edge = countToChoose - 1
|
||||
|
||||
@@ -101,6 +109,7 @@ extension Permutations: Sequence {
|
||||
return true
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func next() -> [Base.Element]? {
|
||||
if !hasMorePermutations { return nil }
|
||||
|
||||
@@ -118,6 +127,7 @@ extension Permutations: Sequence {
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal var permutesFullCollection: Bool {
|
||||
baseCount == countToChoose
|
||||
}
|
||||
@@ -148,6 +158,7 @@ extension MutableCollection
|
||||
/// is in ascending order according to `areInIncreasingOrder`.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of the collection.
|
||||
@usableFromInline
|
||||
internal mutating func nextPermutation() -> Bool {
|
||||
// ensure we have > 1 element in the collection
|
||||
if isEmpty { return false }
|
||||
|
||||
@@ -25,18 +25,24 @@ public struct Product2<Base1: Sequence, Base2: Collection> {
|
||||
extension Product2: Sequence {
|
||||
/// The iterator for a `Product2` sequence.
|
||||
public struct Iterator: IteratorProtocol {
|
||||
var i1: Base1.Iterator
|
||||
var i2: Base2.Iterator
|
||||
var element1: Base1.Element?
|
||||
let base2: Base2
|
||||
@usableFromInline
|
||||
internal var i1: Base1.Iterator
|
||||
@usableFromInline
|
||||
internal var i2: Base2.Iterator
|
||||
@usableFromInline
|
||||
internal var element1: Base1.Element?
|
||||
@usableFromInline
|
||||
internal let base2: Base2
|
||||
|
||||
init(_ c: Product2) {
|
||||
@usableFromInline
|
||||
internal init(_ c: Product2) {
|
||||
self.base2 = c.base2
|
||||
self.i1 = c.base1.makeIterator()
|
||||
self.i2 = c.base2.makeIterator()
|
||||
self.element1 = nil
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func next() -> (Base1.Element, Base2.Element)? {
|
||||
// This is the initial state, where i1.next() has never
|
||||
// been called, or the final state, where i1.next() has
|
||||
@@ -78,29 +84,42 @@ extension Product2: Sequence {
|
||||
extension Product2: Collection where Base1: Collection {
|
||||
/// The index type for a `Product2` collection.
|
||||
public struct Index: Comparable {
|
||||
var i1: Base1.Index
|
||||
var i2: Base2.Index
|
||||
@usableFromInline
|
||||
internal var i1: Base1.Index
|
||||
@usableFromInline
|
||||
internal var i2: Base2.Index
|
||||
|
||||
@usableFromInline
|
||||
internal init(i1: Base1.Index, i2: Base2.Index) {
|
||||
self.i1 = i1
|
||||
self.i2 = i2
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func < (lhs: Index, rhs: Index) -> Bool {
|
||||
(lhs.i1, lhs.i2) < (rhs.i1, rhs.i2)
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var count: Int {
|
||||
base1.count * base2.count
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var startIndex: Index {
|
||||
Index(
|
||||
i1: base2.isEmpty ? base1.endIndex : base1.startIndex,
|
||||
i2: base2.startIndex)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var endIndex: Index {
|
||||
// `base2.startIndex` simplifies index calculations.
|
||||
Index(i1: base1.endIndex, i2: base2.startIndex)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func index(after i: Index) -> Index {
|
||||
precondition(i.i1 != base1.endIndex, "Can't advance past endIndex")
|
||||
let newIndex2 = base2.index(after: i.i2)
|
||||
@@ -111,6 +130,7 @@ extension Product2: Collection where Base1: Collection {
|
||||
|
||||
// TODO: Implement index(_:offsetBy:) and index(_:offsetBy:limitedBy:)
|
||||
|
||||
@inlinable
|
||||
public func distance(from start: Index, to end: Index) -> Int {
|
||||
if start > end {
|
||||
return -distance(from: end, to: start)
|
||||
@@ -123,6 +143,7 @@ extension Product2: Collection where Base1: Collection {
|
||||
+ base2.count * (base1.distance(from: start.i1, to: end.i1) - 1)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public subscript(position: Index) -> (Base1.Element, Base2.Element) {
|
||||
return (base1[position.i1], base2[position.i2])
|
||||
}
|
||||
@@ -131,6 +152,7 @@ extension Product2: Collection where Base1: Collection {
|
||||
extension Product2: BidirectionalCollection
|
||||
where Base1: BidirectionalCollection, Base2: BidirectionalCollection
|
||||
{
|
||||
@inlinable
|
||||
public func index(before i: Index) -> Index {
|
||||
precondition(i != startIndex,
|
||||
"Can't move before startIndex")
|
||||
|
||||
@@ -28,6 +28,7 @@ extension Collection {
|
||||
/// collection's count, then this method returns the full collection.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of the collection.
|
||||
@inlinable
|
||||
public func randomStableSample<G: RandomNumberGenerator>(
|
||||
count k: Int, using rng: inout G
|
||||
) -> [Element] {
|
||||
@@ -67,6 +68,7 @@ extension Collection {
|
||||
/// collection's count, then this method returns the full collection.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of the collection.
|
||||
@inlinable
|
||||
public func randomStableSample(count k: Int) -> [Element] {
|
||||
var g = SystemRandomNumberGenerator()
|
||||
return randomStableSample(count: k, using: &g)
|
||||
@@ -81,13 +83,15 @@ extension Collection {
|
||||
// Algorithms of Time Complexity O(n(1 + log(N/n)))":
|
||||
// https://dl.acm.org/doi/pdf/10.1145/198429.198435
|
||||
|
||||
fileprivate func nextW<G: RandomNumberGenerator>(
|
||||
@usableFromInline
|
||||
internal func nextW<G: RandomNumberGenerator>(
|
||||
k: Int, using rng: inout G
|
||||
) -> Double {
|
||||
Double.root(.random(in: 0..<1, using: &rng), k)
|
||||
}
|
||||
|
||||
fileprivate func nextOffset<G: RandomNumberGenerator>(
|
||||
@usableFromInline
|
||||
internal func nextOffset<G: RandomNumberGenerator>(
|
||||
w: Double, using rng: inout G
|
||||
) -> Int {
|
||||
Int(Double.log(.random(in: 0..<1, using: &rng)) / .log(1 - w))
|
||||
@@ -106,6 +110,7 @@ extension Collection {
|
||||
/// - Complexity: O(*k*), where *k* is the number of elements to select, if
|
||||
/// the collection conforms to `RandomAccessCollection`. Otherwise, O(*n*),
|
||||
/// where *n* is the length of the collection.
|
||||
@inlinable
|
||||
public func randomSample<G: RandomNumberGenerator>(
|
||||
count k: Int, using rng: inout G
|
||||
) -> [Element] {
|
||||
@@ -157,6 +162,7 @@ extension Collection {
|
||||
/// - Complexity: O(*k*), where *k* is the number of elements to select, if
|
||||
/// the collection conforms to `RandomAccessCollection`. Otherwise, O(*n*),
|
||||
/// where *n* is the length of the collection.
|
||||
@inlinable
|
||||
public func randomSample(count k: Int) -> [Element] {
|
||||
var g = SystemRandomNumberGenerator()
|
||||
return randomSample(count: k, using: &g)
|
||||
@@ -174,6 +180,7 @@ extension Sequence {
|
||||
/// method returns the full sequence.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of the sequence.
|
||||
@inlinable
|
||||
public func randomSample<G: RandomNumberGenerator>(
|
||||
count k: Int, using rng: inout G
|
||||
) -> [Element] {
|
||||
@@ -224,6 +231,7 @@ extension Sequence {
|
||||
/// method returns the full sequence.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of the sequence.
|
||||
@inlinable
|
||||
public func randomSample(count k: Int) -> [Element] {
|
||||
var g = SystemRandomNumberGenerator()
|
||||
return randomSample(count: k, using: &g)
|
||||
|
||||
@@ -29,6 +29,7 @@ extension MutableCollection where Self: BidirectionalCollection {
|
||||
///
|
||||
/// - Postcondition: For returned indices `(f, l)`:
|
||||
/// `f == limit || l == limit`
|
||||
@usableFromInline
|
||||
@discardableResult
|
||||
internal mutating func _reverse(
|
||||
subrange: Range<Index>, until limit: Index
|
||||
@@ -55,6 +56,7 @@ extension MutableCollection where Self: BidirectionalCollection {
|
||||
/// - Parameter subrange: The subrange of this collection to reverse.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of `subrange`.
|
||||
@inlinable
|
||||
public mutating func reverse(subrange: Range<Index>) {
|
||||
if subrange.isEmpty { return }
|
||||
var lo = subrange.lowerBound
|
||||
@@ -93,6 +95,7 @@ extension MutableCollection {
|
||||
/// - distance(from: lhs.lowerBound, to: p) == distance(from:
|
||||
/// rhs.lowerBound, to: q)
|
||||
/// - p == lhs.upperBound || q == rhs.upperBound
|
||||
@usableFromInline
|
||||
internal mutating func _swapNonemptySubrangePrefixes(
|
||||
_ lhs: Range<Index>, _ rhs: Range<Index>
|
||||
) -> (Index, Index) {
|
||||
@@ -131,6 +134,7 @@ extension MutableCollection {
|
||||
/// `subrange` pre-rotation.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of `subrange`.
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public mutating func rotate(
|
||||
subrange: Range<Index>,
|
||||
@@ -210,6 +214,7 @@ extension MutableCollection {
|
||||
/// - Returns: The new index of the element that was first pre-rotation.
|
||||
///
|
||||
/// - Complexity: O(*n*)
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public mutating func rotate(toStartAt newStart: Index) -> Index {
|
||||
rotate(subrange: startIndex..<endIndex, toStartAt: newStart)
|
||||
@@ -238,6 +243,7 @@ extension MutableCollection where Self: BidirectionalCollection {
|
||||
/// `subrange` pre-rotation.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the length of `subrange`.
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public mutating func rotate(
|
||||
subrange: Range<Index>,
|
||||
@@ -268,6 +274,7 @@ extension MutableCollection where Self: BidirectionalCollection {
|
||||
/// - Returns: The new index of the element that was first pre-rotation.
|
||||
///
|
||||
/// - Complexity: O(*n*)
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public mutating func rotate(toStartAt newStart: Index) -> Index {
|
||||
rotate(subrange: startIndex..<endIndex, toStartAt: newStart)
|
||||
|
||||
@@ -16,15 +16,9 @@
|
||||
extension Sequence where Element: Hashable {
|
||||
/// Returns an array with only the unique elements of this sequence, in the
|
||||
/// order of the first occurence of each unique element.
|
||||
@inlinable
|
||||
public func uniqued() -> [Element] {
|
||||
var seen: Set<Element> = []
|
||||
var result: [Element] = []
|
||||
for element in self {
|
||||
if seen.insert(element).inserted {
|
||||
result.append(element)
|
||||
}
|
||||
}
|
||||
return result
|
||||
uniqued(on: { $0 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +26,7 @@ extension Sequence {
|
||||
/// Returns an array with the unique elements of this sequence (as determined
|
||||
/// by the given projection), in the order of the first occurence of each
|
||||
/// unique element.
|
||||
@inlinable
|
||||
public func uniqued<Subject: Hashable>(
|
||||
on projection: (Element) throws -> Subject
|
||||
) rethrows -> [Element] {
|
||||
|
||||
Reference in new issue
Block a user