Trait bittersweet::bitline::Bitline
source · pub trait Bitline {
Show 25 methods
// Required methods
fn as_empty() -> Self;
fn as_full() -> Self;
fn by_range(begin: usize, end: usize) -> Self;
fn is_empty(&self) -> bool;
fn is_not_empty(&self) -> bool;
fn is_full(&self) -> bool;
fn is_not_full(&self) -> bool;
fn first_index(&self) -> Option<usize>;
fn last_index(&self) -> Option<usize>;
fn radius(&self, n: usize) -> Self;
fn around(&self, n: usize) -> Self;
fn with_around(&self, n: usize) -> Self;
fn first_bit(&self) -> Self;
fn last_bit(&self) -> Self;
fn first_bits(&self) -> Self;
fn last_bits(&self) -> Self;
fn filled_first_bit_to_last_bit(&self) -> Self;
fn bytes_length() -> usize;
fn length() -> usize;
fn num_bits(&self) -> usize;
fn includes(&self, other: Self) -> bool;
fn overlaps(&self, other: Self) -> bool;
fn range(&self, begin: usize, end: usize) -> Self;
fn remove(&self, other: Self) -> Self;
fn bit_repr(&self) -> String;
}
Required Methods§
sourcefn as_empty() -> Self
fn as_empty() -> Self
Return the bits all set to 0
Examples
use bittersweet::bitline::{Bitline, Bitline8};
assert_eq!(Bitline8::as_empty(), 0b00000000);
sourcefn as_full() -> Self
fn as_full() -> Self
Return the bits all set to 1
Examples
use bittersweet::bitline::{Bitline, Bitline8};
assert_eq!(Bitline8::as_full(), 0b11111111);
sourcefn by_range(begin: usize, end: usize) -> Self
fn by_range(begin: usize, end: usize) -> Self
Return the bits standing in the given range.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
assert_eq!(Bitline8::by_range(2, 5), 0b00111000_u8);
sourcefn is_empty(&self) -> bool
fn is_empty(&self) -> bool
Return true if the bit is filled with zero.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
assert!(!0b00111000_u8.is_empty());
assert!(0b00000000_u8.is_empty());
sourcefn is_not_empty(&self) -> bool
fn is_not_empty(&self) -> bool
Return true if the bit is not filled with zero.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
assert!(0b00111000_u8.is_not_empty());
assert!(!0b00000000_u8.is_not_empty());
sourcefn is_full(&self) -> bool
fn is_full(&self) -> bool
Return true if the bit is filled with one.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
assert!((0b11111111 as Bitline8).is_full());
assert!(!(0b01111111 as Bitline8).is_full());
sourcefn is_not_full(&self) -> bool
fn is_not_full(&self) -> bool
Return true if the bit is not filled with one.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
assert!((0b01111111 as Bitline8).is_not_full());
assert!(!(0b11111111 as Bitline8).is_not_full());
sourcefn first_index(&self) -> Option<usize>
fn first_index(&self) -> Option<usize>
Return the first bit index that is set to one. If there is no bit set to one, return None.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
assert_eq!((0b00111000 as Bitline8).first_index(), Some(2));
assert_eq!((0b00000000 as Bitline8).first_index(), None);
sourcefn last_index(&self) -> Option<usize>
fn last_index(&self) -> Option<usize>
Return the last bit index that is set to one. If there is no bit set to one, return None.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
assert_eq!((0b00111000 as Bitline8).last_index(), Some(4));
assert_eq!((0b00000000 as Bitline8).last_index(), None);
sourcefn radius(&self, n: usize) -> Self
fn radius(&self, n: usize) -> Self
Return the bits standing in n distance from the original starting bit. If there is no bit set to one, return None.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b00001000 as Bitline8;
assert_eq!(bitline.radius(0), 0b00000000);
assert_eq!(bitline.radius(1), 0b00010100);
assert_eq!(bitline.radius(2), 0b00100010);
let bitline = 0b00000000 as Bitline8;
assert_eq!(bitline.radius(0), 0b00000000);
assert_eq!(bitline.radius(1), 0b00000000);
assert_eq!(bitline.radius(2), 0b00000000);
let bitline = 0b00100100 as Bitline8;
assert_eq!(bitline.radius(0), 0b00000000);
assert_eq!(bitline.radius(1), 0b01011010);
assert_eq!(bitline.radius(2), 0b10011001);
sourcefn around(&self, n: usize) -> Self
fn around(&self, n: usize) -> Self
Return all bits standing between n distance from the standing bits (without original standing bits).
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b00001000 as Bitline8;
assert_eq!(bitline.around(0), 0b00000000);
assert_eq!(bitline.around(1), 0b00010100);
assert_eq!(bitline.around(2), 0b00110110);
let bitline = 0b00000000 as Bitline8;
assert_eq!(bitline.around(0), 0b00000000);
assert_eq!(bitline.around(1), 0b00000000);
assert_eq!(bitline.around(2), 0b00000000);
sourcefn with_around(&self, n: usize) -> Self
fn with_around(&self, n: usize) -> Self
Return all bits standing between n distance from the standing bits (with original standing bits).
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b00001000 as Bitline8;
assert_eq!(bitline.with_around(0), 0b00001000);
assert_eq!(bitline.with_around(1), 0b00011100);
assert_eq!(bitline.with_around(2), 0b00111110);
let bitline = 0b00000000 as Bitline8;
assert_eq!(bitline.with_around(0), 0b00000000);
assert_eq!(bitline.with_around(1), 0b00000000);
assert_eq!(bitline.with_around(2), 0b00000000);
sourcefn first_bit(&self) -> Self
fn first_bit(&self) -> Self
Return the first bit from the most significant bit. (last bit from the least significant bit)
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b01001000_u8;
assert_eq!(bitline.first_bit(), 0b01000000_u8);
let bitline = 0b00000000_u8;
assert_eq!(bitline.first_bit(), 0b00000000_u8);
sourcefn last_bit(&self) -> Self
fn last_bit(&self) -> Self
Return the last bit from the most significant bit. (first bit from the least significant bit)
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b01001000_u8;
assert_eq!(bitline.last_bit(), 0b00001000_u8);
let bitline = 0b00000000_u8;
assert_eq!(bitline.last_bit(), 0b00000000_u8);
sourcefn first_bits(&self) -> Self
fn first_bits(&self) -> Self
Return the first bits of each consecutive bits.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b01101100_u8;
assert_eq!(bitline.first_bits(), 0b01001000_u8);
let bitline = 0b00000000_u8;
assert_eq!(bitline.first_bits(), 0b00000000_u8);
sourcefn last_bits(&self) -> Self
fn last_bits(&self) -> Self
Return the last bits of each consecutive bits.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b01101100_u8;
assert_eq!(bitline.last_bits(), 0b00100100_u8);
let bitline = 0b00000000_u8;
assert_eq!(bitline.last_bits(), 0b00000000_u8);
sourcefn filled_first_bit_to_last_bit(&self) -> Self
fn filled_first_bit_to_last_bit(&self) -> Self
Return the bits filled from the first bit to the last bit.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b01000100_u8;
assert_eq!(bitline.filled_first_bit_to_last_bit(), 0b01111100_u8);
let bitline = 0b00000000_u8;
assert_eq!(bitline.filled_first_bit_to_last_bit(), 0b00000000_u8);
sourcefn bytes_length() -> usize
fn bytes_length() -> usize
Return the bytes size of the bitline.
Examples
use bittersweet::bitline::{Bitline, Bitline8, Bitline16, Bitline32, Bitline64};
assert_eq!(Bitline8::bytes_length(), 1);
assert_eq!(Bitline16::bytes_length(), 2);
assert_eq!(Bitline32::bytes_length(), 4);
assert_eq!(Bitline64::bytes_length(), 8);
sourcefn length() -> usize
fn length() -> usize
Return the bits size of the bitline.
Examples
use bittersweet::bitline::{Bitline, Bitline8, Bitline16, Bitline32, Bitline64};
assert_eq!(Bitline8::length(), 8);
assert_eq!(Bitline16::length(), 16);
assert_eq!(Bitline32::length(), 32);
assert_eq!(Bitline64::length(), 64);
sourcefn num_bits(&self) -> usize
fn num_bits(&self) -> usize
Return the bits standing in the given range. (bit count)
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b01101100_u8;
assert_eq!(bitline.num_bits(), 4);
let bitline = 0b00000000_u8;
assert_eq!(bitline.num_bits(), 0);
sourcefn includes(&self, other: Self) -> bool
fn includes(&self, other: Self) -> bool
Return true if every bits are standing in the given standing bits. empty bitlines are always included. (like a empty set in a set)
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b01101100_u8;
assert!(bitline.includes(0b01100000_u8));
assert!(!bitline.includes(0b01100001_u8));
assert!(bitline.includes(0b00000000_u8));
let bitline = 0b00000000_u8;
assert!(bitline.includes(0b00000000_u8));
assert!(!bitline.includes(0b00000001_u8));
sourcefn overlaps(&self, other: Self) -> bool
fn overlaps(&self, other: Self) -> bool
Return true if some bits are standing in the given standing bits.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b01101100_u8;
assert!(bitline.overlaps(0b01100000_u8));
assert!(!bitline.overlaps(0b00000001_u8));
assert!(!bitline.overlaps(0b00000000_u8));
sourcefn range(&self, begin: usize, end: usize) -> Self
fn range(&self, begin: usize, end: usize) -> Self
Return the standing bits by the given range.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b01101100_u8;
assert_eq!(bitline.range(0, 4), 0b01100000_u8);
assert_eq!(bitline.range(4, 8), 0b00001100_u8);
sourcefn remove(&self, other: Self) -> Self
fn remove(&self, other: Self) -> Self
Return the standing bits not included by the given range.
Examples
use bittersweet::bitline::{Bitline, Bitline8};
let bitline = 0b01101100_u8;
assert_eq!(bitline.remove(0b01100000_u8), 0b00001100_u8);
assert_eq!(bitline.remove(0b00001100_u8), 0b01100000_u8);