bitset_core/
uint.rs

1use super::BitSet;
2
3macro_rules! impl_bit_set_uint {
4	($ty:ty, $bits_per_word:literal) => {
5		impl BitSet for $ty {
6			#[inline]
7			fn bit_len(&self) -> usize {
8				$bits_per_word
9			}
10			#[inline]
11			fn bit_init(&mut self, value: bool) -> &mut Self {
12				*self = <$ty>::wrapping_add(!(value as $ty), 1);
13				self
14			}
15			#[inline]
16			fn bit_test(&self, bit: usize) -> bool {
17				*self & (1 << bit as u32) != 0
18			}
19			#[inline]
20			fn bit_set(&mut self, bit: usize) -> &mut Self {
21				*self |= 1 << bit as u32;
22				self
23			}
24			#[inline]
25			fn bit_reset(&mut self, bit: usize) -> &mut Self {
26				*self &= !(1 << bit as u32);
27				self
28			}
29			#[inline]
30			fn bit_flip(&mut self, bit: usize) -> &mut Self {
31				*self ^= 1 << bit as u32;
32				self
33			}
34			#[inline]
35			fn bit_cond(&mut self, bit: usize, value: bool) -> &mut Self {
36				let mask = 1 << bit as u32;
37				*self = (*self & !mask) | (<$ty>::wrapping_add(!(value as $ty), 1) & mask);
38				self
39			}
40			#[inline]
41			fn bit_all(&self) -> bool {
42				*self == !0
43			}
44			#[inline]
45			fn bit_any(&self) -> bool {
46				*self != 0
47			}
48			#[inline]
49			fn bit_none(&self) -> bool {
50				*self == 0
51			}
52			#[inline]
53			fn bit_eq(&self, rhs: &Self) -> bool {
54				*self == *rhs
55			}
56			#[inline]
57			fn bit_disjoint(&self, rhs: &Self) -> bool {
58				*self & *rhs == 0
59			}
60			#[inline]
61			fn bit_subset(&self, rhs: &Self) -> bool {
62				*self | *rhs == *rhs
63			}
64			#[inline]
65			fn bit_superset(&self, rhs: &Self) -> bool {
66				*self | *rhs == *self
67			}
68			#[inline]
69			fn bit_or(&mut self, rhs: &Self) -> &mut Self {
70				*self |= *rhs;
71				self
72			}
73			#[inline]
74			fn bit_and(&mut self, rhs: &Self) -> &mut Self {
75				*self &= *rhs;
76				self
77			}
78			#[inline]
79			fn bit_andnot(&mut self, rhs: &Self) -> &mut Self {
80				*self &= !*rhs;
81				self
82			}
83			#[inline]
84			fn bit_xor(&mut self, rhs: &Self) -> &mut Self {
85				*self ^= *rhs;
86				self
87			}
88			#[inline]
89			fn bit_not(&mut self) -> &mut Self {
90				*self = !*self;
91				self
92			}
93			#[inline]
94			fn bit_mask(&mut self, rhs: &Self, mask: &Self) -> &mut Self {
95				*self = *self & !*mask | *rhs & *mask;
96				self
97			}
98			#[inline]
99			fn bit_count(&self) -> usize {
100				self.count_ones() as usize
101			}
102		}
103	};
104}
105
106impl_bit_set_uint!(u8, 8);
107impl_bit_set_uint!(u16, 16);
108impl_bit_set_uint!(u32, 32);
109impl_bit_set_uint!(u64, 64);
110impl_bit_set_uint!(u128, 128);
111
112//----------------------------------------------------------------
113
114#[test]
115fn tests() {
116	let mut bytes = 0u8;
117	let mut words = 0u16;
118	let mut dwords = 0u32;
119	let mut qwords = 0u64;
120
121	super::unary_tests(&mut bytes);
122	super::unary_tests(&mut words);
123	super::unary_tests(&mut dwords);
124	super::unary_tests(&mut qwords);
125}