Skip to main content

CsrGraph

Struct CsrGraph 

Source
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

Source

pub fn new() -> CsrGraph

Create new empty graph

Source

pub fn from_edge_list( edges: &[(NodeId, NodeId, f32)], ) -> Result<CsrGraph, Error>

Create graph from edge list

§Arguments
  • edges - List of (source, target, weight) tuples
§Errors

Returns error if edge list is invalid (e.g., negative node IDs)

Source

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)

Source

pub fn outgoing_neighbors(&self, node: NodeId) -> Result<&[u32], Error>

Get outgoing neighbors (callees) of a node

§Errors

Returns error if node ID is out of bounds

Source

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

Source

pub fn set_node_name(&mut self, node: NodeId, name: String)

Set node name (for debugging/export)

Source

pub fn get_node_name(&self, node: NodeId) -> Option<&str>

Get node name

Source

pub const fn num_nodes(&self) -> usize

Get number of nodes

Source

pub fn num_edges(&self) -> usize

Get number of edges

Source

pub fn row_offsets_slice(&self) -> &[u32]

Get row offsets as slice (for GPU upload)

Source

pub fn col_indices_slice(&self) -> &[u32]

Get column indices as slice (for GPU upload)

Source

pub fn edge_weights_slice(&self) -> &[f32]

Get edge weights as slice (for GPU upload)

Source

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

Source

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

Source

pub fn csr_components(&self) -> (&[u32], &[u32], &[f32])

Get CSR components (for aprender integration)

Source§

impl CsrGraph

Source

pub async fn write_parquet<P>(&self, path: P) -> Result<(), Error>
where P: AsRef<Path>,

Write graph to Parquet files

Creates two files:

  • {path}_edges.parquet: Edge list (source, target, weight)
  • {path}_nodes.parquet: Node metadata (node_id, name)
§Errors

Returns error if file I/O fails or Arrow conversion fails

Source

pub async fn read_parquet<P>(path: P) -> Result<CsrGraph, Error>
where P: AsRef<Path>,

Read graph from Parquet files

§Errors

Returns error if files don’t exist or Arrow conversion fails

Trait Implementations§

Source§

impl Clone for CsrGraph

Source§

fn clone(&self) -> CsrGraph

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CsrGraph

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for CsrGraph

Source§

fn default() -> CsrGraph

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,