@@ -502,6 +502,21 @@ impl<T, IndexMarker> Default for FlatAlloc<T, IndexMarker> {
502
502
}
503
503
}
504
504
505
+ /// TODO replace once get_many_mut stabilizes (In Rust 1.86 apparently)
506
+ pub fn get2_mut < T > ( slice : & mut [ T ] , a : usize , b : usize ) -> Option < ( & mut T , & mut T ) > {
507
+ match b. cmp ( & a) {
508
+ Ordering :: Equal => None ,
509
+ Ordering :: Less => {
510
+ let ( l, r) = slice. split_at_mut ( a) ;
511
+ Some ( ( & mut r[ 0 ] , & mut l[ b] ) )
512
+ }
513
+ Ordering :: Greater => {
514
+ let ( l, r) = slice. split_at_mut ( b) ;
515
+ Some ( ( & mut l[ a] , & mut r[ 0 ] ) )
516
+ }
517
+ }
518
+ }
519
+
505
520
impl < T , IndexMarker > FlatAlloc < T , IndexMarker > {
506
521
pub const EMPTY_FLAT_ALLOC : Self = Self :: new ( ) ;
507
522
@@ -528,6 +543,14 @@ impl<T, IndexMarker> FlatAlloc<T, IndexMarker> {
528
543
_ph : PhantomData ,
529
544
}
530
545
}
546
+ #[ cfg( test) ]
547
+ // Only for testing, so only enabled with test flag
548
+ pub fn from_vec ( data : Vec < T > ) -> Self {
549
+ Self {
550
+ data,
551
+ _ph : PhantomData ,
552
+ }
553
+ }
531
554
pub fn get_next_alloc_id ( & self ) -> UUID < IndexMarker > {
532
555
let uuid = self . data . len ( ) ;
533
556
UUID ( uuid, PhantomData )
@@ -571,6 +594,16 @@ impl<T, IndexMarker> FlatAlloc<T, IndexMarker> {
571
594
_ph : PhantomData ,
572
595
}
573
596
}
597
+ pub fn map2 < T2 , OT > (
598
+ & self ,
599
+ second : & FlatAlloc < T2 , IndexMarker > ,
600
+ f : impl FnMut ( ( UUID < IndexMarker > , & T , & T2 ) ) -> OT ,
601
+ ) -> FlatAlloc < OT , IndexMarker > {
602
+ FlatAlloc {
603
+ data : Vec :: from_iter ( zip_eq ( self . iter ( ) , second. iter ( ) ) . map ( f) ) ,
604
+ _ph : PhantomData ,
605
+ }
606
+ }
574
607
pub fn find (
575
608
& self ,
576
609
mut predicate : impl FnMut ( UUID < IndexMarker > , & T ) -> bool ,
@@ -588,17 +621,7 @@ impl<T, IndexMarker> FlatAlloc<T, IndexMarker> {
588
621
id_a : UUID < IndexMarker > ,
589
622
id_b : UUID < IndexMarker > ,
590
623
) -> Option < ( & mut T , & mut T ) > {
591
- match id_b. 0 . cmp ( & id_a. 0 ) {
592
- Ordering :: Equal => None ,
593
- Ordering :: Less => {
594
- let ( l, r) = self . data . split_at_mut ( id_a. 0 ) ;
595
- Some ( ( & mut r[ 0 ] , & mut l[ id_b. 0 ] ) )
596
- }
597
- Ordering :: Greater => {
598
- let ( l, r) = self . data . split_at_mut ( id_b. 0 ) ;
599
- Some ( ( & mut l[ id_a. 0 ] , & mut r[ 0 ] ) )
600
- }
601
- }
624
+ get2_mut ( & mut self . data , id_a. 0 , id_b. 0 )
602
625
}
603
626
pub fn get ( & self , id : UUID < IndexMarker > ) -> Option < & T > {
604
627
self . data . get ( id. 0 )
@@ -639,7 +662,9 @@ impl<T, IndexMarker> IndexMut<UUID<IndexMarker>> for FlatAlloc<T, IndexMarker> {
639
662
640
663
impl < T : Debug , IndexMarker > Debug for FlatAlloc < T , IndexMarker > {
641
664
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result {
642
- self . data . fmt ( f)
665
+ f. write_str ( "FlatAlloc::from_vec(vec!" ) ?;
666
+ self . data . fmt ( f) ?;
667
+ f. write_str ( ")" )
643
668
}
644
669
}
645
670
@@ -735,6 +760,41 @@ impl<'a, T, IndexMarker> IntoIterator for &'a mut FlatAlloc<T, IndexMarker> {
735
760
}
736
761
}
737
762
763
+ #[ derive( Debug ) ]
764
+ pub struct FlatAllocConsumingIter < T , IndexMarker > {
765
+ iter : Enumerate < std:: vec:: IntoIter < T > > ,
766
+ _ph : PhantomData < IndexMarker > ,
767
+ }
768
+
769
+ impl < T , IndexMarker > Iterator for FlatAllocConsumingIter < T , IndexMarker > {
770
+ type Item = ( UUID < IndexMarker > , T ) ;
771
+
772
+ fn next ( & mut self ) -> Option < Self :: Item > {
773
+ self . iter . next ( ) . map ( |( id, v) | ( UUID ( id, PhantomData ) , v) )
774
+ }
775
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
776
+ self . iter . size_hint ( )
777
+ }
778
+ }
779
+ impl < T , IndexMarker > ExactSizeIterator for FlatAllocConsumingIter < T , IndexMarker > {
780
+ fn len ( & self ) -> usize {
781
+ self . iter . len ( )
782
+ }
783
+ }
784
+
785
+ impl < T , IndexMarker > IntoIterator for FlatAlloc < T , IndexMarker > {
786
+ type Item = ( UUID < IndexMarker > , T ) ;
787
+
788
+ type IntoIter = FlatAllocConsumingIter < T , IndexMarker > ;
789
+
790
+ fn into_iter ( self ) -> Self :: IntoIter {
791
+ FlatAllocConsumingIter {
792
+ iter : self . data . into_iter ( ) . enumerate ( ) ,
793
+ _ph : PhantomData ,
794
+ }
795
+ }
796
+ }
797
+
738
798
#[ derive( Debug ) ]
739
799
pub struct ZippedIterator <
740
800
IDMarker ,
@@ -757,6 +817,7 @@ impl<
757
817
{
758
818
type Item = ( UUID < IDMarker > , OA , OB ) ;
759
819
820
+ #[ track_caller]
760
821
fn next ( & mut self ) -> Option < Self :: Item > {
761
822
match ( self . iter_a . next ( ) , self . iter_b . next ( ) ) {
762
823
( None , None ) => None ,
@@ -806,6 +867,7 @@ impl<
806
867
{
807
868
type Item = ( UUID < IDMarker > , OA , OB , OC ) ;
808
869
870
+ #[ track_caller]
809
871
fn next ( & mut self ) -> Option < Self :: Item > {
810
872
match ( self . iter_a . next ( ) , self . iter_b . next ( ) , self . iter_c . next ( ) ) {
811
873
( None , None , None ) => None ,
0 commit comments