Skip to content

Commit 7a2eda7

Browse files
committed
Add method documentation
1 parent d895e67 commit 7a2eda7

File tree

2 files changed

+103
-28
lines changed

2 files changed

+103
-28
lines changed

Sources/Algorithms/Permutations.swift

Lines changed: 101 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -316,34 +316,37 @@ extension Collection {
316316
/// // Davide, Celeste
317317
///
318318
/// This example prints _all_ the permutations (including an empty array) from
319-
/// the an array of numbers:
319+
/// an array of numbers:
320320
///
321321
/// 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.
341344
///
342345
/// - Parameter kRange: The number of elements to include in each permutation.
343346
///
344347
/// - 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.
347350
@inlinable
348351
public func permutations<R: RangeExpression>(
349352
ofCount kRange: R
@@ -412,6 +415,11 @@ extension Collection {
412415
// uniquePermutations()
413416
//===----------------------------------------------------------------------===//
414417

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.
415423
public struct UniquePermutations<Element> {
416424
@usableFromInline
417425
internal let elements: [Element]
@@ -441,6 +449,7 @@ public struct UniquePermutations<Element> {
441449
}
442450

443451
extension UniquePermutations: Sequence {
452+
/// The iterator for a `UniquePermutations` instance.
444453
public struct Iterator: IteratorProtocol {
445454
@usableFromInline
446455
var elements: [Element]
@@ -510,9 +519,9 @@ extension Sequence where Element: Comparable {
510519
/// // [2, 1, 2]
511520
/// // [2, 2, 1]
512521
///
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:
516525
///
517526
/// for perm in numbers.permutations()
518527
/// print(perm)
@@ -529,10 +538,60 @@ extension Sequence where Element: Comparable {
529538
UniquePermutations(self, by: <)
530539
}
531540

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.
532571
public func uniquePermutations(ofCount k: Int) -> UniquePermutations<Element> {
533572
UniquePermutations(self, k ..< (k + 1), by: <)
534573
}
535574

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.
536595
public func uniquePermutations<R: RangeExpression>(ofCount kRange: R) -> UniquePermutations<Element>
537596
where R.Bound == Int
538597
{
@@ -541,15 +600,29 @@ extension Sequence where Element: Comparable {
541600
}
542601

543602
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> {
545607
UniquePermutations(self, by: areInIncreasingOrder)
546608
}
547609

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> {
549617
UniquePermutations(self, k ..< (k + 1), by: areInIncreasingOrder)
550618
}
551619

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>
553626
where R.Bound == Int
554627
{
555628
UniquePermutations(self, kRange, by: areInIncreasingOrder)

Tests/SwiftAlgorithmsTests/UniquePermutationsTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ final class UniquePermutationsTests: XCTestCase {
6060
static var numbersAndNilsPermutations: [[ArraySlice<Int?>]] {
6161
numbersPermutations.map { $0.map { ArraySlice($0.map { $0 == 1 ? nil : $0 }) }}
6262
}
63+
}
6364

65+
extension UniquePermutationsTests {
6466
func testEmpty() {
6567
XCTAssertEqualSequences(([] as [Int]).uniquePermutations(), [[]])
6668
XCTAssertEqualSequences(([] as [Int]).uniquePermutations(ofCount: 0), [[]])

0 commit comments

Comments
 (0)