@@ -109,22 +109,27 @@ fn vp_create_node(indexes: &mut [MapIndex], items: &PalF) -> Node {
109
109
debug_assert ! ( !indexes. is_empty( ) ) ;
110
110
let palette = items. as_slice ( ) ;
111
111
112
- if indexes. len ( ) == 1 {
112
+ if indexes. len ( ) <= 1 {
113
+ let idx = indexes. first ( ) . map ( |i| i. idx ) . unwrap_or_default ( ) ;
113
114
return Node {
114
- vantage_point : palette[ usize:: from ( indexes [ 0 ] . idx ) ] ,
115
- idx : indexes [ 0 ] . idx ,
115
+ vantage_point : palette. get ( usize:: from ( idx) ) . copied ( ) . unwrap_or_default ( ) ,
116
+ idx,
116
117
inner : NodeInner :: Leaf { len : 0 , idxs : [ 0 ; LEAF_MAX_SIZE ] , colors : Box :: new ( [ f_pixel:: default ( ) ; LEAF_MAX_SIZE ] ) } ,
117
118
} ;
118
119
}
119
120
120
121
let most_popular_item = indexes. iter ( ) . enumerate ( ) . max_by_key ( move |( _, idx) | {
121
- OrdFloat :: new ( items. pop_as_slice ( ) [ usize:: from ( idx. idx ) ] . popularity ( ) )
122
+ OrdFloat :: new ( items. pop_as_slice ( ) . get ( usize:: from ( idx. idx ) )
123
+ . map ( |p| p. popularity ( ) ) . unwrap_or_default ( ) )
122
124
} ) . map ( |( n, _) | n) . unwrap_or_default ( ) ;
123
125
indexes. swap ( most_popular_item, 0 ) ;
124
126
let ( ref_, indexes) = indexes. split_first_mut ( ) . unwrap ( ) ;
125
127
126
- let vantage_point = palette[ usize:: from ( ref_. idx ) ] ;
127
- indexes. sort_by_cached_key ( move |i| OrdFloat :: new ( vantage_point. diff ( & palette[ usize:: from ( i. idx ) ] ) ) ) ;
128
+ let vantage_point = palette. get ( usize:: from ( ref_. idx ) ) . copied ( ) . unwrap_or_default ( ) ;
129
+ indexes. sort_by_cached_key ( move |i| {
130
+ OrdFloat :: new ( palette. get ( usize:: from ( i. idx ) )
131
+ . map ( |px| vantage_point. diff ( px) ) . unwrap_or_default ( ) )
132
+ } ) ;
128
133
129
134
let num_indexes = indexes. len ( ) ;
130
135
@@ -133,8 +138,10 @@ fn vp_create_node(indexes: &mut [MapIndex], items: &PalF) -> Node {
133
138
let mut idxs = [ Default :: default ( ) ; LEAF_MAX_SIZE ] ;
134
139
135
140
indexes. iter ( ) . zip ( colors. iter_mut ( ) . zip ( idxs. iter_mut ( ) ) ) . for_each ( |( i, ( color, idx) ) | {
136
- * idx = i. idx ;
137
- * color = palette[ usize:: from ( i. idx ) ] ;
141
+ if let Some ( c) = palette. get ( usize:: from ( i. idx ) ) {
142
+ * idx = i. idx ;
143
+ * color = * c;
144
+ }
138
145
} ) ;
139
146
NodeInner :: Leaf {
140
147
len : num_indexes as _ ,
@@ -146,7 +153,8 @@ fn vp_create_node(indexes: &mut [MapIndex], items: &PalF) -> Node {
146
153
let ( near, far) = indexes. split_at_mut ( half_index) ;
147
154
debug_assert ! ( !near. is_empty( ) ) ;
148
155
debug_assert ! ( !far. is_empty( ) ) ;
149
- let radius_squared = vantage_point. diff ( & palette[ usize:: from ( far[ 0 ] . idx ) ] ) ;
156
+ let radius_squared = palette. get ( usize:: from ( far[ 0 ] . idx ) )
157
+ . map ( |px| vantage_point. diff ( px) ) . unwrap_or_default ( ) ;
150
158
let radius = radius_squared. sqrt ( ) ;
151
159
NodeInner :: Nodes {
152
160
radius, radius_squared,
@@ -157,7 +165,7 @@ fn vp_create_node(indexes: &mut [MapIndex], items: &PalF) -> Node {
157
165
158
166
Node {
159
167
inner,
160
- vantage_point : palette [ usize :: from ( ref_ . idx ) ] ,
168
+ vantage_point,
161
169
idx : ref_. idx ,
162
170
}
163
171
}
0 commit comments