1use std::cmp;
2
3pub type Bitline8 = u8;
4pub type Bitline16 = u16;
5pub type Bitline32 = u32;
6pub type Bitline64 = u64;
7pub type Bitline128 = u128;
8
9pub trait Bitline {
10 fn as_empty() -> Self;
17
18 fn as_full() -> Self;
25
26 fn by_range(begin: usize, end: usize) -> Self;
33
34 fn is_empty(&self) -> bool;
42
43 fn is_not_empty(&self) -> bool;
51
52 fn is_full(&self) -> bool;
60
61 fn is_not_full(&self) -> bool;
69
70 fn first_index(&self) -> Option<usize>;
80 fn last_index(&self) -> Option<usize>;
89
90 fn radius(&self, n: usize) -> Self;
111
112 fn around(&self, n: usize) -> Self;
127
128 fn with_around(&self, n: usize) -> Self;
143
144 fn first_bit(&self) -> Self;
154
155 fn last_bit(&self) -> Self;
165
166 fn first_bits(&self) -> Self;
176
177 fn last_bits(&self) -> Self;
187
188 fn filled_first_bit_to_last_bit(&self) -> Self;
198
199 fn bytes_length() -> usize;
209
210 fn length() -> usize;
220
221 fn num_bits(&self) -> usize;
231
232 fn includes(&self, other: Self) -> bool;
247
248 fn overlaps(&self, other: Self) -> bool;
258
259 fn range(&self, begin: usize, end: usize) -> Self;
268
269 fn remove(&self, other: Self) -> Self;
278
279 fn bit_repr(&self) -> String;
286}
287
288macro_rules! impl_Bitline {
289 ($T:ty) => {
290 impl Bitline for $T {
291 #[inline]
292 fn as_empty() -> Self {
293 0
294 }
295 #[inline]
296 fn as_full() -> Self {
297 Self::max_value()
298 }
299 #[inline]
300 fn by_range(begin: usize, end: usize) -> Self {
301 let bits_size = Self::BITS as usize;
302 let last_index = cmp::min(end, bits_size);
303 let first_index = cmp::min(begin, last_index);
304 let size = last_index - first_index;
305 if (size <= 0) {
306 return Self::as_empty();
307 }
308 if (size >= bits_size) {
309 return Self::as_full();
310 }
311 let fill_bits = (1 << size) - 1;
312 let right_pad = bits_size - last_index;
313 fill_bits << right_pad
314 }
315 #[inline]
316 fn is_empty(&self) -> bool {
317 *self == Self::as_empty()
318 }
319 #[inline]
320 fn is_not_empty(&self) -> bool {
321 !self.is_empty()
322 }
323 #[inline]
324 fn is_full(&self) -> bool {
325 *self == Self::as_full()
326 }
327 #[inline]
328 fn is_not_full(&self) -> bool {
329 !self.is_full()
330 }
331 #[inline]
332 fn first_index(&self) -> Option<usize> {
333 let zeros = self.leading_zeros() as usize;
334 if (zeros == Self::length()) {
335 return None;
336 }
337 Some(zeros)
338 }
339 #[inline]
340 fn last_index(&self) -> Option<usize> {
341 let zeros = (Self::length() - self.trailing_zeros() as usize);
342 if (zeros < 1) {
343 return None;
344 }
345 Some(zeros - 1)
346 }
347 #[inline]
348 fn radius(&self, n: usize) -> Self {
349 (self << n) ^ (self >> n)
350 }
351 #[inline]
352 fn around(&self, n: usize) -> Self {
353 let mut a = 0;
354 for m in 0..(n + 1) {
355 a |= self.radius(m);
356 }
357 a
358 }
359 #[inline]
360 fn with_around(&self, n: usize) -> Self {
361 self | self.around(n)
362 }
363 #[inline]
364 fn first_bit(&self) -> Self {
365 let zeros = self.leading_zeros() as usize;
366 if (zeros == Self::length()) {
367 return Self::as_empty();
368 }
369 1 << (Self::length() - zeros - 1)
370 }
371 #[inline]
372 fn last_bit(&self) -> Self {
373 let zeros = self.trailing_zeros() as usize;
374 if (zeros == Self::length()) {
375 return Self::as_empty();
376 }
377 1 << self.trailing_zeros()
378 }
379 #[inline]
380 fn first_bits(&self) -> Self {
381 self & !(self >> 1)
382 }
383 #[inline]
384 fn last_bits(&self) -> Self {
385 self & !(self << 1)
386 }
387 #[inline]
388 fn filled_first_bit_to_last_bit(&self) -> Self {
389 if (self.is_empty()) {
390 return Self::as_empty();
391 }
392 let first_index = self.first_index().unwrap();
393 let last_index = self.last_index().unwrap();
394 Self::by_range(first_index, last_index + 1)
395 }
396
397 #[inline]
398 fn length() -> usize {
399 Self::BITS as usize
400 }
401 #[inline]
402 fn bytes_length() -> usize {
403 (Self::BITS / 8) as usize
404 }
405 #[inline]
406 fn num_bits(&self) -> usize {
407 self.count_ones() as usize
408 }
409 #[inline]
410 fn includes(&self, other: Self) -> bool {
411 (self | other) - self == 0
412 }
413 #[inline]
414 fn overlaps(&self, other: Self) -> bool {
415 self & other != 0
416 }
417 #[inline]
418 fn range(&self, begin: usize, end: usize) -> Self {
419 self & Self::by_range(begin, end)
420 }
421 #[inline]
422 fn remove(&self, other: Self) -> Self {
423 self & !other
424 }
425 #[inline]
426 fn bit_repr(&self) -> String {
427 let formatted = format!("{:b}", self);
428 let lack_bits = Self::length() - formatted.len();
429 "0".repeat(lack_bits) + &formatted
430 }
431 }
432 };
433}
434
435impl_Bitline!(Bitline8);
436impl_Bitline!(Bitline16);
437impl_Bitline!(Bitline32);
438impl_Bitline!(Bitline64);
439impl_Bitline!(Bitline128);
440
441#[test]
442fn test_as_empty() {
443 assert_eq!(u8::as_empty().bit_repr(), "0".repeat(8));
444 assert_eq!(u16::as_empty().bit_repr(), "0".repeat(16));
445 assert_eq!(u32::as_empty().bit_repr(), "0".repeat(32));
446 assert_eq!(u64::as_empty().bit_repr(), "0".repeat(64));
447 assert_eq!(u128::as_empty().bit_repr(), "0".repeat(128));
448}
449
450#[test]
451fn test_as_full() {
452 assert_eq!(u8::as_full().bit_repr(), "1".repeat(8));
453 assert_eq!(u16::as_full().bit_repr(), "1".repeat(16));
454 assert_eq!(u32::as_full().bit_repr(), "1".repeat(32));
455 assert_eq!(u64::as_full().bit_repr(), "1".repeat(64));
456 assert_eq!(u128::as_full().bit_repr(), "1".repeat(128));
457}
458
459#[test]
460fn test_by_range() {
461 assert_eq!(u8::by_range(3, 4), 0b00010000);
462 assert_eq!(u8::by_range(0, 8), 0b11111111);
463 assert_eq!(u8::by_range(0, 0), 0b00000000);
464}
465
466#[test]
467fn test_first_index() {
468 assert_eq!(0b01000000_u8.first_index(), Some(1));
469 assert_eq!(0b00010000_u8.first_index(), Some(3));
470 assert_eq!(0b00010100_u8.first_index(), Some(3));
471 assert_eq!(0b00000100_u8.first_index(), Some(5));
472 assert_eq!(0b00000001_u8.first_index(), Some(7));
473 assert!(0b00000000_u8.first_index().is_none());
474}
475
476#[test]
477fn test_last_index() {
478 assert_eq!(0b01000000_u8.last_index(), Some(1));
479 assert_eq!(0b00010000_u8.last_index(), Some(3));
480 assert_eq!(0b00010100_u8.last_index(), Some(5));
481 assert_eq!(0b00000100_u8.last_index(), Some(5));
482 assert_eq!(0b00000001_u8.last_index(), Some(7));
483 assert!(0b00000000_u8.last_index().is_none());
484}
485
486#[test]
487fn test_radius() {
488 assert_eq!(0b00010000_u8.radius(0), 0b00000000_u8);
489 assert_eq!(0b00010000_u8.radius(1), 0b00101000_u8);
490 assert_eq!(0b00010000_u8.radius(2), 0b01000100_u8);
491 assert_eq!(0b00010000_u8.radius(3), 0b10000010_u8);
492 assert_eq!(0b00010000_u8.radius(4), 0b00000001_u8);
493 assert_eq!(0b00000000_u8.radius(0), 0b00000000_u8);
494 assert_eq!(0b00000000_u8.radius(1), 0b00000000_u8);
495 assert_eq!(0b00000000_u8.radius(2), 0b00000000_u8);
496}
497
498#[test]
499fn test_around() {
500 assert_eq!(0b00010000_u8.around(0), 0b00000000_u8);
501 assert_eq!(0b00010000_u8.around(1), 0b00101000_u8);
502 assert_eq!(0b00010000_u8.around(2), 0b01101100_u8);
503 assert_eq!(0b00010000_u8.around(3), 0b11101110_u8);
504 assert_eq!(0b00010000_u8.around(4), 0b11101111_u8);
505 assert_eq!(0b00000000_u8.around(0), 0b00000000_u8);
506 assert_eq!(0b00000000_u8.around(1), 0b00000000_u8);
507 assert_eq!(0b00000000_u8.around(2), 0b00000000_u8);
508}
509
510#[test]
511fn test_with_around() {
512 assert_eq!(0b00010000_u8.with_around(0), 0b00010000_u8);
513 assert_eq!(0b00010000_u8.with_around(1), 0b00111000_u8);
514 assert_eq!(0b00010000_u8.with_around(2), 0b01111100_u8);
515 assert_eq!(0b00010000_u8.with_around(3), 0b11111110_u8);
516 assert_eq!(0b00010000_u8.with_around(4), 0b11111111_u8);
517 assert_eq!(0b00000000_u8.with_around(0), 0b00000000_u8);
518 assert_eq!(0b00000000_u8.with_around(1), 0b00000000_u8);
519 assert_eq!(0b00000000_u8.with_around(2), 0b00000000_u8);
520}
521
522#[test]
523fn test_first_bit() {
524 assert_eq!(0b01000000_u8.first_bit(), 0b01000000_u8);
525 assert_eq!(0b00010000_u8.first_bit(), 0b00010000_u8);
526 assert_eq!(0b00010100_u8.first_bit(), 0b00010000_u8);
527 assert_eq!(0b00000100_u8.first_bit(), 0b00000100_u8);
528 assert_eq!(0b00000001_u8.first_bit(), 0b00000001_u8);
529 assert_eq!(0b00000000_u8.first_bit(), 0b00000000_u8);
530}
531
532#[test]
533fn test_last_bit() {
534 assert_eq!(0b01000000_u8.last_bit(), 0b01000000_u8);
535 assert_eq!(0b00010000_u8.last_bit(), 0b00010000_u8);
536 assert_eq!(0b00010100_u8.last_bit(), 0b00000100_u8);
537 assert_eq!(0b00000100_u8.last_bit(), 0b00000100_u8);
538 assert_eq!(0b00000001_u8.last_bit(), 0b00000001_u8);
539 assert_eq!(0b00000000_u8.last_bit(), 0b00000000_u8);
540}
541
542#[test]
543fn test_first_bits() {
544 assert_eq!(0b11111111_u8.first_bits(), 0b10000000_u8);
545 assert_eq!(0b01000000_u8.first_bits(), 0b01000000_u8);
546 assert_eq!(0b01100110_u8.first_bits(), 0b01000100_u8);
547}
548
549#[test]
550fn test_last_bits() {
551 assert_eq!(0b11111111_u8.last_bits(), 0b00000001_u8);
552 assert_eq!(0b01000000_u8.last_bits(), 0b01000000_u8);
553 assert_eq!(0b01100110_u8.last_bits(), 0b00100010_u8);
554}
555
556#[test]
557fn test_filled_first_bit_to_last_bit() {
558 assert_eq!(0b01000000_u8.filled_first_bit_to_last_bit(), 0b01000000_u8);
559 assert_eq!(0b00010000_u8.filled_first_bit_to_last_bit(), 0b00010000_u8);
560 assert_eq!(0b00010100_u8.filled_first_bit_to_last_bit(), 0b00011100_u8);
561 assert_eq!(0b00000100_u8.filled_first_bit_to_last_bit(), 0b00000100_u8);
562 assert_eq!(0b00000001_u8.filled_first_bit_to_last_bit(), 0b00000001_u8);
563 assert_eq!(0b00000000_u8.filled_first_bit_to_last_bit(), 0b00000000_u8);
564}
565
566#[test]
567fn test_length() {
568 assert_eq!(u8::length(), 8);
569 assert_eq!(u16::length(), 16);
570 assert_eq!(u32::length(), 32);
571 assert_eq!(u64::length(), 64);
572 assert_eq!(u128::length(), 128);
573}
574
575#[test]
576fn test_bytes_length() {
577 assert_eq!(u8::bytes_length(), 1);
578 assert_eq!(u16::bytes_length(), 2);
579 assert_eq!(u32::bytes_length(), 4);
580 assert_eq!(u64::bytes_length(), 8);
581 assert_eq!(u128::bytes_length(), 16);
582}
583
584#[test]
585fn test_is_empty() {
586 assert!(u8::as_empty().is_empty());
587 assert!(u16::as_empty().is_empty());
588 assert!(u32::as_empty().is_empty());
589 assert!(u64::as_empty().is_empty());
590 assert!(u128::as_empty().is_empty());
591 assert!(0b00000000_u8.is_empty());
592 assert!(!0b00000001_u8.is_empty());
593 assert!(!0b10000000_u8.is_empty());
594 assert!(!0b00001000_u8.is_empty());
595 assert!(!0b00000000_u8.is_not_empty());
596 assert!(0b00000001_u8.is_not_empty());
597 assert!(0b10000000_u8.is_not_empty());
598 assert!(0b00001000_u8.is_not_empty());
599}
600
601#[test]
602fn test_is_full() {
603 assert!(u8::as_full().is_full());
604 assert!(u16::as_full().is_full());
605 assert!(u32::as_full().is_full());
606 assert!(u64::as_full().is_full());
607 assert!(u128::as_full().is_full());
608 assert!(0b11111111_u8.is_full());
609 assert!(!0b11111110_u8.is_full());
610 assert!(!0b01111111_u8.is_full());
611 assert!(!0b11101111_u8.is_full());
612 assert!(!0b11111111_u8.is_not_full());
613 assert!(0b11111110_u8.is_not_full());
614 assert!(0b01111111_u8.is_not_full());
615 assert!(0b11101111_u8.is_not_full());
616}
617
618#[test]
619fn test_num_bits() {
620 assert_eq!(0b00000000_u8.num_bits(), 0);
621 assert_eq!(0b00001000_u8.num_bits(), 1);
622 assert_eq!(0b01001000_u8.num_bits(), 2);
623 assert_eq!(0b01101000_u8.num_bits(), 3);
624 assert_eq!(0b11111111_u8.num_bits(), 8);
625}
626
627#[test]
628fn test_includes() {
629 assert!(0b00000000_u8.includes(0b00000000_u8));
630 assert!(0b00011110_u8.includes(0b00000110_u8));
631}
632
633#[test]
634fn test_overlaps() {
635 assert!(!0b11110000_u8.overlaps(0b00001111_u8));
636 assert!(0b00011110_u8.overlaps(0b00011000_u8));
637}
638
639#[test]
640fn test_range() {
641 assert_eq!(0b11111111_u8.range(2, 6), 0b00111100_u8);
642 assert_eq!(0b10101010_u8.range(2, 6), 0b00101000_u8);
643 assert_eq!(0b01010101_u8.range(2, 6), 0b00010100_u8);
644}
645
646#[test]
647fn test_remove() {
648 assert_eq!(0b11110000_u8.remove(0b00001111_u8), 0b11110000_u8);
649 assert_eq!(0b11110000_u8.remove(0b00111100_u8), 0b11000000_u8);
650}
651
652#[test]
653fn test_bin_repr() {
654 assert_eq!(0b11110000_u8.bit_repr(), "11110000");
655}