fn btree_insert_new_key_at_leaf(key_values: &Vec<(CellRef, CellRef)>, child_nodes: &Vec<CellRef>, key: CellRef, value: CellRef) -> BTreeInsertResult {
    // Need to insert at this leaf node
    if key_values.len() < BTREE_ORDER {
        // Node isn't full: just add to the list (sort here is concise but maybe not as fast as scanning the list of values)
        let mut new_key_values = key_values.clone();
        new_key_values.push((key, value));
        new_key_values.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal));

        return BTreeInsertResult::NewBTree(SafasCell::BTree(new_key_values, child_nodes.clone()).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<_>>();

        // Insert into the left or right depending on the ordering of the median value with the key
        match (&*median_key.0).partial_cmp(&*key) {
            Some(Ordering::Greater) => { left_cells.push((key, value)); left_cells.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal)); },
            Some(Ordering::Less)    => { right_cells.push((key, value)); right_cells.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal)); },
            Some(Ordering::Equal)   => unreachable!(),
            None                    => return BTreeInsertResult::Err(RuntimeError::CannotCompare(median_key.0.clone(), key))
        }

        // 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, vec![]);
        let right_btree = SafasCell::BTree(right_cells, vec![]);
        
        return BTreeInsertResult::Split(left_btree.into(), right_btree.into(), median_key);
    }
}
///
/// Performs the operations required to insert a new key at a leaf node
///
Performs the operations required to insert a new key at a leaf node
Performs the operations required to insert a new key at a leaf node
fn btree_insert_new_key_at_leaf(key_values: &Vec<(CellRef, CellRef)>, child_nodes: &Vec<CellRef>, key: CellRef, value: CellRef) -> BTreeInsertResult {
    // Need to insert at this leaf node
    if key_values.len() < BTREE_ORDER {
        // Node isn't full: just add to the list (sort here is concise but maybe not as fast as scanning the list of values)
        let mut new_key_values = key_values.clone();
        new_key_values.push((key, value));
        new_key_values.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal));
        return BTreeInsertResult::NewBTree(SafasCell::BTree(new_key_values, child_nodes.clone()).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<_>>();
        // Insert into the left or right depending on the ordering of the median value with the key
        match (&*median_key.0).partial_cmp(&*key) {
            Some(Ordering::Greater) => { left_cells.push((key, value)); left_cells.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal)); },
            Some(Ordering::Less)    => { right_cells.push((key, value)); right_cells.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal)); },
            Some(Ordering::Equal)   => unreachable!(),
            None                    => return BTreeInsertResult::Err(RuntimeError::CannotCompare(median_key.0.clone(), key))
        }
        // 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, vec![]);
        let right_btree = SafasCell::BTree(right_cells, vec![]);
        return BTreeInsertResult::Split(left_btree.into(), right_btree.into(), median_key);
    }
}
fn btree_insert_new_key_at_leaf ( key_values : & Vec < ( CellRef , CellRef ) > , child_nodes : & Vec < CellRef > , key : CellRef , value : CellRef ) -> BTreeInsertResult { // Need to insert at this leaf node if key_values . len ( ) < BTREE_ORDER { // Node isn't full: just add to the list (sort here is concise but maybe not as fast as scanning the list of values) let mut new_key_values = key_values . clone ( ) ; new_key_values . push ( ( key , value ) ) ; new_key_values . sort_by ( | a , b | a . 0 . partial_cmp ( & b . 0 ) . unwrap_or ( Ordering :: Equal ) ) ; return BTreeInsertResult :: NewBTree ( SafasCell :: BTree ( new_key_values , child_nodes . clone ( ) ) . 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 < _ > > ( ) ; // Insert into the left or right depending on the ordering of the median value with the key match ( & * median_key . 0 ) . partial_cmp ( & * key ) { Some ( Ordering :: Greater ) => { left_cells . push ( ( key , value ) ) ; left_cells . sort_by ( | a , b | a . 0 . partial_cmp ( & b . 0 ) . unwrap_or ( Ordering :: Equal ) ) ; } , Some ( Ordering :: Less ) => { right_cells . push ( ( key , value ) ) ; right_cells . sort_by ( | a , b | a . 0 . partial_cmp ( & b . 0 ) . unwrap_or ( Ordering :: Equal ) ) ; } , Some ( Ordering :: Equal ) => unreachable ! ( ) , None => return BTreeInsertResult :: Err ( RuntimeError :: CannotCompare ( median_key . 0 . clone ( ) , key ) ) } // 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 , vec ! [ ] ) ; let right_btree = SafasCell :: BTree ( right_cells , vec ! [ ] ) ; return BTreeInsertResult :: Split ( left_btree . into ( ) , right_btree . into ( ) , median_key ) ; } }
Performs the operations required to insert a new key at a leaf node
Performs the operations required to insert a new key at a leaf node
key_values
&Vec<(CellRef, CellRef)>
child_nodes
&Vec<CellRef>
key
CellRef
value
CellRef
key_values
&Vec<(CellRef, CellRef)>
child_nodes
&Vec<CellRef>
key
CellRef
value
CellRef