use crate::{Error, Result};
pub fn element_offset(query: &[u64], row_indices: &[u64], col_ptr: &[u64]) -> Result<Option<u64>> {
if query.len() != 2 {
return Err(Error::IndexRankMismatch {
index_rank: query.len(),
shape_rank: 2,
});
}
let (row, col) = (query[0], query[1]);
if col_ptr.is_empty() {
return Err(Error::InvalidLayout(
"csc: col_ptr must have at least ncols+1 = 1 entries".into(),
));
}
let ncols = (col_ptr.len() - 1) as u64;
if col >= ncols {
return Err(Error::IndexOutOfRange {
dim: 1,
index: col,
size: ncols,
});
}
let start = col_ptr[col as usize] as usize;
let end = col_ptr[col as usize + 1] as usize;
if start > end || end > row_indices.len() {
return Err(Error::InvalidLayout(format!(
"csc: col_ptr[{col}..{}] = [{start}..{end}] out of bounds for row_indices.len()={}",
col + 1,
row_indices.len()
)));
}
match row_indices[start..end].binary_search(&row) {
Ok(k) => Ok(Some((start + k) as u64)),
Err(_) => Ok(None),
}
}
#[cfg(test)]
mod tests {
use super::*;
const ROW_INDICES: &[u64] = &[0, 3, 1, 3, 0, 3];
const COL_PTR: &[u64] = &[0, 2, 3, 4, 6];
#[test]
fn lookup_hits() {
assert_eq!(
element_offset(&[0, 0], ROW_INDICES, COL_PTR).unwrap(),
Some(0)
);
assert_eq!(
element_offset(&[3, 0], ROW_INDICES, COL_PTR).unwrap(),
Some(1)
);
assert_eq!(
element_offset(&[1, 1], ROW_INDICES, COL_PTR).unwrap(),
Some(2)
);
assert_eq!(
element_offset(&[3, 2], ROW_INDICES, COL_PTR).unwrap(),
Some(3)
);
assert_eq!(
element_offset(&[0, 3], ROW_INDICES, COL_PTR).unwrap(),
Some(4)
);
assert_eq!(
element_offset(&[3, 3], ROW_INDICES, COL_PTR).unwrap(),
Some(5)
);
}
#[test]
fn structural_zeros() {
assert_eq!(element_offset(&[1, 0], ROW_INDICES, COL_PTR).unwrap(), None);
assert_eq!(element_offset(&[0, 1], ROW_INDICES, COL_PTR).unwrap(), None);
assert_eq!(element_offset(&[0, 2], ROW_INDICES, COL_PTR).unwrap(), None); assert_eq!(element_offset(&[2, 3], ROW_INDICES, COL_PTR).unwrap(), None);
}
#[test]
fn wrong_rank_rejected() {
assert!(matches!(
element_offset(&[0], ROW_INDICES, COL_PTR),
Err(Error::IndexRankMismatch { .. })
));
}
#[test]
fn col_out_of_range_rejected() {
assert!(matches!(
element_offset(&[0, 4], ROW_INDICES, COL_PTR),
Err(Error::IndexOutOfRange { dim: 1, .. })
));
}
#[test]
fn empty_col_ptr_rejected() {
assert!(matches!(
element_offset(&[0, 0], ROW_INDICES, &[]),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn empty_matrix() {
assert_eq!(element_offset(&[0, 0], &[], &[0, 0, 0]).unwrap(), None);
}
}