refactor(api): rename IndexMap to KeyTable

The former name collided with the popular indexmap crate. KeyTable
lives in its own module. Public API unchanged beyond the rename.

Part of T2 of docs/superpowers/specs/2026-04-23-trueskill-engine-redesign-design.md.
This commit is contained in:
2026-04-24 10:34:14 +02:00
parent 948a7a684b
commit c69fe4e67c
6 changed files with 93 additions and 75 deletions

View File

@@ -1,9 +1,6 @@
use std::{
borrow::{Borrow, ToOwned},
cmp::Reverse,
collections::HashMap,
f64::consts::{FRAC_1_SQRT_2, FRAC_2_SQRT_PI, SQRT_2},
hash::Hash,
};
pub mod agent;
@@ -17,6 +14,7 @@ pub(crate) mod factor;
mod game;
pub mod gaussian;
mod history;
pub mod key_table;
mod matrix;
pub mod player;
pub(crate) mod schedule;
@@ -27,6 +25,7 @@ pub use error::InferenceError;
pub use game::Game;
pub use gaussian::Gaussian;
pub use history::History;
pub use key_table::KeyTable;
use matrix::Matrix;
pub use player::Player;
pub use schedule::ScheduleReport;
@@ -54,59 +53,6 @@ impl From<usize> for Index {
}
}
pub struct IndexMap<K>(HashMap<K, Index>);
impl<K> IndexMap<K>
where
K: Eq + Hash,
{
pub fn new() -> Self {
Self(HashMap::new())
}
pub fn get<Q: ?Sized + Hash + Eq + ToOwned<Owned = K>>(&self, k: &Q) -> Option<Index>
where
K: Borrow<Q>,
{
self.0.get(k).cloned()
}
pub fn get_or_create<Q: ?Sized + Hash + Eq + ToOwned<Owned = K>>(&mut self, k: &Q) -> Index
where
K: Borrow<Q>,
{
if let Some(idx) = self.0.get(k) {
*idx
} else {
let idx = Index::from(self.0.len());
self.0.insert(k.to_owned(), idx);
idx
}
}
pub fn key(&self, idx: Index) -> Option<&K> {
self.0
.iter()
.find(|&(_, value)| *value == idx)
.map(|(key, _)| key)
}
pub fn keys(&self) -> impl Iterator<Item = &K> {
self.0.keys()
}
}
impl<K> Default for IndexMap<K>
where
K: Eq + Hash,
{
fn default() -> Self {
IndexMap::new()
}
}
fn erfc(x: f64) -> f64 {
let z = x.abs();
let t = 1.0 / (1.0 + z / 2.0);