sprk 0.1.0

High-performance spatial index for radius queries in D-dimensional Euclidean space
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use num_traits::Float;

use crate::output::QueryOutput;
use crate::scalar::{IdStorage, Scalar};
use crate::simd::{CompressDispatch, LaneCount, PDVec, SupportedLaneCount, compress_with_ids};
use crate::svd::DynamicSvd;
use crate::tree::{LeafRange, Positions, Snn, build_tree, children, compute_total_depth};
use crate::vec_writer::VecWriter;

const W: usize = 8;
use std::array::from_fn;
use std::cell::Cell;
use std::mem::MaybeUninit;

// ── Position store for flat data ─────────────────────────────────────

pub(crate) struct FlatPositions<'a, F> {
    data: &'a [F],
    dim: usize,
}

impl<'a, F: Scalar> Positions<F> for FlatPositions<'a, F> {
    #[inline(always)]
    fn dim(&self) -> usize {
        self.dim
    }
    #[inline(always)]
    fn coord(&self, id: usize, dim: usize) -> F {
        self.data[id * self.dim + dim]
    }
}

// ── DynPDVec ─────────────────────────────────────────────────────────

#[derive(Clone, Debug)]
pub(crate) struct DynPDVec<const W: usize, F: Scalar = f32, I: IdStorage = u32> {
    lanes: Vec<[F; W]>,
    squared_half: [F; W],
    ids: [I; W],
}

impl<const W: usize, F: Scalar, I: IdStorage> DynPDVec<W, F, I> {
    fn new<'a>(dim: usize, vecs: impl Iterator<Item = (&'a [F], usize)>) -> Self {
        let mut result = Self::inf(dim);
        for (i, (vec, id)) in vecs.enumerate().take(W) {
            result.squared_half[i] = vec.iter().copied().map(|x| x * x).sum::<F>() * F::HALF;
            result.ids[i] = I::from_usize(id);
            for (lane, &v) in result.lanes.iter_mut().zip(vec) {
                lane[i] = v;
            }
        }
        result
    }

    fn inf(dim: usize) -> Self {
        Self {
            lanes: vec![[F::NAN; W]; dim],
            squared_half: [F::INFINITY; W],
            ids: [I::SENTINEL; W],
        }
    }

    #[inline(always)]
    fn dist_squared(&self, pos: &[F]) -> [F; W] {
        let diff: [F; W] = from_fn(|i| self.lanes[0][i] - pos[0]);
        let mut acc = diff.map(|x| x * x);
        for (lane, &p) in self.lanes[1..].iter().zip(&pos[1..]) {
            let diff: [F; W] = from_fn(|i| lane[i] - p);
            acc = from_fn(|i| Float::mul_add(diff[i], diff[i], acc[i]));
        }
        acc
    }

    #[inline(always)]
    fn dist_half_squared(&self, pos: &[F], squared_half: F) -> [F; W] {
        // let dim = self.lanes.len();
        const UNROLL: usize = 8;
        let mut accs: [_; UNROLL] = std::array::from_fn(|i| {
            if i == 0 {
                self.squared_half
            } else if i == 1 {
                [squared_half; W]
            } else {
                [F::ZERO; W]
            }
        });

        let (chunks, remainder) = self.lanes.as_chunks::<UNROLL>();
        let (pos_chunks, pos_remainder) = pos.as_chunks::<UNROLL>();
        for (chunk, pos_slice) in chunks.iter().zip(pos_chunks) {
            for ((acc, slice), &p) in accs.iter_mut().zip(chunk.iter()).zip(pos_slice.iter()) {
                *acc = from_fn(|i| Float::mul_add(slice[i], -p, acc[i]));
            }
        }
        let mut acc: [F; W] = accs[0];
        for (slice, &p) in remainder.iter().zip(pos_remainder.iter()) {
            acc = from_fn(|i| Float::mul_add(slice[i], -p, acc[i]));
        }
        for a in &accs[1..] {
            acc = from_fn(|i| acc[i] + a[i]);
        }

        acc
    }
}

impl<const W: usize, F: Scalar, I: IdStorage> DynPDVec<W, F, I>
where
    LaneCount<W>: SupportedLaneCount,
    PDVec<1, W, F, I>: CompressDispatch<W, F, I>,
{
    #[inline(always)]
    fn compress(&self, distances: [F; W], threshold: F) -> (usize, [I; W], [F; W]) {
        compress_with_ids(self.ids, distances, threshold)
    }

    #[inline(always)]
    fn compare_into<O: QueryOutput<I, F>>(
        &self,
        distances: [F; W],
        threshold: F,
        results: &mut [MaybeUninit<O>; W],
    ) -> usize {
        let (count, ids, dists) = self.compress(distances, threshold);
        O::store_compressed(count, &ids, &dists, results)
    }
}

// ── DynPoint ─────────────────────────────────────────────────────────

struct DynPoint<F: Scalar> {
    pos: Vec<F>,
    squared_half: F,
}

impl<F: Scalar> DynPoint<F> {
    fn new(pos: &[F]) -> Self {
        let squared_half = pos.iter().copied().map(|x| x * x).sum::<F>() * F::HALF;
        Self {
            pos: pos.to_vec(),
            squared_half,
        }
    }
}

// ── DynSprk ─────────────────────────────────────────────────────────

thread_local! {
    static SCRATCH: Cell<Vec<LeafRange>> = Cell::new(Vec::with_capacity(128));
}

/// Sprk with runtime-specified dimensionality.
///
/// Unlike [`Sprk`](crate::Sprk) which uses const generics for the dimension,
/// `DynSprk` accepts the dimension at construction time. Positions are stored
/// as a flat `&[F]` with stride equal to `dim`.
#[derive(Clone)]
pub struct DynSprk<F: Scalar = f32, I: IdStorage = u32> {
    dim: usize,
    projected_dim: usize,
    positions: Vec<F>,
    positions_sorted: Vec<DynPDVec<W, F, I>>,
    node_ids: Vec<usize>,
    d_pos: Vec<F>,
    nodes: Vec<F>,
    leaves: Vec<Snn<F>>,
    total_depth: usize,
    svd: DynamicSvd<F>,
}

impl<F: Scalar, I: IdStorage> DynSprk<F, I>
where
    usize: QueryOutput<I, F>,
    PDVec<1, W, F, I>: CompressDispatch<W, F, I>,
{
    /// Build a new DynSprk from flat position data.
    ///
    /// `positions` has length `n * dim`, laid out as
    /// `[x0, y0, z0, x1, y1, z1, ...]`.
    pub fn new(dim: usize, positions: &[F]) -> Self {
        assert!(dim > 0, "dimension must be at least 1");
        assert!(
            positions.len().is_multiple_of(dim),
            "positions length must be a multiple of dim"
        );
        let n = positions.len() / dim;
        let td = compute_total_depth(n);
        let num_internal = (1usize << td) - 1;
        let num_leaves = 1usize << td;

        let mut tree = DynSprk {
            dim,
            projected_dim: dim.min(td + 1),
            positions: positions.to_vec(),
            positions_sorted: Vec::new(),
            node_ids: Vec::new(),
            d_pos: Vec::new(),
            nodes: vec![F::ZERO; num_internal],
            leaves: vec![Snn::default(); num_leaves],
            total_depth: td,
            svd: DynamicSvd::new(),
        };
        if !positions.is_empty() {
            tree.update(positions);
        }
        tree
    }

    /// Rebuild the tree with new positions. Reuses allocations where possible.
    ///
    /// `positions` must have length `n * dim` (same dim as construction).
    pub fn update(&mut self, positions: &[F]) {
        assert!(positions.len().is_multiple_of(self.dim));
        self.positions.copy_from_slice(positions);
        let n = positions.len() / self.dim;

        let td = compute_total_depth(n);
        self.total_depth = td;
        let k = self.dim.min(td + 1);
        self.projected_dim = k;

        self.svd
            .compute_svd(&positions.chunks(self.dim).collect::<Vec<_>>());

        let positions_projected = self.svd.project_all(positions, self.dim, k);

        let num_internal = (1usize << td) - 1;
        let num_leaves = 1usize << td;

        let mut nodes = std::mem::take(&mut self.nodes);
        let mut leaves = std::mem::take(&mut self.leaves);
        if nodes.len() != num_internal {
            nodes = vec![F::ZERO; num_internal];
        }
        if leaves.len() != num_leaves {
            leaves = vec![Snn::default(); num_leaves];
        }

        let mut node_ids: Vec<_> = (0..n).collect();
        let mut d_pos = vec![F::ZERO; node_ids.len()];

        let pos_view = FlatPositions {
            data: &positions_projected,
            dim: k,
        };
        build_tree(
            &mut nodes,
            &mut leaves,
            node_ids.as_mut_slice(),
            d_pos.as_mut_slice(),
            0,
            td,
            0,
            &pos_view,
            0,
        );

        self.nodes = nodes;
        self.leaves = leaves;
        self.node_ids = node_ids;
        self.positions_sorted.clear();
        self.positions_sorted.reserve(n);

        let dim = self.dim;
        for snn in self.leaves.iter_mut() {
            if snn.lut.is_empty() {
                continue;
            }
            let offset = snn.lut[0];
            let last = snn.lut.last().expect("empty lut");
            let node_ids = &self.node_ids[offset..*last];
            let new_offset = self.positions_sorted.len();

            for chunk in node_ids.chunks(W) {
                let pdvec = DynPDVec::new(
                    dim,
                    chunk.iter().map(|id| {
                        let start = *id * dim;
                        (&self.positions[start..start + dim], *id)
                    }),
                );
                self.positions_sorted.push(pdvec);
            }
            let half_len = snn.lut.len() / 2;
            for lut_entry in &mut snn.lut[0..half_len] {
                *lut_entry = (*lut_entry - offset) / W + new_offset;
            }
            for lut_entry in &mut snn.lut[half_len..] {
                *lut_entry = (*lut_entry - offset).div_ceil(W) + new_offset;
            }
        }
        self.d_pos = d_pos;
    }

    /// Number of indexed positions.
    pub fn len(&self) -> usize {
        self.positions.len() / self.dim
    }

    /// Whether the tree is empty.
    pub fn is_empty(&self) -> bool {
        self.positions.is_empty()
    }

    /// Dimensionality of the indexed positions.
    pub fn dim(&self) -> usize {
        self.dim
    }

    /// Returns the stored position for a given index as a slice.
    pub fn position(&self, index: usize) -> &[F] {
        let start = index * self.dim;
        &self.positions[start..start + self.dim]
    }

    /// Find all points within Euclidean `radius` of `pos`.
    ///
    /// Results are appended to `results`, which is not cleared first. The output
    /// type `O` is determined by the [`QueryOutput`] trait — use `usize` for
    /// indices only, or [`IdDist<usize, f32>`](crate::IdDist) for (index, squared distance) pairs.
    ///
    /// # Panics
    ///
    /// Panics if `pos.len() != self.dim()`.
    pub fn query_radius<O>(&self, pos: &[F], radius: F, results: &mut Vec<O>)
    where
        O: QueryOutput<I, F>,
    {
        assert_eq!(pos.len(), self.dim);
        let pos_projected = DynPoint::new(&self.svd.project_truncated(pos, self.projected_dim));
        let pos = DynPoint::new(pos);
        let radius_sq = radius * radius;
        let normalized_radius = self.svd.normalize_radius(radius);
        let norm_radius_sq = normalized_radius * normalized_radius;

        SCRATCH.with(|scratch| {
            let mut ranges = scratch.take();
            ranges.clear();

            let mut distances = vec![F::ZERO; self.projected_dim];
            let _ = self.collect_ranges(
                &pos_projected,
                0,
                0,
                norm_radius_sq,
                &mut distances,
                &mut ranges,
            );

            self.snn(results, &pos, radius_sq, &ranges);

            scratch.set(ranges);
        });
    }

    fn collect_ranges(
        &self,
        pos: &DynPoint<F>,
        depth: usize,
        heap_idx: usize,
        dim_radius_squared: F,
        distances: &mut [F],
        out: &mut Vec<LeafRange>,
    ) -> usize {
        let dim = depth % self.dim;

        if depth == self.total_depth {
            let leaf_idx = heap_idx - ((1 << self.total_depth) - 1);
            let snn = &self.leaves[leaf_idx];
            if snn.lut.is_empty() {
                return 0;
            }

            let own_pos = pos.pos[dim] - snn.min;
            let reduced_radius = Float::sqrt(dim_radius_squared + distances[dim]);
            let min = own_pos - reduced_radius;
            let max = own_pos + reduced_radius;
            let max_lut = snn.lut.len() / 2 - 1;

            let min_scaled = min * snn.resolution;
            let idx = if min_scaled >= F::ZERO {
                min_scaled.to_usize_unchecked()
            } else {
                0
            }
            .min(max_lut);
            let max_scaled = max * snn.resolution;
            let end_idx = if max_scaled >= F::ZERO {
                max_scaled.to_usize_unchecked()
            } else {
                0
            }
            .min(max_lut);

            let min_i = snn.lut[idx];
            let max_i = snn.lut[end_idx + snn.lut.len() / 2];

            let pdvec_count = max_i - min_i;
            out.push(LeafRange { min_i, max_i });
            return pdvec_count;
        }

        let split = self.nodes[heap_idx];
        let (left, right) = children(heap_idx);
        let own_pos = pos.pos[dim];
        let current_delta = distances[dim];
        let dist = Float::powi(own_pos - split, 2);
        let other_radius = dim_radius_squared + current_delta - dist;

        let mut total = 0;

        if own_pos < split {
            total += self.collect_ranges(pos, depth + 1, left, dim_radius_squared, distances, out);
            distances[dim] = dist;
            if other_radius > F::ZERO {
                total += self.collect_ranges(pos, depth + 1, right, other_radius, distances, out);
            }
            distances[dim] = current_delta;
        } else {
            distances[dim] = dist;
            if other_radius > F::ZERO {
                total += self.collect_ranges(pos, depth + 1, left, other_radius, distances, out);
            }
            distances[dim] = current_delta;
            total += self.collect_ranges(pos, depth + 1, right, dim_radius_squared, distances, out);
        }
        total
    }

    fn snn<O>(&self, results: &mut Vec<O>, pos: &DynPoint<F>, radius_sq: F, ranges: &[LeafRange])
    where
        O: QueryOutput<I, F>,
    {
        let mut writer = VecWriter::new(results);
        let half_radius_threshold = radius_sq * F::HALF + F::from_f32(1e-4).unwrap();
        let use_half = self.dim >= 6;

        for range in ranges.iter() {
            writer.ensure_capacity((range.max_i - range.min_i) * W + W - 1);

            for other_pos in &self.positions_sorted[range.min_i..range.max_i] {
                // SAFETY: ensure_capacity was called above with enough room for
                // all PDVecs in this range. compare_into initializes exactly
                // new_elements entries in the chunk.
                let chunk = unsafe { writer.next_chunk_unchecked::<W>() };
                let new_elements = if !use_half {
                    let distances = other_pos.dist_squared(&pos.pos);
                    other_pos.compare_into(distances, radius_sq, chunk)
                } else {
                    let distances = other_pos.dist_half_squared(&pos.pos, pos.squared_half);
                    other_pos.compare_into(distances, half_radius_threshold, chunk)
                };
                unsafe { writer.advance(new_elements) };
            }
        }
        writer.finish();
    }
}