add LetterSet::{to,from}_index
This commit is contained in:
parent
4dd2ee21fc
commit
211368ee45
1 changed files with 32 additions and 0 deletions
|
|
@ -64,6 +64,17 @@ impl LetterSet {
|
||||||
self.0 == 0
|
self.0 == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn to_index(self) -> usize {
|
||||||
|
self.0 as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_index(index: usize) -> Option<Self> {
|
||||||
|
u32::try_from(index)
|
||||||
|
.ok()
|
||||||
|
.filter(|n| *n <= Self::FULL.0)
|
||||||
|
.map(Self)
|
||||||
|
}
|
||||||
|
|
||||||
const fn letter_index(letter: u8) -> Option<u8> {
|
const fn letter_index(letter: u8) -> Option<u8> {
|
||||||
if letter.is_ascii_alphabetic() {
|
if letter.is_ascii_alphabetic() {
|
||||||
let lower = letter.to_ascii_lowercase();
|
let lower = letter.to_ascii_lowercase();
|
||||||
|
|
@ -203,4 +214,25 @@ mod test {
|
||||||
assert!(!AB.is_empty());
|
assert!(!AB.is_empty());
|
||||||
assert!(!BC.is_empty());
|
assert!(!BC.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn to_index() {
|
||||||
|
assert_eq!(LetterSet::EMPTY.to_index(), 0);
|
||||||
|
assert_eq!(A.to_index(), 1);
|
||||||
|
assert_eq!(B.to_index(), 2);
|
||||||
|
assert_eq!(AB.to_index(), 3);
|
||||||
|
assert_eq!(C.to_index(), 4);
|
||||||
|
assert_eq!(AC.to_index(), 5);
|
||||||
|
assert_eq!(BC.to_index(), 6);
|
||||||
|
assert_eq!(ABC.to_index(), 7);
|
||||||
|
assert_eq!(LetterSet::FULL.to_index(), (1 << 26) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_index() {
|
||||||
|
for ls in [LetterSet::EMPTY, A, B, C, AB, AC, BC, ABC, LetterSet::EMPTY] {
|
||||||
|
assert_eq!(LetterSet::from_index(ls.to_index()), Some(ls));
|
||||||
|
}
|
||||||
|
assert_eq!(LetterSet::from_index(1 << 26), None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue