@@ -431,26 +431,32 @@ public struct UniquePermutations<Base: Collection> where Base.Element: Hashable
431
431
internal let kRange : Range < Int >
432
432
433
433
@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 ] {
442
435
let firstIndexesAndCountsByElement = Dictionary (
443
436
elements. indices. lazy. map { ( elements [ $0] , ( $0, 1 ) ) } ,
444
437
uniquingKeysWith: { indexAndCount, _ in ( indexAndCount. 0 , indexAndCount. 1 + 1 ) } )
445
438
446
- self . indexes = Array ( firstIndexesAndCountsByElement
439
+ return Array ( firstIndexesAndCountsByElement
447
440
. values. sorted ( by: { $0. 0 < $1. 0 } )
448
441
. map { index, count in repeatElement ( index, count: count) }
449
442
. 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)
451
457
self . elements = elements
452
458
453
- let upperBound = self . elements . count + 1
459
+ let upperBound = self . indexes . count + 1
454
460
self . kRange = range. relative ( to: 0 ..< . max)
455
461
. clamped ( to: 0 ..< upperBound)
456
462
}
@@ -515,39 +521,6 @@ extension UniquePermutations: Sequence {
515
521
}
516
522
517
523
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
-
551
524
/// Returns a sequence of the unique permutations of this sequence of the
552
525
/// specified length.
553
526
///
@@ -578,8 +551,12 @@ extension Collection where Element: Hashable {
578
551
/// // [2, 1]
579
552
///
580
553
/// 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
+ }
583
560
}
584
561
585
562
/// Returns a collection of the unique permutations of this sequence with
0 commit comments