diff --git a/src/logic/future_moves.rs b/src/logic/future_moves.rs index 72548d1..3550a29 100644 --- a/src/logic/future_moves.rs +++ b/src/logic/future_moves.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use indicatif::{ProgressIterator, ProgressStyle}; use crate::{ @@ -167,14 +169,21 @@ impl FutureMoves { // PERF! pre-organize all indexes based on what depth they're at // previously, I did a lookup map based on if a node was visited, still resulted in a full // O(n) iteration each depth - let mut by_depth: Vec> = (0..=self.max_depth + 2).map(|_| Vec::new()).collect(); + let mut by_depth: HashMap> = HashMap::new(); for idx in indexes { let depth = self.depth_of(idx); // -1 because `depth_of` is one-indexed - by_depth[depth - 1].push(idx); + if let Some(got) = by_depth.get_mut(&depth) { + got.push(idx); + } else { + by_depth.insert(depth, vec![idx]); + } } - for (depth, nodes) in by_depth.into_iter().enumerate().rev() { + let mut by_depth_vec: Vec<(usize, Vec)> = by_depth.into_iter().collect(); + by_depth_vec.sort_by_key(|x| x.0); + + for (depth, nodes) in by_depth_vec { for idx in nodes { // TODO! impl dynamic sorting based on children's states, maybe it propegates // upwards using the `parent` field @@ -192,7 +201,7 @@ impl FutureMoves { .rev() // rev then reverse so we get an index starting from the back .enumerate() // since children are sorted by value, we should weight the first one more - .map(|(i, &child)| self.arena[child].value * (i as i128 + 1)) + .map(|(i, &child)| self.arena[child].value * (i as i128)) .sum::(); self.arena[idx].value =