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
use super::BitSet;

macro_rules! impl_bit_set_uint {
	($ty:ty, $bits_per_word:literal) => {
		impl BitSet for $ty {
			#[inline]
			fn bit_len(&self) -> usize {
				$bits_per_word
			}
			#[inline]
			fn bit_init(&mut self, value: bool) -> &mut Self {
				*self = <$ty>::wrapping_add(!(value as $ty), 1);
				self
			}
			#[inline]
			fn bit_test(&self, bit: usize) -> bool {
				*self & (1 << bit as u32) != 0
			}
			#[inline]
			fn bit_set(&mut self, bit: usize) -> &mut Self {
				*self |= 1 << bit as u32;
				self
			}
			#[inline]
			fn bit_reset(&mut self, bit: usize) -> &mut Self {
				*self &= !(1 << bit as u32);
				self
			}
			#[inline]
			fn bit_flip(&mut self, bit: usize) -> &mut Self {
				*self ^= 1 << bit as u32;
				self
			}
			#[inline]
			fn bit_cond(&mut self, bit: usize, value: bool) -> &mut Self {
				let mask = 1 << bit as u32;
				*self = (*self & !mask) | (<$ty>::wrapping_add(!(value as $ty), 1) & mask);
				self
			}
			#[inline]
			fn bit_all(&self) -> bool {
				*self == !0
			}
			#[inline]
			fn bit_any(&self) -> bool {
				*self != 0
			}
			#[inline]
			fn bit_none(&self) -> bool {
				*self == 0
			}
			#[inline]
			fn bit_eq(&self, rhs: &Self) -> bool {
				*self == *rhs
			}
			#[inline]
			fn bit_disjoint(&self, rhs: &Self) -> bool {
				*self & *rhs == 0
			}
			#[inline]
			fn bit_subset(&self, rhs: &Self) -> bool {
				*self | *rhs == *rhs
			}
			#[inline]
			fn bit_superset(&self, rhs: &Self) -> bool {
				*self | *rhs == *self
			}
			#[inline]
			fn bit_or(&mut self, rhs: &Self) -> &mut Self {
				*self |= *rhs;
				self
			}
			#[inline]
			fn bit_and(&mut self, rhs: &Self) -> &mut Self {
				*self &= *rhs;
				self
			}
			#[inline]
			fn bit_andnot(&mut self, rhs: &Self) -> &mut Self {
				*self &= !*rhs;
				self
			}
			#[inline]
			fn bit_xor(&mut self, rhs: &Self) -> &mut Self {
				*self ^= *rhs;
				self
			}
			#[inline]
			fn bit_not(&mut self) -> &mut Self {
				*self = !*self;
				self
			}
			#[inline]
			fn bit_mask(&mut self, rhs: &Self, mask: &Self) -> &mut Self {
				*self = *self & !*mask | *rhs & *mask;
				self
			}
			#[inline]
			fn bit_count(&self) -> usize {
				self.count_ones() as usize
			}
		}
	};
}

impl_bit_set_uint!(u8, 8);
impl_bit_set_uint!(u16, 16);
impl_bit_set_uint!(u32, 32);
impl_bit_set_uint!(u64, 64);
impl_bit_set_uint!(u128, 128);

//----------------------------------------------------------------

#[test]
fn tests() {
	let mut bytes = 0u8;
	let mut words = 0u16;
	let mut dwords = 0u32;
	let mut qwords = 0u64;

	super::unary_tests(&mut bytes);
	super::unary_tests(&mut words);
	super::unary_tests(&mut dwords);
	super::unary_tests(&mut qwords);
}