| Crates.io | plain_trie |
| lib.rs | plain_trie |
| version | 8.0.2 |
| created_at | 2024-07-21 21:49:26.111896+00 |
| updated_at | 2025-05-11 19:13:45.898358+00 |
| description | Classic trie implementation capable of mapping any T to char iterator. |
| homepage | |
| repository | https://github.com/deep-outcome/tries/tree/main/trie |
| max_upload_size | |
| id | 1310600 |
| size | 71,460 |
impl Iterator<Item = char> type as keylet mut trie = Trie::new();
let key = || "oomph".chars();
let val = 333;
_ = trie.ins(key(), val);
match trie.acq(key()) {
AcqRes::Ok(v) => assert_eq!(&val, v),
_ => panic!("Expected AcqRes::Ok(_).")
}
let val = 444;
_ = trie.ins(key(), val);
match trie.acq(key()) {
AcqRes::Ok(v) => assert_eq!(&val, v),
_ => panic!("Expected AcqRes::Ok(_).")
}
let catch = catch_unwind(move|| _ = trie.ins("A".chars(), 0));
assert!(catch.is_err());
use Trie::new_with in conjunction with implementation for char-index conversion function and apposite alphabet length
example bellow shows sample implementation for alphabet extended with capital letters
use plain_trie::{AcqRes, RemRes, Trie};
const ALPHABET_LEN: u32 = 52;
fn ix(c: char) -> usize {
let big_a = u32::from('A');
let big_z = u32::from('Z');
let sma_a = u32::from('a');
let sma_z = u32::from('z');
let cod_poi = u32::from(c);
(match cod_poi {
c if c >= big_a && c <= big_z => c - big_a,
c if c >= sma_a && c <= sma_z => c - sma_a + ALPHABET_LEN / 2,
_ => {
panic!("Index conversion impossible.")
}
}) as usize
}
#[test]
fn test() {
let mut trie = Trie::new_with(ix, None, ALPHABET_LEN as usize);
let kv_1 = ("AZ", 1);
let kv_2 = ("az", 2);
for kv in [kv_1, kv_2] {
let res = trie.ins(kv.0.chars(), kv.1);
assert_eq!(true, res.is_ok());
}
for kv in [kv_1, kv_2] {
let res = trie.acq(kv.0.chars());
assert_eq!(AcqRes::Ok(&kv.1), res);
}
for kv in [kv_1, kv_2] {
let res = trie.rem(kv.0.chars());
assert_eq!(RemRes::Ok(kv.1), res);
}
}