@@ -794,6 +794,85 @@ mod doublepq_tests {
794794 */
795795 }
796796
797+ #[ test]
798+ fn retain ( ) {
799+ #[ derive( Hash , PartialEq , Eq , Debug ) ]
800+ struct Animal {
801+ name : String ,
802+ can_fly : bool ,
803+ can_swim : bool ,
804+ }
805+
806+ impl Animal {
807+ pub fn new ( name : String , can_fly : bool , can_swim : bool ) -> Self {
808+ Animal {
809+ name,
810+ can_fly,
811+ can_swim,
812+ }
813+ }
814+ }
815+
816+ let mut pq = DoublePriorityQueue :: new ( ) ;
817+ pq. push ( Animal :: new ( "dog" . to_string ( ) , false , true ) , 1 ) ;
818+ pq. push ( Animal :: new ( "cat" . to_string ( ) , false , false ) , 2 ) ;
819+ pq. push ( Animal :: new ( "bird" . to_string ( ) , true , false ) , 7 ) ;
820+ pq. push ( Animal :: new ( "fish" . to_string ( ) , false , true ) , 4 ) ;
821+ pq. push ( Animal :: new ( "cow" . to_string ( ) , false , false ) , 3 ) ;
822+
823+ pq. retain ( |i, _| i. can_swim ) ;
824+
825+ assert_eq ! (
826+ pq. pop_max( ) ,
827+ Some ( ( Animal :: new( "fish" . to_string( ) , false , true ) , 4 ) )
828+ ) ;
829+ assert_eq ! (
830+ pq. pop_max( ) ,
831+ Some ( ( Animal :: new( "dog" . to_string( ) , false , true ) , 1 ) )
832+ ) ;
833+ }
834+
835+ #[ test]
836+ fn retain_mut ( ) {
837+ #[ derive( Hash , PartialEq , Eq , Debug ) ]
838+ struct Animal {
839+ name : String ,
840+ can_fly : bool ,
841+ can_swim : bool ,
842+ }
843+
844+ impl Animal {
845+ pub fn new ( name : String , can_fly : bool , can_swim : bool ) -> Self {
846+ Animal {
847+ name,
848+ can_fly,
849+ can_swim,
850+ }
851+ }
852+ }
853+
854+ let mut pq = DoublePriorityQueue :: new ( ) ;
855+ pq. push ( Animal :: new ( "dog" . to_string ( ) , false , true ) , 1 ) ;
856+ pq. push ( Animal :: new ( "cat" . to_string ( ) , false , false ) , 2 ) ;
857+ pq. push ( Animal :: new ( "bird" . to_string ( ) , true , false ) , 7 ) ;
858+ pq. push ( Animal :: new ( "fish" . to_string ( ) , false , true ) , 4 ) ;
859+ pq. push ( Animal :: new ( "cow" . to_string ( ) , false , false ) , 3 ) ;
860+
861+ pq. retain_mut ( |i, p| {
862+ * p += 10 ;
863+ i. can_swim
864+ } ) ;
865+
866+ assert_eq ! (
867+ pq. pop_max( ) ,
868+ Some ( ( Animal :: new( "fish" . to_string( ) , false , true ) , 14 ) )
869+ ) ;
870+ assert_eq ! (
871+ pq. pop_max( ) ,
872+ Some ( ( Animal :: new( "dog" . to_string( ) , false , true ) , 11 ) )
873+ ) ;
874+ }
875+
797876 #[ test]
798877 fn into_sorted_iter ( ) {
799878 let mut pq = DoublePriorityQueue :: new ( ) ;
0 commit comments