Skip to main content

LifecycleGraph

Struct LifecycleGraph 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn engine(&self) -> EngineConfig

Returns the engine that answers connectivity.

Source

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.

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn node_active(&self, node: NodeId) -> Option<bool>

Returns Some(active) for a registered node, None otherwise.

Source

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).

Source

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);
Source

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.

Source

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 }]
);
Source

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 }]);
Source

pub fn node_count(&self) -> usize

Returns the number of registered nodes, active or inactive.

Source

pub fn edge_count(&self) -> usize

Returns the number of registered edges, enabled or disabled.

Source

pub fn active_node_count(&self) -> usize

Returns the number of registered nodes whose flag is active.

Source

pub fn enabled_edge_count(&self) -> usize

Returns the number of registered edges whose flag is enabled.

Source

pub fn effective_edge_count(&self) -> usize

Returns the number of effective edges: enabled, with both endpoints active.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for LifecycleGraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for LifecycleGraph

§

impl Freeze for LifecycleGraph
where *const u8 is !null: Freeze, PhantomData<(u64, NodeRecord)>: Freeze, PhantomData<fn() -> IdHasher>: Freeze, PhantomData<(u64, usize)>: Freeze, PhantomData<u64>: Freeze, PhantomData<Vec<usize>>: Freeze, PhantomData<BTreeSet<usize>>: Freeze, PhantomData<((usize, usize), Option<(usize, usize)>)>: Freeze, PhantomData<Token<bool>>: Freeze, PhantomData<usize>: Freeze, PhantomData<((usize, usize), Edge)>: Freeze, PhantomData<Level>: Freeze, *const ClusterForestGraph is !null: Freeze, PhantomData<ClusterForestGraph>: Freeze,

§

impl Move for LifecycleGraph

§

impl RefUnwindSafe for LifecycleGraph

§

impl Send for LifecycleGraph
where RawTable<(u64, NodeRecord)>: Send, RawTable<(u64, usize)>: Send, RawVecInner: Send, NodeRef<Owned, usize, SetValZST, LeafOrInternal>: Send, BoxRaw<(usize, SetValZST)>: Send, RawTable<((usize, usize), Option<(usize, usize)>)>: Send, NonZero<usize>: Send, RawTable<((usize, usize), Edge)>: Send, RawTable<(usize, usize)>: Send, BoxRaw<ClusterForestGraph>: Send,

§

impl Sync for LifecycleGraph
where RawTable<(u64, NodeRecord)>: Sync, RawTable<(u64, usize)>: Sync, RawVecInner: Sync, NodeRef<Owned, usize, SetValZST, LeafOrInternal>: Sync, BoxRaw<(usize, SetValZST)>: Sync, RawTable<((usize, usize), Option<(usize, usize)>)>: Sync, NonZero<usize>: Sync, RawTable<((usize, usize), Edge)>: Sync, RawTable<(usize, usize)>: Sync, BoxRaw<ClusterForestGraph>: Sync,

§

impl Unpin for LifecycleGraph
where *const u8 is !null: Unpin, *const LeafNode<usize, SetValZST> is !null: Unpin, Box<(usize, SetValZST)>: Unpin, NonZero<usize>: Unpin, (usize, usize): Unpin, Box<ClusterForestGraph>: Unpin,

§

impl UnsafeUnpin for LifecycleGraph

§

impl UnwindSafe for LifecycleGraph

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.