Skip to content

Commit

Permalink
Merge pull request #257 from RoaringBitmap/rename-remove-front-back
Browse files Browse the repository at this point in the history
Rename the `remove_front/back` methods into `remove_smallest/biggest`
  • Loading branch information
Kerollmops authored Jul 25, 2023
2 parents 9f69285 + 16b2698 commit b8f3a6a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 60 deletions.
12 changes: 6 additions & 6 deletions src/bitmap/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,33 @@ impl Container {
result
}

pub fn remove_front(&mut self, n: u64) {
pub fn remove_smallest(&mut self, n: u64) {
match &self.store {
Store::Bitmap(bits) => {
if bits.len() - n <= ARRAY_LIMIT {
let mut replace_array = Vec::with_capacity((bits.len() - n) as usize);
replace_array.extend(bits.iter().skip(n as usize));
self.store = Store::Array(store::ArrayStore::from_vec_unchecked(replace_array));
} else {
self.store.remove_front(n)
self.store.remove_smallest(n)
}
}
Store::Array(_) => self.store.remove_front(n),
Store::Array(_) => self.store.remove_smallest(n),
};
}

pub fn remove_back(&mut self, n: u64) {
pub fn remove_biggest(&mut self, n: u64) {
match &self.store {
Store::Bitmap(bits) => {
if bits.len() - n <= ARRAY_LIMIT {
let mut replace_array = Vec::with_capacity((bits.len() - n) as usize);
replace_array.extend(bits.iter().take((bits.len() - n) as usize));
self.store = Store::Array(store::ArrayStore::from_vec_unchecked(replace_array));
} else {
self.store.remove_back(n)
self.store.remove_biggest(n)
}
}
Store::Array(_) => self.store.remove_back(n),
Store::Array(_) => self.store.remove_biggest(n),
};
}

Expand Down
72 changes: 36 additions & 36 deletions src/bitmap/inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,21 +579,21 @@ impl RoaringBitmap {
None
}

/// Removes the specified number of elements from the top.
/// Removes the `n` smallests values from this bitmap.
///
/// # Examples
///
/// ```rust
/// use roaring::RoaringBitmap;
///
/// let mut rb = RoaringBitmap::from_iter([1, 5, 7, 9]);
/// rb.remove_front(2);
/// rb.remove_smallest(2);
/// assert_eq!(rb, RoaringBitmap::from_iter([7, 9]));
///
/// let mut rb = RoaringBitmap::from_iter([1, 3, 7, 9]);
/// rb.remove_front(2);
/// rb.remove_smallest(2);
/// assert_eq!(rb, RoaringBitmap::from_iter([7, 9]));
pub fn remove_front(&mut self, mut n: u64) {
pub fn remove_smallest(&mut self, mut n: u64) {
// remove containers up to the front of the target
let position = self.containers.iter().position(|container| {
let container_len = container.len();
Expand All @@ -611,23 +611,23 @@ impl RoaringBitmap {
// remove data in containers if there are still targets for deletion
if n > 0 && !self.containers.is_empty() {
// container immediately before should have been deleted, so the target is 0 index
self.containers[0].remove_front(n);
self.containers[0].remove_smallest(n);
}
}

/// Removes the specified number of elements from the tail.
/// Removes the `n` biggests values from this bitmap.
///
/// # Examples
///
/// ```rust
/// use roaring::RoaringBitmap;
///
/// let mut rb = RoaringBitmap::from_iter([1, 5, 7, 9]);
/// rb.remove_back(2);
/// rb.remove_biggest(2);
/// assert_eq!(rb, RoaringBitmap::from_iter([1, 5]));
/// rb.remove_back(1);
/// rb.remove_biggest(1);
/// assert_eq!(rb, RoaringBitmap::from_iter([1]));
pub fn remove_back(&mut self, mut n: u64) {
pub fn remove_biggest(&mut self, mut n: u64) {
// remove containers up to the back of the target
let position = self.containers.iter().rposition(|container| {
let container_len = container.len();
Expand All @@ -642,7 +642,7 @@ impl RoaringBitmap {
if let Some(position) = position {
self.containers.drain(position + 1..);
if n > 0 && !self.containers.is_empty() {
self.containers[position].remove_back(n);
self.containers[position].remove_biggest(n);
}
} else {
self.containers.clear();
Expand Down Expand Up @@ -800,127 +800,127 @@ mod tests {
}

#[test]
fn remove_front_for_vec() {
fn remove_smallest_for_vec() {
let mut bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]);
bitmap.remove_front(3);
bitmap.remove_smallest(3);
assert_eq!(bitmap.len(), 3);
assert_eq!(bitmap, RoaringBitmap::from_iter([7, 9, 11]));

bitmap = RoaringBitmap::from_iter([1, 2, 5, 7, 9, 11]);
bitmap.remove_front(3);
bitmap.remove_smallest(3);
assert_eq!(bitmap.len(), 3);
assert_eq!(bitmap, RoaringBitmap::from_iter([7, 9, 11]));

bitmap = RoaringBitmap::from_iter([1, 3]);
bitmap.remove_front(2);
bitmap.remove_smallest(2);
assert_eq!(bitmap.len(), 0);

bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]);
bitmap.remove_front(0);
bitmap.remove_smallest(0);
assert_eq!(bitmap.len(), 6);
assert_eq!(bitmap, RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]));

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..(1_u32 << 16) + 5);
bitmap.remove_front(65537);
bitmap.remove_smallest(65537);
assert_eq!(bitmap.len(), 4);
assert_eq!(bitmap, RoaringBitmap::from_iter([65537, 65538, 65539, 65540]));

bitmap = RoaringBitmap::from_iter([1, 2, 5, 7, 9, 11]);
bitmap.remove_front(7);
bitmap.remove_smallest(7);
assert_eq!(bitmap, RoaringBitmap::default());
}

#[test]
fn remove_front_for_bit() {
fn remove_smallest_for_bit() {
let mut bitmap = RoaringBitmap::new();
bitmap.insert_range(0..4098);
bitmap.remove_front(4095);
bitmap.remove_smallest(4095);
assert_eq!(bitmap.len(), 3);
// removed bit to vec
assert_eq!(bitmap, RoaringBitmap::from_iter([4095, 4096, 4097]));

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..6000);
bitmap.remove_front(999);
bitmap.remove_smallest(999);
assert_eq!(bitmap.len(), 5001);

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..8000);
bitmap.remove_front(10);
bitmap.remove_smallest(10);
assert_eq!(bitmap.len(), 7990);

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..200000);
bitmap.remove_front(2000);
bitmap.remove_smallest(2000);
assert_eq!(bitmap.len(), 198000);
assert_eq!(bitmap, RoaringBitmap::from_iter(2000..200000));

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..2);
bitmap.insert_range(4..7);
bitmap.insert_range(1000..6000);
bitmap.remove_front(30);
bitmap.remove_smallest(30);
assert_eq!(bitmap.len(), 4975);

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..65535);
bitmap.remove_front(0);
bitmap.remove_smallest(0);
assert_eq!(bitmap.len(), 65535);
}

#[test]
fn remove_back_for_bit() {
fn remove_biggest_for_bit() {
let mut bitmap = RoaringBitmap::new();
bitmap.insert_range(0..5000);
bitmap.remove_back(1000);
bitmap.remove_biggest(1000);
assert_eq!(bitmap.len(), 4000);

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..6000);
bitmap.remove_back(1000);
bitmap.remove_biggest(1000);
assert_eq!(bitmap.len(), 5000);

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..200000);
bitmap.remove_back(196000);
bitmap.remove_biggest(196000);
assert_eq!(bitmap.len(), 4000);

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..200000);
bitmap.remove_back(2000);
bitmap.remove_biggest(2000);
assert_eq!(bitmap.len(), 198000);
assert_eq!(bitmap, RoaringBitmap::from_iter(0..198000));

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..65535);
bitmap.remove_back(0);
bitmap.remove_biggest(0);
assert_eq!(bitmap.len(), 65535);
}

#[test]
fn remove_back_for_vec() {
fn remove_biggest_for_vec() {
let mut bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]);
bitmap.remove_back(2);
bitmap.remove_biggest(2);
assert_eq!(bitmap, RoaringBitmap::from_iter([1, 2, 3, 7]));

bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]);
bitmap.remove_back(6);
bitmap.remove_biggest(6);
assert_eq!(bitmap.len(), 0);

bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]);
bitmap.remove_back(0);
bitmap.remove_biggest(0);
assert_eq!(bitmap.len(), 6);
assert_eq!(bitmap, RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]));

bitmap = RoaringBitmap::new();
bitmap.insert_range(0..(1_u32 << 16) + 5);
bitmap.remove_back(65537);
bitmap.remove_biggest(65537);
assert_eq!(bitmap.len(), 4);
assert_eq!(bitmap, RoaringBitmap::from_iter([0, 1, 2, 3]));

let mut bitmap = RoaringBitmap::from_iter([1, 2, 3]);
bitmap.remove_back(4);
bitmap.remove_biggest(4);
assert_eq!(bitmap, RoaringBitmap::default());
}
}
12 changes: 6 additions & 6 deletions src/bitmap/store/array_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ impl ArrayStore {
(pos_end - pos_start) as u64
}

pub fn remove_front(&mut self, n: u64) {
pub fn remove_smallest(&mut self, n: u64) {
self.vec.rotate_left(n as usize);
self.vec.truncate(self.vec.len() - n as usize);
}

pub fn remove_back(&mut self, n: u64) {
pub fn remove_biggest(&mut self, n: u64) {
self.vec.truncate(self.vec.len() - n as usize);
}

Expand Down Expand Up @@ -573,16 +573,16 @@ mod tests {
}

#[test]
fn test_bitmap_remove_front() {
fn test_bitmap_remove_smallest() {
let mut store = Store::Array(ArrayStore::from_vec_unchecked(vec![1, 2, 130, 500]));
store.remove_front(3);
store.remove_smallest(3);
assert_eq!(into_vec(store), vec![500]);
}

#[test]
fn test_bitmap_remove_back() {
fn test_bitmap_remove_biggest() {
let mut store = Store::Array(ArrayStore::from_vec_unchecked(vec![1, 2, 130, 500]));
store.remove_back(2);
store.remove_biggest(2);
assert_eq!(into_vec(store), vec![1, 2]);
}
}
12 changes: 6 additions & 6 deletions src/bitmap/store/bitmap_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl BitmapStore {
}

/// Set N bits that are currently 1 bit from the lower bit to 0.
pub fn remove_front(&mut self, mut clear_bits: u64) {
pub fn remove_smallest(&mut self, mut clear_bits: u64) {
if self.len() < clear_bits {
self.clear();
return;
Expand All @@ -332,7 +332,7 @@ impl BitmapStore {
}

/// Set N bits that are currently 1 bit from the lower bit to 0.
pub fn remove_back(&mut self, mut clear_bits: u64) {
pub fn remove_biggest(&mut self, mut clear_bits: u64) {
if self.len() < clear_bits {
self.clear();
return;
Expand Down Expand Up @@ -547,29 +547,29 @@ mod tests {
use super::*;

#[test]
fn test_bitmap_remove_front() {
fn test_bitmap_remove_smallest() {
let mut store = BitmapStore::new();
let range = RangeInclusive::new(1, 3);
store.insert_range(range);
let range_second = RangeInclusive::new(5, 65535);
// store.bits[0] = 0b1111111111111111111111111111111111111111111111111111111111101110
store.insert_range(range_second);
store.remove_front(2);
store.remove_smallest(2);
assert_eq!(
store.bits[0],
0b1111111111111111111111111111111111111111111111111111111111101000
);
}

#[test]
fn test_bitmap_remove_back() {
fn test_bitmap_remove_biggest() {
let mut store = BitmapStore::new();
let range = RangeInclusive::new(1, 3);
store.insert_range(range);
let range_second = RangeInclusive::new(5, 65535);
// store.bits[1023] = 0b1111111111111111111111111111111111111111111111111111111111111111
store.insert_range(range_second);
store.remove_back(2);
store.remove_biggest(2);
assert_eq!(
store.bits[1023],
0b11111111111111111111111111111111111111111111111111111111111111
Expand Down
12 changes: 6 additions & 6 deletions src/bitmap/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,17 @@ impl Store {
}
}

pub fn remove_front(&mut self, index: u64) {
pub fn remove_smallest(&mut self, index: u64) {
match self {
Array(vec) => vec.remove_front(index),
Bitmap(bits) => bits.remove_front(index),
Array(vec) => vec.remove_smallest(index),
Bitmap(bits) => bits.remove_smallest(index),
}
}

pub fn remove_back(&mut self, index: u64) {
pub fn remove_biggest(&mut self, index: u64) {
match self {
Array(vec) => vec.remove_back(index),
Bitmap(bits) => bits.remove_back(index),
Array(vec) => vec.remove_biggest(index),
Bitmap(bits) => bits.remove_biggest(index),
}
}

Expand Down

0 comments on commit b8f3a6a

Please sign in to comment.