From d5faf6e7a2ba168a2033a032cd9cb4c352dea9d3 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 30 Apr 2024 21:59:51 -0400 Subject: [PATCH] Switch from slab_tree to nary_tree --- Cargo.lock | 23 ++++++++++++----------- otg/core/Cargo.toml | 2 +- otg/core/src/types.rs | 16 ++++++++-------- sgf/Cargo.toml | 2 +- sgf/src/game.rs | 12 +++++++++++- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 93efbef..4118f22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/otg/core/Cargo.toml b/otg/core/Cargo.toml index a6b7215..42af9aa 100644 --- a/otg/core/Cargo.toml +++ b/otg/core/Cargo.toml @@ -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"] } diff --git a/otg/core/src/types.rs b/otg/core/src/types.rs index c48cc7f..7ce672b 100644 --- a/otg/core/src/types.rs +++ b/otg/core/src/types.rs @@ -242,10 +242,10 @@ pub struct Tree { } */ -pub struct DepthTree(slab_tree::Tree); +pub struct DepthTree(nary_tree::Tree); impl Deref for DepthTree { - type Target = slab_tree::Tree; + type Target = nary_tree::Tree; 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, + node_id: nary_tree::NodeId, + parent: Option, 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 = HashMap::new(); - let mut tree = slab_tree::Tree::new(); + let mut id_map: HashMap = 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 { pub struct BFSIter<'a, T> { tree: &'a DepthTree, - queue: VecDeque>, + queue: VecDeque>, } impl<'a, T> Iterator for BFSIter<'a, T> { diff --git a/sgf/Cargo.toml b/sgf/Cargo.toml index b1fda71..0b83c35 100644 --- a/sgf/Cargo.toml +++ b/sgf/Cargo.toml @@ -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"] } diff --git a/sgf/src/game.rs b/sgf/src/game.rs index ae11f54..d21812a 100644 --- a/sgf/src/game.rs +++ b/sgf/src/game.rs @@ -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 {