fn btree_search_internal(btree: &CellRef, key: CellRef) -> BTreeSearchResult {
    match &**btree {
        SafasCell::BTree(key_values, child_nodes)   => {
            // Search the key values
            for idx in 0..key_values.len() {
                // Values are ordered by key
                match (&*key_values[idx].0).partial_cmp(&*key) {
                    Some(Ordering::Equal)   => { return BTreeSearchResult::Found(key_values[idx].1.clone()) },
                    Some(Ordering::Less)    => { },
                    Some(Ordering::Greater) => { 
                        // If there are any child nodes, they contain the items that are between this value and the previous value
                        if child_nodes.len() > 0 {
                            return btree_search_internal(&child_nodes[idx], key);
                        } else {
                            return BTreeSearchResult::NotFound;
                        }
                    },
                    None                    => { return BTreeSearchResult::Err(RuntimeError::CannotCompare(key_values[idx].0.clone(), key)); }
                }
            }

            // Value may be in the final child node
            if child_nodes.len() > 0 {
                return btree_search_internal(&child_nodes[child_nodes.len()-1], key);
            } else {
                return BTreeSearchResult::NotFound;
            }
        },

        SafasCell::Nil                              => BTreeSearchResult::NotFound,
        _                                           => BTreeSearchResult::Err(RuntimeError::NotABTree(btree.clone()))
    }
}
///
/// Implementation of the btree search operation
///
Implementation of the btree search operation
Implementation of the btree search operation
fn btree_search_internal(btree: &CellRef, key: CellRef) -> BTreeSearchResult {
    match &**btree {
        SafasCell::BTree(key_values, child_nodes)   => {
            // Search the key values
            for idx in 0..key_values.len() {
                // Values are ordered by key
                match (&*key_values[idx].0).partial_cmp(&*key) {
                    Some(Ordering::Equal)   => { return BTreeSearchResult::Found(key_values[idx].1.clone()) },
                    Some(Ordering::Less)    => { },
                    Some(Ordering::Greater) => { 
                        // If there are any child nodes, they contain the items that are between this value and the previous value
                        if child_nodes.len() > 0 {
                            return btree_search_internal(&child_nodes[idx], key);
                        } else {
                            return BTreeSearchResult::NotFound;
                        }
                    },
                    None                    => { return BTreeSearchResult::Err(RuntimeError::CannotCompare(key_values[idx].0.clone(), key)); }
                }
            }
            // Value may be in the final child node
            if child_nodes.len() > 0 {
                return btree_search_internal(&child_nodes[child_nodes.len()-1], key);
            } else {
                return BTreeSearchResult::NotFound;
            }
        },
        SafasCell::Nil                              => BTreeSearchResult::NotFound,
        _                                           => BTreeSearchResult::Err(RuntimeError::NotABTree(btree.clone()))
    }
}
fn btree_search_internal ( btree : & CellRef , key : CellRef ) -> BTreeSearchResult { match & * * btree { SafasCell :: BTree ( key_values , child_nodes ) => { // Search the key values for idx in 0 .. key_values . len ( ) { // Values are ordered by key match ( & * key_values [ idx ] . 0 ) . partial_cmp ( & * key ) { Some ( Ordering :: Equal ) => { return BTreeSearchResult :: Found ( key_values [ idx ] . 1 . clone ( ) ) } , Some ( Ordering :: Less ) => { } , Some ( Ordering :: Greater ) => { // If there are any child nodes, they contain the items that are between this value and the previous value if child_nodes . len ( ) > 0 { return btree_search_internal ( & child_nodes [ idx ] , key ) ; } else { return BTreeSearchResult :: NotFound ; } } , None => { return BTreeSearchResult :: Err ( RuntimeError :: CannotCompare ( key_values [ idx ] . 0 . clone ( ) , key ) ) ; } } } // Value may be in the final child node if child_nodes . len ( ) > 0 { return btree_search_internal ( & child_nodes [ child_nodes . len ( ) - 1 ] , key ) ; } else { return BTreeSearchResult :: NotFound ; } } , SafasCell :: Nil => BTreeSearchResult :: NotFound , _ => BTreeSearchResult :: Err ( RuntimeError :: NotABTree ( btree . clone ( ) ) ) } }
Implementation of the btree search operation
Implementation of the btree search operation
btree
&CellRef
key
CellRef
btree
&CellRef
key
CellRef