AstBuilder: move generators to file
All checks were successful
ci/woodpecker/push/build_rust Pipeline was successful
All checks were successful
ci/woodpecker/push/build_rust Pipeline was successful
This commit is contained in:
parent
461bcaee80
commit
0af2dc3efc
2 changed files with 74 additions and 66 deletions
|
@ -1,3 +1,8 @@
|
|||
mod random_generators;
|
||||
|
||||
use random_generators::IntGenerator;
|
||||
use random_generators::FloatGenerator;
|
||||
|
||||
use rand::Rng;
|
||||
use rand::seq::SliceRandom;
|
||||
use rand_distr::Distribution;
|
||||
|
@ -58,6 +63,8 @@ impl AstBuilder {
|
|||
} else if a_lt(&mut p, self.params.global_flow.gen_typedef) {
|
||||
let typedef = self.gen_typedef();
|
||||
self.ast.push(typedef);
|
||||
} else if a_lt(&mut p, self.params.global_flow.gen_subroutine) {
|
||||
panic!("Subroutines aren't ready yet");
|
||||
} else {
|
||||
let decl = self.gen_global_decl();
|
||||
self.ast.push(decl);
|
||||
|
@ -222,69 +229,3 @@ impl ToString for AstBuilder {
|
|||
s
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates values from regions of interest for an integer
|
||||
///
|
||||
/// These are concentrated around i32::MIN, 0, and i32::MAX, though any i32 value is possible
|
||||
struct IntGenerator {
|
||||
distro_pos: rand_distr::Normal<f32>,
|
||||
distro_zero: rand_distr::Normal<f32>,
|
||||
distro_neg: rand_distr::Normal<f32>,
|
||||
}
|
||||
|
||||
impl IntGenerator {
|
||||
fn new(p: &Params) -> Self {
|
||||
Self {
|
||||
distro_pos: rand_distr::Normal::new(0.0, p.dev.int_gen_distro_pos_stddiv).unwrap(),
|
||||
distro_zero: rand_distr::Normal::new(0.0, 3.0).unwrap(),
|
||||
distro_neg: rand_distr::Normal::new(0.0, p.dev.int_gen_distro_neg_stddiv).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Distribution<i32> for IntGenerator {
|
||||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> i32 {
|
||||
let p: f64 = rng.gen();
|
||||
|
||||
if p < 0.3 {
|
||||
i32::MAX - (self.distro_pos.sample(rng).abs() as i32)
|
||||
} else if p < 0.7 {
|
||||
self.distro_zero.sample(rng) as i32
|
||||
} else {
|
||||
-1 * (i32::MAX - (self.distro_pos.sample(rng).abs() as i32))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates values from regions of interest for a float
|
||||
///
|
||||
/// These are concentrated around f32::MIN, 0, and f32::MAX, though any f32 value is possible
|
||||
struct FloatGenerator {
|
||||
distro_pos: rand_distr::Normal<f32>,
|
||||
distro_zero: rand_distr::Normal<f32>,
|
||||
distro_neg: rand_distr::Normal<f32>,
|
||||
}
|
||||
|
||||
impl FloatGenerator {
|
||||
fn new(p: &Params) -> Self {
|
||||
Self {
|
||||
distro_pos: rand_distr::Normal::new(1.0, p.dev.float_gen_distro_pos_stddiv).unwrap(),
|
||||
distro_zero: rand_distr::Normal::new(0.0, 3.0).unwrap(),
|
||||
distro_neg: rand_distr::Normal::new(1.0, p.dev.float_gen_distro_neg_stddiv).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Distribution<f32> for FloatGenerator {
|
||||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f32 {
|
||||
let p: f64 = rng.gen();
|
||||
|
||||
if p < 0.33 {
|
||||
f32::MAX - self.distro_pos.sample(rng).abs()
|
||||
} else if p < 0.66 {
|
||||
self.distro_zero.sample(rng)
|
||||
} else {
|
||||
-1.0 * (f32::MAX - self.distro_neg.sample(rng).abs())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
67
src/ast_builder/random_generators.rs
Normal file
67
src/ast_builder/random_generators.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
use super::*;
|
||||
|
||||
/// Generates values from regions of interest for an integer
|
||||
///
|
||||
/// These are concentrated around i32::MIN, 0, and i32::MAX, though any i32 value is possible
|
||||
pub struct IntGenerator {
|
||||
distro_pos: rand_distr::Normal<f32>,
|
||||
distro_zero: rand_distr::Normal<f32>,
|
||||
distro_neg: rand_distr::Normal<f32>,
|
||||
}
|
||||
|
||||
impl IntGenerator {
|
||||
pub fn new(p: &Params) -> Self {
|
||||
Self {
|
||||
distro_pos: rand_distr::Normal::new(0.0, p.dev.int_gen_distro_pos_stddiv).unwrap(),
|
||||
distro_zero: rand_distr::Normal::new(0.0, 3.0).unwrap(),
|
||||
distro_neg: rand_distr::Normal::new(0.0, p.dev.int_gen_distro_neg_stddiv).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Distribution<i32> for IntGenerator {
|
||||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> i32 {
|
||||
let p: f64 = rng.gen();
|
||||
|
||||
if p < 0.3 {
|
||||
i32::MAX - (self.distro_pos.sample(rng).abs() as i32)
|
||||
} else if p < 0.7 {
|
||||
self.distro_zero.sample(rng) as i32
|
||||
} else {
|
||||
-1 * (i32::MAX - (self.distro_pos.sample(rng).abs() as i32))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates values from regions of interest for a float
|
||||
///
|
||||
/// These are concentrated around f32::MIN, 0, and f32::MAX, though any f32 value is possible
|
||||
pub struct FloatGenerator {
|
||||
distro_pos: rand_distr::Normal<f32>,
|
||||
distro_zero: rand_distr::Normal<f32>,
|
||||
distro_neg: rand_distr::Normal<f32>,
|
||||
}
|
||||
|
||||
impl FloatGenerator {
|
||||
pub fn new(p: &Params) -> Self {
|
||||
Self {
|
||||
distro_pos: rand_distr::Normal::new(1.0, p.dev.float_gen_distro_pos_stddiv).unwrap(),
|
||||
distro_zero: rand_distr::Normal::new(0.0, 3.0).unwrap(),
|
||||
distro_neg: rand_distr::Normal::new(1.0, p.dev.float_gen_distro_neg_stddiv).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Distribution<f32> for FloatGenerator {
|
||||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f32 {
|
||||
let p: f64 = rng.gen();
|
||||
|
||||
if p < 0.33 {
|
||||
f32::MAX - self.distro_pos.sample(rng).abs()
|
||||
} else if p < 0.66 {
|
||||
self.distro_zero.sample(rng)
|
||||
} else {
|
||||
-1.0 * (f32::MAX - self.distro_neg.sample(rng).abs())
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue