cap hashmap at 32M entries

This commit is contained in:
2026-05-19 16:58:46 -07:00
parent bc2d1b126e
commit 418e422f12
2 changed files with 13 additions and 5 deletions
+12 -4
View File
@@ -321,6 +321,9 @@ impl<S> SessionStats<S> {
} }
} }
#[derive(Debug)]
pub struct Oom;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Session<G: Game> { pub struct Session<G: Game> {
stats: SessionStats<G::Stats>, stats: SessionStats<G::Stats>,
@@ -402,10 +405,15 @@ where
pub fn is_win(&self) -> bool { pub fn is_win(&self) -> bool {
self.state.is_win() self.state.is_win()
} }
pub fn is_winnable(&self) -> Option<Vec<StateSnapshot<G>>> { pub fn is_winnable(&self) -> Result<Option<Vec<StateSnapshot<G>>>, Oom> {
let mut state_moves = std::collections::HashMap::new(); const HUGE_CAP: usize = 1 << 25;
let mut state_moves = std::collections::HashMap::with_capacity(HUGE_CAP);
let mut state = self.clone(); let mut state = self.clone();
while !state.is_win() { while !state.is_win() {
if HUGE_CAP <= state_moves.len() {
return Err(Oom);
}
// Continue existing iterator if it exists // Continue existing iterator if it exists
let it = state_moves let it = state_moves
.entry(state.state().clone()) .entry(state.state().clone())
@@ -419,7 +427,7 @@ where
// No more moves. If we can't undo we're done // No more moves. If we can't undo we're done
if state.history().is_empty() { if state.history().is_empty() {
return None; return Ok(None);
} else { } else {
state.undo(); state.undo();
} }
@@ -451,7 +459,7 @@ where
} }
} }
Some(state.state.history) Ok(Some(state.state.history))
} }
} }
impl<G: Game> Game for SessionState<G> impl<G: Game> Game for SessionState<G>
+1 -1
View File
@@ -3,7 +3,7 @@ use klondike::Klondike;
#[test] #[test]
fn test_is_winnable() { fn test_is_winnable() {
// is winnable // is winnable
let is_winnable = Session::new_default(Klondike::with_seed(124)).is_winnable(); let is_winnable = Session::new_default(Klondike::with_seed(0)).is_winnable().unwrap();
if let Some(win_moves) = is_winnable { if let Some(win_moves) = is_winnable {
// for (i, ins) in win_moves.into_iter().enumerate() { // for (i, ins) in win_moves.into_iter().enumerate() {
// println!("{i} = {:?}", ins.instruction()); // println!("{i} = {:?}", ins.instruction());