2023-03-07 14:42:58 +01:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2023-02-21 09:46:00 +01:00
|
|
|
use super::{Edge, RankingRuleGraph, RankingRuleGraphTrait};
|
2023-03-08 09:55:53 +01:00
|
|
|
use crate::search::new::small_bitmap::SmallBitmap;
|
|
|
|
use crate::search::new::{QueryGraph, SearchContext};
|
2023-03-06 19:21:55 +01:00
|
|
|
use crate::Result;
|
2023-02-21 09:46:00 +01:00
|
|
|
|
|
|
|
impl<G: RankingRuleGraphTrait> RankingRuleGraph<G> {
|
2023-03-06 19:21:55 +01:00
|
|
|
pub fn build(ctx: &mut SearchContext, query_graph: QueryGraph) -> Result<Self> {
|
2023-03-07 14:42:58 +01:00
|
|
|
let QueryGraph { nodes: graph_nodes, edges: graph_edges, .. } = &query_graph;
|
|
|
|
|
|
|
|
let mut all_edges = vec![];
|
|
|
|
let mut node_edges = vec![];
|
|
|
|
let mut successors = vec![];
|
2023-02-21 09:46:00 +01:00
|
|
|
|
2023-03-07 14:42:58 +01:00
|
|
|
for (node_idx, node) in graph_nodes.iter().enumerate() {
|
|
|
|
node_edges.push(HashSet::new());
|
|
|
|
successors.push(HashSet::new());
|
|
|
|
let new_edges = node_edges.last_mut().unwrap();
|
|
|
|
let new_successors = successors.last_mut().unwrap();
|
2023-02-21 09:46:00 +01:00
|
|
|
|
2023-03-06 19:21:55 +01:00
|
|
|
let Some(from_node_data) = G::build_visit_from_node(ctx, node)? else { continue };
|
2023-02-21 09:46:00 +01:00
|
|
|
|
2023-03-07 14:42:58 +01:00
|
|
|
for successor_idx in graph_edges[node_idx].successors.iter() {
|
|
|
|
let to_node = &graph_nodes[successor_idx as usize];
|
2023-03-06 19:21:55 +01:00
|
|
|
let mut edges = G::build_visit_to_node(ctx, to_node, &from_node_data)?;
|
2023-02-21 12:33:32 +01:00
|
|
|
if edges.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
edges.sort_by_key(|e| e.0);
|
2023-02-21 09:46:00 +01:00
|
|
|
for (cost, details) in edges {
|
2023-03-07 14:42:58 +01:00
|
|
|
all_edges.push(Some(Edge {
|
|
|
|
from_node: node_idx as u16,
|
2023-02-21 12:55:44 +01:00
|
|
|
to_node: successor_idx,
|
2023-02-21 09:46:00 +01:00
|
|
|
cost,
|
|
|
|
details,
|
|
|
|
}));
|
2023-03-07 14:42:58 +01:00
|
|
|
new_edges.insert(all_edges.len() as u16 - 1);
|
2023-02-21 12:33:32 +01:00
|
|
|
new_successors.insert(successor_idx);
|
2023-02-21 09:46:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 14:42:58 +01:00
|
|
|
let node_edges = node_edges
|
|
|
|
.into_iter()
|
|
|
|
.map(|edges| SmallBitmap::from_iter(edges.into_iter(), all_edges.len() as u16))
|
|
|
|
.collect();
|
|
|
|
let successors = successors
|
|
|
|
.into_iter()
|
|
|
|
.map(|edges| SmallBitmap::from_iter(edges.into_iter(), all_edges.len() as u16))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(RankingRuleGraph { query_graph, all_edges, node_edges, successors })
|
2023-02-21 09:46:00 +01:00
|
|
|
}
|
|
|
|
}
|