Skip to content

Commit 81e6bc2

Browse files
committed
[NBKCoreKit] Cleanup.
1 parent 0c9bcde commit 81e6bc2

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

Sources/NBKCoreKit/Models/NBKPrimeSieve.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ extension NBKPrimeSieve {
150150

151151
for pattern in self.culls.patterns {
152152
var pattern = NBK.CyclicIterator(pattern)!
153-
pattern.set(iteration: iteration)
153+
pattern.formIteration(iteration)
154154
self.cache.sieve(pattern: pattern)
155155
}
156156
//=--------------------------------------=
@@ -172,8 +172,8 @@ extension NBKPrimeSieve {
172172
guard product.partialValue <= limit, !product.overflow else { continue }
173173
Swift.assert(start <= product.partialValue)
174174

175-
inner.set(unchecked: Int(bitPattern: self.wheel.indices[Int(bitPattern: multiple % self.wheel.circumference)]))
176-
cache.sieve(from: (product.partialValue &- start) &>> 1 as UInt,stride:{ prime &* inner.next() &>> 1 as UInt }) // OK
175+
inner.setIndex(unchecked: Int(bitPattern: self.wheel.indices[Int(bitPattern: multiple % self.wheel.circumference)]))
176+
cache.sieve(from: (product.partialValue &- start) &>> 1 as UInt, stride:{ prime &* inner.next() &>> 1 as UInt }) // OK
177177
}
178178
//=--------------------------------------=
179179
Self.commit(&self.cache, to: &self.state)
@@ -236,7 +236,7 @@ extension NBKPrimeSieve {
236236
defer { value &+= outer.next() }
237237
guard cache[value &>> 1 as UInt] else { continue }
238238

239-
inner.set(unchecked: (wheel).indices[Int(bitPattern: value % wheel.circumference)])
239+
inner.setIndex(unchecked: wheel.indices[Int(bitPattern: value % wheel.circumference)])
240240
cache.sieve(from: square.partialValue &>> 1 as UInt, stride:{ value &* inner.next() &>> 1 as UInt }) // OK
241241
}
242242
//=--------------------------------------=
@@ -591,7 +591,7 @@ extension NBKPrimeSieve {
591591

592592
/// Patterns grow multiplicatively, so chunking reduces memory cost.
593593
///
594-
/// g([3, 5, 7, 11, 13]) -> [f([2, 13]), f([5, 11]), f([7])]
594+
/// g([3, 5, 7, 11, 13]) -> [f([3, 13]), f([5, 11]), f([7])]
595595
///
596596
@usableFromInline static func patterns(primes: [UInt]) -> [[UInt]] {
597597
var patterns = [[UInt]]()
@@ -611,7 +611,7 @@ extension NBKPrimeSieve {
611611
return patterns as [[UInt]]
612612
}
613613

614-
/// A cyclical pattern marking each odd multiple of prime in `primes`.
614+
/// A cyclical pattern marking odd multiples of each prime in `primes`.
615615
///
616616
/// - Note: The sieve culls even numbers by omission.
617617
///

Sources/NBKCoreKit/Private/NBKCyclicIterator.swift

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ extension NBK {
2727
// MARK: Initializers
2828
//=--------------------------------------------------------------------=
2929

30+
/// Creates a new cyclic iterator of the given `base`, if possible.
31+
///
32+
/// - Requires: `!base.isEmpty`
33+
///
3034
@inlinable public init?(_ base: Base) {
3135
guard !base.isEmpty else { return nil }
3236
self.base = (base)
@@ -37,22 +41,30 @@ extension NBK {
3741
// MARK: Transformations
3842
//=--------------------------------------------------------------------=
3943

44+
/// Sets the current index to the first index.
45+
@inlinable public mutating func reset() {
46+
self.setIndex(unchecked: self.base.startIndex)
47+
}
48+
49+
/// Sets the current index to the unchecked `index` in `base`.
50+
///
51+
/// - Requires: `base.indices ~= index`
52+
///
53+
@inlinable public mutating func setIndex(unchecked index: Base.Index) {
54+
Swift.assert(index >= self.base.startIndex && index < self.base.endIndex)
55+
self.index = index
56+
}
57+
58+
/// Returns the element at the current index, then sets the next index.
4059
@inlinable public mutating func next() -> Base.Element {
4160
defer {
4261
self.base.formIndex(after: &self.index)
4362
if self.index == self.base.endIndex {
4463
self.reset()
4564
}
46-
}; return self.base[self.index] as Base.Element
47-
}
48-
49-
@inlinable public mutating func reset() {
50-
self.set(unchecked: self.base.startIndex)
51-
}
52-
53-
@inlinable public mutating func set(unchecked index: Base.Index) {
54-
Swift.assert(index >= self.base.startIndex && index < self.base.endIndex)
55-
self.index = index
65+
}
66+
67+
return self.base[self.index] as Base.Element
5668
}
5769
}
5870
}
@@ -67,8 +79,18 @@ extension NBK.CyclicIterator where Base: RandomAccessCollection {
6779
// MARK: Transformations
6880
//=------------------------------------------------------------------------=
6981

70-
@inlinable public mutating func set(iteration distance: UInt) {
71-
let remainder = Int(bitPattern: (distance) % UInt(bitPattern: self.base.count))
72-
self.set(unchecked: self.base.index(self.base.startIndex, offsetBy: remainder))
82+
/// Sets the current index to the cyclic index at `iteration`.
83+
///
84+
/// ```swift
85+
/// var iterator = NBK.CyclicIterator([111, 222, 333])
86+
/// iterator.formIteration(7) // 7 % 3 == 1
87+
/// iterator.next() // 222
88+
/// iterator.next() // 333
89+
/// iterator.next() // 111
90+
/// ```
91+
///
92+
@inlinable public mutating func formIteration(_ iteration: UInt) {
93+
let remainder = Int(bitPattern: iteration % UInt(bitPattern: self.base.count))
94+
self.setIndex(unchecked: self.base.index(self.base.startIndex, offsetBy: remainder))
7395
}
7496
}

0 commit comments

Comments
 (0)