From fba9a90cfc6aca2a26cce72f55fb261789139781 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Fri, 16 Oct 2026 14:01:01 +0800 Subject: new start Now I tested and discussed with claude, and had a working version of this project. The rest is to port the claude version to my own writings. So this serves as the starting point before that attempt. In the new attempt the design is better, and a number of bugs were found and supposedly fixed, so let us see how this goes at the end. --- forest/examples/query_forest.rs | 117 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 forest/examples/query_forest.rs (limited to 'forest/examples/query_forest.rs') diff --git a/forest/examples/query_forest.rs b/forest/examples/query_forest.rs new file mode 100644 index 0000000..fe70ca8 --- /dev/null +++ b/forest/examples/query_forest.rs @@ -0,0 +1,117 @@ +fn main() -> Result<(), Box> { + let mut file = std::fs::OpenOptions::new().read(true).open("forest.rep")?; + + use std::io::Read; + + let mut source: Vec = Vec::new(); + + file.read_to_end(&mut source)?; + + use forest::bytes::BytesForest; + + #[allow(unused_imports)] + use forest::iter::query::{preprocess_forest, QForest}; + + use graph::Graph; + + let bytes_forest = BytesForest::try_from(source)?; + + dbg!( + bytes_forest.nodes_len(), + bytes_forest.children_of(0)?.collect::>() + ); + + #[cfg(feature = "test-print-viz")] + bytes_forest.print_viz("test forest.gv")?; + + let preprocessed = preprocess_forest(bytes_forest, 0)?; + + dbg!(&preprocessed); + + use std::io::{BufRead, Write}; + + println!("Input a query:"); + std::io::stdout().flush()?; + + #[derive(Debug, Default, Copy, Clone)] + enum QueryType { + SubtreeRange(usize), + #[default] + Switches, + DFSNext(usize), + OrderToDFS(usize), + DFSToOrder(usize), + } + + for line in std::io::stdin().lock().lines() { + let line = line?; + + if line == "q" { + break; + } + + let qtype = if line.starts_with('s') { + 1u8 + } else if line.starts_with('b') { + 2u8 + } else if line.starts_with('o') { + 3u8 + } else if line.starts_with('d') { + 4u8 + } else { + 0u8 + }; + + let type_str: &str; + + let q = if line.starts_with('s') || line.starts_with('o') || line.starts_with('d') { + if let Ok(n) = line[1..].parse::() { + n + } else { + 0usize + } + } else if let Ok(n) = line.parse::() { + n + } else { + continue; + }; + + let query: QueryType = match qtype { + 0 => QueryType::DFSNext(q), + 1 => QueryType::SubtreeRange(q), + 2 => QueryType::default(), + 3 => QueryType::OrderToDFS(q), + _ => QueryType::DFSToOrder(q), + }; + + let ans = match query { + QueryType::SubtreeRange(n) => { + type_str = "subtree range"; + format!("{:?}", preprocessed.subtree_ranges.get(n)) + } + QueryType::Switches => { + type_str = "branch switches"; + format!("{:?}", preprocessed.branch_switches) + } + QueryType::DFSNext(n) => { + type_str = "dfs"; + format!("{:?}", preprocessed.dfs_next.get(n).copied()) + } + QueryType::OrderToDFS(n) => { + type_str = "order to dfs"; + format!("{:?}", preprocessed.translate_to_dfs(n)) + } + QueryType::DFSToOrder(n) => { + type_str = "dfs to order"; + format!("{:?}", preprocessed.translate_from_dfs(n)) + } + }; + + println!("answer for {type_str} = {ans}"); + + println!("Input a query:"); + std::io::stdout().flush()?; + } + + Ok(()) +} -- cgit v1.2.3-18-g5258