@@ -578,6 +578,76 @@ impl RoaringBitmap {
578578
579579 None
580580 }
581+
582+ /// Removes the specified number of elements from the top.
583+ ///
584+ /// # Examples
585+ ///
586+ /// ```rust
587+ /// use roaring::RoaringBitmap;
588+ ///
589+ /// let mut rb = RoaringBitmap::from_iter([1, 5, 7, 9]);
590+ /// rb.remove_front(2);
591+ /// assert_eq!(rb, RoaringBitmap::from_iter([7, 9]));
592+ ///
593+ /// let mut rb = RoaringBitmap::from_iter([1, 3, 7, 9]);
594+ /// rb.remove_front(2);
595+ /// assert_eq!(rb, RoaringBitmap::from_iter([7, 9]));
596+ pub fn remove_front ( & mut self , mut n : u64 ) {
597+ // remove containers up to the front of the target
598+ let position = self . containers . iter ( ) . position ( |container| {
599+ let container_len = container. len ( ) ;
600+ if container_len <= n {
601+ n -= container_len;
602+ false
603+ } else {
604+ true
605+ }
606+ } ) ;
607+ let position = position. unwrap_or ( self . containers . len ( ) ) ;
608+ if position > 0 {
609+ self . containers . drain ( ..position) ;
610+ }
611+ // remove data in containers if there are still targets for deletion
612+ if n > 0 && !self . containers . is_empty ( ) {
613+ // container immediately before should have been deleted, so the target is 0 index
614+ self . containers [ 0 ] . remove_front ( n) ;
615+ }
616+ }
617+
618+ /// Removes the specified number of elements from the tail.
619+ ///
620+ /// # Examples
621+ ///
622+ /// ```rust
623+ /// use roaring::RoaringBitmap;
624+ ///
625+ /// let mut rb = RoaringBitmap::from_iter([1, 5, 7, 9]);
626+ /// rb.remove_back(2);
627+ /// assert_eq!(rb, RoaringBitmap::from_iter([1, 5]));
628+ /// rb.remove_back(1);
629+ /// assert_eq!(rb, RoaringBitmap::from_iter([1]));
630+ pub fn remove_back ( & mut self , mut n : u64 ) {
631+ // remove containers up to the back of the target
632+ let position = self . containers . iter ( ) . rposition ( |container| {
633+ let container_len = container. len ( ) ;
634+ if container_len <= n {
635+ n -= container_len;
636+ false
637+ } else {
638+ true
639+ }
640+ } ) ;
641+ // It is checked at the beginning of the function, so it is usually never an Err
642+ if let Some ( position) = position {
643+ self . containers . drain ( position + 1 ..) ;
644+ if n > 0 && !self . containers . is_empty ( ) {
645+ self . containers [ position] . remove_back ( n) ;
646+ }
647+ } else {
648+ self . containers . clear ( ) ;
649+ }
650+ }
581651}
582652
583653impl Default for RoaringBitmap {
@@ -728,4 +798,129 @@ mod tests {
728798 assert_eq ! ( bitmap. containers. len( ) , 1 ) ;
729799 assert_eq ! ( bitmap. containers[ 0 ] . key, 1 ) ;
730800 }
801+
802+ #[ test]
803+ fn remove_front_for_vec ( ) {
804+ let mut bitmap = RoaringBitmap :: from_iter ( [ 1 , 2 , 3 , 7 , 9 , 11 ] ) ;
805+ bitmap. remove_front ( 3 ) ;
806+ assert_eq ! ( bitmap. len( ) , 3 ) ;
807+ assert_eq ! ( bitmap, RoaringBitmap :: from_iter( [ 7 , 9 , 11 ] ) ) ;
808+
809+ bitmap = RoaringBitmap :: from_iter ( [ 1 , 2 , 5 , 7 , 9 , 11 ] ) ;
810+ bitmap. remove_front ( 3 ) ;
811+ assert_eq ! ( bitmap. len( ) , 3 ) ;
812+ assert_eq ! ( bitmap, RoaringBitmap :: from_iter( [ 7 , 9 , 11 ] ) ) ;
813+
814+ bitmap = RoaringBitmap :: from_iter ( [ 1 , 3 ] ) ;
815+ bitmap. remove_front ( 2 ) ;
816+ assert_eq ! ( bitmap. len( ) , 0 ) ;
817+
818+ bitmap = RoaringBitmap :: from_iter ( [ 1 , 2 , 3 , 7 , 9 , 11 ] ) ;
819+ bitmap. remove_front ( 0 ) ;
820+ assert_eq ! ( bitmap. len( ) , 6 ) ;
821+ assert_eq ! ( bitmap, RoaringBitmap :: from_iter( [ 1 , 2 , 3 , 7 , 9 , 11 ] ) ) ;
822+
823+ bitmap = RoaringBitmap :: new ( ) ;
824+ bitmap. insert_range ( 0 ..( 1_u32 << 16 ) + 5 ) ;
825+ bitmap. remove_front ( 65537 ) ;
826+ assert_eq ! ( bitmap. len( ) , 4 ) ;
827+ assert_eq ! ( bitmap, RoaringBitmap :: from_iter( [ 65537 , 65538 , 65539 , 65540 ] ) ) ;
828+
829+ bitmap = RoaringBitmap :: from_iter ( [ 1 , 2 , 5 , 7 , 9 , 11 ] ) ;
830+ bitmap. remove_front ( 7 ) ;
831+ assert_eq ! ( bitmap, RoaringBitmap :: default ( ) ) ;
832+ }
833+
834+ #[ test]
835+ fn remove_front_for_bit ( ) {
836+ let mut bitmap = RoaringBitmap :: new ( ) ;
837+ bitmap. insert_range ( 0 ..4098 ) ;
838+ bitmap. remove_front ( 4095 ) ;
839+ assert_eq ! ( bitmap. len( ) , 3 ) ;
840+ // removed bit to vec
841+ assert_eq ! ( bitmap, RoaringBitmap :: from_iter( [ 4095 , 4096 , 4097 ] ) ) ;
842+
843+ bitmap = RoaringBitmap :: new ( ) ;
844+ bitmap. insert_range ( 0 ..6000 ) ;
845+ bitmap. remove_front ( 999 ) ;
846+ assert_eq ! ( bitmap. len( ) , 5001 ) ;
847+
848+ bitmap = RoaringBitmap :: new ( ) ;
849+ bitmap. insert_range ( 0 ..8000 ) ;
850+ bitmap. remove_front ( 10 ) ;
851+ assert_eq ! ( bitmap. len( ) , 7990 ) ;
852+
853+ bitmap = RoaringBitmap :: new ( ) ;
854+ bitmap. insert_range ( 0 ..200000 ) ;
855+ bitmap. remove_front ( 2000 ) ;
856+ assert_eq ! ( bitmap. len( ) , 198000 ) ;
857+ assert_eq ! ( bitmap, RoaringBitmap :: from_iter( 2000 ..200000 ) ) ;
858+
859+ bitmap = RoaringBitmap :: new ( ) ;
860+ bitmap. insert_range ( 0 ..2 ) ;
861+ bitmap. insert_range ( 4 ..7 ) ;
862+ bitmap. insert_range ( 1000 ..6000 ) ;
863+ bitmap. remove_front ( 30 ) ;
864+ assert_eq ! ( bitmap. len( ) , 4975 ) ;
865+
866+ bitmap = RoaringBitmap :: new ( ) ;
867+ bitmap. insert_range ( 0 ..65535 ) ;
868+ bitmap. remove_front ( 0 ) ;
869+ assert_eq ! ( bitmap. len( ) , 65535 ) ;
870+ }
871+
872+ #[ test]
873+ fn remove_back_for_bit ( ) {
874+ let mut bitmap = RoaringBitmap :: new ( ) ;
875+ bitmap. insert_range ( 0 ..5000 ) ;
876+ bitmap. remove_back ( 1000 ) ;
877+ assert_eq ! ( bitmap. len( ) , 4000 ) ;
878+
879+ bitmap = RoaringBitmap :: new ( ) ;
880+ bitmap. insert_range ( 0 ..6000 ) ;
881+ bitmap. remove_back ( 1000 ) ;
882+ assert_eq ! ( bitmap. len( ) , 5000 ) ;
883+
884+ bitmap = RoaringBitmap :: new ( ) ;
885+ bitmap. insert_range ( 0 ..200000 ) ;
886+ bitmap. remove_back ( 196000 ) ;
887+ assert_eq ! ( bitmap. len( ) , 4000 ) ;
888+
889+ bitmap = RoaringBitmap :: new ( ) ;
890+ bitmap. insert_range ( 0 ..200000 ) ;
891+ bitmap. remove_back ( 2000 ) ;
892+ assert_eq ! ( bitmap. len( ) , 198000 ) ;
893+ assert_eq ! ( bitmap, RoaringBitmap :: from_iter( 0 ..198000 ) ) ;
894+
895+ bitmap = RoaringBitmap :: new ( ) ;
896+ bitmap. insert_range ( 0 ..65535 ) ;
897+ bitmap. remove_back ( 0 ) ;
898+ assert_eq ! ( bitmap. len( ) , 65535 ) ;
899+ }
900+
901+ #[ test]
902+ fn remove_back_for_vec ( ) {
903+ let mut bitmap = RoaringBitmap :: from_iter ( [ 1 , 2 , 3 , 7 , 9 , 11 ] ) ;
904+ bitmap. remove_back ( 2 ) ;
905+ assert_eq ! ( bitmap, RoaringBitmap :: from_iter( [ 1 , 2 , 3 , 7 ] ) ) ;
906+
907+ bitmap = RoaringBitmap :: from_iter ( [ 1 , 2 , 3 , 7 , 9 , 11 ] ) ;
908+ bitmap. remove_back ( 6 ) ;
909+ assert_eq ! ( bitmap. len( ) , 0 ) ;
910+
911+ bitmap = RoaringBitmap :: from_iter ( [ 1 , 2 , 3 , 7 , 9 , 11 ] ) ;
912+ bitmap. remove_back ( 0 ) ;
913+ assert_eq ! ( bitmap. len( ) , 6 ) ;
914+ assert_eq ! ( bitmap, RoaringBitmap :: from_iter( [ 1 , 2 , 3 , 7 , 9 , 11 ] ) ) ;
915+
916+ bitmap = RoaringBitmap :: new ( ) ;
917+ bitmap. insert_range ( 0 ..( 1_u32 << 16 ) + 5 ) ;
918+ bitmap. remove_back ( 65537 ) ;
919+ assert_eq ! ( bitmap. len( ) , 4 ) ;
920+ assert_eq ! ( bitmap, RoaringBitmap :: from_iter( [ 0 , 1 , 2 , 3 ] ) ) ;
921+
922+ let mut bitmap = RoaringBitmap :: from_iter ( [ 1 , 2 , 3 ] ) ;
923+ bitmap. remove_back ( 4 ) ;
924+ assert_eq ! ( bitmap, RoaringBitmap :: default ( ) ) ;
925+ }
731926}
0 commit comments