open_vaf/data_structures/
bit_set.rs1use bitflags::_core::fmt::Binary;
15use bitflags::_core::iter::FromIterator;
16use bitflags::_core::marker::PhantomData;
17use bitflags::_core::ops::{BitAndAssign, BitOr, BitXor, BitXorAssign, Index};
18use fixedbitset::{FixedBitSet, IndexRange};
19use index_vec::Idx;
20use std::fmt;
21use std::fmt::{Display, Formatter};
22use std::ops::{BitAnd, BitOrAssign};
23
24type Block = u32;
27
28#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
29pub struct BitSet<I: Idx + From<usize>> {
30 internal: FixedBitSet,
31 tag: PhantomData<fn(&I)>,
32}
33
34impl<I: Idx + From<usize>> BitSet<I> {
35 pub fn new_empty(end_index: I) -> Self {
37 Self {
38 internal: FixedBitSet::with_capacity(end_index.index()),
39 tag: PhantomData,
40 }
41 }
42
43 pub fn new_filled(end_index: I) -> Self {
45 let mut res = Self {
46 internal: FixedBitSet::with_capacity(end_index.index()),
47 tag: PhantomData,
48 };
49 res.enable_all();
50 res
51 }
52
53 pub fn with_capacity_and_blocks<Iter: IntoIterator<Item = Block>>(
60 bits: I,
61 blocks: Iter,
62 ) -> Self {
63 Self {
64 internal: FixedBitSet::with_capacity_and_blocks(bits.index(), blocks),
65 tag: PhantomData,
66 }
67 }
68
69 pub fn grow(&mut self, len_idx: I) {
71 self.internal.grow(len_idx.index())
72 }
73
74 #[inline]
76 pub fn len(&self) -> usize {
77 self.internal.len()
78 }
79
80 #[inline]
82 pub fn len_idx(&self) -> I {
83 self.internal.len().into()
84 }
85 #[inline]
87 pub fn max(&self) -> I {
88 I::from_usize(self.internal.len())
89 }
90
91 #[inline]
98 pub fn contains(&self, bit: I) -> bool {
99 self.internal.contains(bit.index())
100 }
101
102 #[inline]
104 pub fn clear(&mut self) {
105 self.internal.clear()
106 }
107
108 #[inline]
112 pub fn insert(&mut self, bit: I) {
113 self.internal.insert(bit.index())
114 }
115
116 #[inline]
120 pub fn remove(&mut self, bit: I) -> bool {
121 let prev = self.internal[bit.index()];
122 self.internal.set(bit.index(), false);
123 prev
124 }
125
126 #[inline]
131 pub fn put(&mut self, bit: I) -> bool {
132 self.internal.put(bit.index())
133 }
134
135 #[inline]
139 pub fn toggle(&mut self, bit: I) {
140 self.internal.toggle(bit.index())
141 }
142
143 #[inline]
145 pub fn set(&mut self, bit: I, enabled: bool) {
146 self.internal.set(bit.index(), enabled)
147 }
148
149 #[inline]
153 pub fn copy_bit(&mut self, from: I, to: I) {
154 self.internal.copy_bit(from.index(), to.index())
155 }
156
157 #[inline]
163 pub fn count_ones<T: IndexRange<I>>(&self, range: T) -> usize {
164 let start = range.start().map_or(0, |idx| idx.index());
165 let end = range.end().map_or(self.internal.len(), |idx| idx.index());
166 self.internal.count_ones(start..end)
167 }
168
169 #[inline]
175 pub fn set_range<T: IndexRange<I>>(&mut self, range: T, enabled: bool) {
176 let start = range.start().map_or(0, |idx| idx.index());
177 let end = range.end().map_or(self.internal.len(), |idx| idx.index());
178 self.internal.set_range(start..end, enabled)
179 }
180
181 #[inline]
187 pub fn insert_range<T: IndexRange<I>>(&mut self, range: T) {
188 let start = range.start().map_or(0, |idx| idx.index());
189 let end = range.end().map_or(self.internal.len(), |idx| idx.index());
190 self.internal.insert_range(start..end)
191 }
192
193 #[inline]
195 pub fn enable_all(&mut self) {
196 for block in self.as_mut_slice().iter_mut() {
197 *block = Block::MAX
198 }
199 }
200
201 #[inline]
207 pub fn toggle_range<T: IndexRange<I>>(&mut self, range: T) {
208 let start = range.start().map_or(0, |idx| idx.index());
209 let end = range.end().map_or(self.internal.len(), |idx| idx.index());
210 self.internal.toggle_range(start..end)
211 }
212
213 #[inline]
215 pub fn as_slice(&self) -> &[u32] {
216 self.internal.as_slice()
217 }
218
219 #[inline]
222 pub fn as_mut_slice(&mut self) -> &mut [u32] {
223 self.internal.as_mut_slice()
224 }
225
226 #[inline]
227 pub fn is_empty(&self) -> bool {
228 self.as_slice().iter().all(|block| *block == 0)
229 }
230
231 #[inline]
235 pub fn ones(&self) -> Ones<'_, I> {
236 Ones {
237 iter: self.internal.ones(),
238 tag: PhantomData,
239 }
240 }
241
242 pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, I> {
244 Intersection {
245 iter: self.internal.intersection(&other.internal),
246 tag: PhantomData,
247 }
248 }
249
250 pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, I> {
252 Union {
253 iter: self.internal.union(&other.internal),
254 tag: PhantomData,
255 }
256 }
257
258 pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, I> {
261 Difference {
262 iter: self.internal.difference(&other.internal),
263 tag: PhantomData,
264 }
265 }
266
267 pub fn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, I> {
270 SymmetricDifference {
271 iter: self.internal.symmetric_difference(&other.internal),
272 tag: PhantomData,
273 }
274 }
275
276 pub fn union_with(&mut self, other: &Self) {
280 self.internal.union_with(&other.internal)
281 }
282
283 pub fn intersect_with(&mut self, other: &Self) {
287 self.internal.intersect_with(&other.internal)
288 }
289
290 pub fn difference_with(&mut self, other: &Self) {
294 self.internal.difference_with(&other.internal)
295 }
296
297 pub fn symmetric_difference_with(&mut self, other: &Self) {
301 self.internal.symmetric_difference_with(&other.internal)
302 }
303
304 pub fn is_disjoint(&self, other: &Self) -> bool {
307 self.internal.is_disjoint(&other.internal)
308 }
309
310 pub fn is_subset(&self, other: &Self) -> bool {
313 self.internal.is_subset(&other.internal)
314 }
315
316 pub fn is_superset(&self, other: &Self) -> bool {
319 self.internal.is_superset(&other.internal)
320 }
321}
322
323impl<I: Idx + From<usize>> Binary for BitSet<I> {
324 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
325 Binary::fmt(&self.internal, f)
326 }
327}
328
329impl<I: Idx + From<usize> + Display> Display for BitSet<I> {
330 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
331 f.write_str("{")?;
332 for idx in self.ones() {
333 Display::fmt(&idx, f)?;
334 f.write_str(",")?;
335 }
336 f.write_str("}")
337 }
338}
339
340pub struct Difference<'a, I: Idx + From<usize>> {
344 iter: fixedbitset::Difference<'a>,
345 tag: PhantomData<fn(&I)>,
346}
347
348impl<'a, I: Idx + From<usize>> Iterator for Difference<'a, I> {
349 type Item = I;
350
351 #[inline]
352 fn next(&mut self) -> Option<Self::Item> {
353 self.iter.next().map(|idx| idx.into())
354 }
355}
356
357pub struct SymmetricDifference<'a, I: Idx + From<usize>> {
361 iter: fixedbitset::SymmetricDifference<'a>,
362 tag: PhantomData<fn(&I)>,
363}
364
365impl<'a, I: Idx + From<usize>> Iterator for SymmetricDifference<'a, I> {
366 type Item = I;
367
368 #[inline]
369 fn next(&mut self) -> Option<Self::Item> {
370 self.iter.next().map(|idx| idx.into())
371 }
372}
373
374pub struct Intersection<'a, I: Idx + From<usize>> {
378 iter: fixedbitset::Intersection<'a>,
379 tag: PhantomData<fn(&I)>,
380}
381
382impl<'a, I: Idx + From<usize>> Iterator for Intersection<'a, I> {
383 type Item = I; #[inline]
386 fn next(&mut self) -> Option<Self::Item> {
387 self.iter.next().map(|idx| idx.into())
388 }
389}
390
391pub struct Union<'a, I: Idx + From<usize>> {
395 iter: fixedbitset::Union<'a>,
396 tag: PhantomData<fn(&I)>,
397}
398
399impl<'a, I: Idx + From<usize>> Iterator for Union<'a, I> {
400 type Item = I;
401
402 #[inline]
403 fn next(&mut self) -> Option<Self::Item> {
404 self.iter.next().map(|idx| idx.into())
405 }
406}
407
408pub struct Ones<'a, I: Idx + From<usize>> {
412 iter: fixedbitset::Ones<'a>,
413 tag: PhantomData<fn(&I)>,
414}
415
416impl<'a, I: Idx + From<usize>> Iterator for Ones<'a, I> {
417 type Item = I; #[inline]
420 fn next(&mut self) -> Option<Self::Item> {
421 self.iter.next().map(|idx| idx.into())
422 }
423}
424
425impl<I: Idx + From<usize>> Clone for BitSet<I> {
426 #[inline]
427 fn clone(&self) -> Self {
428 Self {
429 internal: self.internal.clone(),
430 tag: PhantomData,
431 }
432 }
433}
434
435impl<I: Idx + From<usize>> Index<I> for BitSet<I> {
441 type Output = bool;
442
443 #[inline]
444 fn index(&self, bit: I) -> &bool {
445 &self.internal[bit.index()]
446 }
447}
448
449impl<I: Idx + From<usize>> Extend<I> for BitSet<I> {
451 fn extend<Iter: IntoIterator<Item = I>>(&mut self, src: Iter) {
452 let src = src.into_iter().map(|id| id.index());
453 self.internal.extend(src)
454 }
455}
456
457impl<I: Idx + From<usize>> FromIterator<I> for BitSet<I> {
460 fn from_iter<Iter: IntoIterator<Item = I>>(src: Iter) -> Self {
461 let iter = src.into_iter();
462 let mut res = BitSet::new_empty(iter.size_hint().0.into());
463 res.extend(iter);
464 res
465 }
466}
467
468impl<'a, I: Idx + From<usize>> BitAnd for &'a BitSet<I> {
469 type Output = BitSet<I>;
470 fn bitand(self, other: &BitSet<I>) -> BitSet<I> {
471 BitSet {
472 internal: (&self.internal) & (&other.internal),
473 tag: PhantomData,
474 }
475 }
476}
477
478impl<'a, I: Idx + From<usize>> BitAndAssign for BitSet<I> {
479 fn bitand_assign(&mut self, other: Self) {
480 self.internal &= other.internal
481 }
482}
483
484impl<'a, I: Idx + From<usize>> BitOr for &'a BitSet<I> {
485 type Output = BitSet<I>;
486 fn bitor(self, other: &BitSet<I>) -> BitSet<I> {
487 BitSet {
488 internal: (&self.internal) | (&other.internal),
489 tag: PhantomData,
490 }
491 }
492}
493
494impl<'a, I: Idx + From<usize>> BitOrAssign for BitSet<I> {
495 fn bitor_assign(&mut self, other: Self) {
496 self.internal |= other.internal
497 }
498}
499
500impl<'a, I: Idx + From<usize>> BitXor for &'a BitSet<I> {
501 type Output = BitSet<I>;
502 fn bitxor(self, other: &BitSet<I>) -> BitSet<I> {
503 BitSet {
504 internal: (&self.internal) ^ (&other.internal),
505 tag: PhantomData,
506 }
507 }
508}
509
510impl<'a, I: Idx + From<usize>> BitXorAssign for BitSet<I> {
511 fn bitxor_assign(&mut self, other: Self) {
512 self.internal ^= other.internal
513 }
514}