1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
//! Force-directed graph layouts assume edges to be straight lines. A [`Slot`]
//! represents the space that a single edge, _or multiple_ edges can occupy between two nodes.
//!
//! We achieve this by bringing edges into a canonical form via [`SlotId`], which
//! we can then use to find duplicates.
use crate::graph::NodeId;
use super::request::EdgeTemplate;
/// Uniquely identifies a [`Slot`] by ordering the [`NodeIds`](NodeId) that make up an edge.
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct SlotId(NodeId, NodeId);
impl SlotId {
pub fn new(source: NodeId, target: NodeId) -> Self {
if source < target {
Self(source, target)
} else {
Self(target, source)
}
}
}
/// There are different types of [`Slots`](Slot) that are laid out differently.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum SlotKind {
/// An edge slot going from `source` to `target`. Source and target represent the canonical order of the slot, as specified by [`SlotId`]
Regular { source: NodeId, target: NodeId },
/// An edge where `source == target`.
SelfEdge { node: NodeId },
}
pub struct Slot<'a> {
pub kind: SlotKind,
pub edges: Vec<&'a EdgeTemplate>,
}
/// Converts a list of edges into their slotted form.
pub fn slotted_edges<'a>(
edges: impl Iterator<Item = &'a EdgeTemplate>,
) -> ahash::HashMap<SlotId, Slot<'a>> {
let mut slots: ahash::HashMap<SlotId, Slot<'a>> = ahash::HashMap::default();
for e in edges {
let id = SlotId::new(e.source, e.target);
let slot = slots.entry(id).or_insert_with_key(|id| Slot {
kind: match e.source == e.target {
true => SlotKind::SelfEdge { node: e.source },
false => SlotKind::Regular {
source: id.0,
target: id.1,
},
},
edges: Vec::new(),
});
slot.edges.push(e);
}
slots
}