Switch to a standardized tree library in the game tree and depth tree #236

Merged
savanni merged 11 commits from savanni/improved-tree-structure into main 2024-05-01 02:26:47 +00:00
5 changed files with 33 additions and 22 deletions
Showing only changes of commit 8b7add37c1 - Show all commits

23
Cargo.lock generated
View File

@ -2503,6 +2503,16 @@ dependencies = [
"version_check 0.9.4",
]
[[package]]
name = "nary_tree"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb86edb8951cb3852cbb33ef558650e9f18c9d2e7fd79a6849c984a3825719c7"
dependencies = [
"slab",
"snowflake",
]
[[package]]
name = "native-tls"
version = "0.2.11"
@ -2705,10 +2715,10 @@ dependencies = [
"config-derive",
"cool_asserts",
"grid",
"nary_tree",
"serde 1.0.193",
"serde_json",
"sgf",
"slab_tree",
"thiserror",
"uuid 0.8.2",
]
@ -3686,9 +3696,9 @@ version = "0.1.0"
dependencies = [
"chrono",
"cool_asserts",
"nary_tree",
"nom",
"serde 1.0.193",
"slab_tree",
"thiserror",
"typeshare",
"uuid 0.8.2",
@ -3762,15 +3772,6 @@ dependencies = [
"autocfg 1.1.0",
]
[[package]]
name = "slab_tree"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9e8b45abe77b7cab703054a11973cffe164c82c5ff5e211ae5a73af5e42e39f"
dependencies = [
"snowflake",
]
[[package]]
name = "smallvec"
version = "1.11.2"

View File

@ -14,7 +14,7 @@ sgf = { path = "../../sgf" }
grid = { version = "0.9" }
serde_json = { version = "1" }
serde = { version = "1", features = [ "derive" ] }
slab_tree = { version = "0.3" }
nary_tree = { version = "0.4" }
thiserror = { version = "1" }
uuid = { version = "0.8", features = ["v4", "serde"] }

View File

@ -242,10 +242,10 @@ pub struct Tree<T> {
}
*/
pub struct DepthTree(slab_tree::Tree<SizeNode>);
pub struct DepthTree(nary_tree::Tree<SizeNode>);
impl Deref for DepthTree {
type Target = slab_tree::Tree<SizeNode>;
type Target = nary_tree::Tree<SizeNode>;
fn deref(&self) -> &Self::Target {
&self.0
@ -254,8 +254,8 @@ impl Deref for DepthTree {
#[derive(Debug)]
pub struct SizeNode {
node_id: slab_tree::NodeId,
parent: Option<slab_tree::NodeId>,
node_id: nary_tree::NodeId,
parent: Option<nary_tree::NodeId>,
depth: usize,
width: usize,
}
@ -275,7 +275,7 @@ impl DepthTree {
// so I want to eliminate the "Tree" above and keep using the slab tree. I think I should be able
// to build these Node objects without needing a custom data structure.
fn new() -> Self {
Self(slab_tree::Tree::new())
Self(nary_tree::Tree::new())
/*
Tree {
nodes: vec![Node {
@ -415,8 +415,8 @@ impl<'a> From<&'a GameTree> for DepthTree {
// The id_map indexes from the source tree to the destination tree. Reverse
// indexing is accomplished by looking at the node_id in a node in the destination
// tree.
let mut id_map: HashMap<slab_tree::NodeId, slab_tree::NodeId> = HashMap::new();
let mut tree = slab_tree::Tree::new();
let mut id_map: HashMap<nary_tree::NodeId, nary_tree::NodeId> = HashMap::new();
let mut tree = nary_tree::Tree::new();
let mut iter = source_root_node.traverse_pre_order();
let _ = iter.next().unwrap(); // we already know that the first element to be
@ -522,7 +522,7 @@ impl<'a> From<&'a GameNode> for Tree<Uuid> {
pub struct BFSIter<'a, T> {
tree: &'a DepthTree,
queue: VecDeque<slab_tree::NodeRef<'a, T>>,
queue: VecDeque<nary_tree::NodeRef<'a, T>>,
}
impl<'a, T> Iterator for BFSIter<'a, T> {

View File

@ -9,7 +9,7 @@ edition = "2021"
chrono = { version = "0.4", features = [ "serde" ] }
nom = { version = "7" }
serde = { version = "1", features = [ "derive" ] }
slab_tree = { version = "0.3" }
nary_tree = { version = "0.4" }
thiserror = { version = "1"}
typeshare = { version = "1" }
uuid = { version = "0.8", features = ["v4", "serde"] }

View File

@ -3,9 +3,10 @@ use crate::{
Color, Date, GameResult, GameType,
};
use serde::{Deserialize, Serialize};
use slab_tree::{NodeId, NodeMut, NodeRef, Tree};
use nary_tree::{NodeId, NodeMut, NodeRef, Tree};
use std::{
collections::{HashMap, HashSet, VecDeque},
fmt,
fmt::Debug,
ops::{Deref, DerefMut},
time::Duration,
@ -425,6 +426,15 @@ pub enum GameNode {
SetupNode(SetupNode),
}
impl fmt::Display for GameNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
GameNode::MoveNode(_) => write!(f, "MoveNode"),
GameNode::SetupNode(_) => write!(f, "SetupNode"),
}
}
}
impl GameNode {
pub fn id(&self) -> Uuid {
match self {