@@ -26,19 +26,6 @@ pub struct HeterogeneousPaletteData<V: Hash + Eq + Copy, const DIM: usize> {
2626}
2727
2828impl < V : Hash + Eq + Copy , const DIM : usize > HeterogeneousPaletteData < V , DIM > {
29- fn from_cube ( cube : Box < AbstractCube < V , DIM > > ) -> Self {
30- let counts =
31- cube. as_flattened ( )
32- . as_flattened ( )
33- . iter ( )
34- . fold ( HashMap :: new ( ) , |mut acc, key| {
35- acc. entry ( * key) . and_modify ( |count| * count += 1 ) . or_insert ( 1 ) ;
36- acc
37- } ) ;
38-
39- Self { cube, counts }
40- }
41-
4229 fn get ( & self , x : usize , y : usize , z : usize ) -> V {
4330 debug_assert ! ( x < DIM ) ;
4431 debug_assert ! ( y < DIM ) ;
@@ -81,6 +68,23 @@ impl<V: Hash + Eq + Copy + Default, const DIM: usize> PalettedContainer<V, DIM>
8168 pub const SIZE : usize = DIM ;
8269 pub const VOLUME : usize = DIM * DIM * DIM ;
8370
71+ fn from_cube ( cube : Box < AbstractCube < V , DIM > > ) -> Self {
72+ let counts =
73+ cube. as_flattened ( )
74+ . as_flattened ( )
75+ . iter ( )
76+ . fold ( HashMap :: new ( ) , |mut acc, key| {
77+ acc. entry ( * key) . and_modify ( |count| * count += 1 ) . or_insert ( 1 ) ;
78+ acc
79+ } ) ;
80+
81+ if counts. len ( ) == 1 {
82+ Self :: Homogeneous ( * counts. keys ( ) . next ( ) . unwrap ( ) )
83+ } else {
84+ Self :: Heterogeneous ( Box :: new ( HeterogeneousPaletteData { cube, counts } ) )
85+ }
86+ }
87+
8488 fn bits_per_entry ( & self ) -> u8 {
8589 match self {
8690 Self :: Homogeneous ( _) => 0 ,
@@ -181,7 +185,7 @@ impl<V: Hash + Eq + Copy + Default, const DIM: usize> PalettedContainer<V, DIM>
181185 } ) ;
182186 } ) ;
183187
184- Self :: Heterogeneous ( Box :: new ( HeterogeneousPaletteData :: from_cube ( cube) ) )
188+ Self :: from_cube ( cube)
185189 }
186190 }
187191
@@ -202,8 +206,7 @@ impl<V: Hash + Eq + Copy + Default, const DIM: usize> PalettedContainer<V, DIM>
202206 if value != * original {
203207 let mut cube = Box :: new ( [ [ [ * original; DIM ] ; DIM ] ; DIM ] ) ;
204208 cube[ y] [ z] [ x] = value;
205- let data = HeterogeneousPaletteData :: from_cube ( cube) ;
206- * self = Self :: Heterogeneous ( Box :: new ( data) ) ;
209+ * self = Self :: from_cube ( cube) ;
207210 }
208211 }
209212 Self :: Heterogeneous ( data) => {
0 commit comments