-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathPermutations.swift
592 lines (534 loc) · 19.5 KB
/
Permutations.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Algorithms open source project
//
// Copyright (c) 2020 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
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
// nextPermutation()
//===----------------------------------------------------------------------===//
extension MutableCollection
where Self: BidirectionalCollection, Element: Comparable
{
/// Permutes this collection's elements through all the lexical orderings.
///
/// Call `nextPermutation()` repeatedly starting with the collection in sorted
/// order. When the full cycle of all permutations has been completed, the
/// collection will be back in sorted order and this method will return
/// `false`.
///
/// - Returns: A Boolean value indicating whether the collection still has
/// remaining permutations. When this method returns `false`, the collection
/// is in ascending order according to `areInIncreasingOrder`.
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@inlinable
internal mutating func nextPermutation(upperBound: Index? = nil) -> Bool {
// Ensure we have > 1 element in the collection.
guard !isEmpty else { return false }
var i = index(before: endIndex)
if i == startIndex { return false }
let upperBound = upperBound ?? endIndex
while true {
let ip1 = i
formIndex(before: &i)
// Find the last ascending pair (ie. ..., a, b, ... where a < b)
if self[i] < self[ip1] {
// Find the last element greater than self[i]
// This is _always_ at most `ip1` due to if statement above
let j = lastIndex(where: { self[i] < $0 })!
// At this point we have something like this:
// 0, 1, 4, 3, 2
// ^ ^
// i j
swapAt(i, j)
self.reverse(subrange: ip1 ..< endIndex)
// Only return if we've made a change within ..<upperBound region
if i < upperBound {
return true
} else {
i = index(before: endIndex)
continue
}
}
if i == startIndex {
self.reverse()
return false
}
}
}
}
//===----------------------------------------------------------------------===//
// struct Permutations<Base>
//===----------------------------------------------------------------------===//
/// A sequence of all the permutations of a collection's elements.
public struct PermutationsSequence<Base: Collection> {
/// The base collection to iterate over for permutations.
@usableFromInline
internal let base: Base
@usableFromInline
internal let baseCount: Int
/// The range of accepted sizes of permutations.
///
/// - Note: This may be empty if the attempted range entirely exceeds the
/// bounds of the size of the `base` collection.
@usableFromInline
internal let kRange: Range<Int>
/// Initializes a `PermutationsSequence` for all permutations of `base` of
/// size `k`.
///
/// - Parameters:
/// - base: The collection to iterate over for permutations
/// - k: The expected size of each permutation, or `nil` (default) to
/// iterate over all permutations of the same size as the base collection.
@inlinable
internal init(_ base: Base, k: Int? = nil) {
let kRange: ClosedRange<Int>?
if let countToChoose = k {
kRange = countToChoose ... countToChoose
} else {
kRange = nil
}
self.init(base, kRange: kRange)
}
/// Initializes a `PermutationsSequence` for all combinations of `base` of
/// sizes within a given range.
///
/// - Parameters:
/// - base: The collection to iterate over for permutations.
/// - kRange: The range of accepted sizes of permutations, or `nil` to
/// iterate over all permutations of the same size as the base collection.
@inlinable
internal init<R: RangeExpression>(
_ base: Base, kRange: R?
) where R.Bound == Int {
self.base = base
let baseCount = base.count
self.baseCount = baseCount
let upperBound = baseCount + 1
self.kRange = kRange?.relative(to: 0 ..< .max)
.clamped(to: 0 ..< upperBound) ??
baseCount ..< upperBound
}
/// The total number of permutations.
@inlinable
public var count: Int {
kRange.map {
stride(from: baseCount, to: baseCount - $0, by: -1).reduce(1, *)
}.reduce(0, +)
}
}
extension PermutationsSequence: Sequence {
/// The iterator for a `PermutationsSequence` instance.
public struct Iterator: IteratorProtocol {
@usableFromInline
internal let base: Base
@usableFromInline
internal let baseCount: Int
/// The current range of accepted sizes of permutations.
/// - Note: The range is contracted until empty while iterating over
/// permutations of different sizes. When the range is empty, iteration is
/// finished.
@usableFromInline
internal var kRange: Range<Int>
/// Whether or not iteration is finished (`kRange` is empty)
@inlinable
internal var isFinished: Bool {
return kRange.isEmpty
}
@usableFromInline
internal var indexes: [Base.Index]
@inlinable
internal init(_ permutations: PermutationsSequence) {
self.base = permutations.base
self.baseCount = permutations.baseCount
self.kRange = permutations.kRange
self.indexes = Array(permutations.base.indices)
}
/// Advances the `indexes` array such that the first `countToChoose`
/// elements contain the next lexicographic ordering of elements.
///
/// Uses the SEP(n,k) algorithm, as described in:
/// https://alistairisrael.wordpress.com/2009/09/22/simple-efficient-pnk-algorithm/
///
/// - Returns: A Boolean value indicating whether `indexes` still has
/// remaining permutations. When this method returns `false`, `indexes`
/// is in ascending order.
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@inlinable
internal mutating func nextState() -> Bool {
let countToChoose = self.kRange.lowerBound
let edge = countToChoose - 1
// Find first index greater than the one at `edge`.
if let i = indexes[countToChoose...].firstIndex(where: { indexes[edge] < $0 }) {
indexes.swapAt(edge, i)
} else {
indexes.reverse(subrange: countToChoose ..< indexes.endIndex)
// Find last increasing pair below `edge`.
// TODO: This could be indexes[..<edge].adjacentPairs().lastIndex(where: ...)
var lastAscent = edge - 1
while (lastAscent >= 0 && indexes[lastAscent] >= indexes[lastAscent + 1]) {
lastAscent -= 1
}
if lastAscent < 0 {
return false
}
// Find rightmost index less than that at `lastAscent`.
if let i = indexes[lastAscent...].lastIndex(where: { indexes[lastAscent] < $0 }) {
indexes.swapAt(lastAscent, i)
}
indexes.reverse(subrange: (lastAscent + 1) ..< indexes.endIndex)
}
return true
}
@inlinable
public mutating func next() -> [Base.Element]? {
guard !isFinished else { return nil }
/// Advances `kRange` by incrementing its `lowerBound` until the range is
/// empty, when iteration is finished.
func advanceKRange() {
kRange.removeFirst()
indexes = Array(base.indices)
}
let countToChoose = self.kRange.lowerBound
if countToChoose == 0 {
defer {
advanceKRange()
}
return []
}
let permutesFullCollection = (countToChoose == baseCount)
if permutesFullCollection {
// If we're permuting the full collection, each iteration is just a
// call to `nextPermutation` on `indexes`.
defer {
let hasMorePermutations = indexes.nextPermutation()
if !hasMorePermutations {
advanceKRange()
}
}
return indexes.map { base[$0] }
} else {
// Otherwise, return the items at the first `countToChoose` indices and
// advance the state.
defer {
let hasMorePermutations = nextState()
if !hasMorePermutations {
advanceKRange()
}
}
return indexes.prefix(countToChoose).map { base[$0] }
}
}
}
@inlinable
public func makeIterator() -> Iterator {
Iterator(self)
}
}
extension PermutationsSequence: LazySequenceProtocol
where Base: LazySequenceProtocol {}
//===----------------------------------------------------------------------===//
// permutations(ofCount:)
//===----------------------------------------------------------------------===//
extension Collection {
/// Returns a collection of the permutations of this collection with lengths
/// in the specified range.
///
/// This example prints the different permutations of one to two elements from
/// an array of three names:
///
/// let names = ["Alex", "Celeste", "Davide"]
/// for perm in names.permutations(ofCount: 1...2) {
/// print(perm.joined(separator: ", "))
/// }
/// // Alex
/// // Celeste
/// // Davide
/// // Alex, Celeste
/// // Alex, Davide
/// // Celeste, Alex
/// // Celeste, Davide
/// // Davide, Alex
/// // Davide, Celeste
///
/// This example prints _all_ the permutations (including an empty array) from
/// an array of numbers:
///
/// let numbers = [10, 20, 30]
/// for perm in numbers.permutations(ofCount: 0...) {
/// print(perm)
/// }
/// // []
/// // [10]
/// // [20]
/// // [30]
/// // [10, 20]
/// // [10, 30]
/// // [20, 10]
/// // [20, 30]
/// // [30, 10]
/// // [30, 20]
/// // [10, 20, 30]
/// // [10, 30, 20]
/// // [20, 10, 30]
/// // [20, 30, 10]
/// // [30, 10, 20]
/// // [30, 20, 10]
///
/// The returned permutations are in ascending order by length, and then
/// lexicographically within each group of the same length.
///
/// - Parameter kRange: A range of the number of elements to include in each
/// permutation. `kRange` can be any integer range expression, and is
/// clamped to the number of elements in this collection. Passing a range
/// covering sizes greater than the number of elements in this collection
/// results in an empty sequence.
///
/// - Complexity: O(1) for random-access base collections. O(*n*) where *n*
/// is the number of elements in the base collection, since
/// `PermutationsSequence` accesses the `count` of the base collection.
@inlinable
public func permutations<R: RangeExpression>(
ofCount kRange: R
) -> PermutationsSequence<Self> where R.Bound == Int {
PermutationsSequence(self, kRange: kRange)
}
/// Returns a collection of the permutations of this collection of the
/// specified length.
///
/// This example prints the different permutations of two elements from an
/// array of three names:
///
/// let names = ["Alex", "Celeste", "Davide"]
/// for perm in names.permutations(ofCount: 2) {
/// print(perm.joined(separator: ", "))
/// }
/// // Alex, Celeste
/// // Alex, Davide
/// // Celeste, Alex
/// // Celeste, Davide
/// // Davide, Alex
/// // Davide, Celeste
///
/// The permutations present the elements in increasing lexicographic order
/// of the collection's original ordering (rather than the order of the
/// elements themselves). The first permutation will always consist of
/// elements in their original order, and the last will have the elements in
/// the reverse of their original order.
///
/// Values that are repeated in the original collection are always treated as
/// separate values in the resulting permutations:
///
/// let numbers = [20, 10, 10]
/// for perm in numbers.permutations() {
/// print(perm)
/// }
/// // [20, 10, 10]
/// // [20, 10, 10]
/// // [10, 20, 10]
/// // [10, 10, 20]
/// // [10, 20, 10]
/// // [10, 10, 20]
///
/// If `k` is zero, the resulting sequence has exactly one element, an
/// empty array. If `k` is greater than the number of elements in this
/// sequence, the resulting sequence has no elements.
///
/// - Parameter k: The number of elements to include in each permutation.
/// If `k` is `nil`, the resulting sequence represents permutations of this
/// entire collection. If `k` is greater than the number of elements in
/// this collection, the resulting sequence is empty.
///
/// - Complexity: O(1) for random-access base collections. O(*n*) where *n*
/// is the number of elements in the base collection, since
/// `PermutationsSequence` accesses the `count` of the base collection.
@inlinable
public func permutations(ofCount k: Int? = nil) -> PermutationsSequence<Self> {
precondition(
k ?? 0 >= 0,
"Can't have permutations with a negative number of elements.")
return PermutationsSequence(self, k: k)
}
}
//===----------------------------------------------------------------------===//
// uniquePermutations()
//===----------------------------------------------------------------------===//
/// A sequence of the unique permutations of the elements of a sequence or
/// collection.
///
/// To create a `UniquePermutationsSequence` instance, call one of the
/// `uniquePermutations` methods on your collection.
public struct UniquePermutationsSequence<Base: Collection> {
/// The base collection to iterate over for permutations.
@usableFromInline
internal let base: Base
@usableFromInline
internal var indexes: [Base.Index]
@usableFromInline
internal let kRange: Range<Int>
}
extension UniquePermutationsSequence where Base.Element: Hashable {
@inlinable
internal static func _indexes(_ base: Base) -> [Base.Index] {
let firstIndexesAndCountsByElement = Dictionary(
base.indices.lazy.map { (base[$0], ($0, 1)) },
uniquingKeysWith: { indexAndCount, _ in (indexAndCount.0, indexAndCount.1 + 1) })
return firstIndexesAndCountsByElement
.values.sorted(by: { $0.0 < $1.0 })
.flatMap { index, count in repeatElement(index, count: count) }
}
@inlinable
internal init(_ elements: Base) {
self.indexes = Self._indexes(elements)
self.base = elements
self.kRange = self.indexes.count ..< (self.indexes.count + 1)
}
@inlinable
internal init<R: RangeExpression>(_ base: Base, _ range: R)
where R.Bound == Int
{
self.indexes = Self._indexes(base)
self.base = base
let upperBound = self.indexes.count + 1
self.kRange = range.relative(to: 0 ..< .max)
.clamped(to: 0 ..< upperBound)
}
}
extension UniquePermutationsSequence: Sequence {
/// The iterator for a `UniquePermutationsSequence` instance.
public struct Iterator: IteratorProtocol {
@usableFromInline
internal let base: Base
@usableFromInline
internal var indexes: [Base.Index]
@usableFromInline
internal var lengths: Range<Int>
@usableFromInline
internal var initial = true
@inlinable
internal init(_ elements: Base, indexes: [Base.Index], lengths: Range<Int>) {
self.base = elements
self.indexes = indexes
self.lengths = lengths
}
@inlinable
public mutating func next() -> [Base.Element]? {
// In the end case, `lengths` is an empty range.
if lengths.isEmpty {
return nil
}
// The first iteration must produce the original sorted array, before any
// permutations. We skip the permutation the first time so that we can
// always mutate the array _before_ returning a slice, which avoids
// copying when possible.
if initial {
initial = false
return indexes[..<lengths.lowerBound].map { base[$0] }
}
if !indexes.nextPermutation(upperBound: lengths.lowerBound) {
lengths.removeFirst()
if lengths.isEmpty {
return nil
}
}
return indexes[..<lengths.lowerBound].map { base[$0] }
}
}
@inlinable
public func makeIterator() -> Iterator {
Iterator(base, indexes: indexes, lengths: kRange)
}
}
extension UniquePermutationsSequence: LazySequenceProtocol
where Base: LazySequenceProtocol {}
extension Collection where Element: Hashable {
/// Returns a sequence of the unique permutations of this sequence of the
/// specified length.
///
/// Use this method to iterate over the unique permutations of a sequence
/// with repeating elements. This example prints every unique two-element
/// permutation of an array of numbers:
///
/// let numbers = [1, 1, 2]
/// for perm in numbers.uniquePermutations(ofCount: 2) {
/// print(perm)
/// }
/// // [1, 1]
/// // [1, 2]
/// // [2, 1]
///
/// By contrast, the `permutations(ofCount:)` method permutes a collection's
/// elements by position, and can include permutations with equal elements
/// in each permutation:
///
/// for perm in numbers.permutations(ofCount: 2)
/// print(perm)
/// }
/// // [1, 1]
/// // [1, 1]
/// // [1, 2]
/// // [1, 2]
/// // [2, 1]
/// // [2, 1]
///
/// The returned permutations are in lexicographically sorted order.
///
/// - Parameter k: The number of elements to include in each permutation.
/// If `k` is `nil`, the resulting sequence represents permutations of this
/// entire collection. If `k` is greater than the number of elements in
/// this collection, the resulting sequence is empty.
///
/// - Complexity: O(*n*), where *n* is the number of elements in this
/// collection.
@inlinable
public func uniquePermutations(ofCount k: Int? = nil)
-> UniquePermutationsSequence<Self>
{
if let k = k {
return UniquePermutationsSequence(self, k ..< (k + 1))
} else {
return UniquePermutationsSequence(self)
}
}
/// Returns a collection of the unique permutations of this sequence with
/// lengths in the specified range.
///
/// Use this method to iterate over the unique permutations of a sequence
/// with repeating elements. This example prints every unique permutation
/// of an array of numbers with lengths through 2 elements:
///
/// let numbers = [1, 1, 2]
/// for perm in numbers.uniquePermutations(ofCount: ...2) {
/// print(perm)
/// }
/// // []
/// // [1]
/// // [2]
/// // [1, 1]
/// // [1, 2]
/// // [2, 1]
///
/// The returned permutations are in ascending order by length, and then
/// lexicographically within each group of the same length.
///
/// - Parameter kRange: A range of the number of elements to include in each
/// permutation. `kRange` can be any integer range expression, and is
/// clamped to the number of elements in this collection. Passing a range
/// covering sizes greater than the number of elements in this collection
/// results in an empty sequence.
///
/// - Complexity: O(*n*), where *n* is the number of elements in this
/// collection.
@inlinable
public func uniquePermutations<R: RangeExpression>(
ofCount kRange: R
) -> UniquePermutationsSequence<Self> where R.Bound == Int {
UniquePermutationsSequence(self, kRange)
}
}