Skip to content

Commit

Permalink
implemented Sieve enum
Browse files Browse the repository at this point in the history
  • Loading branch information
flexatone committed Feb 17, 2024
1 parent 67c8599 commit 4a3a164
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
57 changes: 54 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
// use std::ops::Not;
use std::ops::Not;
use std::ops::BitAnd;
// use std::ops::BitOr;
use std::ops::BitOr;
use std::cmp::Ordering;


Expand Down Expand Up @@ -146,7 +146,8 @@ mod util {

}

#[derive(Debug)]
//------------------------------------------------------------------------------
#[derive(Clone, Debug)]
pub struct Residual {
modulus: u64,
shift: u64,
Expand Down Expand Up @@ -241,3 +242,53 @@ impl Ord for Residual {
.then_with(|| self.shift.cmp(&other.shift))
}
}

//------------------------------------------------------------------------------
#[derive(Clone, Debug)]
pub enum Sieve {
Residual(Residual),
Intersection(Box<Sieve>, Box<Sieve>),
Union(Box<Sieve>, Box<Sieve>),
Inversion(Box<Sieve>),
}

impl BitAnd for Sieve {
type Output = Sieve;

fn bitand(self, rhs: Self) -> Self::Output {
Sieve::Intersection(Box::new(self), Box::new(rhs))
}
}

impl BitOr for Sieve {
type Output = Sieve;

fn bitor(self, rhs: Self) -> Self::Output {
Sieve::Union(Box::new(self), Box::new(rhs))
}
}

impl Not for Sieve {
type Output = Sieve;

fn not(self) -> Self::Output {
Sieve::Inversion(Box::new(self))
}
}

impl Sieve {
pub fn at(&self, value: i128) -> bool {
match self {
Sieve::Residual(residual) => residual.at(value),
Sieve::Intersection(lhs, rhs) => {
lhs.at(value) && rhs.at(value)
},
Sieve::Union(lhs, rhs) => {
lhs.at(value) || rhs.at(value)
},
Sieve::Inversion(residual) => {
!residual.at(value)
},
}
}
}
15 changes: 14 additions & 1 deletion tests/test_integration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// use xenakis_sieve::util::lcm;
use xenakis_sieve::Residual;

use xenakis_sieve::Sieve;

#[test]
fn test_residual_a() {
Expand Down Expand Up @@ -184,3 +184,16 @@ fn test_residual_at_b() {
assert_eq!(r1.at(2), false);
assert_eq!(r1.at(3), false);
}

//------------------------------------------------------------------------------


#[test]
fn test_sieve_at_a() {
let r1 = Residual::from_components(3, 0);
let s1 = Sieve::Residual(r1);
assert_eq!(s1.at(-3), true);
assert_eq!(s1.at(0), true);
assert_eq!(s1.at(3), true);
assert_eq!(s1.at(4), false);
}

0 comments on commit 4a3a164

Please sign in to comment.