use super::data_models::{LlmCallPrediction, PredictionTrieNode};
pub struct PredictionTrieLookup<'a> {
root: &'a PredictionTrieNode,
}
impl<'a> PredictionTrieLookup<'a> {
pub fn new(root: &'a PredictionTrieNode) -> Self {
Self { root }
}
pub fn find(&self, path: &[String], call_index: u32) -> Option<&'a LlmCallPrediction> {
let mut node = self.root;
let mut deepest_match: Option<&LlmCallPrediction> = None;
if let Some(pred) = Self::get_prediction(node, call_index) {
deepest_match = Some(pred);
}
for func_name in path {
match node.children.get(func_name.as_str()) {
Some(child) => {
node = child;
if let Some(pred) = Self::get_prediction(node, call_index) {
deepest_match = Some(pred);
}
}
None => break,
}
}
deepest_match
}
fn get_prediction(node: &PredictionTrieNode, call_index: u32) -> Option<&LlmCallPrediction> {
node.predictions_by_call_index
.get(&call_index)
.or(node.predictions_any_index.as_ref())
}
}
#[cfg(test)]
#[path = "../../tests/unit/trie/lookup_tests.rs"]
mod tests;