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
//! A fixed-size, stack-allocated bitset.
//!
//! ```
//! use fixed_bitset::Bitset;
//! use typenum::consts::*;
//!
//! let mut set = Bitset::<U100>::new();
//!
//! set.insert(20);
//! set.insert(70);
//! // set.insert(100); // WILL PANIC!
//!
//! let values: Vec<usize> = set.iter().collect();
//! assert_eq!(values, vec![20, 70]);
//!
//! let mut superset = set.clone();
//! superset.insert(50);
//!
//! assert!(superset.is_superset(&set));
//! assert!(set.is_subset(&superset));
//!
//!
//! let difference = &superset - &set;
//! assert_eq!(difference.iter().collect::<Vec<_>>(), vec![50]);
//! assert!(difference.is_disjoint(&set));
//! ```

#![deny(missing_docs)]

mod primitive;

use std::ops::*;
use std::{fmt, iter};

use generic_array::{GenericArray, ArrayLength};
use typenum::Unsigned;

use self::primitive::{Primitive, CeilDiv};

type CeilQuot<T, Q> = <T as CeilDiv<Q>>::Output;

/// Yields the index of each set bit in this block.
fn bits<B>(mut block: B) -> impl Iterator<Item = usize> + Clone
    where B: Primitive
{
    iter::from_fn(move || {
        if block.is_zero() {
            None
        } else {
            let next_bit = block.trailing_zeros() as usize;
            block ^= B::one() << next_bit;
            Some(next_bit)
        }
    })
}

/// A set of unsigned integers whose size is fixed at compile-time.
///
/// A `Bitset` can only store unsigned integers less than `N`, where `N` is a compile-time integer
/// from `typenum`. A `Bitset` uses a single bit to indicate the presence or absence of each value.
pub struct Bitset<N, B = usize>
    where B: Primitive,
          N: CeilDiv<B::Size>,
          CeilQuot<N, B::Size>: ArrayLength<B>,
{
    blocks: GenericArray<B, CeilQuot<N, B::Size>>,
}

impl<N, B> fmt::Debug for Bitset<N, B>
    where B: Primitive,
          N: Unsigned + CeilDiv<B::Size>,
          CeilQuot<N, B::Size>: ArrayLength<B>,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_set()
            .entries(self.iter())
            .finish()
    }
}

impl<N, B> Default for Bitset<N, B>
    where B: Primitive,
          N: CeilDiv<B::Size>,
          CeilQuot<N, B::Size>: ArrayLength<B>,
{
    fn default() -> Self {
        Bitset {
            blocks: Default::default(),
        }
    }
}

impl<N, B> Clone for Bitset<N, B>
    where B: Primitive,
          N: CeilDiv<B::Size>,
          CeilQuot<N, B::Size>: ArrayLength<B>,
{
    fn clone(&self) -> Self {
        Bitset {
            blocks: self.blocks.clone(),
        }
    }
}

impl<N, B> Copy for Bitset<N, B>
    where B: Primitive,
          N: CeilDiv<B::Size>,
          CeilQuot<N, B::Size>: ArrayLength<B>,
          GenericArray<B, CeilQuot<N, B::Size>>: Copy,
{}

impl<N, B> PartialEq for Bitset<N, B>
    where B: Primitive,
          N: CeilDiv<B::Size>,
          CeilQuot<N, B::Size>: ArrayLength<B>,
{
    fn eq(&self, other: &Self) -> bool {
        self.blocks == other.blocks
    }
}

impl<N, B> std::cmp::Eq for Bitset<N, B>
    where B: Primitive,
          N: CeilDiv<B::Size>,
          CeilQuot<N, B::Size>: ArrayLength<B>,
{}

impl<N, B> iter::FromIterator<usize> for Bitset<N, B>
    where B: Primitive,
          N: Unsigned + CeilDiv<B::Size>,
          CeilQuot<N, B::Size>: ArrayLength<B>,
{
    fn from_iter<T>(iter: T ) -> Self
        where T: IntoIterator<Item = usize>
    {
        let mut ret = Self::default();
        for n in iter.into_iter() {
            ret.insert(n);
        }

        ret
    }
}

impl<N, B> Bitset<N, B>
    where B: Primitive,
          N: Unsigned + CeilDiv<B::Size>,
          CeilQuot<N, B::Size>: ArrayLength<B>,
{
    /// Returns an empty bitset.
    pub fn new() -> Self {
        Default::default()
    }

    /// Returns the block index and shift required to access a given bit.
    fn loc(bit: usize) -> (usize, usize) {
        (bit / B::SIZE, bit % B::SIZE)
    }

    /// Returns `true` if the bitset contains a value.
    ///
    /// Panics if `value >= N`.
    pub fn contains(&self, value: usize) -> bool {
        assert!(value < N::USIZE);
        let (block, shift) = Self::loc(value);

        (self.blocks[block] >> shift) & B::one() == B::one()
    }

    /// Inserts a value into the bitset.
    ///
    /// If that value already exists in the bitset, this function has no effect.
    ///
    /// Panics if `value >= N`.
    pub fn insert(&mut self, value: usize) {
        assert!(value < N::USIZE);
        let (block, shift) = Self::loc(value);

        self.blocks[block] |= B::one() << shift;
    }

    /// Removes a value from the bitset.
    ///
    /// If that value does not already exist in the bitset, this function has no effect.
    ///
    /// Panics if `value >= N`.
    pub fn remove(&mut self, value: usize) {
        assert!(value < N::USIZE);
        let (block, shift) = Self::loc(value);

        self.blocks[block] &= !(B::one() << shift);
    }

    /// Returns `true` if the bitset contains no bits.
    pub fn is_empty(&self) -> bool {
        self.blocks
            .iter()
            .all(|b| b.is_zero())
    }

    /// Returns the number of values contained in the bitset.
    pub fn len(&self) -> usize {
        self.blocks
            .iter()
            .map(|b| b.count_ones() as usize)
            .sum()
    }

    /// Returns an iterator over the values in the bitset.
    pub fn iter(&self) -> impl '_ + Iterator<Item = usize> + Clone {
        self.blocks
            .iter()
            .cloned()
            .enumerate()
            .flat_map(|(i, b)| bits(b).map(move |j| B::SIZE * i + j))
    }

    /// Clears the bitset, removing all values.
    pub fn clear(&mut self) {
        for b in &mut self.blocks {
            *b = B::zero();
        }
    }

    fn apply_blocks(&mut self, other: &Self, f: impl Fn(&mut B, B)) {
        for (a, &b) in self.blocks.iter_mut().zip(other.blocks.iter()) {
            f(a, b);
        }
    }

    fn iter_blocks<'a>(&'a self, other: &'a Self, f: impl 'a + Fn(B, B) -> B)
        -> impl 'a + Iterator<Item = usize>
    {
        self.blocks
            .iter()
            .zip(other.blocks.iter())
            .map(move |(&a, &b)| f(a, b))
            .enumerate()
            .flat_map(|(i, b)| bits(b).map(move |j| B::SIZE * i + j))
    }

    /// Returns an iterator over `self | other`.
    pub fn union<'a>(&'a self, other: &'a Self) -> impl 'a + Iterator<Item = usize> {
        self.iter_blocks(other, |a, b| a | b)
    }

    /// Returns an iterator over `self & other`.
    pub fn intersection<'a>(&'a self, other: &'a Self) -> impl 'a + Iterator<Item = usize> {
        self.iter_blocks(other, |a, b| a & b)
    }

    /// Returns an iterator over `self ^ other`.
    pub fn symmetric_difference<'a>(&'a self, other: &'a Self) -> impl 'a + Iterator<Item = usize> {
        self.iter_blocks(other, |a, b| a ^ b)
    }

    /// Returns an iterator over `self - other`.
    pub fn difference<'a>(&'a self, other: &'a Self) -> impl 'a + Iterator<Item = usize> {
        self.iter_blocks(other, |a, b| a & !b)
    }

    /// Returns `true` if `self` has no elements in common with `other`.
    ///
    /// This is more efficient than `self.intersection(other).next().is_none()`.
    pub fn is_disjoint(&self, other: &Self) -> bool {
        self.blocks
            .iter()
            .zip(other.blocks.iter())
            .all(|(&a, &b)| (a & b).is_zero())
    }

    /// Returns `true` if every element in `self` exists in `other`.
    pub fn is_subset(&self, other: &Self) -> bool {
        self.blocks
            .iter()
            .zip(other.blocks.iter())
            .all(|(&a, &b)| a & b == a)
    }

    /// Returns `true` if every element in `other` exists in `self`.
    pub fn is_superset(&self, other: &Self) -> bool {
        self.blocks
            .iter()
            .zip(other.blocks.iter())
            .all(|(&a, &b)| a & b == b)
    }
}

macro_rules! ops {
    ($( $( #[$meta:meta] )* $OpAssign:ident, $op_assign:ident, $Op:ident, $op:ident => $f:expr ),* $(,)?) => {
        $(
            $(#[$meta])*
            impl<N, B> $OpAssign<&Self> for Bitset<N, B>
                where B: Primitive,
                      N: Unsigned + CeilDiv<B::Size>,
                      CeilQuot<N, B::Size>: ArrayLength<B>,
            {
                fn $op_assign(&mut self, other: &Self) {
                    self.apply_blocks(other, $f);
                }
            }

            $(#[$meta])*
            impl<'a, 'b, N, B> $Op<&'b Bitset<N, B>> for &'a Bitset<N, B>
                where B: Primitive,
                      N: Unsigned + CeilDiv<B::Size>,
                      CeilQuot<N, B::Size>: ArrayLength<B>,
                      Bitset<N, B>: Clone,
            {
                type Output = Bitset<N, B>;

                fn $op(self, other: &'b Bitset<N, B>) -> Self::Output {
                    let mut ret = (*self).clone();
                    (&mut ret).$op_assign(other);
                    ret
                }
            }
        )*
    }
}

ops! {
    /// Union
    BitOrAssign,  bitor_assign,  BitOr,  bitor  => |a, b| *a |= b,
    /// Intersection
    BitAndAssign, bitand_assign, BitAnd, bitand => |a, b| *a &= b,
    /// Symmetric Difference
    BitXorAssign, bitxor_assign, BitXor, bitxor => |a, b| *a ^= b,
    /// Difference
    SubAssign,    sub_assign,    Sub,    sub    => |a, b| *a &= !b,
}