use relate_core::NodeId;
use relate_ir::Direction;
use super::{live_version, Graph};
use crate::adjacency::AdjHop;
use crate::store::{hop_other, LiveNode};
pub(super) fn with_node<R>(
graph: &Graph,
node: NodeId,
f: impl FnOnce(&LiveNode) -> R,
) -> Option<R> {
let vers = graph.nodes.get(&node)?;
let live = live_version(&vers)?;
Some(f(live.live))
}
fn live_other(graph: &Graph, hop: &AdjHop, node: NodeId, dir: Direction) -> Option<NodeId> {
let vers = graph.rels.get(&hop.id)?;
let v = live_version(&vers)?;
hop_other(node, v.live.start, v.live.end, dir)
}
pub(super) fn live_neighbors(
graph: &Graph,
node: NodeId,
tid: u32,
dir: Direction,
buf: &mut Vec<NodeId>,
) {
buf.clear();
let ts = graph.read_ts();
for (map, sealed, scan_dir) in [
(&graph.out_adj, &graph.sealed_out, Direction::Outgoing),
(&graph.in_adj, &graph.sealed_in, Direction::Incoming),
] {
if dir != Direction::Both && dir != scan_dir {
continue;
}
let Some(adj) = map.get(&node) else {
continue;
};
let region = sealed.get(&node);
adj.for_each_hop_of(region.as_deref(), tid, ts, |hop| {
if let Some(other) = live_other(graph, &hop, node, scan_dir) {
buf.push(other);
}
});
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn live_neighbors_sees_sealed_hops() {
let g = Graph::default();
let (a, b, _, type_id) = crate::graph::test_support::folded_floor_chunk(&g);
let floor = crate::adjacency::COMPACTION_DELTA_FLOOR;
assert_eq!(g.sealed_out.get(&a).unwrap().chunk_len(type_id), floor);
let mut buf = Vec::new();
live_neighbors(&g, a, type_id, Direction::Outgoing, &mut buf);
assert_eq!(buf, vec![b; floor]);
}
}