Skip to content
Snippets Groups Projects
Unverified Commit 1efe51c5 authored by Cheuk Pui Lam's avatar Cheuk Pui Lam
Browse files

feat(client-brc): Finished best cover function

parent eacf5b96
No related branches found
No related tags found
No related merge requests found
pub mod brc_tree {
#![allow(dead_code)]
use std::{io::Write, path::Path};
use std::{io::Write, path::Path, vec};
use indexmap::IndexMap;
......@@ -115,6 +115,38 @@ pub mod brc_tree {
}
Ok(())
}
pub fn possible_covers(&self, min: isize, max: isize) -> Vec<Subtree> {
match &self.0 {
Some(node) => {
if node.min > max || node.max < min {
return vec![];
}
if min <= node.min && max >= node.max {
return vec![self.clone()];
}
let mut left = node.left.possible_covers(min, max);
let right = node.right.possible_covers(min, max);
left.extend(right);
left
}
None => vec![],
}
}
pub fn range(&self) -> (isize, isize) {
match &self.0 {
Some(node) => (node.min, node.max),
None => (0, 0),
}
}
}
#[test]
fn test_possible_covers() {
let tree = parse_from_file(std::path::Path::new("../brc_table.partitions")).unwrap();
let covers = tree.possible_covers(0, 111000);
println!("{:?}", tree);
println!("{:?}", covers.iter().map(|x| x.range()).collect::<Vec<_>>());
}
pub fn parse_from_file(file_name: &Path) -> anyhow::Result<Subtree> {
let contents = std::fs::read_to_string(file_name)?;
......@@ -128,7 +160,6 @@ pub mod brc_tree {
}
let (min_max, rest) = s.split_at(s.find('(').unwrap());
println!("MM:{:?}", min_max);
let (min, max) = {
let mut parts = min_max.split(',');
(
......@@ -151,10 +182,8 @@ pub mod brc_tree {
}
}
let left_str = &rest[1..split_index];
let right_str = &rest[split_index + 2..rest.len() - 2];
let left = parse_from_string(left_str.to_string())?;
let right = parse_from_string(right_str.to_string())?;
let left = parse_from_string(rest[1..split_index].to_string())?;
let right = parse_from_string(rest[split_index + 2..rest.len() - 2].to_string())?;
Ok(Subtree(Some(Box::new(Node {
min,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment