MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / flat_index_for_coord

Function flat_index_for_coord

nodedb-array/src/tile/promotion.rs:80–110  ·  view source on GitHub ↗

Row-major flat index `((c0 - lo0) * extent1 * extent2 ...) + ...`.

(
    schema: &ArraySchema,
    coord: &[crate::types::coord::value::CoordValue],
)

Source from the content-addressed store, hash-verified

78
79/// Row-major flat index `((c0 - lo0) * extent1 * extent2 ...) + ...`.
80fn flat_index_for_coord(
81 schema: &ArraySchema,
82 coord: &[crate::types::coord::value::CoordValue],
83) -> ArrayResult<usize> {
84 use crate::schema::dim_spec::DimType;
85 use crate::types::coord::value::CoordValue;
86 use crate::types::domain::DomainBound;
87 let mut flat: usize = 0;
88 for (i, dim) in schema.dims.iter().enumerate() {
89 let extent = schema.tile_extents[i] as usize;
90 let lo = match (&dim.dtype, &dim.domain.lo) {
91 (DimType::Int64, DomainBound::Int64(v))
92 | (DimType::TimestampMs, DomainBound::TimestampMs(v)) => *v,
93 _ => 0,
94 };
95 let off = match coord.get(i) {
96 Some(CoordValue::Int64(v)) | Some(CoordValue::TimestampMs(v)) => {
97 ((*v - lo) as usize) % extent
98 }
99 _ => {
100 return Err(ArrayError::CoordOutOfDomain {
101 array: schema.name.clone(),
102 dim: dim.name.clone(),
103 detail: "non-integer coord in dense promotion".to_string(),
104 });
105 }
106 };
107 flat = flat * extent + off;
108 }
109 Ok(flat)
110}
111
112#[cfg(test)]
113mod tests {

Callers 1

sparse_to_denseFunction · 0.85

Calls 4

to_stringMethod · 0.80
iterMethod · 0.45
getMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected