use super::*;
pub(super) struct ChunkSums {
pub(super) probe: usize,
pub(super) keys: crate::intern::Chunk,
pub(super) sums: Vec<(u128, u64, Aabb)>,
pub(super) by_shard: Vec<u32>,
pub(super) shard_ends: Vec<u32>,
}
pub(super) const FIXED: f64 = 18_446_744_073_709_551_616.0;
impl ChunkSums {
pub(super) fn shard(&self, s: usize) -> &[u32] {
if self.shard_ends.is_empty() {
return &[];
}
let start = if s == 0 { 0 } else { self.shard_ends[s - 1] };
&self.by_shard[start as usize..self.shard_ends[s] as usize]
}
}
pub(super) const SHARDS: usize = 64;
pub(super) fn shard_of(hash: u64) -> usize {
(hash >> 58) as usize
}
#[derive(Default)]
pub(super) struct Shard {
pub(super) index: FxHashMap<Box<[u32]>, u32>,
pub(super) keys: Vec<Box<[u32]>>,
pub(super) first: Vec<u64>,
pub(super) lengths: Vec<u128>,
pub(super) segments: Vec<u64>,
pub(super) bboxes: Vec<Aabb>,
}
impl Shard {
pub(super) fn add(&mut self, c: &ChunkSums, s: usize, chunk: u32, probes: usize) {
for &i in c.shard(s) {
let key = c.keys.key(i as usize);
let at = (chunk as u64) << 32 | i as u64;
let j = match self.index.get(key) {
Some(&j) => {
let j = j as usize;
self.first[j] = self.first[j].min(at);
j
}
None => {
let j = self.keys.len();
self.index.insert(key.into(), j as u32);
self.keys.push(key.into());
self.first.push(at);
self.lengths.resize(self.lengths.len() + probes, 0);
self.segments.push(0);
self.bboxes.push(Aabb::EMPTY);
j
}
};
let (len, n, bbox) = &c.sums[i as usize];
self.lengths[j * probes + c.probe] += len;
self.segments[j] += n;
self.bboxes[j] = self.bboxes[j].union(bbox);
}
}
pub(super) fn volume(&self, j: usize, probes: usize) -> Volume {
Volume {
lengths: self.lengths[j * probes..(j + 1) * probes]
.iter()
.map(|&l| l as f64 / FIXED)
.collect(),
segments: self.segments[j],
bbox: self.bboxes[j],
..Volume::empty(0)
}
}
}