Skip to content

Commit

Permalink
[NBKCoreKit] Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Dec 7, 2023
1 parent 0c9bcde commit 81e6bc2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
12 changes: 6 additions & 6 deletions Sources/NBKCoreKit/Models/NBKPrimeSieve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
//=--------------------------------------=
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
//=--------------------------------------=
Expand Down Expand Up @@ -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]]()
Expand All @@ -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.
///
Expand Down
48 changes: 35 additions & 13 deletions Sources/NBKCoreKit/Private/NBKCyclicIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
}
Expand All @@ -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))
}
}

0 comments on commit 81e6bc2

Please sign in to comment.