use nalgebra::{allocator::Allocator, DefaultAllocator, Dim, OVector, Scalar, U1}; use rand::{rng, RngCore}; use crate::{binary_string::BinaryString, bounded::{Bounded, BoundedBinaryString}}; pub trait Initializer { fn initialize_single(&mut self, size: D) -> T; fn initialize(&mut self, size: D, count: usize) -> impl Iterator { let size = size; (0..count).map(move |_| self.initialize_single(size)) } } // Always initializes with zeros pub struct ZeroInitializer; impl ZeroInitializer { pub fn new() -> Self { Self { } } } impl Initializer> for ZeroInitializer where D: Dim, DefaultAllocator: Allocator { fn initialize_single(&mut self, size: D) -> BinaryString { BinaryString::::from_ovector( >>::initialize_single(self, size) ) } } impl Initializer> for ZeroInitializer where T: Scalar + Default, D: Dim, DefaultAllocator: Allocator { fn initialize_single(&mut self, size: D) -> OVector { OVector::::from_element_generic(size, U1, Default::default()) } } pub struct RandomInitializer { rng: Box, bounded: Box> } impl RandomInitializer { pub fn new(bounded: Box>) -> Self { Self { rng: Box::new(rand::rng()), bounded } } } impl RandomInitializer> where D: Dim, DefaultAllocator: Allocator { pub fn new_binary() -> Self { Self { rng: Box::new(rand::rng()), bounded: Box::new(BoundedBinaryString::unbounded()) } } } impl Initializer> for RandomInitializer> where D: Dim, DefaultAllocator: Allocator { fn initialize_single(&mut self, size: D) -> BinaryString { self.bounded.next_random(size, &mut self.rng) } } impl Initializer> for RandomInitializer> where T: Scalar + Default, D: Dim, DefaultAllocator: Allocator { fn initialize_single(&mut self, size: D) -> OVector { self.bounded.next_random(size, &mut self.rng) } }