@@ -14,6 +14,9 @@ public struct Combinations<Base: Collection> {
14
14
/// The collection to iterate over for combinations.
15
15
public let base : Base
16
16
17
+ @usableFromInline
18
+ internal let baseCount : Int
19
+
17
20
/// The range of accepted sizes of combinations.
18
21
/// - Note: This may be `nil` if the attempted range entirely exceeds the
19
22
/// upper bounds of the size of the `base` collection.
@@ -40,18 +43,20 @@ public struct Combinations<Base: Collection> {
40
43
) where R. Bound == Int {
41
44
let range = kRange. relative ( to: 0 ..< . max)
42
45
self . base = base
43
- let upperBound = base. count + 1
46
+ let baseCount = base. count
47
+ self . baseCount = baseCount
48
+ let upperBound = baseCount + 1
44
49
self . kRange = range. lowerBound < upperBound
45
- ? range. clamped ( to: 0 ..< upperBound)
50
+ ? range. clamped ( to: 0 ..< upperBound)
46
51
: nil
47
52
}
48
53
49
54
/// The total number of combinations.
50
55
@inlinable
51
56
public var count : Int {
52
57
guard let k = self . kRange else { return 0 }
53
- let n = base . count
54
- if k == 0 ..< ( n + 1 ) {
58
+ let n = baseCount
59
+ if k == 0 ..< ( n + 1 ) {
55
60
return 1 << n
56
61
}
57
62
@@ -123,7 +128,7 @@ extension Combinations: Sequence {
123
128
func advanceKRange( ) {
124
129
if kRange. lowerBound < kRange. upperBound {
125
130
let advancedLowerBound = kRange. lowerBound + 1
126
- kRange = advancedLowerBound..< kRange. upperBound
131
+ kRange = advancedLowerBound ..< kRange. upperBound
127
132
indexes. removeAll ( keepingCapacity: true )
128
133
indexes. append ( contentsOf: base. indices. prefix ( kRange. lowerBound) )
129
134
}
0 commit comments