Skip to content

Commit

Permalink
Some documentation and such (#114).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Dec 5, 2023
1 parent dfec34c commit 6514050
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
20 changes: 17 additions & 3 deletions Sources/NBKCoreKit/Models/NBKPrimeSieve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

/// A generator of all prime `elements` through some `limit`.
///
/// It sieves one page on creation, then one per call to `increment()`.
///
/// ### Sieve all primes through some « limit »
///
/// ```swift
/// let ((sieve)) = NBKPrimeSieve(size: .KiB(64))
/// let ((sieve)) = NBKPrimeSieve()
/// while sieve.limit < 1_000_000 {
/// ((sieve)).increment()
/// }
Expand All @@ -25,12 +27,24 @@
/// ### Sieve at least « count » number of primes
///
/// ```swift
/// let ((sieve)) = NBKPrimeSieve(size: .KiB(64))
/// let ((sieve)) = NBKPrimeSieve()
/// while sieve.elements.count < 1_000_000 {
/// ((sieve)).increment()
/// }
/// ```
///
/// ### Customization
///
/// This sieve offers multiple customization options, which may improve performance.
/// Here's an example that's suitable for numbers up to a billion on a machine where
/// the CPU's L1 data cache is 128 KiB:
///
/// ```swift
/// NBKPrimeSieve(cache: .KiB(128), wheel: .x11, culls: .x31, capacity: 50863957)
/// ```
///
/// - Note: The size of the cache determines the size of each increment.
///
public final class NBKPrimeSieve: CustomStringConvertible {

//=------------------------------------------------------------------------=
Expand Down Expand Up @@ -593,7 +607,7 @@ extension NBKPrimeSieve {

@usableFromInline static func pattern(primes: [UInt]) -> [UInt] {
var pattern = [UInt](repeating: UInt.max, count: Int(primes.reduce(1, *)))
var next:(prime: UInt, product: UInt); next.prime = primes.first!; next.product = next.prime
var next:(prime: UInt, product: UInt); next.prime = primes.first!; next.product = next.prime
var primeIndex = primes.startIndex; while primeIndex < primes.endIndex {
//=----------------------------------=
let current: (prime: UInt, product: UInt) = next
Expand Down
8 changes: 4 additions & 4 deletions Tests/NBKCoreKitTests/Models/NBKPrimeSieve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,10 @@ final class NBKPrimeSieveTests: XCTestCase {
}

func testDescriptionDoesNotContainEveryPrimeBecauseThatWouldBeSilly() {
XCTAssertEqual(T(cache: .words(064 / UInt.bitWidth)).description, "\(T.self)(limit: 127, count: 31)")
XCTAssertEqual(T(cache: .words(128 / UInt.bitWidth)).description, "\(T.self)(limit: 255, count: 54)")
XCTAssertEqual(T(cache: .words(192 / UInt.bitWidth)).description, "\(T.self)(limit: 383, count: 76)")
XCTAssertEqual(T(cache: .words(256 / UInt.bitWidth)).description, "\(T.self)(limit: 511, count: 97)")
XCTAssertEqual(String(describing: T(cache: .words(064 / UInt.bitWidth))), "\(T.self)(limit: 127, count: 31)")
XCTAssertEqual(String(describing: T(cache: .words(128 / UInt.bitWidth))), "\(T.self)(limit: 255, count: 54)")
XCTAssertEqual(String(describing: T(cache: .words(192 / UInt.bitWidth))), "\(T.self)(limit: 383, count: 76)")
XCTAssertEqual(String(describing: T(cache: .words(256 / UInt.bitWidth))), "\(T.self)(limit: 511, count: 97)")
}

//=------------------------------------------------------------------------=
Expand Down

0 comments on commit 6514050

Please sign in to comment.