/rust/registry/src/index.crates.io-1949cf8c6b5b557f/roaring-0.11.4/src/bitmap/inherent.rs
Line | Count | Source |
1 | | use core::cmp::Ordering; |
2 | | use core::mem::size_of; |
3 | | use core::ops::{RangeBounds, RangeInclusive}; |
4 | | |
5 | | use crate::bitmap::store::BITMAP_LENGTH; |
6 | | use crate::{IntegerTooSmall, RoaringBitmap}; |
7 | | |
8 | | use super::container::Container; |
9 | | use super::util; |
10 | | |
11 | | #[cfg(not(feature = "std"))] |
12 | | use alloc::vec::Vec; |
13 | | |
14 | | impl RoaringBitmap { |
15 | | /// Creates an empty `RoaringBitmap`. |
16 | | /// |
17 | | /// # Examples |
18 | | /// |
19 | | /// ```rust |
20 | | /// use roaring::RoaringBitmap; |
21 | | /// let rb = RoaringBitmap::new(); |
22 | | /// ``` |
23 | 0 | pub fn new() -> RoaringBitmap { |
24 | 0 | RoaringBitmap { containers: Vec::new() } |
25 | 0 | } |
26 | | |
27 | | /// Creates a full `RoaringBitmap`. |
28 | | /// |
29 | | /// # Examples |
30 | | /// |
31 | | /// ```rust |
32 | | /// use roaring::RoaringBitmap; |
33 | | /// let rb = RoaringBitmap::full(); |
34 | | /// ``` |
35 | 0 | pub fn full() -> RoaringBitmap { |
36 | 0 | RoaringBitmap { containers: (0..=u16::MAX).map(Container::full).collect() } |
37 | 0 | } |
38 | | |
39 | | /// Creates a `RoaringBitmap` from a byte slice, interpreting the bytes as a bitmap with a specified offset. |
40 | | /// |
41 | | /// # Arguments |
42 | | /// |
43 | | /// - `offset: u32` - The starting position in the bitmap where the byte slice will be applied, specified in bits. |
44 | | /// This means that if `offset` is `n`, the first byte in the slice will correspond to the `n`th bit(0-indexed) in the bitmap. |
45 | | /// - `bytes: &[u8]` - The byte slice containing the bitmap data. The bytes are interpreted in "Least-Significant-First" bit order. |
46 | | /// |
47 | | /// # Interpretation of `bytes` |
48 | | /// |
49 | | /// The `bytes` slice is interpreted in "Least-Significant-First" bit order. Each byte is read from least significant bit (LSB) to most significant bit (MSB). |
50 | | /// For example, the byte `0b00000101` represents the bits `1, 0, 1, 0, 0, 0, 0, 0` in that order (see Examples section). |
51 | | /// |
52 | | /// |
53 | | /// # Panics |
54 | | /// |
55 | | /// This function will panic if `bytes.len() + offset` is greater than 2^32. |
56 | | /// |
57 | | /// |
58 | | /// # Examples |
59 | | /// |
60 | | /// ```rust |
61 | | /// use roaring::RoaringBitmap; |
62 | | /// |
63 | | /// let bytes = [0b00000101, 0b00000010, 0b00000000, 0b10000000]; |
64 | | /// // ^^^^^^^^ ^^^^^^^^ ^^^^^^^^ ^^^^^^^^ |
65 | | /// // 76543210 98 |
66 | | /// let rb = RoaringBitmap::from_lsb0_bytes(0, &bytes); |
67 | | /// assert!(rb.contains(0)); |
68 | | /// assert!(!rb.contains(1)); |
69 | | /// assert!(rb.contains(2)); |
70 | | /// assert!(rb.contains(9)); |
71 | | /// assert!(rb.contains(31)); |
72 | | /// |
73 | | /// let rb = RoaringBitmap::from_lsb0_bytes(8, &bytes); |
74 | | /// assert!(rb.contains(8)); |
75 | | /// assert!(!rb.contains(9)); |
76 | | /// assert!(rb.contains(10)); |
77 | | /// assert!(rb.contains(17)); |
78 | | /// assert!(rb.contains(39)); |
79 | | /// |
80 | | /// let rb = RoaringBitmap::from_lsb0_bytes(3, &bytes); |
81 | | /// assert!(rb.contains(3)); |
82 | | /// assert!(!rb.contains(4)); |
83 | | /// assert!(rb.contains(5)); |
84 | | /// assert!(rb.contains(12)); |
85 | | /// assert!(rb.contains(34)); |
86 | | /// ``` |
87 | 0 | pub fn from_lsb0_bytes(offset: u32, mut bytes: &[u8]) -> RoaringBitmap { |
88 | 0 | fn shift_bytes(bytes: &[u8], amount: usize) -> Vec<u8> { |
89 | 0 | let mut result = Vec::with_capacity(bytes.len() + 1); |
90 | 0 | let mut carry = 0u8; |
91 | | |
92 | 0 | for &byte in bytes { |
93 | 0 | let shifted = (byte << amount) | carry; |
94 | 0 | carry = byte >> (8 - amount); |
95 | 0 | result.push(shifted); |
96 | 0 | } |
97 | | |
98 | 0 | if carry != 0 { |
99 | 0 | result.push(carry); |
100 | 0 | } |
101 | | |
102 | 0 | result |
103 | 0 | } |
104 | 0 | if !offset.is_multiple_of(8) { |
105 | 0 | let shift = offset as usize % 8; |
106 | 0 | let shifted_bytes = shift_bytes(bytes, shift); |
107 | 0 | return RoaringBitmap::from_lsb0_bytes(offset - shift as u32, &shifted_bytes); |
108 | 0 | } |
109 | | |
110 | 0 | if bytes.is_empty() { |
111 | 0 | return RoaringBitmap::new(); |
112 | 0 | } |
113 | | |
114 | | // Using inclusive range avoids overflow: the max exclusive value is 2^32 (u32::MAX + 1). |
115 | 0 | let end_bit_inc = u32::try_from(bytes.len()) |
116 | 0 | .ok() |
117 | 0 | .and_then(|len_bytes| len_bytes.checked_mul(8)) |
118 | | // `bytes` is non-empty, so len_bits is > 0 |
119 | 0 | .and_then(|len_bits| offset.checked_add(len_bits - 1)) |
120 | 0 | .expect("offset + bytes.len() must be <= 2^32"); |
121 | | |
122 | | // offsets are in bytes |
123 | 0 | let (mut start_container, start_offset) = |
124 | 0 | (offset as usize >> 16, (offset as usize % 0x1_0000) / 8); |
125 | 0 | let (end_container_inc, end_offset) = |
126 | 0 | (end_bit_inc as usize >> 16, (end_bit_inc as usize % 0x1_0000 + 1) / 8); |
127 | | |
128 | 0 | let n_containers_needed = end_container_inc + 1 - start_container; |
129 | 0 | let mut containers = Vec::with_capacity(n_containers_needed); |
130 | | |
131 | | // Handle a partial first container |
132 | 0 | if start_offset != 0 { |
133 | 0 | let end_byte = if end_container_inc == start_container { |
134 | 0 | end_offset |
135 | | } else { |
136 | 0 | BITMAP_LENGTH * size_of::<u64>() |
137 | | }; |
138 | | |
139 | 0 | let (src, rest) = bytes.split_at(end_byte - start_offset); |
140 | 0 | bytes = rest; |
141 | | |
142 | 0 | if let Some(container) = |
143 | 0 | Container::from_lsb0_bytes(start_container as u16, src, start_offset) |
144 | 0 | { |
145 | 0 | containers.push(container); |
146 | 0 | } |
147 | | |
148 | 0 | start_container += 1; |
149 | 0 | } |
150 | | |
151 | | // Handle all full containers |
152 | 0 | for full_container_key in start_container..end_container_inc { |
153 | 0 | let (src, rest) = bytes.split_at(BITMAP_LENGTH * size_of::<u64>()); |
154 | 0 | bytes = rest; |
155 | | |
156 | 0 | if let Some(container) = Container::from_lsb0_bytes(full_container_key as u16, src, 0) { |
157 | 0 | containers.push(container); |
158 | 0 | } |
159 | | } |
160 | | |
161 | | // Handle a last container |
162 | 0 | if !bytes.is_empty() { |
163 | 0 | if let Some(container) = Container::from_lsb0_bytes(end_container_inc as u16, bytes, 0) |
164 | 0 | { |
165 | 0 | containers.push(container); |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | 0 | RoaringBitmap { containers } |
170 | 0 | } |
171 | | |
172 | | /// Adds a value to the set. |
173 | | /// |
174 | | /// Returns whether the value was absent from the set. |
175 | | /// |
176 | | /// # Examples |
177 | | /// |
178 | | /// ```rust |
179 | | /// use roaring::RoaringBitmap; |
180 | | /// |
181 | | /// let mut rb = RoaringBitmap::new(); |
182 | | /// assert_eq!(rb.insert(3), true); |
183 | | /// assert_eq!(rb.insert(3), false); |
184 | | /// assert_eq!(rb.contains(3), true); |
185 | | /// ``` |
186 | | #[inline] |
187 | 0 | pub fn insert(&mut self, value: u32) -> bool { |
188 | 0 | let (key, index) = util::split(value); |
189 | 0 | let container = match self.containers.binary_search_by_key(&key, |c| c.key) { |
190 | 0 | Ok(loc) => &mut self.containers[loc], |
191 | 0 | Err(loc) => { |
192 | 0 | self.containers.insert(loc, Container::new(key)); |
193 | 0 | &mut self.containers[loc] |
194 | | } |
195 | | }; |
196 | 0 | container.insert(index) |
197 | 0 | } |
198 | | |
199 | | /// Searches for the specific container by the given key. |
200 | | /// Creates a new container if it doesn't exist. |
201 | | /// |
202 | | /// Return the index of the target container. |
203 | | #[inline] |
204 | 0 | pub(crate) fn find_container_by_key(&mut self, key: u16) -> usize { |
205 | 0 | match self.containers.binary_search_by_key(&key, |c| c.key) { |
206 | 0 | Ok(loc) => loc, |
207 | 0 | Err(loc) => { |
208 | 0 | self.containers.insert(loc, Container::new(key)); |
209 | 0 | loc |
210 | | } |
211 | | } |
212 | 0 | } |
213 | | |
214 | | /// Searches and then modifies a specific container with `M` by the given key. |
215 | | /// Creates a new container using `B` if it doesn't exist. |
216 | | /// |
217 | | /// Returns `R` based on `M` or `B`. |
218 | | #[inline] |
219 | 0 | pub(crate) fn mod_or_build_container_by_key< |
220 | 0 | R, |
221 | 0 | M: FnMut(&mut Container) -> R, |
222 | 0 | B: FnMut(u16) -> (Container, R), |
223 | 0 | >( |
224 | 0 | &mut self, |
225 | 0 | key: u16, |
226 | 0 | mut modifier: M, |
227 | 0 | mut builder: B, |
228 | 0 | ) -> R { |
229 | 0 | match self.containers.binary_search_by_key(&key, |c| c.key) { |
230 | 0 | Ok(loc) => modifier(&mut self.containers[loc]), |
231 | 0 | Err(loc) => { |
232 | 0 | let build_value = builder(key); |
233 | 0 | self.containers.insert(loc, build_value.0); |
234 | 0 | build_value.1 |
235 | | } |
236 | | } |
237 | 0 | } |
238 | | |
239 | | /// Inserts a range of values. |
240 | | /// Returns the number of inserted values. |
241 | | /// |
242 | | /// # Examples |
243 | | /// |
244 | | /// ```rust |
245 | | /// use roaring::RoaringBitmap; |
246 | | /// |
247 | | /// let mut rb = RoaringBitmap::new(); |
248 | | /// rb.insert_range(2..4); |
249 | | /// assert!(rb.contains(2)); |
250 | | /// assert!(rb.contains(3)); |
251 | | /// assert!(!rb.contains(4)); |
252 | | /// ``` |
253 | | #[inline] |
254 | 0 | pub fn insert_range<R>(&mut self, range: R) -> u64 |
255 | 0 | where |
256 | 0 | R: RangeBounds<u32>, |
257 | | { |
258 | 0 | let (start, end) = match util::convert_range_to_inclusive(range) { |
259 | 0 | Ok(range) => (*range.start(), *range.end()), |
260 | 0 | Err(_) => return 0, |
261 | | }; |
262 | | |
263 | 0 | let (start_container_key, start_index) = util::split(start); |
264 | 0 | let (end_container_key, end_index) = util::split(end); |
265 | 0 | let modify_container_range = |
266 | 0 | |bitmap: &mut Self, container_key: u16, range: RangeInclusive<u16>| { |
267 | 0 | bitmap.mod_or_build_container_by_key( |
268 | 0 | container_key, |
269 | 0 | |container| container.insert_range(range.clone()), |
270 | 0 | |key| (Container::new_with_range(key, range.clone()), range.len() as u64), |
271 | | ) |
272 | 0 | }; |
273 | | |
274 | | // If the end range value is in the same container, just call into |
275 | | // the one container. |
276 | 0 | if start_container_key == end_container_key { |
277 | 0 | return modify_container_range(self, start_container_key, start_index..=end_index); |
278 | 0 | } |
279 | | |
280 | | // For the first container, insert start_index..=u16::MAX, with |
281 | | // subsequent containers inserting 0..MAX. |
282 | | // |
283 | | // The last container (end_container_key) is handled explicitly outside |
284 | | // the loop. |
285 | 0 | let mut low = start_index; |
286 | 0 | let mut inserted = 0; |
287 | | |
288 | 0 | for i in start_container_key..end_container_key { |
289 | 0 | inserted += modify_container_range(self, i, low..=u16::MAX); |
290 | 0 |
|
291 | 0 | // After the first container, always fill the containers. |
292 | 0 | low = 0; |
293 | 0 | } |
294 | | |
295 | | // Handle the last container |
296 | 0 | inserted += modify_container_range(self, end_container_key, 0..=end_index); |
297 | | |
298 | 0 | inserted |
299 | 0 | } |
300 | | |
301 | | /// Pushes `value` in the bitmap only if it is greater than the current maximum value. |
302 | | /// |
303 | | /// Returns whether the value was inserted. |
304 | | /// |
305 | | /// # Examples |
306 | | /// |
307 | | /// ```rust |
308 | | /// use roaring::RoaringBitmap; |
309 | | /// |
310 | | /// let mut rb = RoaringBitmap::new(); |
311 | | /// assert!(rb.push(1)); |
312 | | /// assert!(rb.push(3)); |
313 | | /// assert_eq!(rb.push(3), false); |
314 | | /// assert!(rb.push(5)); |
315 | | /// |
316 | | /// assert_eq!(rb.iter().collect::<Vec<u32>>(), vec![1, 3, 5]); |
317 | | /// ``` |
318 | | #[inline] |
319 | | #[deprecated(since = "0.11.0", note = "use `try_push` instead")] |
320 | 0 | pub fn push(&mut self, value: u32) -> bool { |
321 | 0 | self.try_push(value).is_ok() |
322 | 0 | } |
323 | | |
324 | | /// Pushes `value` in the bitmap only if it is greater than the current maximum value. |
325 | | /// |
326 | | /// Returns an error if the value is not greater than the current maximum value. |
327 | | /// |
328 | | /// # Examples |
329 | | /// |
330 | | /// ```rust |
331 | | /// use roaring::{RoaringBitmap, IntegerTooSmall}; |
332 | | /// |
333 | | /// let mut rb = RoaringBitmap::new(); |
334 | | /// assert!(rb.try_push(1).is_ok()); |
335 | | /// assert!(rb.try_push(3).is_ok()); |
336 | | /// assert_eq!(rb.try_push(3), Err(IntegerTooSmall)); |
337 | | /// assert!(rb.try_push(5).is_ok()); |
338 | | /// |
339 | | /// assert_eq!(rb.iter().collect::<Vec<u32>>(), vec![1, 3, 5]); |
340 | | /// ``` |
341 | | #[inline] |
342 | 0 | pub fn try_push(&mut self, value: u32) -> Result<(), IntegerTooSmall> { |
343 | 0 | let (key, index) = util::split(value); |
344 | | |
345 | 0 | match self.containers.last_mut() { |
346 | 0 | Some(container) if container.key == key => { |
347 | 0 | if container.push(index) { |
348 | 0 | Ok(()) |
349 | | } else { |
350 | 0 | Err(IntegerTooSmall) |
351 | | } |
352 | | } |
353 | 0 | Some(container) if container.key > key => Err(IntegerTooSmall), |
354 | 0 | _otherwise => { |
355 | 0 | let mut container = Container::new(key); |
356 | 0 | container.push(index); |
357 | 0 | self.containers.push(container); |
358 | 0 | Ok(()) |
359 | | } |
360 | | } |
361 | 0 | } |
362 | | |
363 | | /// Pushes `value` at the end of the bitmap. |
364 | | /// It is up to the caller to have validated index > self.max() |
365 | | /// |
366 | | /// # Panics |
367 | | /// |
368 | | /// If debug_assertions enabled and index is > self.max() |
369 | | #[inline] |
370 | 0 | pub(crate) fn push_unchecked(&mut self, value: u32) { |
371 | 0 | let (key, index) = util::split(value); |
372 | | |
373 | 0 | match self.containers.last_mut() { |
374 | 0 | Some(container) if container.key == key => container.push_unchecked(index), |
375 | 0 | Some(container) if cfg!(debug_assertions) && container.key > key => { |
376 | 0 | panic!("last container key > key of value") |
377 | | } |
378 | 0 | _otherwise => { |
379 | 0 | let mut container = Container::new(key); |
380 | 0 | container.push_unchecked(index); |
381 | 0 | self.containers.push(container); |
382 | 0 | } |
383 | | } |
384 | 0 | } |
385 | | |
386 | | /// Removes a value from the set. Returns `true` if the value was present in the set. |
387 | | /// |
388 | | /// # Examples |
389 | | /// |
390 | | /// ```rust |
391 | | /// use roaring::RoaringBitmap; |
392 | | /// |
393 | | /// let mut rb = RoaringBitmap::new(); |
394 | | /// rb.insert(3); |
395 | | /// assert_eq!(rb.remove(3), true); |
396 | | /// assert_eq!(rb.remove(3), false); |
397 | | /// assert_eq!(rb.contains(3), false); |
398 | | /// ``` |
399 | | #[inline] |
400 | 0 | pub fn remove(&mut self, value: u32) -> bool { |
401 | 0 | let (key, index) = util::split(value); |
402 | 0 | match self.containers.binary_search_by_key(&key, |c| c.key) { |
403 | 0 | Ok(loc) if self.containers[loc].remove(index) => { |
404 | 0 | if self.containers[loc].is_empty() { |
405 | 0 | self.containers.remove(loc); |
406 | 0 | } |
407 | 0 | true |
408 | | } |
409 | 0 | _ => false, |
410 | | } |
411 | 0 | } |
412 | | |
413 | | /// Removes a range of values. |
414 | | /// Returns the number of removed values. |
415 | | /// |
416 | | /// # Examples |
417 | | /// |
418 | | /// ```rust |
419 | | /// use roaring::RoaringBitmap; |
420 | | /// |
421 | | /// let mut rb = RoaringBitmap::new(); |
422 | | /// rb.insert(2); |
423 | | /// rb.insert(3); |
424 | | /// assert_eq!(rb.remove_range(2..4), 2); |
425 | | /// ``` |
426 | | #[inline] |
427 | 0 | pub fn remove_range<R>(&mut self, range: R) -> u64 |
428 | 0 | where |
429 | 0 | R: RangeBounds<u32>, |
430 | | { |
431 | 0 | let (start, end) = match util::convert_range_to_inclusive(range) { |
432 | 0 | Ok(range) => (*range.start(), *range.end()), |
433 | 0 | Err(_) => return 0, |
434 | | }; |
435 | | |
436 | 0 | let (start_container_key, start_index) = util::split(start); |
437 | 0 | let (end_container_key, end_index) = util::split(end); |
438 | | |
439 | 0 | let mut index = 0; |
440 | 0 | let mut removed = 0; |
441 | 0 | while index < self.containers.len() { |
442 | 0 | let key = self.containers[index].key; |
443 | 0 | if key >= start_container_key && key <= end_container_key { |
444 | 0 | let a = if key == start_container_key { start_index } else { 0 }; |
445 | 0 | let b = if key == end_container_key { end_index } else { u16::MAX }; |
446 | 0 | removed += self.containers[index].remove_range(a..=b); |
447 | 0 | if self.containers[index].is_empty() { |
448 | 0 | self.containers.remove(index); |
449 | 0 | continue; |
450 | 0 | } |
451 | 0 | } |
452 | 0 | index += 1; |
453 | | } |
454 | 0 | removed |
455 | 0 | } |
456 | | |
457 | | /// Returns `true` if this set contains the specified integer. |
458 | | /// |
459 | | /// # Examples |
460 | | /// |
461 | | /// ```rust |
462 | | /// use roaring::RoaringBitmap; |
463 | | /// |
464 | | /// let mut rb = RoaringBitmap::new(); |
465 | | /// rb.insert(1); |
466 | | /// assert_eq!(rb.contains(0), false); |
467 | | /// assert_eq!(rb.contains(1), true); |
468 | | /// assert_eq!(rb.contains(100), false); |
469 | | /// ``` |
470 | | #[inline] |
471 | 0 | pub fn contains(&self, value: u32) -> bool { |
472 | 0 | let (key, index) = util::split(value); |
473 | 0 | match self.containers.binary_search_by_key(&key, |c| c.key) { |
474 | 0 | Ok(loc) => self.containers[loc].contains(index), |
475 | 0 | Err(_) => false, |
476 | | } |
477 | 0 | } |
478 | | |
479 | | /// Returns `true` if all values in the range are present in this set. |
480 | | /// |
481 | | /// # Examples |
482 | | /// |
483 | | /// ``` |
484 | | /// use roaring::RoaringBitmap; |
485 | | /// |
486 | | /// let mut rb = RoaringBitmap::new(); |
487 | | /// // An empty range is always contained |
488 | | /// assert!(rb.contains_range(7..7)); |
489 | | /// |
490 | | /// rb.insert_range(1..0xFFF); |
491 | | /// assert!(rb.contains_range(1..0xFFF)); |
492 | | /// assert!(rb.contains_range(2..0xFFF)); |
493 | | /// // 0 is not contained |
494 | | /// assert!(!rb.contains_range(0..2)); |
495 | | /// // 0xFFF is not contained |
496 | | /// assert!(!rb.contains_range(1..=0xFFF)); |
497 | | /// ``` |
498 | | #[inline] |
499 | 0 | pub fn contains_range<R>(&self, range: R) -> bool |
500 | 0 | where |
501 | 0 | R: RangeBounds<u32>, |
502 | | { |
503 | 0 | let (start, end) = match util::convert_range_to_inclusive(range) { |
504 | 0 | Ok(range) => (*range.start(), *range.end()), |
505 | | // Empty/Invalid ranges are always contained |
506 | 0 | Err(_) => return true, |
507 | | }; |
508 | 0 | let (start_high, start_low) = util::split(start); |
509 | 0 | let (end_high, end_low) = util::split(end); |
510 | 0 | debug_assert!(start_high <= end_high); |
511 | | |
512 | 0 | let containers = |
513 | 0 | match self.containers.binary_search_by_key(&start_high, |container| container.key) { |
514 | 0 | Ok(i) => &self.containers[i..], |
515 | 0 | Err(_) => return false, |
516 | | }; |
517 | | |
518 | 0 | if start_high == end_high { |
519 | 0 | return containers[0].contains_range(start_low..=end_low); |
520 | 0 | } |
521 | | |
522 | 0 | let high_span = usize::from(end_high - start_high); |
523 | | // If this contains everything in the range, there should be a container for every item in the span |
524 | | // and the container that many items away should be the high key |
525 | 0 | let containers = match containers.get(high_span) { |
526 | 0 | Some(c) if c.key == end_high => &containers[..=high_span], |
527 | 0 | _ => return false, |
528 | | }; |
529 | | |
530 | 0 | match containers { |
531 | 0 | [first, rest @ .., last] => { |
532 | 0 | first.contains_range(start_low..=u16::MAX) |
533 | 0 | && rest.iter().all(|container| container.is_full()) |
534 | 0 | && last.contains_range(0..=end_low) |
535 | | } |
536 | 0 | _ => unreachable!("already validated containers has at least 2 items"), |
537 | | } |
538 | 0 | } |
539 | | |
540 | | /// Returns the number of elements in this set which are in the passed range. |
541 | | /// |
542 | | /// # Examples |
543 | | /// |
544 | | /// ``` |
545 | | /// use roaring::RoaringBitmap; |
546 | | /// |
547 | | /// let mut rb = RoaringBitmap::new(); |
548 | | /// rb.insert_range(0x10000..0x40000); |
549 | | /// rb.insert(0x50001); |
550 | | /// rb.insert(0x50005); |
551 | | /// rb.insert(u32::MAX); |
552 | | /// |
553 | | /// assert_eq!(rb.range_cardinality(0..0x10000), 0); |
554 | | /// assert_eq!(rb.range_cardinality(0x10000..0x40000), 0x30000); |
555 | | /// assert_eq!(rb.range_cardinality(0x50000..0x60000), 2); |
556 | | /// assert_eq!(rb.range_cardinality(0x10000..0x10000), 0); |
557 | | /// assert_eq!(rb.range_cardinality(0x50000..=u32::MAX), 3); |
558 | | /// ``` |
559 | | #[inline] |
560 | 0 | pub fn range_cardinality<R>(&self, range: R) -> u64 |
561 | 0 | where |
562 | 0 | R: RangeBounds<u32>, |
563 | | { |
564 | 0 | let (start, end) = match util::convert_range_to_inclusive(range) { |
565 | 0 | Ok(range) => (*range.start(), *range.end()), |
566 | | // Empty/invalid ranges have 0 bits set in them |
567 | 0 | Err(_) => return 0, |
568 | | }; |
569 | | |
570 | 0 | let (start_key, start_low) = util::split(start); |
571 | 0 | let (end_key, end_low) = util::split(end); |
572 | | |
573 | 0 | let mut cardinality = 0; |
574 | | |
575 | 0 | let i = match self.containers.binary_search_by_key(&start_key, |c| c.key) { |
576 | 0 | Ok(i) => { |
577 | 0 | let container = &self.containers[i]; |
578 | 0 | if start_key == end_key { |
579 | 0 | cardinality += container.rank(end_low) |
580 | 0 | } else { |
581 | 0 | cardinality += container.len(); |
582 | 0 | } |
583 | 0 | if start_low != 0 { |
584 | 0 | cardinality -= container.rank(start_low - 1); |
585 | 0 | } |
586 | 0 | i + 1 |
587 | | } |
588 | 0 | Err(i) => i, |
589 | | }; |
590 | 0 | for container in &self.containers[i..] { |
591 | 0 | match container.key.cmp(&end_key) { |
592 | 0 | Ordering::Less => cardinality += container.len(), |
593 | | Ordering::Equal => { |
594 | 0 | cardinality += container.rank(end_low); |
595 | 0 | break; |
596 | | } |
597 | | Ordering::Greater => { |
598 | 0 | break; |
599 | | } |
600 | | } |
601 | | } |
602 | | |
603 | 0 | cardinality |
604 | 0 | } |
605 | | |
606 | | /// Clears all integers in this set. |
607 | | /// |
608 | | /// # Examples |
609 | | /// |
610 | | /// ```rust |
611 | | /// use roaring::RoaringBitmap; |
612 | | /// |
613 | | /// let mut rb = RoaringBitmap::new(); |
614 | | /// rb.insert(1); |
615 | | /// assert_eq!(rb.contains(1), true); |
616 | | /// rb.clear(); |
617 | | /// assert_eq!(rb.contains(1), false); |
618 | | /// ``` |
619 | | #[inline] |
620 | 0 | pub fn clear(&mut self) { |
621 | 0 | self.containers.clear(); |
622 | 0 | } |
623 | | |
624 | | /// Returns `true` if there are no integers in this set. |
625 | | /// |
626 | | /// # Examples |
627 | | /// |
628 | | /// ```rust |
629 | | /// use roaring::RoaringBitmap; |
630 | | /// |
631 | | /// let mut rb = RoaringBitmap::new(); |
632 | | /// assert_eq!(rb.is_empty(), true); |
633 | | /// |
634 | | /// rb.insert(3); |
635 | | /// assert_eq!(rb.is_empty(), false); |
636 | | /// ``` |
637 | | #[inline] |
638 | 0 | pub fn is_empty(&self) -> bool { |
639 | 0 | self.containers.is_empty() |
640 | 0 | } |
641 | | |
642 | | /// Returns `true` if there are every possible integers in this set. |
643 | | /// |
644 | | /// # Examples |
645 | | /// |
646 | | /// ```rust |
647 | | /// use roaring::RoaringBitmap; |
648 | | /// |
649 | | /// let mut rb = RoaringBitmap::full(); |
650 | | /// assert!(!rb.is_empty()); |
651 | | /// assert!(rb.is_full()); |
652 | | /// ``` |
653 | | #[inline] |
654 | 0 | pub fn is_full(&self) -> bool { |
655 | 0 | self.containers.len() == (u16::MAX as usize + 1) |
656 | 0 | && self.containers.iter().all(Container::is_full) |
657 | 0 | } |
658 | | |
659 | | /// Returns the number of distinct integers added to the set. |
660 | | /// |
661 | | /// # Examples |
662 | | /// |
663 | | /// ```rust |
664 | | /// use roaring::RoaringBitmap; |
665 | | /// |
666 | | /// let mut rb = RoaringBitmap::new(); |
667 | | /// assert_eq!(rb.len(), 0); |
668 | | /// |
669 | | /// rb.insert(3); |
670 | | /// assert_eq!(rb.len(), 1); |
671 | | /// |
672 | | /// rb.insert(3); |
673 | | /// rb.insert(4); |
674 | | /// assert_eq!(rb.len(), 2); |
675 | | /// ``` |
676 | | #[inline] |
677 | 0 | pub fn len(&self) -> u64 { |
678 | 0 | self.containers.iter().map(|container| container.len()).sum() |
679 | 0 | } |
680 | | |
681 | | /// Returns the minimum value in the set (if the set is non-empty). |
682 | | /// |
683 | | /// # Examples |
684 | | /// |
685 | | /// ```rust |
686 | | /// use roaring::RoaringBitmap; |
687 | | /// |
688 | | /// let mut rb = RoaringBitmap::new(); |
689 | | /// assert_eq!(rb.min(), None); |
690 | | /// |
691 | | /// rb.insert(3); |
692 | | /// rb.insert(4); |
693 | | /// assert_eq!(rb.min(), Some(3)); |
694 | | /// ``` |
695 | | #[inline] |
696 | 0 | pub fn min(&self) -> Option<u32> { |
697 | 0 | self.containers.first().and_then(|tail| tail.min().map(|min| util::join(tail.key, min))) |
698 | 0 | } |
699 | | |
700 | | /// Returns the maximum value in the set (if the set is non-empty). |
701 | | /// |
702 | | /// # Examples |
703 | | /// |
704 | | /// ```rust |
705 | | /// use roaring::RoaringBitmap; |
706 | | /// |
707 | | /// let mut rb = RoaringBitmap::new(); |
708 | | /// assert_eq!(rb.max(), None); |
709 | | /// |
710 | | /// rb.insert(3); |
711 | | /// rb.insert(4); |
712 | | /// assert_eq!(rb.max(), Some(4)); |
713 | | /// ``` |
714 | | #[inline] |
715 | 0 | pub fn max(&self) -> Option<u32> { |
716 | 0 | self.containers.last().and_then(|tail| tail.max().map(|max| util::join(tail.key, max))) |
717 | 0 | } |
718 | | |
719 | | /// Returns the number of integers that are <= value. rank(u32::MAX) == len() |
720 | | /// |
721 | | /// # Examples |
722 | | /// |
723 | | /// ```rust |
724 | | /// use roaring::RoaringBitmap; |
725 | | /// |
726 | | /// let mut rb = RoaringBitmap::new(); |
727 | | /// assert_eq!(rb.rank(0), 0); |
728 | | /// |
729 | | /// rb.insert(3); |
730 | | /// rb.insert(4); |
731 | | /// assert_eq!(rb.rank(3), 1); |
732 | | /// assert_eq!(rb.rank(10), 2) |
733 | | /// ``` |
734 | | #[inline] |
735 | 0 | pub fn rank(&self, value: u32) -> u64 { |
736 | | // if len becomes cached for RoaringBitmap: return len if len > value |
737 | | |
738 | 0 | let (key, index) = util::split(value); |
739 | | |
740 | 0 | match self.containers.binary_search_by_key(&key, |c| c.key) { |
741 | 0 | Ok(i) => { |
742 | | // For optimal locality of reference: |
743 | | // * container[i] should be a cache hit after binary search, rank it first |
744 | | // * sum in reverse to avoid cache misses near i |
745 | 0 | unsafe { self.containers.get_unchecked(i) }.rank(index) |
746 | 0 | + self.containers[..i].iter().rev().map(|c| c.len()).sum::<u64>() |
747 | | } |
748 | 0 | Err(i) => self.containers[..i].iter().map(|c| c.len()).sum(), |
749 | | } |
750 | 0 | } |
751 | | |
752 | | /// Returns the `n`th integer in the set or `None` if `n >= len()` |
753 | | /// |
754 | | /// # Examples |
755 | | /// |
756 | | /// ```rust |
757 | | /// use roaring::RoaringBitmap; |
758 | | /// |
759 | | /// let mut rb = RoaringBitmap::new(); |
760 | | /// assert_eq!(rb.select(0), None); |
761 | | /// |
762 | | /// rb.append(vec![0, 10, 100]); |
763 | | /// |
764 | | /// assert_eq!(rb.select(0), Some(0)); |
765 | | /// assert_eq!(rb.select(1), Some(10)); |
766 | | /// assert_eq!(rb.select(2), Some(100)); |
767 | | /// assert_eq!(rb.select(3), None); |
768 | | /// ``` |
769 | | #[inline] |
770 | 0 | pub fn select(&self, n: u32) -> Option<u32> { |
771 | 0 | let mut n = n as u64; |
772 | | |
773 | 0 | for container in &self.containers { |
774 | 0 | let len = container.len(); |
775 | 0 | if len > n { |
776 | 0 | return container |
777 | 0 | .store |
778 | 0 | .select(n as u16) |
779 | 0 | .map(|index| util::join(container.key, index)); |
780 | 0 | } |
781 | 0 | n -= len; |
782 | | } |
783 | | |
784 | 0 | None |
785 | 0 | } |
786 | | |
787 | | /// Removes the `n` smallests values from this bitmap. |
788 | | /// |
789 | | /// # Examples |
790 | | /// |
791 | | /// ```rust |
792 | | /// use roaring::RoaringBitmap; |
793 | | /// |
794 | | /// let mut rb = RoaringBitmap::from_iter([1, 5, 7, 9]); |
795 | | /// rb.remove_smallest(2); |
796 | | /// assert_eq!(rb, RoaringBitmap::from_iter([7, 9])); |
797 | | /// |
798 | | /// let mut rb = RoaringBitmap::from_iter([1, 3, 7, 9]); |
799 | | /// rb.remove_smallest(2); |
800 | | /// assert_eq!(rb, RoaringBitmap::from_iter([7, 9])); |
801 | | #[inline] |
802 | 0 | pub fn remove_smallest(&mut self, mut n: u64) { |
803 | | // remove containers up to the front of the target |
804 | 0 | let position = self.containers.iter().position(|container| { |
805 | 0 | let container_len = container.len(); |
806 | 0 | if container_len <= n { |
807 | 0 | n -= container_len; |
808 | 0 | false |
809 | | } else { |
810 | 0 | true |
811 | | } |
812 | 0 | }); |
813 | 0 | let position = position.unwrap_or(self.containers.len()); |
814 | 0 | if position > 0 { |
815 | 0 | self.containers.drain(..position); |
816 | 0 | } |
817 | | // remove data in containers if there are still targets for deletion |
818 | 0 | if n > 0 && !self.containers.is_empty() { |
819 | 0 | // container immediately before should have been deleted, so the target is 0 index |
820 | 0 | self.containers[0].remove_smallest(n); |
821 | 0 | } |
822 | 0 | } |
823 | | |
824 | | /// Removes the `n` biggests values from this bitmap. |
825 | | /// |
826 | | /// # Examples |
827 | | /// |
828 | | /// ```rust |
829 | | /// use roaring::RoaringBitmap; |
830 | | /// |
831 | | /// let mut rb = RoaringBitmap::from_iter([1, 5, 7, 9]); |
832 | | /// rb.remove_biggest(2); |
833 | | /// assert_eq!(rb, RoaringBitmap::from_iter([1, 5])); |
834 | | /// rb.remove_biggest(1); |
835 | | /// assert_eq!(rb, RoaringBitmap::from_iter([1])); |
836 | | #[inline] |
837 | 0 | pub fn remove_biggest(&mut self, mut n: u64) { |
838 | | // remove containers up to the back of the target |
839 | 0 | let position = self.containers.iter().rposition(|container| { |
840 | 0 | let container_len = container.len(); |
841 | 0 | if container_len <= n { |
842 | 0 | n -= container_len; |
843 | 0 | false |
844 | | } else { |
845 | 0 | true |
846 | | } |
847 | 0 | }); |
848 | | // It is checked at the beginning of the function, so it is usually never an Err |
849 | 0 | if let Some(position) = position { |
850 | 0 | self.containers.drain(position + 1..); |
851 | 0 | if n > 0 && !self.containers.is_empty() { |
852 | 0 | self.containers[position].remove_biggest(n); |
853 | 0 | } |
854 | 0 | } else { |
855 | 0 | self.containers.clear(); |
856 | 0 | } |
857 | 0 | } |
858 | | |
859 | | /// Optimizes the container storage for this bitmap. |
860 | | /// Returns true if the container storage was modified, false if not. |
861 | | /// |
862 | | /// # Examples |
863 | | /// |
864 | | /// ``` |
865 | | /// use roaring::RoaringBitmap; |
866 | | /// |
867 | | /// let mut rb = RoaringBitmap::from_iter(1000..100000); |
868 | | /// rb.optimize(); |
869 | | /// ``` |
870 | 0 | pub fn optimize(&mut self) -> bool { |
871 | 0 | let mut changed = false; |
872 | 0 | for container in &mut self.containers { |
873 | 0 | changed |= container.optimize() |
874 | | } |
875 | 0 | changed |
876 | 0 | } |
877 | | |
878 | | /// Removes run-length encoding even when it is more space efficient. |
879 | | /// |
880 | | /// Returns true if the container storage was modified, false if not. |
881 | | /// |
882 | | /// # Examples |
883 | | /// |
884 | | /// ``` |
885 | | /// use roaring::RoaringBitmap; |
886 | | /// |
887 | | /// let mut rb = RoaringBitmap::from_iter(0..=10000); |
888 | | /// rb.optimize(); |
889 | | /// assert!(rb.remove_run_compression()); |
890 | | /// ``` |
891 | 0 | pub fn remove_run_compression(&mut self) -> bool { |
892 | 0 | let mut changed = false; |
893 | 0 | for container in &mut self.containers { |
894 | 0 | changed |= container.remove_run_compression() |
895 | | } |
896 | 0 | changed |
897 | 0 | } |
898 | | |
899 | | /// Ensure the bitmap is internally valid |
900 | | /// |
901 | | /// This is useful for development, but is not needed for normal use: |
902 | | /// bitmaps should _always_ be internally valid. |
903 | | /// |
904 | | /// # Errors |
905 | | /// |
906 | | /// Returns an error if the bitmap is not valid, with a description of the problem. |
907 | | #[doc(hidden)] |
908 | 0 | pub fn internal_validate(&self) -> Result<(), &'static str> { |
909 | 0 | for window in self.containers.windows(2) { |
910 | 0 | let [first, second] = window else { unreachable!() }; |
911 | 0 | if second.key <= first.key { |
912 | 0 | return Err("keys are not strictly increasing"); |
913 | 0 | } |
914 | | } |
915 | 0 | for container in &self.containers { |
916 | 0 | container.store.internal_validate()?; |
917 | | } |
918 | 0 | Ok(()) |
919 | 0 | } |
920 | | } |
921 | | |
922 | | impl Default for RoaringBitmap { |
923 | 0 | fn default() -> RoaringBitmap { |
924 | 0 | RoaringBitmap::new() |
925 | 0 | } |
926 | | } |
927 | | |
928 | | impl Clone for RoaringBitmap { |
929 | 0 | fn clone(&self) -> Self { |
930 | 0 | RoaringBitmap { containers: self.containers.clone() } |
931 | 0 | } |
932 | | |
933 | 0 | fn clone_from(&mut self, other: &Self) { |
934 | 0 | self.containers.clone_from(&other.containers); |
935 | 0 | } |
936 | | } |
937 | | |
938 | | #[cfg(test)] |
939 | | mod tests { |
940 | | use proptest::collection::vec; |
941 | | use proptest::prelude::*; |
942 | | |
943 | | use super::*; |
944 | | |
945 | | proptest! { |
946 | | #[test] |
947 | | fn insert_range( |
948 | | lo in 0u32..=65535, hi in 65536u32..=131071, |
949 | | checks in vec(0u32..=262143, 1000) |
950 | | ){ |
951 | | let r = lo..hi; |
952 | | let mut b = RoaringBitmap::new(); |
953 | | let inserted = b.insert_range(r.clone()); |
954 | | if r.end > r.start { |
955 | | assert_eq!(inserted, r.end as u64 - r.start as u64); |
956 | | } else { |
957 | | assert_eq!(inserted, 0); |
958 | | } |
959 | | |
960 | | // Assert all values in the range are present |
961 | | for i in r.clone() { |
962 | | assert!(b.contains(i), "does not contain {i}"); |
963 | | } |
964 | | |
965 | | // Run the check values looking for any false positives |
966 | | for i in checks { |
967 | | let bitmap_has = b.contains(i); |
968 | | let range_has = r.contains(&i); |
969 | | assert_eq!( |
970 | | bitmap_has, range_has, |
971 | | "value {i} in bitmap={bitmap_has} and range={range_has}" |
972 | | ); |
973 | | } |
974 | | } |
975 | | } |
976 | | |
977 | | #[test] |
978 | | fn test_insert_remove_range_same_container() { |
979 | | let mut b = RoaringBitmap::new(); |
980 | | let inserted = b.insert_range(1..5); |
981 | | assert_eq!(inserted, 4); |
982 | | |
983 | | for i in 1..5 { |
984 | | assert!(b.contains(i)); |
985 | | } |
986 | | |
987 | | let removed = b.remove_range(2..10); |
988 | | assert_eq!(removed, 3); |
989 | | assert!(b.contains(1)); |
990 | | for i in 2..5 { |
991 | | assert!(!b.contains(i)); |
992 | | } |
993 | | } |
994 | | |
995 | | #[test] |
996 | | fn test_insert_remove_range_pre_populated() { |
997 | | let mut b = RoaringBitmap::new(); |
998 | | let inserted = b.insert_range(1..20_000); |
999 | | assert_eq!(inserted, 19_999); |
1000 | | |
1001 | | let removed = b.remove_range(10_000..21_000); |
1002 | | assert_eq!(removed, 10_000); |
1003 | | |
1004 | | let inserted = b.insert_range(1..20_000); |
1005 | | assert_eq!(inserted, 10_000); |
1006 | | } |
1007 | | |
1008 | | #[test] |
1009 | | fn test_insert_max_u32() { |
1010 | | let mut b = RoaringBitmap::new(); |
1011 | | let inserted = b.insert(u32::MAX); |
1012 | | // We are allowed to add u32::MAX |
1013 | | assert!(inserted); |
1014 | | } |
1015 | | |
1016 | | #[test] |
1017 | | fn test_insert_remove_across_container() { |
1018 | | let mut b = RoaringBitmap::new(); |
1019 | | let inserted = b.insert_range(u16::MAX as u32..=u16::MAX as u32 + 1); |
1020 | | assert_eq!(inserted, 2); |
1021 | | |
1022 | | assert_eq!(b.containers.len(), 2); |
1023 | | |
1024 | | let removed = b.remove_range(u16::MAX as u32 + 1..=u16::MAX as u32 + 1); |
1025 | | assert_eq!(removed, 1); |
1026 | | |
1027 | | assert_eq!(b.containers.len(), 1); |
1028 | | } |
1029 | | |
1030 | | #[test] |
1031 | | fn test_insert_remove_single_element() { |
1032 | | let mut b = RoaringBitmap::new(); |
1033 | | let inserted = b.insert_range(u16::MAX as u32 + 1..=u16::MAX as u32 + 1); |
1034 | | assert_eq!(inserted, 1); |
1035 | | |
1036 | | assert_eq!(b.containers[0].len(), 1); |
1037 | | assert_eq!(b.containers.len(), 1); |
1038 | | |
1039 | | let removed = b.remove_range(u16::MAX as u32 + 1..=u16::MAX as u32 + 1); |
1040 | | assert_eq!(removed, 1); |
1041 | | |
1042 | | assert_eq!(b.containers.len(), 0); |
1043 | | } |
1044 | | |
1045 | | #[test] |
1046 | | fn test_insert_remove_range_multi_container() { |
1047 | | let mut bitmap = RoaringBitmap::new(); |
1048 | | assert_eq!(bitmap.insert_range(0..((1_u32 << 16) + 1)), (1_u64 << 16) + 1); |
1049 | | assert_eq!(bitmap.containers.len(), 2); |
1050 | | assert_eq!(bitmap.containers[0].key, 0); |
1051 | | assert_eq!(bitmap.containers[1].key, 1); |
1052 | | assert_eq!(bitmap.insert_range(0..((1_u32 << 16) + 1)), 0); |
1053 | | |
1054 | | assert!(bitmap.insert((1_u32 << 16) * 4)); |
1055 | | assert_eq!(bitmap.containers.len(), 3); |
1056 | | assert_eq!(bitmap.containers[2].key, 4); |
1057 | | |
1058 | | assert_eq!(bitmap.remove_range(((1_u32 << 16) * 3)..=((1_u32 << 16) * 4)), 1); |
1059 | | assert_eq!(bitmap.containers.len(), 2); |
1060 | | } |
1061 | | |
1062 | | #[test] |
1063 | | fn insert_range_single() { |
1064 | | let mut bitmap = RoaringBitmap::new(); |
1065 | | assert_eq!(bitmap.insert_range((1_u32 << 16)..(2_u32 << 16)), 1_u64 << 16); |
1066 | | assert_eq!(bitmap.containers.len(), 1); |
1067 | | assert_eq!(bitmap.containers[0].key, 1); |
1068 | | } |
1069 | | |
1070 | | #[test] |
1071 | | fn remove_smallest_for_vec() { |
1072 | | let mut bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]); |
1073 | | bitmap.remove_smallest(3); |
1074 | | assert_eq!(bitmap.len(), 3); |
1075 | | assert_eq!(bitmap, RoaringBitmap::from_iter([7, 9, 11])); |
1076 | | |
1077 | | bitmap = RoaringBitmap::from_iter([1, 2, 5, 7, 9, 11]); |
1078 | | bitmap.remove_smallest(3); |
1079 | | assert_eq!(bitmap.len(), 3); |
1080 | | assert_eq!(bitmap, RoaringBitmap::from_iter([7, 9, 11])); |
1081 | | |
1082 | | bitmap = RoaringBitmap::from_iter([1, 3]); |
1083 | | bitmap.remove_smallest(2); |
1084 | | assert_eq!(bitmap.len(), 0); |
1085 | | |
1086 | | bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]); |
1087 | | bitmap.remove_smallest(0); |
1088 | | assert_eq!(bitmap.len(), 6); |
1089 | | assert_eq!(bitmap, RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11])); |
1090 | | |
1091 | | bitmap = RoaringBitmap::new(); |
1092 | | bitmap.insert_range(0..(1_u32 << 16) + 5); |
1093 | | bitmap.remove_smallest(65537); |
1094 | | assert_eq!(bitmap.len(), 4); |
1095 | | assert_eq!(bitmap, RoaringBitmap::from_iter([65537, 65538, 65539, 65540])); |
1096 | | |
1097 | | bitmap = RoaringBitmap::from_iter([1, 2, 5, 7, 9, 11]); |
1098 | | bitmap.remove_smallest(7); |
1099 | | assert_eq!(bitmap, RoaringBitmap::default()); |
1100 | | } |
1101 | | |
1102 | | #[test] |
1103 | | fn remove_smallest_for_bit() { |
1104 | | let mut bitmap = RoaringBitmap::new(); |
1105 | | bitmap.insert_range(0..4098); |
1106 | | bitmap.remove_smallest(4095); |
1107 | | assert_eq!(bitmap.len(), 3); |
1108 | | // removed bit to vec |
1109 | | assert_eq!(bitmap, RoaringBitmap::from_iter([4095, 4096, 4097])); |
1110 | | |
1111 | | bitmap = RoaringBitmap::new(); |
1112 | | bitmap.insert_range(0..6000); |
1113 | | bitmap.remove_smallest(999); |
1114 | | assert_eq!(bitmap.len(), 5001); |
1115 | | |
1116 | | bitmap = RoaringBitmap::new(); |
1117 | | bitmap.insert_range(0..8000); |
1118 | | bitmap.remove_smallest(10); |
1119 | | assert_eq!(bitmap.len(), 7990); |
1120 | | |
1121 | | bitmap = RoaringBitmap::new(); |
1122 | | bitmap.insert_range(0..200000); |
1123 | | bitmap.remove_smallest(2000); |
1124 | | assert_eq!(bitmap.len(), 198000); |
1125 | | assert_eq!(bitmap, RoaringBitmap::from_iter(2000..200000)); |
1126 | | |
1127 | | bitmap = RoaringBitmap::new(); |
1128 | | bitmap.insert_range(0..2); |
1129 | | bitmap.insert_range(4..7); |
1130 | | bitmap.insert_range(1000..6000); |
1131 | | bitmap.remove_smallest(30); |
1132 | | assert_eq!(bitmap.len(), 4975); |
1133 | | |
1134 | | bitmap = RoaringBitmap::new(); |
1135 | | bitmap.insert_range(0..65535); |
1136 | | bitmap.remove_smallest(0); |
1137 | | assert_eq!(bitmap.len(), 65535); |
1138 | | } |
1139 | | |
1140 | | #[test] |
1141 | | fn remove_biggest_for_bit() { |
1142 | | let mut bitmap = RoaringBitmap::new(); |
1143 | | bitmap.insert_range(0..5000); |
1144 | | bitmap.remove_biggest(1000); |
1145 | | assert_eq!(bitmap.len(), 4000); |
1146 | | |
1147 | | bitmap = RoaringBitmap::new(); |
1148 | | bitmap.insert_range(0..6000); |
1149 | | bitmap.remove_biggest(1000); |
1150 | | assert_eq!(bitmap.len(), 5000); |
1151 | | |
1152 | | bitmap = RoaringBitmap::new(); |
1153 | | bitmap.insert_range(0..200000); |
1154 | | bitmap.remove_biggest(196000); |
1155 | | assert_eq!(bitmap.len(), 4000); |
1156 | | |
1157 | | bitmap = RoaringBitmap::new(); |
1158 | | bitmap.insert_range(0..200000); |
1159 | | bitmap.remove_biggest(2000); |
1160 | | assert_eq!(bitmap.len(), 198000); |
1161 | | assert_eq!(bitmap, RoaringBitmap::from_iter(0..198000)); |
1162 | | |
1163 | | bitmap = RoaringBitmap::new(); |
1164 | | bitmap.insert_range(0..65535); |
1165 | | bitmap.remove_biggest(0); |
1166 | | assert_eq!(bitmap.len(), 65535); |
1167 | | } |
1168 | | |
1169 | | #[test] |
1170 | | fn remove_biggest_for_vec() { |
1171 | | let mut bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]); |
1172 | | bitmap.remove_biggest(2); |
1173 | | assert_eq!(bitmap, RoaringBitmap::from_iter([1, 2, 3, 7])); |
1174 | | |
1175 | | bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]); |
1176 | | bitmap.remove_biggest(6); |
1177 | | assert_eq!(bitmap.len(), 0); |
1178 | | |
1179 | | bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]); |
1180 | | bitmap.remove_biggest(0); |
1181 | | assert_eq!(bitmap.len(), 6); |
1182 | | assert_eq!(bitmap, RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11])); |
1183 | | |
1184 | | bitmap = RoaringBitmap::new(); |
1185 | | bitmap.insert_range(0..(1_u32 << 16) + 5); |
1186 | | bitmap.remove_biggest(65537); |
1187 | | assert_eq!(bitmap.len(), 4); |
1188 | | assert_eq!(bitmap, RoaringBitmap::from_iter([0, 1, 2, 3])); |
1189 | | |
1190 | | let mut bitmap = RoaringBitmap::from_iter([1, 2, 3]); |
1191 | | bitmap.remove_biggest(4); |
1192 | | assert_eq!(bitmap, RoaringBitmap::default()); |
1193 | | } |
1194 | | } |