pub struct CsrGraph { /* private fields */ }Expand description
CSR (Compressed Sparse Row) graph
Optimized for:
- O(1) access to outgoing edges (via forward CSR)
- O(1) access to incoming edges (via reverse CSR)
- GPU-friendly memory layout
- Sparse matrix operations (via aprender)
§Example
use trueno_graph::{CsrGraph, NodeId};
let mut graph = CsrGraph::new();
graph.add_edge(NodeId(0), NodeId(1), 1.0).unwrap();
graph.add_edge(NodeId(0), NodeId(2), 1.0).unwrap();
let neighbors = graph.outgoing_neighbors(NodeId(0)).unwrap();
assert_eq!(neighbors.len(), 2);Implementations§
Source§impl CsrGraph
impl CsrGraph
Sourcepub fn add_edge(
&mut self,
src: NodeId,
dst: NodeId,
weight: f32,
) -> Result<(), Error>
pub fn add_edge( &mut self, src: NodeId, dst: NodeId, weight: f32, ) -> Result<(), Error>
Add edge to graph (dynamic insertion)
Note: For large graphs, use from_edge_list for better performance.
§Errors
Returns error if graph is already finalized (immutable CSR)
Sourcepub fn incoming_neighbors(&self, target: NodeId) -> Result<&[u32], Error>
pub fn incoming_neighbors(&self, target: NodeId) -> Result<&[u32], Error>
Get incoming neighbors (callers) of a node
Returns O(1) access to incoming edges via reverse CSR.
§Errors
Returns error if node ID is out of bounds
Sourcepub fn set_node_name(&mut self, node: NodeId, name: String)
pub fn set_node_name(&mut self, node: NodeId, name: String)
Set node name (for debugging/export)
Sourcepub fn get_node_name(&self, node: NodeId) -> Option<&str>
pub fn get_node_name(&self, node: NodeId) -> Option<&str>
Get node name
Sourcepub fn row_offsets_slice(&self) -> &[u32]
pub fn row_offsets_slice(&self) -> &[u32]
Get row offsets as slice (for GPU upload)
Sourcepub fn col_indices_slice(&self) -> &[u32]
pub fn col_indices_slice(&self) -> &[u32]
Get column indices as slice (for GPU upload)
Sourcepub fn edge_weights_slice(&self) -> &[f32]
pub fn edge_weights_slice(&self) -> &[f32]
Get edge weights as slice (for GPU upload)
Sourcepub fn adjacency(&self, node_id: NodeId) -> (&[u32], &[f32])
pub fn adjacency(&self, node_id: NodeId) -> (&[u32], &[f32])
Get adjacency list for a specific node
Returns (targets, weights) slices for the node’s outgoing edges
Sourcepub fn iter_adjacency(&self) -> impl Iterator<Item = (NodeId, &[u32], &[f32])>
pub fn iter_adjacency(&self) -> impl Iterator<Item = (NodeId, &[u32], &[f32])>
Iterate over all nodes and their adjacency lists
Yields (node_id, targets, weights) tuples
Sourcepub fn csr_components(&self) -> (&[u32], &[u32], &[f32])
pub fn csr_components(&self) -> (&[u32], &[u32], &[f32])
Get CSR components (for aprender integration)
Source§impl CsrGraph
impl CsrGraph
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CsrGraph
impl RefUnwindSafe for CsrGraph
impl Send for CsrGraph
impl Sync for CsrGraph
impl Unpin for CsrGraph
impl UnsafeUnpin for CsrGraph
impl UnwindSafe for CsrGraph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more