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

(main):Started BRC implementation

parent 8b165c03
No related branches found
No related tags found
No related merge requests found
......@@ -347,6 +347,10 @@ dependencies = [
"url",
]
[[package]]
name = "client-brc"
version = "0.1.0"
[[package]]
name = "colorchoice"
version = "1.0.3"
......
......@@ -4,5 +4,5 @@ resolver = "2"
members = [
"client",
"server",
"crypto-utils",
"crypto-utils", "client-brc",
]
[package]
name = "client-brc"
version = "0.1.0"
edition = "2021"
[dependencies]
use std::{collections::HashMap, vec};
/// A node in the binary tree.
#[derive(Debug, Clone)]
struct Node<T: Ord> {
min: T,
max: T,
left: Subtree<T>,
right: Subtree<T>,
}
/// A possibly-empty subtree.
#[derive(Debug, Clone)]
struct Subtree<T: Ord>(Option<Box<Node<T>>>);
impl Subtree<usize> {
fn to_hashtable(&self) -> HashMap<(usize, usize), Subtree<usize>>{
let mut hashtable = HashMap::new();
match &self.0 {
Some(node) => {
let left = node.left.to_hashtable();
let right = node.right.to_hashtable();
if node.left.0.is_some() && node.right.0.is_some(){
hashtable.insert((node.min, node.max), Subtree(Some(Box::new(Node {
min: node.left.0.as_ref().unwrap().min,
max: node.right.0.as_ref().unwrap().max,
left: node.left.clone(),
right: node.right.clone(),
}))));
}
hashtable.insert((node.min, node.max), Subtree(Some(Box::new(Node {
min: node.min,
max: node.max,
left: node.left.clone(),
right: node.right.clone(),
}))));
for (key, value) in left.iter() {
hashtable.insert(*key, value.clone());
}
for (key, value) in right.iter() {
hashtable.insert(*key, value.clone());
}
},
None => {}
}
hashtable
}
}
fn main() {
let vec: Vec<usize> = vec![0,1,2,3,4,5,6,7];
// println!("{:?}", from_sorted_list(vec).to_hashtable());
for (key, value) in from_sorted_list(vec).to_hashtable().iter() {
println!("{:?} / {:?}", key, value);
// map.remove(key);
}
}
fn from_sorted_list(vec: Vec<usize>) -> Subtree<usize> {
let len = vec.len();
if len == 1{
return Subtree(Some(Box::new(Node {
min: vec[0],
max: vec[0],
left: Subtree(None),
right: Subtree(None),
})));
}
if len == 0 {
return Subtree(None);
}
let mid = len / 2;
let left = from_sorted_list(vec[..mid].to_vec());
let right = from_sorted_list(vec[mid..].to_vec());
let min = match &left.0 {
Some(node) => node.min,
None => match &right.0 {
Some(node) => node.min,
None => vec[0],
},
};
let max = match &right.0 {
Some(node) => node.max,
None => match &left.0 {
Some(node) => node.max,
None => vec[len - 1],
},
};
Subtree(Some(Box::new(Node {
left,
right,
min,
max,
})))
}
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