use std::fmt::Debug;
pub trait State: Sized + Clone + Default + Send + Sync + Debug {
fn subdivide(&self, subdivisions: usize) -> Vec<Self>;
fn merge(states: &[Self]) -> Self;
fn super_state_at_level(&self, dimensions: usize, level: usize) -> Self {
let states = std::iter::repeat(self.clone())
.take((dimensions + 1)
.pow(level as u32))
.collect::<Vec<Self>>();
Self::merge(&states)
}
}
impl State for i8 {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for i16 {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for i32 {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for i64 {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for u8 {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for u16 {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for u32 {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for u64 {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for f32 {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for f64 {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for isize {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}
impl State for usize {
fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
::std::iter::repeat(self / subdivisions as Self)
.take(subdivisions)
.collect()
}
fn merge(states: &[Self]) -> Self {
states.iter().sum()
}
}