forked from RoaringBitmap/roaring-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.rs
332 lines (279 loc) · 8.9 KB
/
container.rs
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
use core::fmt;
use core::ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, RangeInclusive, Sub, SubAssign,
};
use super::store::{self, Store};
use super::util;
pub const ARRAY_LIMIT: u64 = 4096;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[derive(PartialEq, Clone)]
pub struct Container {
pub key: u16,
pub store: Store,
}
pub struct Iter<'a> {
pub key: u16,
inner: store::Iter<'a>,
}
impl Container {
pub fn new(key: u16) -> Container {
Container { key, store: Store::new() }
}
pub fn full(key: u16) -> Container {
Container { key, store: Store::full() }
}
}
impl Container {
pub fn len(&self) -> u64 {
self.store.len()
}
pub fn is_empty(&self) -> bool {
self.store.is_empty()
}
pub fn insert(&mut self, index: u16) -> bool {
if self.store.insert(index) {
self.ensure_correct_store();
true
} else {
false
}
}
pub fn insert_range(&mut self, range: RangeInclusive<u16>) -> u64 {
// If inserting the range will make this a bitmap by itself, do it now
if range.len() as u64 > ARRAY_LIMIT {
if let Store::Array(arr) = &self.store {
self.store = Store::Bitmap(arr.to_bitmap_store());
}
}
let inserted = self.store.insert_range(range);
self.ensure_correct_store();
inserted
}
/// Pushes `index` at the end of the container only if `index` is the new max.
///
/// Returns whether the `index` was effectively pushed.
pub fn push(&mut self, index: u16) -> bool {
if self.store.push(index) {
self.ensure_correct_store();
true
} else {
false
}
}
///
/// Pushes `index` at the end of the container.
/// It is up to the caller to have validated index > self.max()
///
/// # Panics
///
/// If debug_assertions enabled and index is > self.max()
pub(crate) fn push_unchecked(&mut self, index: u16) {
self.store.push_unchecked(index);
self.ensure_correct_store();
}
pub fn remove(&mut self, index: u16) -> bool {
if self.store.remove(index) {
self.ensure_correct_store();
true
} else {
false
}
}
pub fn remove_range(&mut self, range: RangeInclusive<u16>) -> u64 {
let result = self.store.remove_range(range);
self.ensure_correct_store();
result
}
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_smallest(n)
}
}
Store::Array(_) => self.store.remove_smallest(n),
};
}
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_biggest(n)
}
}
Store::Array(_) => self.store.remove_biggest(n),
};
}
pub fn contains(&self, index: u16) -> bool {
self.store.contains(index)
}
pub fn contains_range(&self, range: RangeInclusive<u16>) -> bool {
self.store.contains_range(range)
}
pub fn is_full(&self) -> bool {
self.store.is_full()
}
pub fn is_disjoint(&self, other: &Self) -> bool {
self.store.is_disjoint(&other.store)
}
pub fn is_subset(&self, other: &Self) -> bool {
self.len() <= other.len() && self.store.is_subset(&other.store)
}
pub fn intersection_len(&self, other: &Self) -> u64 {
self.store.intersection_len(&other.store)
}
pub fn min(&self) -> Option<u16> {
self.store.min()
}
pub fn max(&self) -> Option<u16> {
self.store.max()
}
pub fn rank(&self, index: u16) -> u64 {
self.store.rank(index)
}
pub(crate) fn ensure_correct_store(&mut self) {
match &self.store {
Store::Bitmap(ref bits) => {
if bits.len() <= ARRAY_LIMIT {
self.store = Store::Array(bits.to_array_store())
}
}
Store::Array(ref vec) => {
if vec.len() > ARRAY_LIMIT {
self.store = Store::Bitmap(vec.to_bitmap_store())
}
}
};
}
}
impl BitOr<&Container> for &Container {
type Output = Container;
fn bitor(self, rhs: &Container) -> Container {
let store = BitOr::bitor(&self.store, &rhs.store);
let mut container = Container { key: self.key, store };
container.ensure_correct_store();
container
}
}
impl BitOrAssign<Container> for Container {
fn bitor_assign(&mut self, rhs: Container) {
BitOrAssign::bitor_assign(&mut self.store, rhs.store);
self.ensure_correct_store();
}
}
impl BitOrAssign<&Container> for Container {
fn bitor_assign(&mut self, rhs: &Container) {
BitOrAssign::bitor_assign(&mut self.store, &rhs.store);
self.ensure_correct_store();
}
}
impl BitAnd<&Container> for &Container {
type Output = Container;
fn bitand(self, rhs: &Container) -> Container {
let store = BitAnd::bitand(&self.store, &rhs.store);
let mut container = Container { key: self.key, store };
container.ensure_correct_store();
container
}
}
impl BitAndAssign<Container> for Container {
fn bitand_assign(&mut self, rhs: Container) {
BitAndAssign::bitand_assign(&mut self.store, rhs.store);
self.ensure_correct_store();
}
}
impl BitAndAssign<&Container> for Container {
fn bitand_assign(&mut self, rhs: &Container) {
BitAndAssign::bitand_assign(&mut self.store, &rhs.store);
self.ensure_correct_store();
}
}
impl Sub<&Container> for &Container {
type Output = Container;
fn sub(self, rhs: &Container) -> Container {
let store = Sub::sub(&self.store, &rhs.store);
let mut container = Container { key: self.key, store };
container.ensure_correct_store();
container
}
}
impl SubAssign<&Container> for Container {
fn sub_assign(&mut self, rhs: &Container) {
SubAssign::sub_assign(&mut self.store, &rhs.store);
self.ensure_correct_store();
}
}
impl BitXor<&Container> for &Container {
type Output = Container;
fn bitxor(self, rhs: &Container) -> Container {
let store = BitXor::bitxor(&self.store, &rhs.store);
let mut container = Container { key: self.key, store };
container.ensure_correct_store();
container
}
}
impl BitXorAssign<Container> for Container {
fn bitxor_assign(&mut self, rhs: Container) {
BitXorAssign::bitxor_assign(&mut self.store, rhs.store);
self.ensure_correct_store();
}
}
impl BitXorAssign<&Container> for Container {
fn bitxor_assign(&mut self, rhs: &Container) {
BitXorAssign::bitxor_assign(&mut self.store, &rhs.store);
self.ensure_correct_store();
}
}
impl<'a> IntoIterator for &'a Container {
type Item = u32;
type IntoIter = Iter<'a>;
fn into_iter(self) -> Iter<'a> {
let store: &Store = &self.store;
Iter { key: self.key, inner: store.into_iter() }
}
}
impl IntoIterator for Container {
type Item = u32;
type IntoIter = Iter<'static>;
fn into_iter(self) -> Iter<'static> {
Iter { key: self.key, inner: self.store.into_iter() }
}
}
impl Iterator for Iter<'_> {
type Item = u32;
fn next(&mut self) -> Option<u32> {
self.inner.next().map(|i| util::join(self.key, i))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
fn count(self) -> usize
where
Self: Sized,
{
self.inner.count()
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.inner.nth(n).map(|i| util::join(self.key, i))
}
}
impl DoubleEndedIterator for Iter<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next_back().map(|i| util::join(self.key, i))
}
}
impl ExactSizeIterator for Iter<'_> {}
impl fmt::Debug for Container {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
format!("Container<{:?} @ {:?}>", self.len(), self.key).fmt(formatter)
}
}