fn btree_insert_new_key_at_subtree(key_values: &Vec<(CellRef, CellRef)>, child_nodes: &Vec<CellRef>, key_value: (CellRef, CellRef), left: CellRef, right: CellRef) -> BTreeInsertResult {
    // Need to insert at this leaf node
    if key_values.len() < BTREE_ORDER {
        // Node isn't full: just add to the list
        let mut new_key_values  = key_values.clone();
        let mut new_child_nodes = child_nodes.clone();

        btree_insert_new_node_and_children(&mut new_key_values, &mut new_child_nodes, key_value, left, right);

        return BTreeInsertResult::NewBTree(SafasCell::BTree(new_key_values, new_child_nodes).into());
    } else {
        // Node is full: need to split at the median value
        let median_key              = key_values[BTREE_MEDIAN].clone();
        let mut left_cells          = key_values[0..BTREE_MEDIAN].iter().cloned().collect::<Vec<_>>();
        let mut right_cells         = key_values[(BTREE_MEDIAN+1)..key_values.len()].iter().cloned().collect::<Vec<_>>();
        let mut left_children       = child_nodes[0..(BTREE_MEDIAN+1)].iter().cloned().collect::<Vec<_>>();
        let mut right_children      = child_nodes[(BTREE_MEDIAN+1)..(key_values.len()+1)].iter().cloned().collect::<Vec<_>>();

        // Insert into the left or right depending on the ordering of the median value with the key
        match (&*median_key.0).partial_cmp(&*key_value.0) {
            Some(Ordering::Greater) => btree_insert_new_node_and_children(&mut left_cells, &mut left_children, key_value, left, right),
            Some(Ordering::Less)    => btree_insert_new_node_and_children(&mut right_cells, &mut right_children, key_value, left, right),
            Some(Ordering::Equal)   => unreachable!(),
            None                    => return BTreeInsertResult::Err(RuntimeError::CannotCompare(median_key.0.clone(), key_value.0))
        }

        // We're at the leaf node here so there are no child nodes for the two new trees
        let left_btree  = SafasCell::BTree(left_cells, left_children);
        let right_btree = SafasCell::BTree(right_cells, right_children);
        
        return BTreeInsertResult::Split(left_btree.into(), right_btree.into(), median_key);
    }
}
///
/// Performs the operations required to insert a new key at a subtree node
///
Performs the operations required to insert a new key at a subtree node
Performs the operations required to insert a new key at a subtree node
fn btree_insert_new_key_at_subtree(key_values: &Vec<(CellRef, CellRef)>, child_nodes: &Vec<CellRef>, key_value: (CellRef, CellRef), left: CellRef, right: CellRef) -> BTreeInsertResult {
    // Need to insert at this leaf node
    if key_values.len() < BTREE_ORDER {
        // Node isn't full: just add to the list
        let mut new_key_values  = key_values.clone();
        let mut new_child_nodes = child_nodes.clone();
        btree_insert_new_node_and_children(&mut new_key_values, &mut new_child_nodes, key_value, left, right);
        return BTreeInsertResult::NewBTree(SafasCell::BTree(new_key_values, new_child_nodes).into());
    } else {
        // Node is full: need to split at the median value
        let median_key              = key_values[BTREE_MEDIAN].clone();
        let mut left_cells          = key_values[0..BTREE_MEDIAN].iter().cloned().collect::<Vec<_>>();
        let mut right_cells         = key_values[(BTREE_MEDIAN+1)..key_values.len()].iter().cloned().collect::<Vec<_>>();
        let mut left_children       = child_nodes[0..(BTREE_MEDIAN+1)].iter().cloned().collect::<Vec<_>>();
        let mut right_children      = child_nodes[(BTREE_MEDIAN+1)..(key_values.len()+1)].iter().cloned().collect::<Vec<_>>();
        // Insert into the left or right depending on the ordering of the median value with the key
        match (&*median_key.0).partial_cmp(&*key_value.0) {
            Some(Ordering::Greater) => btree_insert_new_node_and_children(&mut left_cells, &mut left_children, key_value, left, right),
            Some(Ordering::Less)    => btree_insert_new_node_and_children(&mut right_cells, &mut right_children, key_value, left, right),
            Some(Ordering::Equal)   => unreachable!(),
            None                    => return BTreeInsertResult::Err(RuntimeError::CannotCompare(median_key.0.clone(), key_value.0))
        }
        // We're at the leaf node here so there are no child nodes for the two new trees
        let left_btree  = SafasCell::BTree(left_cells, left_children);
        let right_btree = SafasCell::BTree(right_cells, right_children);
        return BTreeInsertResult::Split(left_btree.into(), right_btree.into(), median_key);
    }
}
fn btree_insert_new_key_at_subtree ( key_values : & Vec < ( CellRef , CellRef ) > , child_nodes : & Vec < CellRef > , key_value : ( CellRef , CellRef ) , left : CellRef , right : CellRef ) -> BTreeInsertResult { // Need to insert at this leaf node if key_values . len ( ) < BTREE_ORDER { // Node isn't full: just add to the list let mut new_key_values = key_values . clone ( ) ; let mut new_child_nodes = child_nodes . clone ( ) ; btree_insert_new_node_and_children ( & mut new_key_values , & mut new_child_nodes , key_value , left , right ) ; return BTreeInsertResult :: NewBTree ( SafasCell :: BTree ( new_key_values , new_child_nodes ) . into ( ) ) ; } else { // Node is full: need to split at the median value let median_key = key_values [ BTREE_MEDIAN ] . clone ( ) ; let mut left_cells = key_values [ 0 .. BTREE_MEDIAN ] . iter ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) ; let mut right_cells = key_values [ ( BTREE_MEDIAN + 1 ) .. key_values . len ( ) ] . iter ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) ; let mut left_children = child_nodes [ 0 .. ( BTREE_MEDIAN + 1 ) ] . iter ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) ; let mut right_children = child_nodes [ ( BTREE_MEDIAN + 1 ) .. ( key_values . len ( ) + 1 ) ] . iter ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) ; // Insert into the left or right depending on the ordering of the median value with the key match ( & * median_key . 0 ) . partial_cmp ( & * key_value . 0 ) { Some ( Ordering :: Greater ) => btree_insert_new_node_and_children ( & mut left_cells , & mut left_children , key_value , left , right ) , Some ( Ordering :: Less ) => btree_insert_new_node_and_children ( & mut right_cells , & mut right_children , key_value , left , right ) , Some ( Ordering :: Equal ) => unreachable ! ( ) , None => return BTreeInsertResult :: Err ( RuntimeError :: CannotCompare ( median_key . 0 . clone ( ) , key_value . 0 ) ) } // We're at the leaf node here so there are no child nodes for the two new trees let left_btree = SafasCell :: BTree ( left_cells , left_children ) ; let right_btree = SafasCell :: BTree ( right_cells , right_children ) ; return BTreeInsertResult :: Split ( left_btree . into ( ) , right_btree . into ( ) , median_key ) ; } }
Performs the operations required to insert a new key at a subtree node
Performs the operations required to insert a new key at a subtree node
key_values
&Vec<(CellRef, CellRef)>
child_nodes
&Vec<CellRef>
key_value
(CellRef, CellRef)
left
CellRef
right
CellRef
key_values
&Vec<(CellRef, CellRef)>
child_nodes
&Vec<CellRef>
key_value
(CellRef, CellRef)
left
CellRef
right
CellRef