Skip to content

Instantly share code, notes, and snippets.

@greister
Created April 20, 2016 00:19
Show Gist options
  • Save greister/c20fa9396d0b58917431a32bacdc3204 to your computer and use it in GitHub Desktop.
Save greister/c20fa9396d0b58917431a32bacdc3204 to your computer and use it in GitHub Desktop.
use std::sync::Arc;
use std::rc::Rc;
use std::fmt::Debug;
use std::cell::RefCell;
#[derive(Debug, PartialEq, Clone, Copy)]
enum NodeType{
Node,
Leaf
}
#[derive(Debug)]
struct Node<K, V> {
node_type: NodeType,
keys: Vec<K>,
values: Option<Vec<V>>,
children: Option<Vec<Rc<RefCell<Node<K, V>>>>>,
next: Option<Rc<RefCell<Node<K, V>>>>
}
pub fn upper_bound2<K: Ord + Debug + Clone>(keys: &[K], key: &K) -> usize {
let mut index: i32 = -1;
for i in 0..keys.len(){
let ref k = keys[i];
if *key < *k { index = i as i32; break; }
}
if index == -1{ index = keys.len() as i32; }
return index as usize;
}
fn choose_subtree_mut<'a, K: Ord + Debug + Clone, V: Debug + Clone>(node: &'a mut Node<K,V>,k: &K) ->&'a mut Node<K, V> {
match node.node_type {
NodeType::Leaf => { return node; }
NodeType::Node => {
let index = upper_bound2(&node.keys, &k);
println!("Child index: {:?}", index);
match node.children {
Some(ref mut node) => choose_subtree_mut(&mut node[index].borrow_mut(),k),
None => panic!(""),
}
}
}
}
fn main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment