use rustc_hash::FxHashMap;
pub(super) fn reconstruct_void_index(
void_keys: &[u32],
void_counts: &[u32],
void_values: &[u32],
) -> FxHashMap<u32, Vec<u32>> {
let mut void_index: FxHashMap<u32, Vec<u32>> = FxHashMap::default();
if void_counts.len() != void_keys.len() {
return void_index;
}
let mut value_offset = 0usize;
for (i, &host_id) in void_keys.iter().enumerate() {
let count = void_counts[i] as usize;
let end = value_offset.saturating_add(count);
let Some(openings) = void_values.get(value_offset..end) else {
break;
};
void_index.insert(host_id, openings.to_vec());
value_offset = end;
}
void_index
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn well_formed_arrays_reconstruct() {
let idx = reconstruct_void_index(&[10, 20], &[2, 1], &[1, 2, 3]);
assert_eq!(idx.get(&10), Some(&vec![1, 2]));
assert_eq!(idx.get(&20), Some(&vec![3]));
}
#[test]
fn mismatched_counts_length_drops_index_not_panics() {
let idx = reconstruct_void_index(&[10, 20, 30], &[2], &[1, 2]);
assert!(idx.is_empty());
}
#[test]
fn overlong_count_stops_without_panic() {
let idx = reconstruct_void_index(&[10, 20], &[1, 99], &[7, 8]);
assert_eq!(idx.get(&10), Some(&vec![7]));
assert_eq!(idx.get(&20), None); }
}