|
| 1 | +use std::num::NonZeroUsize; |
| 2 | + |
| 3 | +#[cfg(feature = "serde-serialize")] |
| 4 | +use serde::Serialize; |
| 5 | + |
| 6 | +use super::LinkMatrix; |
| 7 | +use crate::lattice::LatticeCyclique; |
| 8 | +use crate::CMatrix3; |
| 9 | + |
| 10 | +#[derive(Debug, PartialEq)] |
| 11 | +#[cfg_attr(feature = "serde-serialize", derive(Serialize))] |
| 12 | +enum LinkMatrixBuilderType<'a, 'lat, Rng: rand::Rng + ?Sized, const D: usize> { |
| 13 | + Generated(&'lat LatticeCyclique<D>, GenType<'a, Rng>), |
| 14 | + Data(Vec<CMatrix3>), |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, PartialEq)] |
| 18 | +#[cfg_attr(feature = "serde-serialize", derive(Serialize))] |
| 19 | +enum GenType<'a, Rng: rand::Rng + ?Sized> { |
| 20 | + Cold, |
| 21 | + Hot(&'a mut Rng), |
| 22 | + HotThreaded(NonZeroUsize), |
| 23 | +} |
| 24 | + |
| 25 | +impl<'a, 'lat, Rng: rand::Rng + ?Sized, const D: usize> LinkMatrixBuilderType<'a, 'lat, Rng, D> { |
| 26 | + pub fn into_link_matrix(self) -> LinkMatrix { |
| 27 | + match self { |
| 28 | + Self::Data(data) => LinkMatrix::new(data), |
| 29 | + Self::Generated(l, gen_type) => match gen_type { |
| 30 | + GenType::Cold => LinkMatrix::new_cold(l), |
| 31 | + GenType::Hot(rng) => LinkMatrix::new_deterministe(l, rng), |
| 32 | + // the unwrap is safe because n is non zero |
| 33 | + // there is a possibility to panic in a thread but very unlikly |
| 34 | + // (either something break in this API or in thread_rng()) |
| 35 | + GenType::HotThreaded(n) => LinkMatrix::new_random_threaded(l, n.get()).unwrap(), |
| 36 | + }, |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Debug, PartialEq)] |
| 42 | +#[cfg_attr(feature = "serde-serialize", derive(Serialize))] |
| 43 | +pub struct LinkMatrixBuilder<'a, 'lat, Rng: rand::Rng + ?Sized, const D: usize> { |
| 44 | + builder_type: LinkMatrixBuilderType<'a, 'lat, Rng, D>, |
| 45 | +} |
| 46 | + |
| 47 | +impl<'a, 'lat, Rng: rand::Rng + ?Sized, const D: usize> LinkMatrixBuilder<'a, 'lat, Rng, D> { |
| 48 | + pub fn new_from_data(data: Vec<CMatrix3>) -> Self { |
| 49 | + Self { |
| 50 | + builder_type: LinkMatrixBuilderType::Data(data), |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + pub fn new_generated(l: &'lat LatticeCyclique<D>) -> Self { |
| 55 | + Self { |
| 56 | + builder_type: LinkMatrixBuilderType::Generated(l, GenType::Cold), |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + pub fn set_cold(&mut self) -> &mut Self { |
| 61 | + match self.builder_type { |
| 62 | + LinkMatrixBuilderType::Data(_) => {} |
| 63 | + LinkMatrixBuilderType::Generated(l, _) => { |
| 64 | + self.builder_type = LinkMatrixBuilderType::Generated(l, GenType::Cold); |
| 65 | + } |
| 66 | + } |
| 67 | + self |
| 68 | + } |
| 69 | + |
| 70 | + pub fn set_hot(&mut self, rng: &'a mut Rng) -> &mut Self { |
| 71 | + match self.builder_type { |
| 72 | + LinkMatrixBuilderType::Data(_) => {} |
| 73 | + LinkMatrixBuilderType::Generated(l, _) => { |
| 74 | + self.builder_type = LinkMatrixBuilderType::Generated(l, GenType::Hot(rng)); |
| 75 | + } |
| 76 | + } |
| 77 | + self |
| 78 | + } |
| 79 | + |
| 80 | + pub fn set_hot_threaded(&mut self, number_of_threads: NonZeroUsize) -> &mut Self { |
| 81 | + match self.builder_type { |
| 82 | + LinkMatrixBuilderType::Data(_) => {} |
| 83 | + LinkMatrixBuilderType::Generated(l, _) => { |
| 84 | + self.builder_type = |
| 85 | + LinkMatrixBuilderType::Generated(l, GenType::HotThreaded(number_of_threads)); |
| 86 | + } |
| 87 | + } |
| 88 | + self |
| 89 | + } |
| 90 | + |
| 91 | + pub fn build(self) -> LinkMatrix { |
| 92 | + self.builder_type.into_link_matrix() |
| 93 | + } |
| 94 | +} |
0 commit comments