use core::ops::Index;
use super::SkipMap;
use crate::{
comparator::{Comparator, ComparatorKey},
level_generator::LevelGenerator,
};
impl<K, V, C, G: LevelGenerator, const N: usize> Index<&K> for SkipMap<K, V, N, C, G>
where
C: Comparator<K> + ComparatorKey<K, K>,
{
type Output = V;
#[expect(
clippy::panic,
reason = "panicking on absent key is the documented behaviour of Index"
)]
#[expect(
clippy::renamed_function_params,
reason = "using `key` instead of `index` for the parameter name is clearer in context"
)]
#[inline]
fn index(&self, key: &K) -> &V {
self.get(key)
.unwrap_or_else(|| panic!("key not found in SkipMap"))
}
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::super::SkipMap;
#[test]
#[expect(
clippy::indexing_slicing,
reason = "explicitly testing Index panics on present keys"
)]
fn index_present_key() {
let mut map = SkipMap::<i32, &str>::new();
map.insert(1, "a");
map.insert(2, "b");
assert_eq!(map[&1], "a");
assert_eq!(map[&2], "b");
}
#[test]
#[should_panic(expected = "key not found")]
#[expect(
clippy::indexing_slicing,
reason = "testing that Index panics on absent keys"
)]
fn index_absent_key_panics() {
let map = SkipMap::<i32, &str>::new();
let _: &str = map[&99];
}
#[test]
#[expect(
clippy::indexing_slicing,
reason = "explicitly testing Index on present keys"
)]
fn index_returns_correct_value() {
let mut map = SkipMap::<&str, i32>::new();
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);
assert_eq!(map[&"one"], 1);
assert_eq!(map[&"two"], 2);
assert_eq!(map[&"three"], 3);
}
}