pub fn element_offset(
query_coords: &[u64],
mode_order: &[u32],
pos_levels: &[&[u64]],
crd_levels: &[&[u64]],
) -> Result<Option<u64>>Expand description
Looks up the storage offset (values buffer index) for a logical index in a
CSF tensor, or returns None if the element is structurally absent (implicit zero).
§Arguments
query_coords— logical index[idx[0], ..., idx[rank-1]]; length must equalrank.mode_order— the level-to-dimension permutation from the CSF descriptor.pos_levels— slice ofrankpos arrays;pos_levels[L]is thepos_Lbuffer as a slice ofuint64values.crd_levels— slice ofrankcrd arrays;crd_levels[L]is thecrd_Lbuffer as a slice ofuint64values.
§Return value
Returns Ok(Some(p)) where p is the leaf position in values, or Ok(None) if
the element is not stored (structural zero). Returns Err only on structural
violations (malformed buffers that make the search impossible without panicking),
not on valid structural-zero hits.
§Errors
Error::IndexRankMismatch—query_coords.len() != mode_order.len().Error::IndexOutOfRange— any query coordinate is out of bounds for its mode. (Caller must validate coordinates against the shape before calling.)Error::InvalidLayout— a pos/crd buffer is so malformed that a safe traversal is impossible (e.g.pos[p] > crd.len()).Error::AddressOverflow— intermediate index arithmetic overflowedu64.
§Examples
use hurray_core::layout::addressing::csf::element_offset;
// Rank-3 sparse tensor with shape [2, 3, 4], mode_order = [0, 1, 2], nnz = 4.
// Non-zeros: (0,0,1)→1.0, (0,2,3)→2.0, (1,1,0)→3.0, (1,1,2)→4.0
//
// Level 0: pos_0 = [0, 2], crd_0 = [0, 1]
// Level 1: pos_1 = [0, 2, 3], crd_1 = [0, 2, 1]
// Level 2: pos_2 = [0, 1, 2, 4], crd_2 = [1, 3, 0, 2]
let mode_order: &[u32] = &[0, 1, 2];
let pos_levels: &[&[u64]] = &[&[0, 2], &[0, 2, 3], &[0, 1, 2, 4]];
let crd_levels: &[&[u64]] = &[&[0, 1], &[0, 2, 1], &[1, 3, 0, 2]];
// Lookup (1, 1, 2) → leaf position 3 (values[3] = 4.0).
assert_eq!(
element_offset(&[1, 1, 2], mode_order, pos_levels, crd_levels).unwrap(),
Some(3)
);
// Lookup (0, 1, 0) → not stored (structural zero).
assert_eq!(
element_offset(&[0, 1, 0], mode_order, pos_levels, crd_levels).unwrap(),
None
);