@@ -5,6 +5,7 @@ use crate::{
5
5
} ;
6
6
use bevy_ptr:: { OwningPtr , Ptr } ;
7
7
use std:: { cell:: UnsafeCell , hash:: Hash , marker:: PhantomData } ;
8
+ use nonmax:: NonMaxUsize ;
8
9
9
10
type EntityIndex = u32 ;
10
11
@@ -335,7 +336,7 @@ impl ComponentSparseSet {
335
336
pub struct SparseSet < I , V : ' static > {
336
337
dense : Vec < V > ,
337
338
indices : Vec < I > ,
338
- sparse : SparseArray < I , usize > ,
339
+ sparse : SparseArray < I , NonMaxUsize > ,
339
340
}
340
341
341
342
/// A space-optimized version of [`SparseSet`] that cannot be changed
@@ -344,7 +345,7 @@ pub struct SparseSet<I, V: 'static> {
344
345
pub ( crate ) struct ImmutableSparseSet < I , V : ' static > {
345
346
dense : Box < [ V ] > ,
346
347
indices : Box < [ I ] > ,
347
- sparse : ImmutableSparseArray < I , usize > ,
348
+ sparse : ImmutableSparseArray < I , NonMaxUsize > ,
348
349
}
349
350
350
351
macro_rules! impl_sparse_set {
@@ -368,7 +369,7 @@ macro_rules! impl_sparse_set {
368
369
pub fn get( & self , index: I ) -> Option <& V > {
369
370
self . sparse. get( index) . map( |dense_index| {
370
371
// SAFETY: if the sparse index points to something in the dense vec, it exists
371
- unsafe { self . dense. get_unchecked( * dense_index) }
372
+ unsafe { self . dense. get_unchecked( dense_index. get ( ) ) }
372
373
} )
373
374
}
374
375
@@ -379,7 +380,7 @@ macro_rules! impl_sparse_set {
379
380
let dense = & mut self . dense;
380
381
self . sparse. get( index) . map( move |dense_index| {
381
382
// SAFETY: if the sparse index points to something in the dense vec, it exists
382
- unsafe { dense. get_unchecked_mut( * dense_index) }
383
+ unsafe { dense. get_unchecked_mut( dense_index. get ( ) ) }
383
384
} )
384
385
}
385
386
@@ -454,10 +455,10 @@ impl<I: SparseSetIndex, V> SparseSet<I, V> {
454
455
if let Some ( dense_index) = self . sparse . get ( index. clone ( ) ) . cloned ( ) {
455
456
// SAFETY: dense indices stored in self.sparse always exist
456
457
unsafe {
457
- * self . dense . get_unchecked_mut ( dense_index) = value;
458
+ * self . dense . get_unchecked_mut ( dense_index. get ( ) ) = value;
458
459
}
459
460
} else {
460
- self . sparse . insert ( index. clone ( ) , self . dense . len ( ) ) ;
461
+ self . sparse . insert ( index. clone ( ) , NonMaxUsize :: new ( self . dense . len ( ) ) . unwrap ( ) ) ;
461
462
self . indices . push ( index) ;
462
463
self . dense . push ( value) ;
463
464
}
@@ -468,11 +469,11 @@ impl<I: SparseSetIndex, V> SparseSet<I, V> {
468
469
pub fn get_or_insert_with ( & mut self , index : I , func : impl FnOnce ( ) -> V ) -> & mut V {
469
470
if let Some ( dense_index) = self . sparse . get ( index. clone ( ) ) . cloned ( ) {
470
471
// SAFETY: dense indices stored in self.sparse always exist
471
- unsafe { self . dense . get_unchecked_mut ( dense_index) }
472
+ unsafe { self . dense . get_unchecked_mut ( dense_index. get ( ) ) }
472
473
} else {
473
474
let value = func ( ) ;
474
475
let dense_index = self . dense . len ( ) ;
475
- self . sparse . insert ( index. clone ( ) , dense_index) ;
476
+ self . sparse . insert ( index. clone ( ) , NonMaxUsize :: new ( dense_index) . unwrap ( ) ) ;
476
477
self . indices . push ( index) ;
477
478
self . dense . push ( value) ;
478
479
// SAFETY: dense index was just populated above
@@ -491,11 +492,12 @@ impl<I: SparseSetIndex, V> SparseSet<I, V> {
491
492
/// Returns `None` if `index` does not have a value in the sparse set.
492
493
pub fn remove ( & mut self , index : I ) -> Option < V > {
493
494
self . sparse . remove ( index) . map ( |dense_index| {
494
- let is_last = dense_index == self . dense . len ( ) - 1 ;
495
- let value = self . dense . swap_remove ( dense_index) ;
496
- self . indices . swap_remove ( dense_index) ;
495
+ let index = dense_index. get ( ) ;
496
+ let is_last = index == self . dense . len ( ) - 1 ;
497
+ let value = self . dense . swap_remove ( index) ;
498
+ self . indices . swap_remove ( index) ;
497
499
if !is_last {
498
- let swapped_index = self . indices [ dense_index ] . clone ( ) ;
500
+ let swapped_index = self . indices [ index ] . clone ( ) ;
499
501
* self . sparse . get_mut ( swapped_index) . unwrap ( ) = dense_index;
500
502
}
501
503
value
0 commit comments