use core::fmt;
use rand::{
distributions::{Distribution, Standard},
seq::SliceRandom,
Rng, SeedableRng,
};
use rand_xorshift::XorShiftRng;
const TABLE_SIZE: usize = 256;
pub trait NoiseHasher: Send + Sync {
fn hash(&self, to_hash: &[isize]) -> usize;
}
#[derive(Copy, Clone)]
pub struct PermutationTable {
values: [u8; TABLE_SIZE],
}
impl Distribution<PermutationTable> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> PermutationTable {
let mut perm_table = PermutationTable {
values: [0; TABLE_SIZE],
};
perm_table
.values
.iter_mut()
.enumerate()
.for_each(|(i, b)| *b = i as u8);
perm_table.values.shuffle(rng);
perm_table
}
}
impl PermutationTable {
pub fn new(seed: u32) -> Self {
let mut real = [0; 16];
real[0] = 1;
for i in 1..4 {
real[i * 4] = seed as u8;
real[(i * 4) + 1] = (seed >> 8) as u8;
real[(i * 4) + 2] = (seed >> 16) as u8;
real[(i * 4) + 3] = (seed >> 24) as u8;
}
let mut rng: XorShiftRng = SeedableRng::from_seed(real);
rng.gen()
}
}
impl NoiseHasher for PermutationTable {
fn hash(&self, to_hash: &[isize]) -> usize {
let index = to_hash
.iter()
.map(|&a| (a & 0xff) as usize)
.reduce(|a, b| self.values[a] as usize ^ b)
.unwrap();
self.values[index] as usize
}
}
impl fmt::Debug for PermutationTable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PermutationTable {{ .. }}")
}
}
#[cfg(test)]
mod tests {
use crate::{NoiseFn, Perlin, Seedable};
use rand::random;
#[test]
fn test_random_seed() {
let perlin = Perlin::default().set_seed(random());
let _ = perlin.get([1.0, 2.0, 3.0]);
}
#[test]
fn test_negative_params() {
let perlin = Perlin::default();
let _ = perlin.get([-1.0, 2.0, 3.0]);
}
}