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

Function tile_id_of_coord

nodedb-cluster/src/array_routing.rs:77–110  ·  view source on GitHub ↗

Compute the tile identifier for a coordinate given tile extents. Uses row-major ordering over the tile grid: each dimension's tile index is `coord[i] / tile_extents[i]`, and the final `tile_id` multiplies those indices by the product of the tile-grid dimensions for all *later* dimensions (standard C-order / row-major strides). Returns `None` when: - `coord` and `tile_extents` have different leng

(coord: &[u64], tile_extents: &[u64])

Source from the content-addressed store, hash-verified

75/// Callers that need a fallback should call [`vshard_for_array_coord`] directly,
76/// which transparently falls back to collection-level routing in all error cases.
77pub fn tile_id_of_coord(coord: &[u64], tile_extents: &[u64]) -> Option<u64> {
78 if coord.is_empty() || tile_extents.is_empty() {
79 return None;
80 }
81 if coord.len() != tile_extents.len() {
82 return None;
83 }
84 if tile_extents.contains(&0) {
85 return None;
86 }
87
88 // Compute per-dimension tile indices.
89 let tile_indices: Vec<u64> = coord
90 .iter()
91 .zip(tile_extents.iter())
92 .map(|(&c, &e)| c / e)
93 .collect();
94
95 // Compute row-major tile_id via stride accumulation from the last dimension.
96 // stride[i] = product of tile widths for dims i+1..N.
97 // We use the tile index itself as a conservative upper bound per dim, which
98 // preserves uniqueness for monotonically growing coordinate spaces and is
99 // sufficient as a hash pre-image.
100 let n = tile_indices.len();
101 let mut tile_id: u64 = 0;
102 let mut stride: u64 = 1;
103
104 for i in (0..n).rev() {
105 tile_id = tile_id.wrapping_add(tile_indices[i].wrapping_mul(stride));
106 stride = stride.wrapping_mul(tile_indices[i].wrapping_add(1).max(1));
107 }
108
109 Some(tile_id)
110}
111
112// ─── vShard routing ───────────────────────────────────────────────────────────
113

Calls 5

collectMethod · 0.80
is_emptyMethod · 0.45
lenMethod · 0.45
containsMethod · 0.45
iterMethod · 0.45