|
| 1 | +// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use std::fmt; |
| 12 | +use std::marker::PhantomData; |
| 13 | +use std::mem; |
| 14 | +use bitslice::{BitSlice, Word}; |
| 15 | + |
| 16 | +pub trait Indexed { |
| 17 | + type Idx: Idx; |
| 18 | +} |
| 19 | + |
| 20 | +pub trait Idx { |
| 21 | + fn idx(&self) -> usize; |
| 22 | +} |
| 23 | + |
| 24 | +pub struct OwnIdxSet<T: Idx> { |
| 25 | + _pd: PhantomData<fn(&[T], usize) -> &T>, |
| 26 | + bits: Vec<Word>, |
| 27 | +} |
| 28 | + |
| 29 | +// pnkfelix wants to have this be `IdxSet<T>([Word]) and then pass |
| 30 | +// around `&mut IdxSet<T>` or `&IdxSet<T>`. |
| 31 | +// |
| 32 | +// Mmapping a `&OwnIdxSet<T>` to `&IdxSet<T>` (at least today) |
| 33 | +// requires a transmute relying on representation guarantees that may |
| 34 | +// not hold in the future. |
| 35 | + |
| 36 | +pub struct IdxSet<T: Idx> { |
| 37 | + _pd: PhantomData<fn(&[T], usize) -> &T>, |
| 38 | + bits: [Word], |
| 39 | +} |
| 40 | + |
| 41 | +impl<T: Idx> fmt::Debug for OwnIdxSet<T> { |
| 42 | + fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { self.bits.fmt(w) } |
| 43 | +} |
| 44 | + |
| 45 | +impl<T: Idx> fmt::Debug for IdxSet<T> { |
| 46 | + fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { self.bits.fmt(w) } |
| 47 | +} |
| 48 | + |
| 49 | +impl<T: Idx> OwnIdxSet<T> { |
| 50 | + fn new(init: Word, universe_size: usize) -> Self { |
| 51 | + let bits_per_word = mem::size_of::<Word>(); |
| 52 | + let num_words = (universe_size + (bits_per_word - 1)) / bits_per_word; |
| 53 | + OwnIdxSet { |
| 54 | + _pd: Default::default(), |
| 55 | + bits: vec![init; num_words], |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// Creates set holding every element whose index falls in range 0..universe_size. |
| 60 | + pub fn new_filled(universe_size: usize) -> Self { |
| 61 | + Self::new(!0, universe_size) |
| 62 | + } |
| 63 | + |
| 64 | + /// Creates set holding no elements. |
| 65 | + pub fn new_empty(universe_size: usize) -> Self { |
| 66 | + Self::new(0, universe_size) |
| 67 | + } |
| 68 | + |
| 69 | + /// Removes `elem` from the set `self`; returns true iff this changed `self`. |
| 70 | + pub fn clear(&mut self, elem: &T) -> bool { |
| 71 | + self.bits.clear_bit(elem.idx()) |
| 72 | + } |
| 73 | + |
| 74 | + /// Adds `elem` to the set `self`; returns true iff this changed `self`. |
| 75 | + pub fn add(&mut self, elem: &T) -> bool { |
| 76 | + self.bits.set_bit(elem.idx()) |
| 77 | + } |
| 78 | + |
| 79 | + /// Returns true iff set `self` contains `elem`. |
| 80 | + pub fn contains(&self, elem: &T) -> bool { |
| 81 | + self.bits.get_bit(elem.idx()) |
| 82 | + } |
| 83 | + |
| 84 | + pub fn bits(&self) -> &[Word] { |
| 85 | + &self.bits[..] |
| 86 | + } |
| 87 | +} |
0 commit comments