Skip to content

Commit

Permalink
Merge pull request #3 from vic1707/inplace_simplify_for
Browse files Browse the repository at this point in the history
Inplace simplify for
  • Loading branch information
vic1707 authored Oct 13, 2023
2 parents 4beaab2 + 1c049e7 commit 016ee69
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM alpine:latest
RUN apk add g++ valgrind rust cargo
24 changes: 17 additions & 7 deletions src/xprs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/* Built-in imports */
use core::fmt;
use core::{fmt, ptr};
use std::collections::{HashMap, HashSet};
/* Crate imports */
use crate::{element::Element, macros::yeet, token::Operator};
use crate::{
element::Element,
macros::{trust_me, yeet},
token::Operator,
};

#[derive(Debug, PartialEq)]
#[non_exhaustive]
Expand Down Expand Up @@ -34,13 +38,19 @@ impl Xprs<'_> {
XprsImpl::new(variables).eval_element(&self.root)
}

#[inline]
pub fn simplify_for_inplace(&mut self, var: (&str, f64)) {
let mut tmp = trust_me!(ptr::read(&self.root));
tmp = tmp.simplify_for(var);
trust_me!(ptr::write(&mut self.root, tmp););
self.vars.remove(var.0);
}

#[inline]
#[must_use]
pub fn simplify_for(self, var: (&str, f64)) -> Self {
let root = self.root.simplify_for(var);
let mut vars = self.vars;
vars.remove(var.0);
Self { root, vars }
pub fn simplify_for(mut self, var: (&str, f64)) -> Self {
self.simplify_for_inplace(var);
self
}
}

Expand Down
17 changes: 17 additions & 0 deletions valgrind.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

## Because valgrind is not supported on arm macOS
## so we use docker to run valgrind on linux

docker image rm -f valgrind
docker build -t valgrind .
docker run -it --rm -v $(pwd):/home/valgrind valgrind sh -c "\
cd /home/valgrind && \
cargo build --release && \
valgrind \
--leak-check=full \
--show-leak-kinds=all \
--track-origins=yes \
--verbose \
--log-file=valgrind-out.txt \
./target/release/repl"

0 comments on commit 016ee69

Please sign in to comment.