From 81e6bc2a756bf08a825f572375ebeb4b79172475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Thu, 7 Dec 2023 11:51:58 +0100 Subject: [PATCH] [NBKCoreKit] Cleanup. --- Sources/NBKCoreKit/Models/NBKPrimeSieve.swift | 12 ++--- .../Private/NBKCyclicIterator.swift | 48 ++++++++++++++----- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/Sources/NBKCoreKit/Models/NBKPrimeSieve.swift b/Sources/NBKCoreKit/Models/NBKPrimeSieve.swift index 4f2b29dd..b84b7cf7 100644 --- a/Sources/NBKCoreKit/Models/NBKPrimeSieve.swift +++ b/Sources/NBKCoreKit/Models/NBKPrimeSieve.swift @@ -150,7 +150,7 @@ extension NBKPrimeSieve { for pattern in self.culls.patterns { var pattern = NBK.CyclicIterator(pattern)! - pattern.set(iteration: iteration) + pattern.formIteration(iteration) self.cache.sieve(pattern: pattern) } //=--------------------------------------= @@ -172,8 +172,8 @@ extension NBKPrimeSieve { guard product.partialValue <= limit, !product.overflow else { continue } Swift.assert(start <= product.partialValue) - inner.set(unchecked: Int(bitPattern: self.wheel.indices[Int(bitPattern: multiple % self.wheel.circumference)])) - cache.sieve(from: (product.partialValue &- start) &>> 1 as UInt,stride:{ prime &* inner.next() &>> 1 as UInt }) // OK + inner.setIndex(unchecked: Int(bitPattern: self.wheel.indices[Int(bitPattern: multiple % self.wheel.circumference)])) + cache.sieve(from: (product.partialValue &- start) &>> 1 as UInt, stride:{ prime &* inner.next() &>> 1 as UInt }) // OK } //=--------------------------------------= Self.commit(&self.cache, to: &self.state) @@ -236,7 +236,7 @@ extension NBKPrimeSieve { defer { value &+= outer.next() } guard cache[value &>> 1 as UInt] else { continue } - inner.set(unchecked: (wheel).indices[Int(bitPattern: value % wheel.circumference)]) + inner.setIndex(unchecked: wheel.indices[Int(bitPattern: value % wheel.circumference)]) cache.sieve(from: square.partialValue &>> 1 as UInt, stride:{ value &* inner.next() &>> 1 as UInt }) // OK } //=--------------------------------------= @@ -591,7 +591,7 @@ extension NBKPrimeSieve { /// Patterns grow multiplicatively, so chunking reduces memory cost. /// - /// g([3, 5, 7, 11, 13]) -> [f([2, 13]), f([5, 11]), f([7])] + /// g([3, 5, 7, 11, 13]) -> [f([3, 13]), f([5, 11]), f([7])] /// @usableFromInline static func patterns(primes: [UInt]) -> [[UInt]] { var patterns = [[UInt]]() @@ -611,7 +611,7 @@ extension NBKPrimeSieve { return patterns as [[UInt]] } - /// A cyclical pattern marking each odd multiple of prime in `primes`. + /// A cyclical pattern marking odd multiples of each prime in `primes`. /// /// - Note: The sieve culls even numbers by omission. /// diff --git a/Sources/NBKCoreKit/Private/NBKCyclicIterator.swift b/Sources/NBKCoreKit/Private/NBKCyclicIterator.swift index ed61089b..bf0b3ba0 100644 --- a/Sources/NBKCoreKit/Private/NBKCyclicIterator.swift +++ b/Sources/NBKCoreKit/Private/NBKCyclicIterator.swift @@ -27,6 +27,10 @@ extension NBK { // MARK: Initializers //=--------------------------------------------------------------------= + /// Creates a new cyclic iterator of the given `base`, if possible. + /// + /// - Requires: `!base.isEmpty` + /// @inlinable public init?(_ base: Base) { guard !base.isEmpty else { return nil } self.base = (base) @@ -37,22 +41,30 @@ extension NBK { // MARK: Transformations //=--------------------------------------------------------------------= + /// Sets the current index to the first index. + @inlinable public mutating func reset() { + self.setIndex(unchecked: self.base.startIndex) + } + + /// Sets the current index to the unchecked `index` in `base`. + /// + /// - Requires: `base.indices ~= index` + /// + @inlinable public mutating func setIndex(unchecked index: Base.Index) { + Swift.assert(index >= self.base.startIndex && index < self.base.endIndex) + self.index = index + } + + /// Returns the element at the current index, then sets the next index. @inlinable public mutating func next() -> Base.Element { defer { self.base.formIndex(after: &self.index) if self.index == self.base.endIndex { self.reset() } - }; return self.base[self.index] as Base.Element - } - - @inlinable public mutating func reset() { - self.set(unchecked: self.base.startIndex) - } - - @inlinable public mutating func set(unchecked index: Base.Index) { - Swift.assert(index >= self.base.startIndex && index < self.base.endIndex) - self.index = index + } + + return self.base[self.index] as Base.Element } } } @@ -67,8 +79,18 @@ extension NBK.CyclicIterator where Base: RandomAccessCollection { // MARK: Transformations //=------------------------------------------------------------------------= - @inlinable public mutating func set(iteration distance: UInt) { - let remainder = Int(bitPattern: (distance) % UInt(bitPattern: self.base.count)) - self.set(unchecked: self.base.index(self.base.startIndex, offsetBy: remainder)) + /// Sets the current index to the cyclic index at `iteration`. + /// + /// ```swift + /// var iterator = NBK.CyclicIterator([111, 222, 333]) + /// iterator.formIteration(7) // 7 % 3 == 1 + /// iterator.next() // 222 + /// iterator.next() // 333 + /// iterator.next() // 111 + /// ``` + /// + @inlinable public mutating func formIteration(_ iteration: UInt) { + let remainder = Int(bitPattern: iteration % UInt(bitPattern: self.base.count)) + self.setIndex(unchecked: self.base.index(self.base.startIndex, offsetBy: remainder)) } }