Skip to content

Commit

Permalink
NotEnoughMemory --> TooSmallSieveSize
Browse files Browse the repository at this point in the history
  • Loading branch information
JSorngard committed May 10, 2024
1 parent 58e7cbe commit b44fa9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub const fn primes<const N: usize>() -> [Underlying; N] {
/// # use const_primes::{primes_lt, GenerationError};
/// const TOO_LARGE_LIMIT: Result<[u64; 3], GenerationError> = primes_lt::<3, 5>(26);
/// const TOO_SMALL_LIMIT: Result<[u64; 1], GenerationError> = primes_lt::<1, 1>(1);
/// assert_eq!(TOO_LARGE_LIMIT, Err(GenerationError::NotEnoughMemory));
/// assert_eq!(TOO_LARGE_LIMIT, Err(GenerationError::TooSmallSieveSize));
/// assert_eq!(TOO_SMALL_LIMIT, Err(GenerationError::TooSmallLimit));
/// ```
///
Expand All @@ -184,7 +184,7 @@ pub const fn primes_lt<const N: usize, const MEM: usize>(
}

if upper_limit > mem_sqr {
return Err(GenerationError::NotEnoughMemory);
return Err(GenerationError::TooSmallSieveSize);
}

let mut primes: [u64; N] = [0; N];
Expand Down Expand Up @@ -319,7 +319,7 @@ macro_rules! primes_segment {
/// ```
/// # use const_primes::{primes_geq, GenerationError};
/// const PRIMES: Result<[u64; 5], GenerationError> = primes_geq::<5, 5>(26);
/// assert_eq!(PRIMES, Err(GenerationError::NotEnoughMemory));
/// assert_eq!(PRIMES, Err(GenerationError::TooSmallSieveSize));
/// ```
///
/// # Panics
Expand Down Expand Up @@ -352,7 +352,7 @@ pub const fn primes_geq<const N: usize, const MEM: usize>(
}

if lower_limit >= mem_sqr {
return Err(GenerationError::NotEnoughMemory);
return Err(GenerationError::TooSmallSieveSize);
}

let mut primes = [0; N];
Expand Down Expand Up @@ -397,7 +397,7 @@ pub const fn primes_geq<const N: usize, const MEM: usize>(
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GenerationError {
/// The limit was larger than or equal to `MEM^2`.
NotEnoughMemory,
TooSmallSieveSize,
/// The limit was smaller than or equal to 2.
TooSmallLimit,
/// Encountered a number larger than or equal to `MEM`^2.
Expand All @@ -409,7 +409,7 @@ pub enum GenerationError {
impl fmt::Display for GenerationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::NotEnoughMemory => write!(
Self::TooSmallSieveSize => write!(
f,
"the limit was larger than `MEM`^2"
),
Expand Down
18 changes: 10 additions & 8 deletions src/sieving.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;

use crate::isqrt;

/// Uses the primalities of the first `N` integers in `base_sieve` to sieve the numbers in the range `[upper_limit - N, upper_limit)`.
Expand Down Expand Up @@ -104,7 +106,7 @@ pub(crate) const fn sieve_segment<const N: usize>(
/// ```
/// # use const_primes::{sieve_lt, SieveError};
/// const PS: Result<[bool; 5], SieveError> = sieve_lt::<5, 5>(26);
/// assert_eq!(PS, Err(SieveError::NotEnoughMemory));
/// assert_eq!(PS, Err(SieveError::TooSmallSieveSize));
/// ```
/// or smaller than `N`:
/// ```
Expand Down Expand Up @@ -133,7 +135,7 @@ pub const fn sieve_lt<const N: usize, const MEM: usize>(
};

if upper_limit > mem_sqr {
return Err(SieveError::NotEnoughMemory);
return Err(SieveError::TooSmallSieveSize);
}

let n64 = N as u64;
Expand Down Expand Up @@ -225,16 +227,16 @@ pub enum SieveError {
/// The limit was less than or equal to `N` (for `sieve_lt`).
TooSmallLimit,
/// `limit` or `limit + MEM` was larger than or equal to `MEM`^2.
NotEnoughMemory,
TooSmallSieveSize,
/// `limit + MEM` did not fit in a `u64`.
TotalDoesntFitU64,
}

impl core::fmt::Display for SieveError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
impl fmt::Display for SieveError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooSmallLimit => write!(f, "`limit` must be at least `N`"),
Self::NotEnoughMemory => {
Self::TooSmallSieveSize => {
write!(f, "`MEM`^2 was smaller than the largest encountered value")
}
Self::TotalDoesntFitU64 => write!(f, "`MEM + limit` must fit in a `u64`"),
Expand Down Expand Up @@ -290,7 +292,7 @@ impl std::error::Error for SieveError {}
/// # use const_primes::{sieve_geq, SieveError};
/// const P1: Result<[bool; 5], SieveError> = sieve_geq::<5, 5>(21);
/// const P2: Result<[bool; 5], SieveError> = sieve_geq::<5, 5>(u64::MAX);
/// assert_eq!(P1, Err(SieveError::NotEnoughMemory));
/// assert_eq!(P1, Err(SieveError::TooSmallSieveSize));
/// assert_eq!(P2, Err(SieveError::TotalDoesntFitU64));
/// ```
///
Expand Down Expand Up @@ -318,7 +320,7 @@ pub const fn sieve_geq<const N: usize, const MEM: usize>(
};

if upper_limit > mem_sqr {
return Err(SieveError::NotEnoughMemory);
return Err(SieveError::TooSmallSieveSize);
}

// If `lower_limit` is zero then this is the same as just calling `sieve`, and we can return early.
Expand Down

0 comments on commit b44fa9d

Please sign in to comment.