|
1 | 1 | use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};
|
2 | 2 |
|
3 |
| -use crate::RoaringBitmap; |
| 3 | +use crate::{retain_mut, RoaringBitmap}; |
4 | 4 |
|
5 | 5 | impl RoaringBitmap {
|
6 | 6 | /// Unions in-place with the specified other bitmap.
|
@@ -72,23 +72,15 @@ impl RoaringBitmap {
|
72 | 72 | /// assert_eq!(rb1, rb3);
|
73 | 73 | /// ```
|
74 | 74 | pub fn intersect_with(&mut self, other: &RoaringBitmap) {
|
75 |
| - let mut index = 0; |
76 |
| - while index < self.containers.len() { |
77 |
| - let key = self.containers[index].key; |
78 |
| - match other.containers.binary_search_by_key(&key, |c| c.key) { |
79 |
| - Err(_) => { |
80 |
| - self.containers.remove(index); |
81 |
| - } |
| 75 | + retain_mut(&mut self.containers, |cont| { |
| 76 | + match other.containers.binary_search_by_key(&cont.key, |c| c.key) { |
82 | 77 | Ok(loc) => {
|
83 |
| - self.containers[index].intersect_with(&other.containers[loc]); |
84 |
| - if self.containers[index].len == 0 { |
85 |
| - self.containers.remove(index); |
86 |
| - } else { |
87 |
| - index += 1; |
88 |
| - } |
| 78 | + cont.intersect_with(&other.containers[loc]); |
| 79 | + cont.len != 0 |
89 | 80 | }
|
| 81 | + Err(_) => false, |
90 | 82 | }
|
91 |
| - } |
| 83 | + }) |
92 | 84 | }
|
93 | 85 |
|
94 | 86 | /// Removes all values in the specified other bitmap from self, in-place.
|
@@ -121,23 +113,15 @@ impl RoaringBitmap {
|
121 | 113 | /// assert_eq!(rb1, rb3);
|
122 | 114 | /// ```
|
123 | 115 | pub fn difference_with(&mut self, other: &RoaringBitmap) {
|
124 |
| - let mut index = 0; |
125 |
| - while index < self.containers.len() { |
126 |
| - let key = self.containers[index].key; |
127 |
| - match other.containers.binary_search_by_key(&key, |c| c.key) { |
| 116 | + retain_mut(&mut self.containers, |cont| { |
| 117 | + match other.containers.binary_search_by_key(&cont.key, |c| c.key) { |
128 | 118 | Ok(loc) => {
|
129 |
| - self.containers[index].difference_with(&other.containers[loc]); |
130 |
| - if self.containers[index].len == 0 { |
131 |
| - self.containers.remove(index); |
132 |
| - } else { |
133 |
| - index += 1; |
134 |
| - } |
135 |
| - } |
136 |
| - _ => { |
137 |
| - index += 1; |
| 119 | + cont.difference_with(&other.containers[loc]); |
| 120 | + cont.len != 0 |
138 | 121 | }
|
| 122 | + Err(_) => true, |
139 | 123 | }
|
140 |
| - } |
| 124 | + }) |
141 | 125 | }
|
142 | 126 |
|
143 | 127 | /// Replaces this bitmap with one that is equivalent to `self XOR other`.
|
|
0 commit comments