@@ -316,34 +316,37 @@ extension Collection {
316
316
/// // Davide, Celeste
317
317
///
318
318
/// This example prints _all_ the permutations (including an empty array) from
319
- /// the an array of numbers:
319
+ /// an array of numbers:
320
320
///
321
321
/// let numbers = [10, 20, 30]
322
- /// for perm in numbers.permutations(ofCount: 0...) {
323
- /// print(perm)
324
- /// }
325
- /// // []
326
- /// // [10]
327
- /// // [20]
328
- /// // [30]
329
- /// // [10, 20]
330
- /// // [10, 30]
331
- /// // [20, 10]
332
- /// // [20, 30]
333
- /// // [30, 10]
334
- /// // [30, 20]
335
- /// // [10, 20, 30]
336
- /// // [10, 30, 20]
337
- /// // [20, 10, 30]
338
- /// // [20, 30, 10]
339
- /// // [30, 10, 20]
340
- /// // [30, 20, 10]
322
+ /// for perm in numbers.permutations(ofCount: 0...) {
323
+ /// print(perm)
324
+ /// }
325
+ /// // []
326
+ /// // [10]
327
+ /// // [20]
328
+ /// // [30]
329
+ /// // [10, 20]
330
+ /// // [10, 30]
331
+ /// // [20, 10]
332
+ /// // [20, 30]
333
+ /// // [30, 10]
334
+ /// // [30, 20]
335
+ /// // [10, 20, 30]
336
+ /// // [10, 30, 20]
337
+ /// // [20, 10, 30]
338
+ /// // [20, 30, 10]
339
+ /// // [30, 10, 20]
340
+ /// // [30, 20, 10]
341
+ ///
342
+ /// The returned permutations are in ascending order by length, and then
343
+ /// lexicographically within each group of the same length.
341
344
///
342
345
/// - Parameter kRange: The number of elements to include in each permutation.
343
346
///
344
347
/// - Complexity: O(1) for random-access base collections. O(*n*) where *n*
345
- /// is the number of elements in the base collection, since `Permutations`
346
- /// accesses the `count` of the base collection.
348
+ /// is the number of elements in the base collection, since `Permutations`
349
+ /// accesses the `count` of the base collection.
347
350
@inlinable
348
351
public func permutations< R: RangeExpression > (
349
352
ofCount kRange: R
@@ -412,6 +415,11 @@ extension Collection {
412
415
// uniquePermutations()
413
416
//===----------------------------------------------------------------------===//
414
417
418
+ /// A sequence of the unique permutations of the elements of a sequence or
419
+ /// collection.
420
+ ///
421
+ /// To create a `UniquePermutations` instance, call one of the
422
+ /// `uniquePermutations` methods on your collection.
415
423
public struct UniquePermutations < Element> {
416
424
@usableFromInline
417
425
internal let elements : [ Element ]
@@ -441,6 +449,7 @@ public struct UniquePermutations<Element> {
441
449
}
442
450
443
451
extension UniquePermutations : Sequence {
452
+ /// The iterator for a `UniquePermutations` instance.
444
453
public struct Iterator : IteratorProtocol {
445
454
@usableFromInline
446
455
var elements : [ Element ]
@@ -510,9 +519,9 @@ extension Sequence where Element: Comparable {
510
519
/// // [2, 1, 2]
511
520
/// // [2, 2, 1]
512
521
///
513
- /// By contrast, the unadorned `permutations()` method permutes over a
514
- /// collection by position, which includes permutations with equal elements
515
- /// in each permutation:
522
+ /// By contrast, the unadorned `permutations()` method permutes a collection's
523
+ /// elements by position, and includes permutations with equal elements in
524
+ /// each permutation:
516
525
///
517
526
/// for perm in numbers.permutations()
518
527
/// print(perm)
@@ -529,10 +538,60 @@ extension Sequence where Element: Comparable {
529
538
UniquePermutations ( self , by: < )
530
539
}
531
540
541
+ /// Returns a sequence of the unique permutations of this sequence of the
542
+ /// specified length.
543
+ ///
544
+ /// Use this method to iterate over the unique permutations of a sequence
545
+ /// with repeating elements. This example prints every unique two-element
546
+ /// permutation of an array of numbers:
547
+ ///
548
+ /// let numbers = [1, 1, 2]
549
+ /// for perm in numbers.uniquePermutations(ofCount: 2) {
550
+ /// print(perm)
551
+ /// }
552
+ /// // [1, 1]
553
+ /// // [1, 2]
554
+ /// // [2, 1]
555
+ ///
556
+ /// By contrast, the `permutations(ofCount:)` method permutes a collection's
557
+ /// elements by position, and can include permutations with equal elements
558
+ /// in each permutation:
559
+ ///
560
+ /// for perm in numbers.permutations(ofCount: 2)
561
+ /// print(perm)
562
+ /// }
563
+ /// // [1, 1]
564
+ /// // [1, 1]
565
+ /// // [1, 2]
566
+ /// // [1, 2]
567
+ /// // [2, 1]
568
+ /// // [2, 1]
569
+ ///
570
+ /// The returned permutations are in lexicographically sorted order.
532
571
public func uniquePermutations( ofCount k: Int ) -> UniquePermutations < Element > {
533
572
UniquePermutations ( self , k ..< ( k + 1 ) , by: < )
534
573
}
535
574
575
+ /// Returns a collection of the unique permutations of this sequence with
576
+ /// lengths in the specified range.
577
+ ///
578
+ /// Use this method to iterate over the unique permutations of a sequence
579
+ /// with repeating elements. This example prints every unique permutation
580
+ /// of an array of numbers with lengths through 2 elements:
581
+ ///
582
+ /// let numbers = [1, 1, 2]
583
+ /// for perm in numbers.uniquePermutations(ofCount: ...2) {
584
+ /// print(perm)
585
+ /// }
586
+ /// // []
587
+ /// // [1]
588
+ /// // [2]
589
+ /// // [1, 1]
590
+ /// // [1, 2]
591
+ /// // [2, 1]
592
+ ///
593
+ /// The returned permutations are in ascending order by length, and then
594
+ /// lexicographically within each group of the same length.
536
595
public func uniquePermutations< R: RangeExpression > ( ofCount kRange: R ) -> UniquePermutations < Element >
537
596
where R. Bound == Int
538
597
{
@@ -541,15 +600,29 @@ extension Sequence where Element: Comparable {
541
600
}
542
601
543
602
extension Sequence {
544
- public func uniquePermutations( by areInIncreasingOrder: @escaping ( Element , Element ) -> Bool ) -> UniquePermutations < Element > {
603
+ /// Returns a sequence of the unique permutations of this sequence.
604
+ public func uniquePermutations(
605
+ by areInIncreasingOrder: @escaping ( Element , Element ) -> Bool
606
+ ) -> UniquePermutations < Element > {
545
607
UniquePermutations ( self , by: areInIncreasingOrder)
546
608
}
547
609
548
- public func uniquePermutations( ofCount k: Int , by areInIncreasingOrder: @escaping ( Element , Element ) -> Bool ) -> UniquePermutations < Element > {
610
+ /// Returns a sequence of the unique permutations of this sequence of the
611
+ /// specified length.
612
+ ///
613
+ public func uniquePermutations(
614
+ ofCount k: Int ,
615
+ by areInIncreasingOrder: @escaping ( Element , Element ) -> Bool
616
+ ) -> UniquePermutations < Element > {
549
617
UniquePermutations ( self , k ..< ( k + 1 ) , by: areInIncreasingOrder)
550
618
}
551
619
552
- public func uniquePermutations< R: RangeExpression > ( ofCount kRange: R , by areInIncreasingOrder: @escaping ( Element , Element ) -> Bool ) -> UniquePermutations < Element >
620
+ /// Returns a collection of the unique permutations of this sequence with
621
+ /// lengths in the specified range.
622
+ public func uniquePermutations< R: RangeExpression > (
623
+ ofCount kRange: R ,
624
+ by areInIncreasingOrder: @escaping ( Element , Element ) -> Bool
625
+ ) -> UniquePermutations < Element >
553
626
where R. Bound == Int
554
627
{
555
628
UniquePermutations ( self , kRange, by: areInIncreasingOrder)
0 commit comments