Skip to content

Commit 2ff5a19

Browse files
committed
Use optional count to collapse no-count method
1 parent 04021b8 commit 2ff5a19

File tree

1 file changed

+23
-46
lines changed

1 file changed

+23
-46
lines changed

Sources/Algorithms/Permutations.swift

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -431,26 +431,32 @@ public struct UniquePermutations<Base: Collection> where Base.Element: Hashable
431431
internal let kRange: Range<Int>
432432

433433
@inlinable
434-
internal init(_ elements: Base) {
435-
self.init(elements, 0..<Int.max)
436-
}
437-
438-
@inlinable
439-
internal init<R: RangeExpression>(_ elements: Base, _ range: R)
440-
where R.Bound == Int
441-
{
434+
static func _indexes(_ elements: Base) -> [Base.Index] {
442435
let firstIndexesAndCountsByElement = Dictionary(
443436
elements.indices.lazy.map { (elements[$0], ($0, 1)) },
444437
uniquingKeysWith: { indexAndCount, _ in (indexAndCount.0, indexAndCount.1 + 1) })
445438

446-
self.indexes = Array(firstIndexesAndCountsByElement
439+
return Array(firstIndexesAndCountsByElement
447440
.values.sorted(by: { $0.0 < $1.0 })
448441
.map { index, count in repeatElement(index, count: count) }
449442
.joined())
450-
443+
}
444+
445+
@inlinable
446+
internal init(_ elements: Base) {
447+
self.indexes = Self._indexes(elements)
448+
self.elements = elements
449+
self.kRange = self.indexes.count ..< (self.indexes.count + 1)
450+
}
451+
452+
@inlinable
453+
internal init<R: RangeExpression>(_ elements: Base, _ range: R)
454+
where R.Bound == Int
455+
{
456+
self.indexes = Self._indexes(elements)
451457
self.elements = elements
452458

453-
let upperBound = self.elements.count + 1
459+
let upperBound = self.indexes.count + 1
454460
self.kRange = range.relative(to: 0 ..< .max)
455461
.clamped(to: 0 ..< upperBound)
456462
}
@@ -515,39 +521,6 @@ extension UniquePermutations: Sequence {
515521
}
516522

517523
extension Collection where Element: Hashable {
518-
/// Returns a sequence of the unique permutations of this sequence.
519-
///
520-
/// Use this method to iterate over the unique permutations of a sequence
521-
/// with repeating elements. This example prints every permutation of an
522-
/// array of numbers:
523-
///
524-
/// let numbers = [1, 2, 2]
525-
/// for perm in numbers.uniquePermutations() {
526-
/// print(perm)
527-
/// }
528-
/// // [1, 2, 2]
529-
/// // [2, 1, 2]
530-
/// // [2, 2, 1]
531-
///
532-
/// By contrast, the unadorned `permutations()` method permutes a collection's
533-
/// elements by position, and includes permutations with equal elements in
534-
/// each permutation:
535-
///
536-
/// for perm in numbers.permutations()
537-
/// print(perm)
538-
/// }
539-
/// // [1, 2, 2]
540-
/// // [1, 2, 2]
541-
/// // [2, 1, 2]
542-
/// // [2, 2, 1]
543-
/// // [2, 1, 2]
544-
/// // [2, 2, 1]
545-
///
546-
/// The returned permutations are in lexicographically sorted order.
547-
public func uniquePermutations() -> UniquePermutations<Self> {
548-
UniquePermutations(self)
549-
}
550-
551524
/// Returns a sequence of the unique permutations of this sequence of the
552525
/// specified length.
553526
///
@@ -578,8 +551,12 @@ extension Collection where Element: Hashable {
578551
/// // [2, 1]
579552
///
580553
/// The returned permutations are in lexicographically sorted order.
581-
public func uniquePermutations(ofCount k: Int) -> UniquePermutations<Self> {
582-
UniquePermutations(self, k ..< (k + 1))
554+
public func uniquePermutations(ofCount k: Int? = nil) -> UniquePermutations<Self> {
555+
if let k = k {
556+
return UniquePermutations(self, k ..< (k + 1))
557+
} else {
558+
return UniquePermutations(self)
559+
}
583560
}
584561

585562
/// Returns a collection of the unique permutations of this sequence with

0 commit comments

Comments
 (0)