use relate_core::NodeId;
use relate_ir::Direction;
use super::{live_version, Graph};
use crate::adjacency::Hop;
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: &Hop, 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 (side, scan_dir) in graph.sides(dir) {
side.with_adj(node, |adj, region| {
adj.for_each_hop_of(region, 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!(
crate::graph::test_support::sealed_chunk_len(&g.out_side(), a, type_id),
floor
);
let mut buf = Vec::new();
live_neighbors(&g, a, type_id, Direction::Outgoing, &mut buf);
assert_eq!(buf, vec![b; floor]);
}
}