cargo fmt
This commit is contained in:
parent
5d84b017b9
commit
07b5cd8e6a
5 changed files with 105 additions and 39 deletions
|
@ -15,9 +15,9 @@ struct PreHasseNode {
|
|||
|
||||
/* fullfillment of links is sort-of complicated, because the tree of possiblities is really large
|
||||
|
||||
* start by splitting links according to their roots (the variable of which derivative of such is taken)
|
||||
* split the derivation-vectors into levels (sum of their components)
|
||||
*/
|
||||
* start by splitting links according to their roots (the variable of which derivative of such is taken)
|
||||
* split the derivation-vectors into levels (sum of their components)
|
||||
*/
|
||||
|
||||
fn sum(drvs: &[u8]) -> usize {
|
||||
drvs.iter().copied().map(usize::from).sum::<usize>()
|
||||
|
@ -47,15 +47,24 @@ fn calculate_votes(mut que: Vec<Box<[u8]>>) -> Vec<BTreeMap<Box<[u8]>, VoteEntry
|
|||
|
||||
// split que into levels
|
||||
let mut levels = core::iter::repeat_with(BTreeMap::new)
|
||||
.take(que.iter().map(|i| sum(&i[..])).max().map(|i| i.checked_add(1).unwrap()).unwrap_or(0))
|
||||
.take(
|
||||
que.iter()
|
||||
.map(|i| sum(&i[..]))
|
||||
.max()
|
||||
.map(|i| i.checked_add(1).unwrap())
|
||||
.unwrap_or(0),
|
||||
)
|
||||
.collect::<Vec<BTreeMap<Box<[u8]>, VoteEntry>>>();
|
||||
|
||||
for i in core::mem::take(&mut que) {
|
||||
levels[sum(&i[..])].insert(i, VoteEntry {
|
||||
votes: 1,
|
||||
best_parent: 0,
|
||||
is_used: true,
|
||||
});
|
||||
levels[sum(&i[..])].insert(
|
||||
i,
|
||||
VoteEntry {
|
||||
votes: 1,
|
||||
best_parent: 0,
|
||||
is_used: true,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// process levels (start from largest)
|
||||
|
@ -90,12 +99,17 @@ fn calculate_votes(mut que: Vec<Box<[u8]>>) -> Vec<BTreeMap<Box<[u8]>, VoteEntry
|
|||
} else {
|
||||
x
|
||||
}
|
||||
},
|
||||
}
|
||||
});
|
||||
}
|
||||
let pbs = pbs.unwrap();
|
||||
let pbs_index = {
|
||||
let mut pbsi = j.iter().zip(pbs.iter()).map(|(i, p)| i - p).enumerate().filter(|(_, i)| *i > 0);
|
||||
let mut pbsi = j
|
||||
.iter()
|
||||
.zip(pbs.iter())
|
||||
.map(|(i, p)| i - p)
|
||||
.enumerate()
|
||||
.filter(|(_, i)| *i > 0);
|
||||
let pbs_index = pbsi.next().unwrap();
|
||||
assert_eq!(pbsi.next(), None);
|
||||
pbs_index
|
||||
|
|
|
@ -33,12 +33,14 @@ pub enum Expr<L> {
|
|||
impl<L: Clone + Eq + Ord> Expr<L> {
|
||||
fn extract_links_intern(&self, out: &mut Vec<L>) {
|
||||
match self {
|
||||
Expr::Const(_) => {},
|
||||
Expr::Const(_) => {}
|
||||
Expr::Link(l) => out.push(l.clone()),
|
||||
Expr::Sum(xs) => xs.iter().for_each(|i| i.extract_links_intern(out)),
|
||||
Expr::Prod(xs) => xs.iter().for_each(|i| i.extract_links_intern(out)),
|
||||
|
||||
Expr::Sine(x) | Expr::Cosine(x) | Expr::Tangens(x) | Expr::ArcTan(x) => x.extract_links_intern(out),
|
||||
Expr::Sine(x) | Expr::Cosine(x) | Expr::Tangens(x) | Expr::ArcTan(x) => {
|
||||
x.extract_links_intern(out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,8 +72,12 @@ pub struct System {
|
|||
impl System {
|
||||
pub fn extract_links(&self) -> Vec<Link> {
|
||||
let mut ret = Vec::new();
|
||||
self.vs.iter().for_each(|i| i.extract_links_intern(&mut ret));
|
||||
self.mons.iter().for_each(|i| i.1.extract_links_intern(&mut ret));
|
||||
self.vs
|
||||
.iter()
|
||||
.for_each(|i| i.extract_links_intern(&mut ret));
|
||||
self.mons
|
||||
.iter()
|
||||
.for_each(|i| i.1.extract_links_intern(&mut ret));
|
||||
ret.sort_unstable();
|
||||
ret.dedup();
|
||||
ret
|
||||
|
@ -79,7 +85,9 @@ impl System {
|
|||
|
||||
// this is invoked for each time step times points in space for which the links can be computed
|
||||
pub fn interp_vs(&self, link_data: &BTreeMap<Link, f64>, out: &mut [f64]) {
|
||||
self.vs.iter().zip(out.iter_mut()).for_each(|(x, y)| { *y = x.interp(link_data); });
|
||||
self.vs.iter().zip(out.iter_mut()).for_each(|(x, y)| {
|
||||
*y = x.interp(link_data);
|
||||
});
|
||||
}
|
||||
|
||||
// this is invoked once per time step
|
||||
|
@ -97,7 +105,9 @@ impl System {
|
|||
}
|
||||
|
||||
pub fn interp_mons_data(&self, link_data: &BTreeMap<Link, f64>, out: &mut [f64]) {
|
||||
self.mons.iter().zip(out.iter_mut()).for_each(|(x, y)| { *y = x.1.interp(link_data); });
|
||||
self.mons.iter().zip(out.iter_mut()).for_each(|(x, y)| {
|
||||
*y = x.1.interp(link_data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -186,7 +196,10 @@ fn parse_expr<L: Clone + ParseLink>(slb: &mut StrLexerBase<'_>) -> Result<Expr<L
|
|||
return Ok(Expr::Link(x));
|
||||
}
|
||||
|
||||
if slb.inp.starts_with(|i: char| i == '-' || i.is_ascii_digit()) {
|
||||
if slb
|
||||
.inp
|
||||
.starts_with(|i: char| i == '-' || i.is_ascii_digit())
|
||||
{
|
||||
// float
|
||||
let mut new_slb = *slb;
|
||||
|
||||
|
@ -246,12 +259,16 @@ fn parse_expr<L: Clone + ParseLink>(slb: &mut StrLexerBase<'_>) -> Result<Expr<L
|
|||
("cos", [arg]) => Ok(Expr::Cosine(Box::new(arg.clone()))),
|
||||
("tan", [arg]) => Ok(Expr::Tangens(Box::new(arg.clone()))),
|
||||
("atan", [arg]) => Ok(Expr::ArcTan(Box::new(arg.clone()))),
|
||||
("sin" | "cos" | "tan" | "atan", _) => Err(ParseError::InvalidArgCount(ident.to_string(), args.len())),
|
||||
("sin" | "cos" | "tan" | "atan", _) => {
|
||||
Err(ParseError::InvalidArgCount(ident.to_string(), args.len()))
|
||||
}
|
||||
_ => Err(ParseError::UnknownFun(ident.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_expr_final<L: Clone + ParseLink>(slb: &mut StrLexerBase) -> Result<Expr<L>, ParseError> {
|
||||
pub fn parse_expr_final<L: Clone + ParseLink>(
|
||||
slb: &mut StrLexerBase,
|
||||
) -> Result<Expr<L>, ParseError> {
|
||||
let x = parse_expr(slb)?;
|
||||
if !slb.inp.is_empty() {
|
||||
return Err(ParseError::InvalidTrail);
|
||||
|
@ -318,7 +335,7 @@ pub fn parse_system(s: &str) -> Result<System, ((usize, usize), ParseError)> {
|
|||
[x] => (0, x),
|
||||
[x, y] => {
|
||||
match x.parse::<usize>() {
|
||||
Ok(xid) if xid == ret.vs.len() => {},
|
||||
Ok(xid) if xid == ret.vs.len() => {}
|
||||
_ => return Err(((lnr, 0), ParseError::InvalidStmtId)),
|
||||
}
|
||||
(x.len() + 1, y)
|
||||
|
@ -337,7 +354,10 @@ pub fn parse_system(s: &str) -> Result<System, ((usize, usize), ParseError)> {
|
|||
if parts.len() < 2 {
|
||||
return Err(((lnr, 0), ParseError::InvalidLineFormat));
|
||||
}
|
||||
let mut slb = StrLexerBase { inp: parts[0], offset: 0 };
|
||||
let mut slb = StrLexerBase {
|
||||
inp: parts[0],
|
||||
offset: 0,
|
||||
};
|
||||
let valxp = match parse_expr_final::<Link>(&mut slb) {
|
||||
Ok(x) => x,
|
||||
Err(e) => return Err(((lnr, slb.offset), e)),
|
||||
|
@ -367,17 +387,25 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn example0_fail() {
|
||||
assert_eq!(parse_system("
|
||||
assert_eq!(
|
||||
parse_system(
|
||||
"
|
||||
%vs
|
||||
# u_t + 0.11 u_x = 0
|
||||
0; -0.11 0_0_
|
||||
|
||||
"), Err(((3, 8), ParseError::InvalidTrail)));
|
||||
"
|
||||
),
|
||||
Err(((3, 8), ParseError::InvalidTrail))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example0() {
|
||||
println!("{:?}", parse_system("
|
||||
println!(
|
||||
"{:?}",
|
||||
parse_system(
|
||||
"
|
||||
%vs
|
||||
# u_t + 0.11 u_x = 0
|
||||
0; prod(-0.11, 0_1_)
|
||||
|
@ -385,6 +413,9 @@ mod tests {
|
|||
%mons
|
||||
# monitor u @ 1 + sin(t)
|
||||
0_0; sum(1.0, sin(.))
|
||||
").unwrap());
|
||||
"
|
||||
)
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -42,12 +42,17 @@ fn main() {
|
|||
// parse system
|
||||
let system = {
|
||||
let system = std::fs::read(&cli.system).expect("unable to read system to render");
|
||||
let system = std::str::from_utf8(&system).expect("unable to interpret system description as UTF-8");
|
||||
let system =
|
||||
std::str::from_utf8(&system).expect("unable to interpret system description as UTF-8");
|
||||
expr::parse_system(system).expect("unable to parse system")
|
||||
};
|
||||
|
||||
// parse init function
|
||||
let init = expr::parse_expr_final::<expr::Link>(&mut yz_string_utils::StrLexerBase { inp: &cli.init, offset: 0 }).expect("unable to parse init function");
|
||||
let init = expr::parse_expr_final::<expr::Link>(&mut yz_string_utils::StrLexerBase {
|
||||
inp: &cli.init,
|
||||
offset: 0,
|
||||
})
|
||||
.expect("unable to parse init function");
|
||||
|
||||
// construct shape
|
||||
let shape = mesh::grid::Shape(cli.shape);
|
||||
|
@ -61,7 +66,12 @@ fn main() {
|
|||
// check monitor consistency with shape
|
||||
for (n, (i, _)) in system.mons[..].iter().enumerate() {
|
||||
if i.len() != shape.0.len() {
|
||||
panic!("monitor {} has incompatible dimension {} vs {}", n, i.len(), shape.0.len());
|
||||
panic!(
|
||||
"monitor {} has incompatible dimension {} vs {}",
|
||||
n,
|
||||
i.len(),
|
||||
shape.0.len()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,7 +92,7 @@ fn main() {
|
|||
let mut grid = nalgebra::DMatrix::<f64>::zeros(shape_amount, system.vs.len());
|
||||
let mut grid_stage = nalgebra::DMatrix::<f64>::zeros(shape_amount, system.vs.len());
|
||||
|
||||
// data =
|
||||
// data =
|
||||
|
||||
let mut grid_next = grid.clone();
|
||||
|
||||
|
|
|
@ -18,12 +18,16 @@ struct ForEachStencilData<'a, F> {
|
|||
|
||||
impl<F: FnMut(&[u16])> ForEachStencilData<'_, F> {
|
||||
fn fe_st_intern(&mut self, shape: &[u16]) {
|
||||
let prefix: usize = self.buf.len().checked_sub(shape.len()).expect("buffer too small");
|
||||
let prefix: usize = self
|
||||
.buf
|
||||
.len()
|
||||
.checked_sub(shape.len())
|
||||
.expect("buffer too small");
|
||||
|
||||
// recurse over `this`
|
||||
if let Some(&fi) = shape.first() {
|
||||
let shnext = &shape[1..];
|
||||
for i in self.stencil_start .. (fi + 1 - self.stencil_end) {
|
||||
for i in self.stencil_start..(fi + 1 - self.stencil_end) {
|
||||
self.buf[prefix] = i;
|
||||
self.fe_st_intern(shnext);
|
||||
}
|
||||
|
@ -36,12 +40,14 @@ impl<F: FnMut(&[u16])> ForEachStencilData<'_, F> {
|
|||
impl Shape {
|
||||
pub fn amount(&self) -> Option<usize> {
|
||||
assert!(self.0.len() <= usize::from(u16::MAX));
|
||||
self.0.iter()
|
||||
.try_fold(1, |acc: usize, &i| acc.checked_mul(usize::from(i).checked_add(1)?))
|
||||
self.0.iter().try_fold(1, |acc: usize, &i| {
|
||||
acc.checked_mul(usize::from(i).checked_add(1)?)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn decode_pos_with_ret(&self, x: usize, out: &mut [u16]) {
|
||||
self.0.iter()
|
||||
self.0
|
||||
.iter()
|
||||
.map(|&i| usize::from(i) + 1)
|
||||
.zip(out.iter_mut())
|
||||
.fold(x, |x: usize, (i, o)| {
|
||||
|
@ -58,7 +64,8 @@ impl Shape {
|
|||
}
|
||||
|
||||
pub fn encode_pos(&self, x: &[u16]) -> Option<usize> {
|
||||
self.0.iter()
|
||||
self.0
|
||||
.iter()
|
||||
.map(|&i| usize::from(i) + 1)
|
||||
.zip(x.iter().map(|&i| usize::from(i)))
|
||||
.rev()
|
||||
|
@ -71,7 +78,11 @@ impl Shape {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn for_each_stencil(&self, stencil: core::ops::RangeInclusive<i16>, mut f: impl FnMut(&[u16])) {
|
||||
pub fn for_each_stencil(
|
||||
&self,
|
||||
stencil: core::ops::RangeInclusive<i16>,
|
||||
mut f: impl FnMut(&[u16]),
|
||||
) {
|
||||
assert!(*stencil.start() <= 0);
|
||||
assert!(*stencil.end() >= 0);
|
||||
assert!(self.0.len() <= usize::from(u16::MAX));
|
||||
|
@ -81,7 +92,8 @@ impl Shape {
|
|||
stencil_end: *stencil.end() as u16,
|
||||
buf: &mut buf[..],
|
||||
f: &mut f,
|
||||
}.fe_st_intern(&self.0[..])
|
||||
}
|
||||
.fe_st_intern(&self.0[..])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
* SPDX-License-Identifier: EUPL-1.2
|
||||
*/
|
||||
/// pdxdrive meshes
|
||||
|
||||
pub mod grid;
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in a new issue