|
1 | 1 | use std::marker::PhantomData; |
2 | 2 | #[cfg(not(feature = "nightly"))] |
3 | 3 | use std::mem; |
4 | | -use std::ops::{BitAnd, BitAndAssign, BitOrAssign, Bound, Not, Range, RangeBounds, Shl}; |
| 4 | +use std::ops::{Bound, Range, RangeBounds}; |
5 | 5 | use std::rc::Rc; |
6 | 6 | use std::{fmt, iter, slice}; |
7 | 7 |
|
@@ -1736,114 +1736,3 @@ fn max_bit(word: Word) -> usize { |
1736 | 1736 | fn count_ones(words: &[Word]) -> usize { |
1737 | 1737 | words.iter().map(|word| word.count_ones() as usize).sum() |
1738 | 1738 | } |
1739 | | - |
1740 | | -/// Integral type used to represent the bit set. |
1741 | | -pub trait FiniteBitSetTy: |
1742 | | - BitAnd<Output = Self> |
1743 | | - + BitAndAssign |
1744 | | - + BitOrAssign |
1745 | | - + Clone |
1746 | | - + Copy |
1747 | | - + Shl |
1748 | | - + Not<Output = Self> |
1749 | | - + PartialEq |
1750 | | - + Sized |
1751 | | -{ |
1752 | | - /// Size of the domain representable by this type, e.g. 64 for `u64`. |
1753 | | - const DOMAIN_SIZE: u32; |
1754 | | - |
1755 | | - /// Value which represents the `FiniteBitSet` having every bit set. |
1756 | | - const FILLED: Self; |
1757 | | - /// Value which represents the `FiniteBitSet` having no bits set. |
1758 | | - const EMPTY: Self; |
1759 | | - |
1760 | | - /// Value for one as the integral type. |
1761 | | - const ONE: Self; |
1762 | | - /// Value for zero as the integral type. |
1763 | | - const ZERO: Self; |
1764 | | - |
1765 | | - /// Perform a checked left shift on the integral type. |
1766 | | - fn checked_shl(self, rhs: u32) -> Option<Self>; |
1767 | | - /// Perform a checked right shift on the integral type. |
1768 | | - fn checked_shr(self, rhs: u32) -> Option<Self>; |
1769 | | -} |
1770 | | - |
1771 | | -impl FiniteBitSetTy for u32 { |
1772 | | - const DOMAIN_SIZE: u32 = 32; |
1773 | | - |
1774 | | - const FILLED: Self = Self::MAX; |
1775 | | - const EMPTY: Self = Self::MIN; |
1776 | | - |
1777 | | - const ONE: Self = 1u32; |
1778 | | - const ZERO: Self = 0u32; |
1779 | | - |
1780 | | - fn checked_shl(self, rhs: u32) -> Option<Self> { |
1781 | | - self.checked_shl(rhs) |
1782 | | - } |
1783 | | - |
1784 | | - fn checked_shr(self, rhs: u32) -> Option<Self> { |
1785 | | - self.checked_shr(rhs) |
1786 | | - } |
1787 | | -} |
1788 | | - |
1789 | | -impl std::fmt::Debug for FiniteBitSet<u32> { |
1790 | | - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
1791 | | - write!(f, "{:032b}", self.0) |
1792 | | - } |
1793 | | -} |
1794 | | - |
1795 | | -/// A fixed-sized bitset type represented by an integer type. Indices outwith than the range |
1796 | | -/// representable by `T` are considered set. |
1797 | | -#[cfg_attr(feature = "nightly", derive(Decodable_NoContext, Encodable_NoContext))] |
1798 | | -#[derive(Copy, Clone, Eq, PartialEq)] |
1799 | | -pub struct FiniteBitSet<T: FiniteBitSetTy>(pub T); |
1800 | | - |
1801 | | -impl<T: FiniteBitSetTy> FiniteBitSet<T> { |
1802 | | - /// Creates a new, empty bitset. |
1803 | | - pub fn new_empty() -> Self { |
1804 | | - Self(T::EMPTY) |
1805 | | - } |
1806 | | - |
1807 | | - /// Sets the `index`th bit. |
1808 | | - pub fn set(&mut self, index: u32) { |
1809 | | - self.0 |= T::ONE.checked_shl(index).unwrap_or(T::ZERO); |
1810 | | - } |
1811 | | - |
1812 | | - /// Unsets the `index`th bit. |
1813 | | - pub fn clear(&mut self, index: u32) { |
1814 | | - self.0 &= !T::ONE.checked_shl(index).unwrap_or(T::ZERO); |
1815 | | - } |
1816 | | - |
1817 | | - /// Sets the `i`th to `j`th bits. |
1818 | | - pub fn set_range(&mut self, range: Range<u32>) { |
1819 | | - let bits = T::FILLED |
1820 | | - .checked_shl(range.end - range.start) |
1821 | | - .unwrap_or(T::ZERO) |
1822 | | - .not() |
1823 | | - .checked_shl(range.start) |
1824 | | - .unwrap_or(T::ZERO); |
1825 | | - self.0 |= bits; |
1826 | | - } |
1827 | | - |
1828 | | - /// Is the set empty? |
1829 | | - pub fn is_empty(&self) -> bool { |
1830 | | - self.0 == T::EMPTY |
1831 | | - } |
1832 | | - |
1833 | | - /// Returns the domain size of the bitset. |
1834 | | - pub fn within_domain(&self, index: u32) -> bool { |
1835 | | - index < T::DOMAIN_SIZE |
1836 | | - } |
1837 | | - |
1838 | | - /// Returns if the `index`th bit is set. |
1839 | | - pub fn contains(&self, index: u32) -> Option<bool> { |
1840 | | - self.within_domain(index) |
1841 | | - .then(|| ((self.0.checked_shr(index).unwrap_or(T::ONE)) & T::ONE) == T::ONE) |
1842 | | - } |
1843 | | -} |
1844 | | - |
1845 | | -impl<T: FiniteBitSetTy> Default for FiniteBitSet<T> { |
1846 | | - fn default() -> Self { |
1847 | | - Self::new_empty() |
1848 | | - } |
1849 | | -} |
0 commit comments