#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NodeId {
index: u32,
generation: u32,
}
impl NodeId {
pub const fn new(index: u32, generation: u32) -> Self {
Self { index, generation }
}
pub const fn from_index(index: usize) -> Self {
Self::new(index as u32, 0)
}
pub const fn index(self) -> usize {
self.index as usize
}
pub const fn generation(self) -> u32 {
self.generation
}
}
impl std::fmt::Debug for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.generation == 0 {
write!(f, "n{}", self.index)
} else {
write!(f, "n{}v{}", self.index, self.generation)
}
}
}
impl std::fmt::Display for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.index)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EdgeId {
index: u32,
generation: u32,
}
impl EdgeId {
pub const fn new(index: u32, generation: u32) -> Self {
Self { index, generation }
}
pub const fn from_index(index: usize) -> Self {
Self::new(index as u32, 0)
}
pub const fn index(self) -> usize {
self.index as usize
}
pub const fn generation(self) -> u32 {
self.generation
}
}
impl std::fmt::Debug for EdgeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.generation == 0 {
write!(f, "e{}", self.index)
} else {
write!(f, "e{}v{}", self.index, self.generation)
}
}
}
impl std::fmt::Display for EdgeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.index)
}
}
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Generations {
counts: Vec<u32>,
}
impl Generations {
pub fn new() -> Self {
Self::default()
}
pub fn issue(&mut self, index: usize) -> u32 {
if index >= self.counts.len() {
self.counts.resize(index + 1, 0);
}
self.counts[index]
}
pub fn retire(&mut self, index: usize) {
if let Some(count) = self.counts.get_mut(index) {
*count = count.wrapping_add(1);
}
}
pub fn is_current(&self, index: usize, generation: u32) -> bool {
self.counts.get(index) == Some(&generation)
}
pub fn generation(&self, index: usize) -> u32 {
self.counts.get(index).copied().unwrap_or(0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Link<W> {
pub target: NodeId,
pub edge: EdgeId,
pub weight: W,
}
impl<W> Link<W> {
pub fn new(target: NodeId, edge: EdgeId, weight: W) -> Self {
Self {
target,
edge,
weight,
}
}
}
pub trait GraphView {
type Weight: Copy;
type NodeIds<'a>: Iterator<Item = NodeId>
where
Self: 'a;
type Links<'a>: Iterator<Item = Link<Self::Weight>>
where
Self: 'a;
fn is_directed(&self) -> bool;
fn node_count(&self) -> usize;
fn edge_count(&self) -> usize;
fn contains_node(&self, node: NodeId) -> bool;
fn node_ids(&self) -> Self::NodeIds<'_>;
fn links(&self, node: NodeId) -> Self::Links<'_>;
fn edge_endpoints(&self, edge: EdgeId) -> Option<(NodeId, NodeId)>;
fn node_id_vec(&self) -> Vec<NodeId> {
self.node_ids().collect()
}
fn degree(&self, node: NodeId) -> usize {
self.links(node).count()
}
}
impl<G: GraphView + ?Sized> GraphView for &G {
type Weight = G::Weight;
type NodeIds<'a>
= G::NodeIds<'a>
where
Self: 'a;
type Links<'a>
= G::Links<'a>
where
Self: 'a;
fn is_directed(&self) -> bool {
(**self).is_directed()
}
fn node_count(&self) -> usize {
(**self).node_count()
}
fn edge_count(&self) -> usize {
(**self).edge_count()
}
fn contains_node(&self, node: NodeId) -> bool {
(**self).contains_node(node)
}
fn node_ids(&self) -> Self::NodeIds<'_> {
(**self).node_ids()
}
fn links(&self, node: NodeId) -> Self::Links<'_> {
(**self).links(node)
}
fn edge_endpoints(&self, edge: EdgeId) -> Option<(NodeId, NodeId)> {
(**self).edge_endpoints(edge)
}
}