pub struct LifecycleGraph { /* private fields */ }Expand description
A graph with reversible node activation, reversible edge enabling and permanent node removal.
Connectivity is answered over the effective graph: active nodes and
the registered edges that are enabled and have two active endpoints. The
plain operations add_node, link and
cut keep their ConnectivityGraph meaning on the
registered topology: add_node never reactivates a node, link never
re-enables an edge and cut removes a registered edge whether or not it is
effective.
GraphLimits bound registered nodes and edges, including inactive
nodes and disabled edges.
§Costs
Let d be the registered degree of the node concerned and U the cost of
one effective edge update in the selected engine. Deactivation and
activation cost O(d) plus one engine update per effective incident edge.
Removal also edits every neighbor’s incident vector, O(d + sum of neighbor degrees), and pays an amortized share of engine compaction (see
the module documentation). None of these bounds is logarithmic.
Memory holds the registered topology in a catalog and the effective graph in the engine, roughly twice the plain engine for an all-active graph.
§Failure
A method that returns Err made no change. A panic during a mutation
leaves the graph unspecified; discard it.
§Examples
use knotrel_core::{EngineConfig, LifecycleGraph};
let mut graph = LifecycleGraph::new(EngineConfig::CompactBfs)?;
graph.link(1, 2)?;
graph.link(2, 3)?;
// Deactivate node 2: both incident edges stop being effective.
assert!(graph.set_node_active(2, false)?);
assert!(!graph.connected(1, 3)?);
// Disable one incident edge while node 2 is inactive.
assert!(graph.set_edge_enabled(2, 3, false)?);
// Reactivate: only the still-enabled edge returns.
assert!(graph.set_node_active(2, true)?);
assert!(graph.connected(1, 2)?);
assert!(!graph.connected(1, 3)?);
// Removal is permanent; re-adding the ID creates an isolated node.
assert!(graph.remove_node(2));
assert!(graph.add_node(2)?);
assert!(!graph.connected(1, 2)?);Implementations§
Source§impl LifecycleGraph
impl LifecycleGraph
Sourcepub const fn supports_engine(engine: EngineConfig) -> bool
pub const fn supports_engine(engine: EngineConfig) -> bool
Reports whether lifecycle support is available for engine.
EngineConfig::CompactBfs and, from 1.1,
EngineConfig::ClusterForest are supported. Lifecycle support for
the experimental ett and hdt engines is deferred; their plain
ConnectivityGraph use is unaffected.
Sourcepub fn new(engine: EngineConfig) -> Result<Self, LifecycleError>
pub fn new(engine: EngineConfig) -> Result<Self, LifecycleError>
Creates an empty unlimited graph using engine for connectivity.
§Errors
LifecycleError::UnsupportedEngine unless
supports_engine holds for engine.
Sourcepub fn with_limits(
engine: EngineConfig,
limits: GraphLimits,
) -> Result<Self, LifecycleError>
pub fn with_limits( engine: EngineConfig, limits: GraphLimits, ) -> Result<Self, LifecycleError>
Creates an empty graph with cardinality limits on registered topology.
§Errors
LifecycleError::UnsupportedEngine unless
supports_engine holds for engine.
Sourcepub fn try_from_topology(
engine: EngineConfig,
limits: GraphLimits,
nodes: &[NodeSpec],
edges: &[EdgeSpec],
) -> Result<Self, LifecycleError>
pub fn try_from_topology( engine: EngineConfig, limits: GraphLimits, nodes: &[NodeSpec], edges: &[EdgeSpec], ) -> Result<Self, LifecycleError>
Builds a graph from complete registered topology.
Registers nodes in order, then edges in order, creating missing
edge endpoints as active nodes. Repeated entries with identical flags
are accepted once; edges compare as unordered pairs.
The catalog is assembled first and incident vectors are sorted once,
O((V + E) log E); the engine is then built in one pass from the
catalog with exactly the effective edges, O(V log V + E).
§Errors
LifecycleError::UnsupportedEngine first, before any entry is
examined, unless supports_engine holds for
engine. Otherwise processing stops at the first failing entry, in
input order:
LifecycleError::ConflictingNode or
LifecycleError::ConflictingEdge for a repeated entry with a
different flag; GraphError::SelfLoop, then
GraphError::NodeLimitExceeded, then
GraphError::EdgeLimitExceeded (for a new edge) wrapped in
LifecycleError::Graph.
Sourcepub fn limits(&self) -> GraphLimits
pub fn limits(&self) -> GraphLimits
Returns the configured limits on registered topology, exactly as passed at construction; they are not adjusted for the engine.
The enforced limits are the configured ones, except with
EngineConfig::ClusterForest, where they can be lower:
at most 2²⁷ − 512 registered nodes and 2²⁸ registered edges,
whatever is configured (the engine’s capacity, with room for retired
slots); mutations beyond them return GraphError::NodeLimitExceeded
or GraphError::EdgeLimitExceeded carrying the enforced limit.
Sourcepub fn engine(&self) -> EngineConfig
pub fn engine(&self) -> EngineConfig
Returns the engine that answers connectivity.
Sourcepub fn add_node(&mut self, node: NodeId) -> Result<bool, GraphError>
pub fn add_node(&mut self, node: NodeId) -> Result<bool, GraphError>
Registers node as an active isolated node.
Returns Ok(false) if the node is already registered; an inactive
node stays inactive. Use set_node_active to
reactivate it. A previously removed ID is registered as a fresh node
without its former edges.
§Errors
GraphError::NodeLimitExceeded when registering a new node would
exceed the enforced node limit: max_nodes, or the engine’s lower
capacity (see Self::limits). The error carries the enforced value.
Sourcepub fn link(
&mut self,
source: NodeId,
target: NodeId,
) -> Result<bool, GraphError>
pub fn link( &mut self, source: NodeId, target: NodeId, ) -> Result<bool, GraphError>
Registers the undirected edge {source, target} as enabled.
Missing endpoints are registered as active nodes. The edge is
effective only if both endpoints are active. Returns Ok(false) if
the edge is already registered, enabled or disabled; a disabled edge
stays disabled.
§Errors
In precedence order: GraphError::SelfLoop;
GraphError::NodeLimitExceeded when the missing endpoints do not
fit; GraphError::EdgeLimitExceeded for a new edge at the enforced
edge limit. Both carry the enforced value, which can be lower than the
configured one (see Self::limits).
Sourcepub fn cut(
&mut self,
source: NodeId,
target: NodeId,
) -> Result<bool, GraphError>
pub fn cut( &mut self, source: NodeId, target: NodeId, ) -> Result<bool, GraphError>
Removes the registered edge {source, target}, whether or not it is
enabled or effective. Both endpoints stay registered.
Returns Ok(false) if the edge or an endpoint is not registered.
§Errors
GraphError::SelfLoop if source == target.
Sourcepub fn set_node_active(
&mut self,
node: NodeId,
active: bool,
) -> Result<bool, LifecycleError>
pub fn set_node_active( &mut self, node: NodeId, active: bool, ) -> Result<bool, LifecycleError>
Sets the active flag of a registered node.
Incident registered edges and their enabled flags are preserved.
Activation makes effective exactly the incident edges that are enabled
and whose other endpoint is active. Returns Ok(false) if the flag
already has this value.
Costs O(d) for registered degree d, plus one engine update for
each incident edge whose effectiveness changes.
§Errors
GraphError::UnknownNode (wrapped) if node is not registered.
Sourcepub fn set_edge_enabled(
&mut self,
source: NodeId,
target: NodeId,
enabled: bool,
) -> Result<bool, LifecycleError>
pub fn set_edge_enabled( &mut self, source: NodeId, target: NodeId, enabled: bool, ) -> Result<bool, LifecycleError>
Sets the enabled flag of a registered edge, including while an
endpoint is inactive; the flag then takes effect when both endpoints
are active. Returns Ok(false) if the flag already has this value.
§Errors
In precedence order: GraphError::SelfLoop;
GraphError::UnknownNode for source, then for target;
LifecycleError::UnknownEdge if both nodes are registered but the
edge is not.
Sourcepub fn remove_node(&mut self, node: NodeId) -> bool
pub fn remove_node(&mut self, node: NodeId) -> bool
Permanently removes node and every incident registered edge,
including disabled edges and edges to inactive neighbors.
Returns false if node is not registered. Registering the same ID
later creates a fresh active isolated node.
Costs O(d + sum of neighbor degrees) catalog work for registered
degree d, one engine update per effective incident edge, and an
amortized share of engine compaction.
Sourcepub fn connected(
&self,
source: NodeId,
target: NodeId,
) -> Result<bool, GraphError>
pub fn connected( &self, source: NodeId, target: NodeId, ) -> Result<bool, GraphError>
Returns whether source and target are connected in the effective
graph.
An inactive endpoint returns Ok(false), including
connected(n, n). An active node is connected to itself.
§Errors
GraphError::UnknownNode for source, then for target, if not
registered.
Sourcepub fn node_active(&self, node: NodeId) -> Option<bool>
pub fn node_active(&self, node: NodeId) -> Option<bool>
Returns Some(active) for a registered node, None otherwise.
Sourcepub fn degree(&self, node: NodeId) -> Option<usize>
pub fn degree(&self, node: NodeId) -> Option<usize>
Returns the number of registered edges incident to node (enabled or
not), or None if it is not registered. Expected O(1).
Sourcepub fn incident_after(
&self,
node: NodeId,
after: Option<NodeId>,
) -> Option<impl Iterator<Item = (NodeId, EdgeState)> + '_>
pub fn incident_after( &self, node: NodeId, after: Option<NodeId>, ) -> Option<impl Iterator<Item = (NodeId, EdgeState)> + '_>
Iterates the registered edges incident to node in ascending neighbor
ID, starting strictly after the neighbor after (or from the start),
with each edge’s flags. Returns None if node is not registered.
after is a pagination cursor: it need not be registered or still
linked, so every edge that stays registered while a caller pages through
the list is visited exactly once; edges registered or removed meanwhile
may or may not be visited.
Positioning is O(log deg(node)); each step costs one expected-O(1)
catalog lookup of the neighbor (for effective).
use knotrel_core::{EngineConfig, LifecycleGraph};
let mut graph = LifecycleGraph::new(EngineConfig::CompactBfs)?;
graph.link(5, 9)?;
graph.link(5, 1)?;
graph.set_node_active(9, false)?;
let edges: Vec<_> = graph.incident_after(5, None).unwrap().collect();
assert_eq!(edges.len(), 2);
assert_eq!(edges[0].0, 1); // ascending neighbor ID
assert!(edges[0].1.effective);
assert!(edges[1].1.enabled && !edges[1].1.effective); // 9 is inactive
assert_eq!(graph.incident_after(5, Some(1)).unwrap().count(), 1);Sourcepub fn edge_state(&self, source: NodeId, target: NodeId) -> Option<EdgeState>
pub fn edge_state(&self, source: NodeId, target: NodeId) -> Option<EdgeState>
Returns the flags of the registered edge {source, target}, or None
if it is not registered. A self-loop pair is never registered.
Sourcepub fn for_each_node(&self, visit: impl FnMut(NodeSpec))
pub fn for_each_node(&self, visit: impl FnMut(NodeSpec))
Calls visit once for every registered node with its flag, in
unspecified order.
Experimental (new in 1.3): with for_each_edge,
a bulk read whose output try_from_topology
accepts as is, for snapshots and exports. O(V) time; allocates
nothing.
use knotrel_core::{EngineConfig, LifecycleGraph, NodeSpec};
let mut graph = LifecycleGraph::new(EngineConfig::CompactBfs)?;
graph.link(1, 2)?;
graph.set_node_active(2, false)?;
let mut nodes = Vec::new();
graph.for_each_node(|spec| nodes.push(spec));
nodes.sort_unstable_by_key(|spec| spec.node);
assert_eq!(
nodes,
[NodeSpec { node: 1, active: true }, NodeSpec { node: 2, active: false }]
);Sourcepub fn for_each_edge(&self, visit: impl FnMut(EdgeSpec))
pub fn for_each_edge(&self, visit: impl FnMut(EdgeSpec))
Calls visit once for every registered edge with its own flag, with
source < target, in unspecified order.
Experimental (new in 1.3): see for_each_node.
The flag is the edge’s own enabled flag, independent of its
endpoints. O(V + E) time; allocates nothing.
use knotrel_core::{EdgeSpec, EngineConfig, LifecycleGraph};
let mut graph = LifecycleGraph::new(EngineConfig::CompactBfs)?;
graph.link(9, 4)?;
graph.set_edge_enabled(4, 9, false)?;
let mut edges = Vec::new();
graph.for_each_edge(|spec| edges.push(spec));
assert_eq!(edges, [EdgeSpec { source: 4, target: 9, enabled: false }]);Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Returns the number of registered nodes, active or inactive.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Returns the number of registered edges, enabled or disabled.
Sourcepub fn active_node_count(&self) -> usize
pub fn active_node_count(&self) -> usize
Returns the number of registered nodes whose flag is active.
Sourcepub fn enabled_edge_count(&self) -> usize
pub fn enabled_edge_count(&self) -> usize
Returns the number of registered edges whose flag is enabled.
Sourcepub fn effective_edge_count(&self) -> usize
pub fn effective_edge_count(&self) -> usize
Returns the number of effective edges: enabled, with both endpoints active.
Sourcepub fn engine_vertex_count(&self) -> usize
pub fn engine_vertex_count(&self) -> usize
Returns the number of vertices held by the inner engine: every registered node plus retired slots of removed nodes not yet compacted.
It is a storage diagnostic, bounded by 2 × node_count() + 1024.
Sourcepub fn storage_stats(&self) -> LifecycleStorage
pub fn storage_stats(&self) -> LifecycleStorage
Returns storage diagnostics: logical sizes and retained capacities.
Costs O(V) time (one pass over the catalog) and no allocation, and
changes no state. LifecycleStorage lists which properties of the
values are guaranteed and which are implementation-dependent.