use super::*;
#[derive(Default)]
pub(super) struct Row {
pub(super) nodes: Vec<Node>,
pub(super) ranked: bool,
}
impl Row {
pub(super) fn new() -> Self {
Self {
nodes: Vec::with_capacity(MAX_NODES_PER_ROW),
ranked: false,
}
}
pub(super) fn extend(&mut self, prefix: &str, suffix: &str, cost: i64, right_id: u16) {
if self.nodes.len() < MAX_NODES_PER_ROW {
self.nodes.push(Node {
text: join_text(prefix, suffix),
cost,
right_id,
});
return;
}
if !self.ranked {
self.nodes.sort_by_key(|node| node.cost);
self.ranked = true;
}
if cost >= self.nodes.last().expect("full row").cost {
return;
}
self.nodes.pop();
let index = self.nodes.partition_point(|node| node.cost <= cost);
self.nodes.insert(
index,
Node {
text: join_text(prefix, suffix),
cost,
right_id,
},
);
}
}