diff --git a/Cargo.toml b/Cargo.toml index fc1a739..0353088 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,3 +42,7 @@ criterion = { version = "0.5", features = [ "html_reports" ] } [[bench]] name = "future_children" harness = false + +[lints.rust] +# fix weird warnings about `test` not being expected +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(test)'] } diff --git a/src/logic/future_moves.rs b/src/logic/future_moves.rs index cba8ba5..b21f4dc 100644 --- a/src/logic/future_moves.rs +++ b/src/logic/future_moves.rs @@ -59,10 +59,38 @@ impl FutureMoves { self.arena.len() } + fn leaf_moves(&self) -> Vec { + let mut indexes = (0..self.arena.len()) + // we want to select all nodes that don't have children, or are lazy (need to maybe be regenerated) + .filter(|&idx| { + let got = &self.arena[idx]; + !got.is_trimmed && !got.tried_children && got.winner == Winner::None + }) + .filter(|&idx| self.is_connected_to_root(idx)) + .collect::>(); + + // we want to try and make the tree even in depth + // by first generating children for younger moves + indexes.sort_by_key(|&x| self.depth_of(x)); + indexes + } + + /// Find the current depth of the arena by + /// looking at leaf moves and finding the smallest value + fn determine_current_depth(&self) -> Option { + // leaf_moves is sorted from min to max depth + self.leaf_moves().first().map(|&i| self.depth_of(i)) + } + /// Generate children for all children of `nodes` /// only `pub` for the sake of benchmarking pub fn extend_layers(&mut self) { - for _ in (self.current_depth + 1)..=self.config.max_depth { + // recover from partial tree extention + if let Some(current_depth) = self.determine_current_depth() { + self.current_depth = current_depth; + } + + for _ in self.current_depth..self.config.max_depth { let pstyle_inner = if cfg!(test) { "" } else { @@ -73,13 +101,10 @@ impl FutureMoves { ) }; - let cf = (0..self.arena.len()) - // we want to select all nodes that don't have children, or are lazy (need to maybe be regenerated) - .filter(|&idx| { - let got = &self.arena[idx]; - !got.is_trimmed && !got.tried_children && got.winner == Winner::None - }) - .filter(|&idx| self.is_connected_to_root(idx)) + let cf = self + .leaf_moves() + .into_iter() + .filter(|&i| self.depth_of(i) == self.current_depth) .collect::>() .into_iter() .progress_with_style(ProgressStyle::with_template(pstyle_inner).unwrap()) @@ -198,7 +223,7 @@ impl FutureMoves { let by_depth_vec = self.by_depth(indexes); // reversed so we build up the value of the closest (in time) moves from the future - for (depth, nodes) in by_depth_vec.into_iter().rev() { + for (_, nodes) in by_depth_vec.into_iter().rev() { for idx in nodes { let children_value = self.arena[idx] .children