#![allow(
clippy::unwrap_used,
clippy::float_cmp,
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::cast_sign_loss,
clippy::cast_possible_wrap
)]
use super::super::bounds::bounds_sq;
use super::super::bounds::tests::{Rng, libm_exp2};
use super::super::tests::{Searched, churned, exact_sq_int, integer_entries, trees};
use crate::Rect;
use crate::dynamic::DynamicIndex;
use crate::nearest::{Neighbor, QueryError};
use crate::static_index::Index;
use crate::tests::{CASES, r};
use proptest::prelude::*;
use std::format;
use std::string::{String, ToString};
use std::vec;
use std::vec::Vec;
fn brute_exact(index: &dyn Searched, x: i64, y: i64, k: usize) -> Vec<Neighbor> {
let mut all: Vec<(u128, u32, usize, Rect)> = index
.stored()
.into_iter()
.map(|(rect, id, pos)| (exact_sq_int(x, y, &rect), id, pos, rect))
.collect();
all.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.cmp(&b.1)).then(a.2.cmp(&b.2)));
let keep = if k == 0 {
0
} else if k >= all.len() {
all.len()
} else {
let kth = all[k - 1].0;
k + all[k..].iter().take_while(|e| e.0 == kth).count()
};
all.truncate(keep);
all.into_iter()
.map(|(_, id, _, rect)| {
let (lower_sq, upper_sq) = bounds_sq(x as f64, y as f64, &rect);
Neighbor {
id,
lower_sq,
upper_sq,
}
})
.collect()
}
fn ks(n: usize) -> [usize; 7] {
let m = Index::NODE_CAPACITY;
[0, 1, 2, m, n.saturating_sub(1), n, n + 5]
}
fn integer_case() -> impl Strategy<Value = (Vec<(Rect, u32)>, i64, i64)> {
(any::<u64>(), 1_usize..400, 0_usize..3).prop_map(|(seed, n, range)| {
let bound = [8_i64, 1 << 12, 1 << 26][range];
let mut rng = Rng::new(seed);
let entries = integer_entries(&mut rng, n, bound);
let (x, y) = (rng.int(bound), rng.int(bound));
(entries, x, y)
})
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(CASES))]
#[test]
fn exact_equals_brute_force_on_integer_coordinates((es, x, y) in integer_case()) {
for index in trees(&es) {
for k in ks(es.len()) {
let got = index.exact(x as f64, y as f64, k).unwrap();
let want = brute_exact(&*index, x, y, k);
prop_assert_eq!(got, want, "{} k = {}", index.which(), k);
}
}
}
#[test]
fn exact_agrees_with_the_candidates_and_across_trees((es, x, y) in integer_case(), k in 0_usize..24) {
let (px, py) = (x as f64, y as f64);
let mut sequences: Vec<Vec<Neighbor>> = Vec::new();
for index in trees(&es) {
let exact = index.exact(px, py, k).unwrap();
let cands = index.cands(px, py, k).unwrap();
let key = |n: &Neighbor| (n.lower_sq.to_bits(), n.upper_sq.to_bits(), n.id);
let mut have: Vec<_> = cands.iter().map(key).collect();
have.sort_unstable();
let mut sub: Vec<_> = exact.iter().map(key).collect();
sub.sort_unstable();
let mut rest = have.clone();
for e in &sub {
let at = rest.iter().position(|c| c == e);
prop_assert!(at.is_some(), "an exact entry is not a candidate");
rest.remove(at.unwrap());
}
let d2 = |n: &Neighbor| {
es.iter()
.filter(|(_, id)| *id == n.id)
.map(|(b, _)| exact_sq_int(x, y, b))
.next()
.unwrap()
};
if k > 0 && !exact.is_empty() {
let kth = d2(&exact[exact.len().min(k) - 1]);
let beyond = cands.iter().any(|c| d2(c) > kth);
if !beyond {
prop_assert_eq!(&sub, &have);
}
}
sequences.push(exact);
}
prop_assert_eq!(&sequences[0], &sequences[1]);
prop_assert_eq!(&sequences[0], &sequences[2]);
}
}
#[test]
fn the_exact_order_overrules_a_lower_bound_tie_broken_by_id() {
let p27 = libm_exp2(27);
let near = r(p27, 0.0, p27, 0.0); let far = r(p27, 1.0, p27, 1.0); for index in trees(&[(far, 1), (near, 2)]) {
let cands = index.cands(0.0, 0.0, 1).unwrap();
assert_eq!(cands.iter().map(|n| n.id).collect::<Vec<_>>(), [1, 2]);
assert_eq!(cands[0].lower_sq, cands[1].lower_sq, "the bounds tie");
let one = index.exact(0.0, 0.0, 1).unwrap();
assert_eq!(
one.iter().map(|n| n.id).collect::<Vec<_>>(),
[2],
"{}",
index.which()
);
let both = index.exact(0.0, 0.0, 2).unwrap();
assert_eq!(both.iter().map(|n| n.id).collect::<Vec<_>>(), [2, 1]);
}
}
#[test]
fn near_ties_one_ulp_apart_are_resolved() {
for e in [-500_i32, -30, 0, 26, 400] {
let s = libm_exp2(e);
let a = r(s, s, s, s);
let b = r(s * (1.0 + f64::EPSILON), s, s * (1.0 + f64::EPSILON), s);
for index in trees(&[(b, 1), (a, 2)]) {
let one = index.exact(0.0, 0.0, 1).unwrap();
assert_eq!(one.iter().map(|n| n.id).collect::<Vec<_>>(), [2], "e = {e}");
assert!(!index.cands(0.0, 0.0, 1).unwrap().is_empty());
}
}
}
#[test]
fn exact_ties_are_all_returned_in_id_order() {
let ring = [
(r(3.0, 4.0, 4.0, 5.0), 9),
(r(-5.0, -4.0, -4.0, -3.0), 3),
(r(4.0, -4.0, 5.0, -3.0), 7),
(r(-5.0, 3.0, -4.0, 4.0), 1),
(r(40.0, 40.0, 41.0, 41.0), 0),
];
for index in trees(&ring) {
for k in 1..=4 {
let got = index.exact(0.0, 0.0, k).unwrap();
assert_eq!(
got.iter().map(|n| n.id).collect::<Vec<_>>(),
[1, 3, 7, 9],
"k = {k}"
);
}
let all = index.exact(0.0, 0.0, 5).unwrap();
assert_eq!(
all.iter().map(|n| n.id).collect::<Vec<_>>(),
[1, 3, 7, 9, 0]
);
}
let nested: Vec<(Rect, u32)> = (0..12)
.map(|i| {
(
r(-f64::from(i) - 1.0, -1.0, f64::from(i) + 1.0, 1.0),
20 - i,
)
})
.collect();
for index in trees(&nested) {
let got = index.exact(0.0, 0.0, 1).unwrap();
let ids: Vec<u32> = got.iter().map(|n| n.id).collect();
assert_eq!(ids, (9..=20).collect::<Vec<u32>>());
}
}
#[test]
fn the_edges_of_k_and_the_refusals() {
let es = [
(r(0.0, 0.0, 1.0, 1.0), 4),
(r(5.0, 5.0, 6.0, 6.0), 2),
(r(2.0, 2.0, 3.0, 3.0), 3),
];
for index in trees(&es) {
assert_eq!(index.exact(0.0, 0.0, 0).unwrap(), []);
let all = index.exact(0.0, 0.0, 10).unwrap();
assert_eq!(all.iter().map(|n| n.id).collect::<Vec<_>>(), [4, 3, 2]);
for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
assert_eq!(index.exact(bad, 0.0, 1), Err(QueryError::NonFinite));
assert_eq!(index.exact(0.0, bad, 0), Err(QueryError::NonFinite));
}
}
for empty in trees(&[]) {
assert_eq!(empty.exact(1.0, 2.0, 3).unwrap(), []);
}
let mut drained = DynamicIndex::bulk_load(vec![(r(1.0, 1.0, 2.0, 2.0), 1)]);
assert!(drained.remove(&r(1.0, 1.0, 2.0, 2.0), 1));
assert_eq!(drained.nearest_exact(0.0, 0.0, 2).unwrap(), []);
}
#[test]
fn the_ranking_measures_to_the_box_not_its_center() {
let long = r(-100.0, 2.0, 100.0, 3.0);
let below = -2.0 - libm_exp2(-51);
let point = r(60.0, below, 60.0, below);
for index in trees(&[(point, 1), (long, 2)]) {
assert_eq!(
index.cands(60.0, 0.0, 1).unwrap().len(),
2,
"both are candidates"
);
let got = index.exact(60.0, 0.0, 1).unwrap();
assert_eq!(
got.iter().map(|n| n.id).collect::<Vec<_>>(),
[2],
"{}",
index.which()
);
}
}
fn hex(v: f64) -> String {
format!("{:016x}", v.to_bits())
}
#[test]
#[ignore = "writes the case file situs_nearest_exact.csv to the temporary directory"]
fn write_exact_csv() {
let mut rows: Vec<String> = Vec::new();
let mut rng = Rng::new(0x5EED_B0CD_8000_0001);
let regimes: [(i32, i32); 5] = [
(-8, 8),
(-1074, -1000),
(900, 1023),
(-30, 30),
(-520, -490),
];
let mut push = |x: f64, y: f64, k: usize, es: &[(Rect, u32)]| {
let s = Index::bulk_load(es.to_vec())
.nearest_exact(x, y, k)
.unwrap();
let d = churned(es).nearest_exact(x, y, k).unwrap();
let ids = |v: &[Neighbor]| {
v.iter()
.map(|n| n.id.to_string())
.collect::<Vec<_>>()
.join(";")
};
let boxes = es
.iter()
.map(|(b, id)| {
format!(
"{}:{}:{}:{}:{id}",
hex(b.min_x()),
hex(b.min_y()),
hex(b.max_x()),
hex(b.max_y())
)
})
.collect::<Vec<_>>()
.join(";");
rows.push(format!(
"{},{},{k},{boxes},{},{}",
hex(x),
hex(y),
ids(&s),
ids(&d)
));
};
for round in 0..52_000_usize {
let pe = regimes[round % regimes.len()];
let be = regimes[(round / regimes.len()) % regimes.len()];
let n = 2 + (rng.next() % 8) as usize;
let (x, y) = (rng.float_in(pe.0, pe.1), rng.float_in(pe.0, pe.1));
let mut es: Vec<(Rect, u32)> = Vec::with_capacity(n);
for id in 0..n as u32 {
let (a, b) = (rng.float_in(be.0, be.1), rng.float_in(be.0, be.1));
let (c, d) = if rng.next().is_multiple_of(3) {
(a, b) } else {
(rng.float_in(be.0, be.1), rng.float_in(be.0, be.1))
};
es.push((r(a.min(c), b.min(d), a.max(c), b.max(d)), id));
}
if rng.next().is_multiple_of(4) {
let (b0, _) = es[0];
let up = |v: f64| {
f64::from_bits(if v >= 0.0 {
v.to_bits() + 1
} else {
v.to_bits() - 1
})
};
let moved = Rect::new(
up(b0.min_x()),
b0.min_y(),
up(b0.max_x()).max(up(b0.min_x())),
b0.max_y(),
);
if let Ok(m) = moved {
es.push((m, n as u32));
}
}
let k = 1 + (rng.next() % (es.len() as u64 + 1)) as usize;
push(x, y, k, &es);
}
let path = std::env::temp_dir().join("situs_nearest_exact.csv");
std::println!("oracle CSV: {}", path.display());
let header = "x,y,k,entries,static_ids,dynamic_ids";
std::fs::write(&path, format!("{header}\n{}\n", rows.join("\n")))
.expect("write the oracle CSV");
assert!(rows.len() >= 50_000);
}