Skip to content

Commit

Permalink
Replace insert_many by the Extend trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Jan 7, 2025
1 parent 245d604 commit 0dcd67a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 58 deletions.
52 changes: 1 addition & 51 deletions roaring/src/bitmap/inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl RoaringBitmap {
///
/// Return the index of the target container.
#[inline]
fn find_container_by_key(&mut self, key: u16) -> usize {
pub(crate) fn find_container_by_key(&mut self, key: u16) -> usize {
match self.containers.binary_search_by_key(&key, |c| c.key) {
Ok(loc) => loc,
Err(loc) => {
Expand All @@ -76,56 +76,6 @@ impl RoaringBitmap {
}
}

/// Inserts multiple values and returns the count of new additions.
/// This is expected to be faster than calling [`RoaringBitmap::insert`] on each value.
///
/// The provided integers values don't have to be in sorted order, but it may be preferable
/// to sort them from a performance point of view.
///
/// # Examples
///
/// ```rust
/// use roaring::RoaringBitmap;
///
/// let mut rb = RoaringBitmap::new();
/// rb.insert_many([1, 2, 3, 4, 1500, 1508, 1507, 1509]);
/// assert!(rb.contains(2));
/// assert!(rb.contains(1508));
/// assert!(!rb.contains(5));
/// ```
#[inline]
pub fn insert_many<I>(&mut self, values: I) -> u64
where
I: IntoIterator<Item = u32>,
{
let mut values = values.into_iter();
let value = match values.next() {
Some(value) => value,
None => return 0,
};

let mut inserted = 0;
let (mut currenthb, lowbit) = util::split(value);
let mut current_container_index = self.find_container_by_key(currenthb);
let mut current_cont = &mut self.containers[current_container_index];
inserted += current_cont.insert(lowbit) as u64;

for val in values {
let (newhb, lowbit) = util::split(val);
if currenthb == newhb {
// easy case, this could be quite frequent
inserted += current_cont.insert(lowbit) as u64;
} else {
currenthb = newhb;
current_container_index = self.find_container_by_key(currenthb);
current_cont = &mut self.containers[current_container_index];
inserted += current_cont.insert(lowbit) as u64;
}
}

inserted
}

/// Inserts a range of values.
/// Returns the number of inserted values.
///
Expand Down
66 changes: 59 additions & 7 deletions roaring/src/bitmap/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,18 +715,70 @@ impl<'a> FromIterator<&'a u32> for RoaringBitmap {
}

impl Extend<u32> for RoaringBitmap {
fn extend<I: IntoIterator<Item = u32>>(&mut self, iterator: I) {
for value in iterator {
self.insert(value);
/// Inserts multiple values and returns the count of new additions.
/// This is expected to be faster than calling [`RoaringBitmap::insert`] on each value.
///
/// The provided integers values don't have to be in sorted order, but it may be preferable
/// to sort them from a performance point of view.
///
/// # Examples
///
/// ```rust
/// use roaring::RoaringBitmap;
///
/// let mut rb = RoaringBitmap::new();
/// rb.insert_many([1, 2, 3, 4, 1500, 1508, 1507, 1509]);
/// assert!(rb.contains(2));
/// assert!(rb.contains(1508));
/// assert!(!rb.contains(5));
/// ```
fn extend<I: IntoIterator<Item = u32>>(&mut self, values: I) {
let mut values = values.into_iter();
let value = match values.next() {
Some(value) => value,
None => return,
};

let (mut currenthb, lowbit) = util::split(value);
let mut current_container_index = self.find_container_by_key(currenthb);
let mut current_cont = &mut self.containers[current_container_index];
current_cont.insert(lowbit) as u64;

for val in values {
let (newhb, lowbit) = util::split(val);
if currenthb == newhb {
// easy case, this could be quite frequent
current_cont.insert(lowbit) as u64;
} else {
currenthb = newhb;
current_container_index = self.find_container_by_key(currenthb);
current_cont = &mut self.containers[current_container_index];
current_cont.insert(lowbit) as u64;
}
}
}
}

impl<'a> Extend<&'a u32> for RoaringBitmap {
fn extend<I: IntoIterator<Item = &'a u32>>(&mut self, iterator: I) {
for value in iterator {
self.insert(*value);
}
/// Inserts multiple values and returns the count of new additions.
/// This is expected to be faster than calling [`RoaringBitmap::insert`] on each value.
///
/// The provided integers values don't have to be in sorted order, but it may be preferable
/// to sort them from a performance point of view.
///
/// # Examples
///
/// ```rust
/// use roaring::RoaringBitmap;
///
/// let mut rb = RoaringBitmap::new();
/// rb.insert_many([1, 2, 3, 4, 1500, 1508, 1507, 1509]);
/// assert!(rb.contains(2));
/// assert!(rb.contains(1508));
/// assert!(!rb.contains(5));
/// ```
fn extend<I: IntoIterator<Item = &'a u32>>(&mut self, values: I) {
self.extend(values.into_iter().copied());
}
}

Expand Down

0 comments on commit 0dcd67a

Please sign in to comment.