use alloc::vec;
use alloc::vec::Vec;
use core::fmt::Debug;
#[derive(Debug)]
pub struct SlotIndex<'a> {
pub keys: &'a [&'a str],
}
impl<'a> SlotIndex<'a> {
pub fn slot_count(&self) -> usize {
self.keys.len()
}
pub fn to_slot(&self, key: &str) -> usize {
for (i, k) in self.keys.iter().enumerate() {
if *k == key {
return i;
}
}
panic!("Unknown key: {}", key)
}
}
pub struct SlotMap<T>
where
T: 'static + Debug + Clone,
{
pub slots: Vec<Option<T>>,
}
impl<T> SlotMap<T>
where
T: 'static + Debug + Clone,
{
pub fn new_with_capacity(capacity: usize) -> Self {
Self {
slots: vec![None; capacity],
}
}
pub fn clear(&mut self) {
self.slots.fill(None);
}
pub fn get(&self, index: usize) -> Option<&T> {
self.slots.get(index).and_then(|x| x.as_ref())
}
pub fn set(&mut self, index: usize, value: T) {
self.slots[index] = Some(value);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_slot_index() {
static INDEX: SlotIndex<'static> = SlotIndex {
keys: &["a", "b", "c"],
};
assert_eq!(INDEX.slot_count(), 3);
assert_eq!(INDEX.to_slot("a"), 0);
assert_eq!(INDEX.to_slot("b"), 1);
assert_eq!(INDEX.to_slot("c"), 2);
}
#[test]
#[should_panic(expected = "Unknown key: d")]
fn test_slot_index_panic() {
static INDEX: SlotIndex<'static> = SlotIndex {
keys: &["a", "b", "c"],
};
INDEX.to_slot("d");
}
#[test]
fn test_slot_map() {
let mut map = SlotMap::new_with_capacity(3);
map.set(0, 1);
assert_eq!(map.get(0), Some(&1));
assert_eq!(map.get(1), None);
map.clear();
assert_eq!(map.get(0), None);
}
}