1#![no_std]
78
79#[cfg(any(test, feature = "std"))]
80#[macro_use]
81extern crate std;
82
83pub trait BitSet {
85 fn bit_len(&self) -> usize;
87
88 fn bit_init(&mut self, value: bool) -> &mut Self;
90 #[inline]
92 fn bit_fmt(&self) -> &BitFmt<Self> {
93 unsafe { &*(self as *const _ as *const _) }
94 }
95
96 fn bit_test(&self, bit: usize) -> bool;
98 fn bit_set(&mut self, bit: usize) -> &mut Self;
100 fn bit_reset(&mut self, bit: usize) -> &mut Self;
102 fn bit_flip(&mut self, bit: usize) -> &mut Self;
104 fn bit_cond(&mut self, bit: usize, value: bool) -> &mut Self;
106
107 fn bit_all(&self) -> bool;
109 fn bit_any(&self) -> bool;
111 #[inline]
113 fn bit_none(&self) -> bool {
114 !self.bit_any()
115 }
116
117 fn bit_eq(&self, rhs: &Self) -> bool;
119 fn bit_disjoint(&self, rhs: &Self) -> bool;
121 fn bit_subset(&self, rhs: &Self) -> bool;
123 #[inline]
125 fn bit_superset(&self, rhs: &Self) -> bool {
126 rhs.bit_subset(self)
127 }
128
129 fn bit_or(&mut self, rhs: &Self) -> &mut Self;
131 fn bit_and(&mut self, rhs: &Self) -> &mut Self;
133 fn bit_andnot(&mut self, rhs: &Self) -> &mut Self;
135 fn bit_xor(&mut self, rhs: &Self) -> &mut Self;
137 fn bit_not(&mut self) -> &mut Self;
139 fn bit_mask(&mut self, rhs: &Self, mask: &Self) -> &mut Self;
141
142 fn bit_count(&self) -> usize;
144}
145
146#[macro_export]
156macro_rules! bitset {
157 ($init:expr; $($bit:expr),* $(,)?) => {{
158 use $crate::BitSet;
159 #[allow(unused_mut)]
160 match $init {
161 mut this => {
162 $(this.bit_set($bit as usize);)*
163 this
164 },
165 }
166 }};
167}
168
169#[macro_export]
179macro_rules! bitor {
180 ($init:expr; $($bits:expr),* $(,)?) => {{
181 use $crate::BitSet;
182 #[allow(unused_mut)]
183 match $init {
184 mut this => {
185 $(this.bit_or(&$bits);)*
186 this
187 },
188 }
189 }};
190}
191
192#[macro_export]
196macro_rules! impl_bitset {
197 () => {
198 #[inline]
199 fn bit_len(&self) -> usize {
200 use ::core::ops;
201 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_len(<Self as ops::Deref>::deref(self))
202 }
203
204 #[inline]
205 fn bit_init(&mut self, value: bool) -> &mut Self {
206 use ::core::ops;
207 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_init(<Self as ops::DerefMut>::deref_mut(self), value);
208 self
209 }
210
211 #[inline]
212 fn bit_test(&self, bit: usize) -> bool {
213 use ::core::ops;
214 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_test(<Self as ops::Deref>::deref(self), bit)
215 }
216 #[inline]
217 fn bit_set(&mut self, bit: usize) -> &mut Self {
218 use ::core::ops;
219 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_set(<Self as ops::DerefMut>::deref_mut(self), bit);
220 self
221 }
222 #[inline]
223 fn bit_reset(&mut self, bit: usize) -> &mut Self {
224 use ::core::ops;
225 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_reset(<Self as ops::DerefMut>::deref_mut(self), bit);
226 self
227 }
228 #[inline]
229 fn bit_flip(&mut self, bit: usize) -> &mut Self {
230 use ::core::ops;
231 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_flip(<Self as ops::DerefMut>::deref_mut(self), bit);
232 self
233 }
234 #[inline]
235 fn bit_cond(&mut self, bit: usize, value: bool) -> &mut Self {
236 use ::core::ops;
237 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_cond(<Self as ops::DerefMut>::deref_mut(self), bit, value);
238 self
239 }
240
241 #[inline]
242 fn bit_all(&self) -> bool {
243 use ::core::ops;
244 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_all(<Self as ops::Deref>::deref(self))
245 }
246 #[inline]
247 fn bit_any(&self) -> bool {
248 use ::core::ops;
249 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_any(<Self as ops::Deref>::deref(self))
250 }
251 #[inline]
252 fn bit_none(&self) -> bool {
253 use ::core::ops;
254 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_none(<Self as ops::Deref>::deref(self))
255 }
256
257 #[inline]
258 fn bit_eq(&self, rhs: &Self) -> bool {
259 use ::core::ops;
260 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_eq(<Self as ops::Deref>::deref(self), <Self as ops::Deref>::deref(rhs))
261 }
262 #[inline]
263 fn bit_disjoint(&self, rhs: &Self) -> bool {
264 use ::core::ops;
265 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_disjoint(<Self as ops::Deref>::deref(self), <Self as ops::Deref>::deref(rhs))
266 }
267 #[inline]
268 fn bit_subset(&self, rhs: &Self) -> bool {
269 use ::core::ops;
270 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_subset(<Self as ops::Deref>::deref(self), <Self as ops::Deref>::deref(rhs))
271 }
272 #[inline]
273 fn bit_superset(&self, rhs: &Self) -> bool {
274 use ::core::ops;
275 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_superset(<Self as ops::Deref>::deref(self), <Self as ops::Deref>::deref(rhs))
276 }
277
278 #[inline]
279 fn bit_or(&mut self, rhs: &Self) -> &mut Self {
280 use ::core::ops;
281 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_or(<Self as ops::DerefMut>::deref_mut(self), <Self as ops::Deref>::deref(rhs));
282 self
283 }
284 #[inline]
285 fn bit_and(&mut self, rhs: &Self) -> &mut Self {
286 use ::core::ops;
287 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_and(<Self as ops::DerefMut>::deref_mut(self), <Self as ops::Deref>::deref(rhs));
288 self
289 }
290 #[inline]
291 fn bit_andnot(&mut self, rhs: &Self) -> &mut Self {
292 use ::core::ops;
293 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_andnot(<Self as ops::DerefMut>::deref_mut(self), <Self as ops::Deref>::deref(rhs));
294 self
295 }
296 #[inline]
297 fn bit_xor(&mut self, rhs: &Self) -> &mut Self {
298 use ::core::ops;
299 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_xor(<Self as ops::DerefMut>::deref_mut(self), <Self as ops::Deref>::deref(rhs));
300 self
301 }
302 #[inline]
303 fn bit_not(&mut self) -> &mut Self {
304 use ::core::ops;
305 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_not(<Self as ops::DerefMut>::deref_mut(self));
306 self
307 }
308 #[inline]
309 fn bit_mask(&mut self, rhs: &Self, mask: &Self) -> &mut Self {
310 use ::core::ops;
311 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_mask(<Self as ops::DerefMut>::deref_mut(self), <Self as ops::Deref>::deref(rhs), <Self as ops::Deref>::deref(mask));
312 self
313 }
314
315 #[inline]
316 fn bit_count(&self) -> usize {
317 use ::core::ops;
318 <<Self as ops::Deref>::Target as $crate::BitSet>::bit_count(<Self as ops::Deref>::deref(self))
319 }
320 };
321}
322
323mod uint;
324mod slice;
325mod simd;
326
327#[cfg(feature = "std")]
328mod stdty;
329
330mod fmt;
331pub use self::fmt::BitFmt;
332
333#[cfg(test)]
336fn unary_tests<T: ?Sized + BitSet>(bits: &mut T) {
337 bits.bit_init(false);
339 assert_eq!(bits.bit_any(), false);
340 assert_eq!(bits.bit_all(), false);
341 for i in 0..bits.bit_len() {
343 bits.bit_set(i & !1);
344 }
345 assert_eq!(bits.bit_any(), true);
346 assert_eq!(bits.bit_all(), false);
347 for i in 0..bits.bit_len() {
348 assert_eq!(bits.bit_test(i), i & 1 == 0);
349 }
350 bits.bit_not();
352 for i in 0..bits.bit_len() {
353 bits.bit_flip(i);
354 assert_eq!(bits.bit_test(i), i & 1 == 0);
355 }
356
357 bits.bit_init(true);
359 assert_eq!(bits.bit_any(), true);
360 assert_eq!(bits.bit_all(), true);
361 for i in 0..bits.bit_len() {
363 bits.bit_reset(i & !1);
364 }
365 assert_eq!(bits.bit_any(), true);
366 assert_eq!(bits.bit_all(), false);
367 for i in 0..bits.bit_len() {
368 assert_eq!(bits.bit_test(i), i & 1 != 0);
369 }
370 bits.bit_not();
372 for i in 0..bits.bit_len() {
373 bits.bit_flip(i);
374 assert_eq!(bits.bit_test(i), i & 1 != 0);
375 }
376
377 assert!(!bits.bit_disjoint(bits));
378 assert!(bits.bit_subset(bits));
379 assert!(bits.bit_superset(bits));
380}