quantized_density_fields/qdf/
state.rs

1use std::fmt::Debug;
2
3/// Trait that describes QDF space state.
4///
5/// # Examples
6/// ```
7/// use quantized_density_fields::State;
8/// use std::iter::repeat;
9///
10/// #[derive(Debug, Default, Eq, PartialEq, Clone)]
11/// struct Integer(i32);
12///
13/// impl State for Integer {
14///     fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
15///         repeat(Integer(self.0 / subdivisions as i32)).take(subdivisions).collect()
16///     }
17///     fn merge(states: &[Self]) -> Self {
18///         Integer(states.iter().map(|v| v.0).sum())
19///     }
20/// }
21///
22/// let substates = Integer(16).subdivide(4);
23/// assert_eq!(substates, vec![Integer(4), Integer(4), Integer(4), Integer(4)]);
24/// let state = State::merge(&substates);
25/// assert_eq!(state, Integer(16));
26/// ```
27pub trait State: Sized + Clone + Default + Send + Sync + Debug {
28    /// Create data template that we get by subdivision of source data.
29    ///
30    /// # Arguments
31    /// * `subdivisions` - number of subdivisions.
32    fn subdivide(&self, subdivisions: usize) -> Vec<Self>;
33    /// Merge multiple data instances into one.
34    ///
35    /// # Arguments
36    /// * `states` - list of source data to merge.
37    fn merge(states: &[Self]) -> Self;
38    /// Multiply and merge multiple instances of itself into one super state.
39    ///
40    /// # Arguments
41    /// * `dimensions` - number of dimensions.
42    /// * `level` - number level at which you merge.
43    fn super_state_at_level(&self, dimensions: usize, level: usize) -> Self {
44        let states = std::iter::repeat(self.clone())
45            .take((dimensions + 1)
46            .pow(level as u32))
47            .collect::<Vec<Self>>();
48        Self::merge(&states)
49    }
50}
51
52impl State for i8 {
53    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
54        ::std::iter::repeat(self / subdivisions as Self)
55            .take(subdivisions)
56            .collect()
57    }
58    fn merge(states: &[Self]) -> Self {
59        states.iter().sum()
60    }
61}
62impl State for i16 {
63    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
64        ::std::iter::repeat(self / subdivisions as Self)
65            .take(subdivisions)
66            .collect()
67    }
68    fn merge(states: &[Self]) -> Self {
69        states.iter().sum()
70    }
71}
72impl State for i32 {
73    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
74        ::std::iter::repeat(self / subdivisions as Self)
75            .take(subdivisions)
76            .collect()
77    }
78    fn merge(states: &[Self]) -> Self {
79        states.iter().sum()
80    }
81}
82impl State for i64 {
83    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
84        ::std::iter::repeat(self / subdivisions as Self)
85            .take(subdivisions)
86            .collect()
87    }
88    fn merge(states: &[Self]) -> Self {
89        states.iter().sum()
90    }
91}
92impl State for u8 {
93    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
94        ::std::iter::repeat(self / subdivisions as Self)
95            .take(subdivisions)
96            .collect()
97    }
98    fn merge(states: &[Self]) -> Self {
99        states.iter().sum()
100    }
101}
102impl State for u16 {
103    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
104        ::std::iter::repeat(self / subdivisions as Self)
105            .take(subdivisions)
106            .collect()
107    }
108    fn merge(states: &[Self]) -> Self {
109        states.iter().sum()
110    }
111}
112impl State for u32 {
113    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
114        ::std::iter::repeat(self / subdivisions as Self)
115            .take(subdivisions)
116            .collect()
117    }
118    fn merge(states: &[Self]) -> Self {
119        states.iter().sum()
120    }
121}
122impl State for u64 {
123    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
124        ::std::iter::repeat(self / subdivisions as Self)
125            .take(subdivisions)
126            .collect()
127    }
128    fn merge(states: &[Self]) -> Self {
129        states.iter().sum()
130    }
131}
132impl State for f32 {
133    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
134        ::std::iter::repeat(self / subdivisions as Self)
135            .take(subdivisions)
136            .collect()
137    }
138    fn merge(states: &[Self]) -> Self {
139        states.iter().sum()
140    }
141}
142impl State for f64 {
143    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
144        ::std::iter::repeat(self / subdivisions as Self)
145            .take(subdivisions)
146            .collect()
147    }
148    fn merge(states: &[Self]) -> Self {
149        states.iter().sum()
150    }
151}
152impl State for isize {
153    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
154        ::std::iter::repeat(self / subdivisions as Self)
155            .take(subdivisions)
156            .collect()
157    }
158    fn merge(states: &[Self]) -> Self {
159        states.iter().sum()
160    }
161}
162impl State for usize {
163    fn subdivide(&self, subdivisions: usize) -> Vec<Self> {
164        ::std::iter::repeat(self / subdivisions as Self)
165            .take(subdivisions)
166            .collect()
167    }
168    fn merge(states: &[Self]) -> Self {
169        states.iter().sum()
170    }
171}