Skip to content

Commit

Permalink
use Rc instead of Box
Browse files Browse the repository at this point in the history
  • Loading branch information
flexatone committed Apr 23, 2024
1 parent 0e23796 commit 58ce543
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::ops::BitAnd;
use std::ops::BitOr;
use std::ops::BitXor;
use std::ops::Not;
use std::rc::Rc;

mod parser;
mod util;
Expand Down Expand Up @@ -88,10 +89,10 @@ impl Ord for Residual {
#[derive(Clone, Debug)]
pub(crate) enum SieveNode {
Unit(Residual),
Intersection(Box<SieveNode>, Box<SieveNode>),
Union(Box<SieveNode>, Box<SieveNode>),
SymmetricDifference(Box<SieveNode>, Box<SieveNode>),
Inversion(Box<SieveNode>),
Intersection(RC<SieveNode>, RC<SieveNode>),
Union(RC<SieveNode>, Rc<SieveNode>),
SymmetricDifference(RC<SieveNode>, RC<SieveNode>),
Inversion(RC<SieveNode>),
}

impl fmt::Display for SieveNode {
Expand Down Expand Up @@ -141,15 +142,15 @@ impl SieveNode {
/// The representation of a Xenakis Sieve, constructed from a string notation of one or more Residual classes combined with logical operators. This Rust implementation follows the Python implementation in Ariza (2005), with significant performance and interface enhancements: https://direct.mit.edu/comj/article/29/2/40/93957
#[derive(Clone, Debug)]
pub struct Sieve {
root: SieveNode,
root: Rc<SieveNode>,
}

impl BitAnd for Sieve {
type Output = Sieve;

fn bitand(self, rhs: Self) -> Self::Output {
Sieve {
root: SieveNode::Intersection(Box::new(self.root), Box::new(rhs.root)),
root: SieveNode::Intersection(Rc::clone(&self.root), Rc::clone(&rhs.root)),
}
}
}
Expand All @@ -159,7 +160,7 @@ impl BitAnd for &Sieve {

fn bitand(self, rhs: Self) -> Self::Output {
Sieve {
root: SieveNode::Intersection(Box::new(self.root.clone()), Box::new(rhs.root.clone())),
root: SieveNode::Intersection(Rc::clone(&self.root), Rc::clone(&rhs.root)),
}
}
}
Expand All @@ -169,7 +170,7 @@ impl BitOr for Sieve {

fn bitor(self, rhs: Self) -> Self::Output {
Sieve {
root: SieveNode::Union(Box::new(self.root), Box::new(rhs.root)),
root: SieveNode::Union(Rc::clone(&self.root), Rc::clone(&rhs.root)),
}
}
}
Expand All @@ -179,7 +180,7 @@ impl BitOr for &Sieve {

fn bitor(self, rhs: Self) -> Self::Output {
Sieve {
root: SieveNode::Union(Box::new(self.root.clone()), Box::new(rhs.root.clone())),
root: SieveNode::Union(Rc::clone(&self.root), Rc::clone(&rhs.root)),
}
}
}
Expand All @@ -189,7 +190,7 @@ impl BitXor for Sieve {

fn bitxor(self, rhs: Self) -> Self::Output {
Sieve {
root: SieveNode::SymmetricDifference(Box::new(self.root), Box::new(rhs.root)),
root: SieveNode::SymmetricDifference(Rc::clone(self.root), Rc::clone(rhs.root)),
}
}
}
Expand All @@ -200,8 +201,8 @@ impl BitXor for &Sieve {
fn bitxor(self, rhs: Self) -> Self::Output {
Sieve {
root: SieveNode::SymmetricDifference(
Box::new(self.root.clone()),
Box::new(rhs.root.clone()),
Rc::clone(&self.root),
Rc::clone(&rhs.root),
),
}
}
Expand All @@ -212,7 +213,7 @@ impl Not for Sieve {

fn not(self) -> Self::Output {
Sieve {
root: SieveNode::Inversion(Box::new(self.root)),
root: SieveNode::Inversion(Rc::clone(&self.root)),
}
}
}
Expand All @@ -222,7 +223,7 @@ impl Not for &Sieve {

fn not(self) -> Self::Output {
Sieve {
root: SieveNode::Inversion(Box::new(self.root.clone())),
root: SieveNode::Inversion(Rc::clone(&self.root)),
}
}
}
Expand Down Expand Up @@ -301,7 +302,7 @@ impl Sieve {
// NOTE: do not want to clone self here...
IterValue {
iterator,
sieve_node: self.root.clone(),
sieve_node: Rc::new(self.root),
}
}

Expand Down Expand Up @@ -351,7 +352,7 @@ where
I: Iterator<Item = i128>,
{
iterator: I,
sieve_node: SieveNode,
sieve_node: Rc<SieveNode>,
}

impl<I> Iterator for IterValue<I>
Expand Down Expand Up @@ -662,7 +663,7 @@ mod tests {
fn test_sieve_contains_b() {
let r1 = Residual::new(3, 0);
let r2 = Residual::new(3, 1);
let s1 = SieveNode::Union(Box::new(SieveNode::Unit(r1)), Box::new(SieveNode::Unit(r2)));
let s1 = SieveNode::Union(Rc::new(SieveNode::Unit(r1)), Rc::new(SieveNode::Unit(r2)));

assert_eq!(s1.contains(-2), true);
assert_eq!(s1.contains(-1), false);
Expand Down

0 comments on commit 58ce543

Please sign in to comment.