1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};
use std::collections::btree_map::Entry;

use RoaringTreemap;

impl RoaringTreemap {
    /// Unions in-place with the specified other bitmap.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use roaring::RoaringTreemap;
    ///
    /// let mut rb1: RoaringTreemap = (1..4).collect();
    /// let rb2: RoaringTreemap = (3..5).collect();
    /// let rb3: RoaringTreemap = (1..5).collect();
    ///
    /// rb1.union_with(&rb2);
    ///
    /// assert_eq!(rb1, rb3);
    /// ```
    ///
    /// Can also be done via the `BitOr` operator.
    ///
    /// ```rust
    /// use roaring::RoaringTreemap;
    ///
    /// let mut rb1: RoaringTreemap = (1..4).collect();
    /// let rb2: RoaringTreemap = (3..5).collect();
    /// let rb3: RoaringTreemap = (1..5).collect();
    ///
    /// let rb1 = rb1 | rb2;
    ///
    /// assert_eq!(rb1, rb3);
    /// ```
    pub fn union_with(&mut self, other: &RoaringTreemap) {
        for (key, other_rb) in &other.map {
            match self.map.entry(*key) {
                Entry::Vacant(ent) => {
                    ent.insert(other_rb.clone());
                }
                Entry::Occupied(mut ent) => {
                    ent.get_mut().union_with(other_rb);
                }
            };
        }
    }

    /// Intersects in-place with the specified other bitmap.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use roaring::RoaringTreemap;
    ///
    /// let mut rb1: RoaringTreemap = (1..4).collect();
    /// let rb2: RoaringTreemap = (3..5).collect();
    /// let rb3: RoaringTreemap = (3..4).collect();
    ///
    /// rb1.intersect_with(&rb2);
    ///
    /// assert_eq!(rb1, rb3);
    /// ```
    ///
    /// Can also be done via the `BitAnd` operator.
    ///
    /// ```rust
    /// use roaring::RoaringTreemap;
    ///
    /// let mut rb1: RoaringTreemap = (1..4).collect();
    /// let rb2: RoaringTreemap = (3..5).collect();
    /// let rb3: RoaringTreemap = (3..4).collect();
    ///
    /// let rb1 = rb1 & rb2;
    ///
    /// assert_eq!(rb1, rb3);
    /// ```
    pub fn intersect_with(&mut self, other: &RoaringTreemap) {
        let mut keys_to_remove: Vec<u32> = Vec::new();
        for (key, self_rb) in &mut self.map {
            match other.map.get(key) {
                None => {
                    keys_to_remove.push(*key);
                }
                Some(other_rb) => {
                    self_rb.intersect_with(other_rb);
                    if self_rb.is_empty() {
                        keys_to_remove.push(*key);
                    }
                }
            }
        }

        for key in keys_to_remove {
            self.map.remove(&key);
        }
    }

    /// Removes all values in the specified other bitmap from self, in-place.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use roaring::RoaringTreemap;
    ///
    /// let mut rb1: RoaringTreemap = (1..4).collect();
    /// let rb2: RoaringTreemap = (3..5).collect();
    /// let rb3: RoaringTreemap = (1..3).collect();
    ///
    /// rb1.difference_with(&rb2);
    ///
    /// assert_eq!(rb1, rb3);
    /// ```
    ///
    /// Can also be done via the `Sub` operator.
    ///
    /// ```rust
    /// use roaring::RoaringTreemap;
    ///
    /// let mut rb1: RoaringTreemap = (1..4).collect();
    /// let rb2: RoaringTreemap = (3..5).collect();
    /// let rb3: RoaringTreemap = (1..3).collect();
    ///
    /// let rb1 = rb1 - rb2;
    ///
    /// assert_eq!(rb1, rb3);
    /// ```
    pub fn difference_with(&mut self, other: &RoaringTreemap) {
        let mut keys_to_remove: Vec<u32> = Vec::new();
        for (key, self_rb) in &mut self.map {
            if let Some(other_rb) = other.map.get(key) {
                self_rb.difference_with(other_rb);
                if self_rb.is_empty() {
                    keys_to_remove.push(*key);
                }
            }
        }

        for key in keys_to_remove {
            self.map.remove(&key);
        }
    }

    /// Replaces this bitmap with one that is equivalent to `self XOR other`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use roaring::RoaringTreemap;
    ///
    /// let mut rb1: RoaringTreemap = (1..4).collect();
    /// let rb2: RoaringTreemap = (3..6).collect();
    /// let rb3: RoaringTreemap = (1..3).chain(4..6).collect();
    ///
    /// rb1.symmetric_difference_with(&rb2);
    ///
    /// assert_eq!(rb1, rb3);
    /// ```
    ///
    /// Can also be done via the `BitXor` operator.
    ///
    /// ```rust
    /// use roaring::RoaringTreemap;
    ///
    /// let mut rb1: RoaringTreemap = (1..4).collect();
    /// let rb2: RoaringTreemap = (3..6).collect();
    /// let rb3: RoaringTreemap = (1..3).chain(4..6).collect();
    ///
    /// let rb1 = rb1 ^ rb2;
    ///
    /// assert_eq!(rb1, rb3);
    /// ```
    pub fn symmetric_difference_with(&mut self, other: &RoaringTreemap) {
        let mut keys_to_remove: Vec<u32> = Vec::new();
        for (key, other_rb) in &other.map {
            match self.map.entry(*key) {
                Entry::Vacant(ent) => {
                    ent.insert(other_rb.clone());
                }
                Entry::Occupied(mut ent) => {
                    ent.get_mut().symmetric_difference_with(other_rb);
                    if ent.get().is_empty() {
                        keys_to_remove.push(*key);
                    }
                }
            };
        }

        for key in keys_to_remove {
            self.map.remove(&key);
        }
    }
}

impl BitOr<RoaringTreemap> for RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitor(mut self, rhs: RoaringTreemap) -> RoaringTreemap {
        self.union_with(&rhs);
        self
    }
}

impl<'a> BitOr<&'a RoaringTreemap> for RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitor(mut self, rhs: &'a RoaringTreemap) -> RoaringTreemap {
        self.union_with(rhs);
        self
    }
}

impl<'a> BitOr<RoaringTreemap> for &'a RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitor(self, rhs: RoaringTreemap) -> RoaringTreemap {
        rhs | self
    }
}

impl<'a, 'b> BitOr<&'a RoaringTreemap> for &'b RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitor(self, rhs: &'a RoaringTreemap) -> RoaringTreemap {
        self.clone() | rhs
    }
}

impl BitOrAssign<RoaringTreemap> for RoaringTreemap {
    fn bitor_assign(&mut self, rhs: RoaringTreemap) {
        self.union_with(&rhs)
    }
}

impl<'a> BitOrAssign<&'a RoaringTreemap> for RoaringTreemap {
    fn bitor_assign(&mut self, rhs: &'a RoaringTreemap) {
        self.union_with(rhs)
    }
}

impl BitAnd<RoaringTreemap> for RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitand(mut self, rhs: RoaringTreemap) -> RoaringTreemap {
        self.intersect_with(&rhs);
        self
    }
}

impl<'a> BitAnd<&'a RoaringTreemap> for RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitand(mut self, rhs: &'a RoaringTreemap) -> RoaringTreemap {
        self.intersect_with(rhs);
        self
    }
}

impl<'a> BitAnd<RoaringTreemap> for &'a RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitand(self, rhs: RoaringTreemap) -> RoaringTreemap {
        rhs & self
    }
}

impl<'a, 'b> BitAnd<&'a RoaringTreemap> for &'b RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitand(self, rhs: &'a RoaringTreemap) -> RoaringTreemap {
        self.clone() & rhs
    }
}

impl BitAndAssign<RoaringTreemap> for RoaringTreemap {
    fn bitand_assign(&mut self, rhs: RoaringTreemap) {
        self.intersect_with(&rhs)
    }
}

impl<'a> BitAndAssign<&'a RoaringTreemap> for RoaringTreemap {
    fn bitand_assign(&mut self, rhs: &'a RoaringTreemap) {
        self.intersect_with(rhs)
    }
}

impl Sub<RoaringTreemap> for RoaringTreemap {
    type Output = RoaringTreemap;

    fn sub(mut self, rhs: RoaringTreemap) -> RoaringTreemap {
        self.difference_with(&rhs);
        self
    }
}

impl<'a> Sub<&'a RoaringTreemap> for RoaringTreemap {
    type Output = RoaringTreemap;

    fn sub(mut self, rhs: &'a RoaringTreemap) -> RoaringTreemap {
        self.difference_with(rhs);
        self
    }
}

impl<'a> Sub<RoaringTreemap> for &'a RoaringTreemap {
    type Output = RoaringTreemap;

    fn sub(self, rhs: RoaringTreemap) -> RoaringTreemap {
        self.clone() - rhs
    }
}

impl<'a, 'b> Sub<&'a RoaringTreemap> for &'b RoaringTreemap {
    type Output = RoaringTreemap;

    fn sub(self, rhs: &'a RoaringTreemap) -> RoaringTreemap {
        self.clone() - rhs
    }
}

impl SubAssign<RoaringTreemap> for RoaringTreemap {
    fn sub_assign(&mut self, rhs: RoaringTreemap) {
        self.difference_with(&rhs)
    }
}

impl<'a> SubAssign<&'a RoaringTreemap> for RoaringTreemap {
    fn sub_assign(&mut self, rhs: &'a RoaringTreemap) {
        self.difference_with(rhs)
    }
}

impl BitXor<RoaringTreemap> for RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitxor(mut self, rhs: RoaringTreemap) -> RoaringTreemap {
        self.symmetric_difference_with(&rhs);
        self
    }
}

impl<'a> BitXor<&'a RoaringTreemap> for RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitxor(mut self, rhs: &'a RoaringTreemap) -> RoaringTreemap {
        self.symmetric_difference_with(rhs);
        self
    }
}

impl<'a> BitXor<RoaringTreemap> for &'a RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitxor(self, rhs: RoaringTreemap) -> RoaringTreemap {
        rhs ^ self
    }
}

impl<'a, 'b> BitXor<&'a RoaringTreemap> for &'b RoaringTreemap {
    type Output = RoaringTreemap;

    fn bitxor(self, rhs: &'a RoaringTreemap) -> RoaringTreemap {
        self.clone() ^ rhs
    }
}

impl BitXorAssign<RoaringTreemap> for RoaringTreemap {
    fn bitxor_assign(&mut self, rhs: RoaringTreemap) {
        self.symmetric_difference_with(&rhs)
    }
}

impl<'a> BitXorAssign<&'a RoaringTreemap> for RoaringTreemap {
    fn bitxor_assign(&mut self, rhs: &'a RoaringTreemap) {
        self.symmetric_difference_with(rhs)
    }
}