mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
Add some (untested) PermutableContainer algorithms
This commit is contained in:
5 files changed
+273
No files matched your search
@@ -41,6 +41,10 @@ target_sources(${module_name} PRIVATE
|
||||
"Protocols/Container/DynamicContainer.swift"
|
||||
"Protocols/Container/MutableContainer.swift"
|
||||
"Protocols/Container/PermutableContainer.swift"
|
||||
"Protocols/Container/PermutableContainer+HeapSort.swift"
|
||||
"Protocols/Container/PermutableContainer+MoveSubrange.swift"
|
||||
"Protocols/Container/PermutableContainer+Reverse.swift"
|
||||
"Protocols/Container/PermutableContainer+Shuffle.swift"
|
||||
"Protocols/Container/RandomAccessContainer.swift"
|
||||
"Protocols/Container/RangeExpression2.swift"
|
||||
"Protocols/Container/RangeReplaceableContainer.swift"
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension PermutableContainer
|
||||
where
|
||||
Self: RandomAccessContainer,
|
||||
Element: ~Copyable
|
||||
{
|
||||
@inlinable
|
||||
public mutating func heapSort(
|
||||
in subrange: Range<Index>,
|
||||
by areInIncreasingOrder: (borrowing Element, borrowing Element) -> Bool
|
||||
) {
|
||||
let c = self.distance(from: subrange.lowerBound, to: subrange.upperBound)
|
||||
var start = c / 2
|
||||
var end = c
|
||||
// Invariants:
|
||||
// - 0 <= start <= end <= c
|
||||
// - `0 ..< start` has unprocessed elements
|
||||
// - `start ..< end` is a forest of heap-ordered segments
|
||||
// - `end ..< subrange.upperBound` is sorted
|
||||
while end > 1 {
|
||||
if start > 0 {
|
||||
// Heap construction
|
||||
start -= 1
|
||||
} else {
|
||||
// Heap extraction
|
||||
end -= 1
|
||||
self.swapAt(
|
||||
subrange.lowerBound,
|
||||
self.index(subrange.lowerBound, offsetBy: end))
|
||||
}
|
||||
var root = start
|
||||
while true {
|
||||
var child = root * 2 + 1
|
||||
guard child < end else { break }
|
||||
var i = self.index(subrange.lowerBound, offsetBy: child)
|
||||
if child + 1 < end {
|
||||
let j = self.index(after: i)
|
||||
if areInIncreasingOrder(self[i], self[j]) {
|
||||
child += 1
|
||||
i = j
|
||||
}
|
||||
}
|
||||
let r = self.index(subrange.lowerBound, offsetBy: root)
|
||||
guard areInIncreasingOrder(self[r], self[i]) else { break }
|
||||
self.swapAt(r, i)
|
||||
root = child
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@export(implementation)
|
||||
@_transparent
|
||||
public mutating func heapSort(
|
||||
by areInIncreasingOrder: (borrowing Element, borrowing Element) -> Bool
|
||||
) {
|
||||
self.heapSort(in: self.startIndex ..< self.endIndex, by: areInIncreasingOrder)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension PermutableContainer
|
||||
where
|
||||
Self: RandomAccessContainer,
|
||||
Element: Comparable & ~Copyable
|
||||
{
|
||||
@export(implementation)
|
||||
@_transparent
|
||||
public mutating func heapSort() {
|
||||
self.heapSort(in: self.startIndex ..< self.endIndex)
|
||||
}
|
||||
|
||||
@export(implementation)
|
||||
@_transparent
|
||||
public mutating func heapSort(in subrange: Range<Index>) {
|
||||
self.heapSort(in: subrange, by: <)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,48 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension PermutableContainer
|
||||
where
|
||||
Self: BidirectionalContainer,
|
||||
Element: ~Copyable,
|
||||
Index: Comparable
|
||||
{
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public mutating func moveSubrange(
|
||||
_ subrange: Range<Index>,
|
||||
to insertionPoint: Index
|
||||
) -> Range<Index> {
|
||||
if subrange.contains(insertionPoint) {
|
||||
return subrange
|
||||
}
|
||||
let c = self.distance(from: subrange.lowerBound, to: subrange.upperBound)
|
||||
if insertionPoint < subrange.lowerBound {
|
||||
self.reverseSubrange(insertionPoint ..< subrange.upperBound)
|
||||
let cut = self.index(insertionPoint, offsetBy: c)
|
||||
self.reverseSubrange(insertionPoint ..< cut)
|
||||
self.reverseSubrange(cut ..< subrange.upperBound)
|
||||
return insertionPoint ..< cut
|
||||
}
|
||||
self.reverseSubrange(subrange.lowerBound ..< insertionPoint)
|
||||
let cut = self.index(insertionPoint, offsetBy: -c)
|
||||
self.reverseSubrange(subrange.lowerBound ..< cut)
|
||||
self.reverseSubrange(cut ..< insertionPoint)
|
||||
return cut ..< insertionPoint
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension PermutableContainer
|
||||
where Self: BidirectionalContainer, Element: ~Copyable
|
||||
{
|
||||
@inlinable
|
||||
public mutating func reverse() {
|
||||
var i = self.startIndex
|
||||
var j = self.endIndex
|
||||
while i != j {
|
||||
self.formIndex(before: &j)
|
||||
guard i != j else { break }
|
||||
self.swapAt(i, j)
|
||||
self.formIndex(after: &i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension PermutableContainer
|
||||
where
|
||||
Self: BidirectionalContainer,
|
||||
Element: ~Copyable,
|
||||
Index: Comparable
|
||||
{
|
||||
@inlinable
|
||||
public mutating func reverseSubrange(_ range: Range<Index>) {
|
||||
var i = range.lowerBound
|
||||
var j = range.upperBound
|
||||
while i != j {
|
||||
self.formIndex(before: &j)
|
||||
guard i != j else { break }
|
||||
self.swapAt(i, j)
|
||||
self.formIndex(after: &i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension PermutableContainer
|
||||
where Self: RandomAccessContainer, Element: ~Copyable {
|
||||
/// Shuffles the container in place, using the given generator as a source
|
||||
/// for randomness.
|
||||
///
|
||||
/// You use this method to randomize the order of elements in a container
|
||||
/// when you are using a custom random number generator. For example, you
|
||||
/// can use the `shuffle(using:)` method to randomly reorder the elements
|
||||
/// of an array.
|
||||
///
|
||||
/// var names = ["Alejandro", "Camila", "Diego", "Luciana", "Luis", "Sofía"]
|
||||
/// names.shuffle(using: &myGenerator)
|
||||
/// // names == ["Sofía", "Alejandro", "Camila", "Luis", "Diego", "Luciana"]
|
||||
///
|
||||
/// - Parameter generator: The random number generator to use when shuffling
|
||||
/// the container.
|
||||
///
|
||||
/// - Complexity: O(`count`)
|
||||
/// - Note: The algorithm used to shuffle a container may change in a future
|
||||
/// version of Swift. If you're passing a generator that results in the
|
||||
/// same shuffled order each time you run your program, that sequence may
|
||||
/// change when your program is compiled using a different version of
|
||||
/// Swift.
|
||||
@inlinable
|
||||
public mutating func shuffle<T: RandomNumberGenerator>(
|
||||
using generator: inout T
|
||||
) {
|
||||
// Fisher-Yates shuffle
|
||||
var remaining = self.count
|
||||
guard remaining > 1 else { return }
|
||||
var i = startIndex
|
||||
while remaining > 1 {
|
||||
let random = Int.random(in: 0 ..< remaining, using: &generator)
|
||||
self.swapAt(i, self.index(i, offsetBy: random))
|
||||
self.formIndex(after: &i)
|
||||
remaining -= 1
|
||||
}
|
||||
}
|
||||
|
||||
/// Shuffles the container in place.
|
||||
///
|
||||
/// Use the `shuffle()` method to randomly reorder the elements of an array.
|
||||
///
|
||||
/// var names = ["Alejandro", "Camila", "Diego", "Luciana", "Luis", "Sofía"]
|
||||
/// names.shuffle()
|
||||
/// // names == ["Luis", "Camila", "Luciana", "Sofía", "Alejandro", "Diego"]
|
||||
///
|
||||
/// This method is equivalent to calling `shuffle(using:)`, passing in the
|
||||
/// system's default random generator.
|
||||
///
|
||||
/// - Complexity: O(`count`)
|
||||
@inlinable
|
||||
public mutating func shuffle() {
|
||||
var g = SystemRandomNumberGenerator()
|
||||
shuffle(using: &g)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user