#[non_exhaustive]pub struct CsfLayout {
pub nnz: u64,
pub mode_order: Vec<u32>,
}Expand description
Descriptor for the CSF (Compressed Sparse Fiber) sparse layout.
CSF is the rank-N generalisation of CSR/CSC, storing a sparse tensor as a
tree of rank levels. Each level compresses one mode with a (pos, crd) pair;
the values buffer holds non-zero values at the leaves.
CSF is defined only for rank ≥ 3 tensors in this version of the specification.
Rank-2 sparse matrices are served by CSR/CSC; see crate::layout::CsrLayout.
buffer_count = 2·rank+1 comes from the layout descriptor alone because
mode_order.len() equals the tensor rank — the rank is embedded in the field,
not repeated separately.
§Buffer table
| Buffer index | Name | Element type | Length |
|---|---|---|---|
0 | values | tensor element type | nnz elements |
2L + 1 | pos_L | uint64 | n_{L-1} + 1 elements (2 for L=0) |
2L + 2 | crd_L | uint64 | n_L elements (nnz for the leaf level) |
where n_L is the count of tree nodes at level L, n_{-1} = 1 (virtual root),
and n_{rank-1} = nnz.
The byte_offset field MUST be 0 for CSF tensors — the first element is
reached by descending the tree, not at a fixed offset.
§Examples
use hurray_core::layout::{CsfLayout, LayoutDescriptor};
// Rank-3 sparse tensor with 4 non-zeros, identity mode_order.
let layout = CsfLayout::new(4, vec![0, 1, 2]);
let desc = LayoutDescriptor::Csf(layout);
assert_eq!(desc.tag(), 0x09);
// 2*rank+1 = 2*3+1 = 7 buffers.
assert_eq!(desc.buffer_count().map(|n| n.get()), Some(7));Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.nnz: u64Number of stored (non-zero) elements. MAY be 0 for an empty sparse tensor.
mode_order: Vec<u32>Permutation of 0..rank-1; mode_order[L] is the logical dimension stored at
tree level L. The tensor rank equals mode_order.len().
mode_order is stored here (rather than just rank) because buffer_count()
and validate_against_shape() both need it, and carrying the full permutation
avoids a separate rank field while making the accessor surface explicit.
Implementations§
Source§impl CsfLayout
impl CsfLayout
Sourcepub fn new(nnz: u64, mode_order: Vec<u32>) -> Self
pub fn new(nnz: u64, mode_order: Vec<u32>) -> Self
Creates a new CsfLayout with the given number of non-zeros and mode order.
This constructor does not validate that mode_order is a valid permutation
of 0..mode_order.len() or that mode_order.len() >= 3; call
crate::layout::LayoutDescriptor::validate_against_shape with the tensor
shape to perform those checks.
§Examples
use hurray_core::layout::CsfLayout;
// Rank-3 tensor, identity mode order, 10 non-zeros.
let c = CsfLayout::new(10, vec![0, 1, 2]);
assert_eq!(c.nnz, 10);
assert_eq!(c.mode_order, [0, 1, 2]);
// Rank-4 tensor with reordered modes.
let c4 = CsfLayout::new(0, vec![3, 0, 1, 2]);
assert_eq!(c4.mode_order.len(), 4);