Skip to main content

element_offset

Function element_offset 

Source
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 equal rank.
  • mode_order — the level-to-dimension permutation from the CSF descriptor.
  • pos_levels — slice of rank pos arrays; pos_levels[L] is the pos_L buffer as a slice of uint64 values.
  • crd_levels — slice of rank crd arrays; crd_levels[L] is the crd_L buffer as a slice of uint64 values.

§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

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