remove redundant internal index, much faster now

This commit is contained in:
Joe 2025-10-18 11:34:07 -07:00
parent 2496741ec7
commit 3fb418a525

View file

@ -1,7 +1,4 @@
use std::{
sync::{Arc, RwLock},
thread,
};
use std::{sync::RwLock, thread};
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
pub struct Point2d {
@ -54,20 +51,15 @@ impl PVal {
}
}
#[derive(Debug, Default, Clone)]
#[derive(Debug, Default)]
pub struct Spindex2d {
pub xpoints: Arc<RwLock<Vec<usize>>>,
pub ypoints: Arc<RwLock<Vec<usize>>>,
pub xs: Arc<RwLock<Vec<PVal>>>,
pub ys: Arc<RwLock<Vec<PVal>>>,
pub xs: RwLock<Vec<PVal>>,
pub ys: RwLock<Vec<PVal>>,
}
impl Spindex2d {
pub fn new(points: &[Point2d]) -> Self {
let mut xpoints = Vec::with_capacity(points.len());
let mut xs = Vec::with_capacity(points.len());
let mut ypoints = Vec::with_capacity(points.len());
let mut ys = Vec::with_capacity(points.len());
thread::scope(|s| {
@ -75,7 +67,6 @@ impl Spindex2d {
for (i, point) in points.iter().enumerate() {
let x = PVal::new(i, point.x);
xs.push(x);
xpoints.push(i);
}
});
@ -83,16 +74,13 @@ impl Spindex2d {
for (i, point) in points.iter().enumerate() {
let y = PVal::new(i, point.y);
ys.push(y);
ypoints.push(i);
}
});
});
let index = Self {
xpoints: Arc::new(RwLock::new(xpoints)),
ypoints: Arc::new(RwLock::new(ypoints)),
xs: Arc::new(RwLock::new(xs)),
ys: Arc::new(RwLock::new(ys)),
xs: xs.into(),
ys: ys.into(),
};
index.sort();
@ -107,23 +95,15 @@ impl Spindex2d {
thread::scope(|s| {
s.spawn(|| {
let mut xs = self.xs.write().unwrap();
let xpoints = self.xpoints.read().unwrap();
for (i, point) in points.iter().enumerate() {
let xi = xpoints[i];
let x = PVal::new(i, point.x);
xs[xi] = x;
for point in xs.iter_mut() {
point.val = points[point.point_index].x;
}
});
s.spawn(|| {
let mut ys = self.ys.write().unwrap();
let ypoints = self.ypoints.read().unwrap();
for (i, point) in points.iter().enumerate() {
let yi = ypoints[i];
let y = PVal::new(i, point.y);
ys[yi] = y;
for point in ys.iter_mut() {
point.val = points[point.point_index].y;
}
});
});
@ -132,31 +112,16 @@ impl Spindex2d {
}
fn sort(&self) {
let xs = self.xs.clone();
let points = self.xpoints.clone();
let xhandle = thread::spawn(move || {
let mut xs = xs.write().unwrap();
xs.sort_unstable();
let mut points = points.write().unwrap();
for (i, x) in xs.iter().enumerate() {
let p = x.point_index;
points[p] = i;
}
});
thread::scope(|s| {
s.spawn(|| {
let mut xs = self.xs.write().unwrap();
xs.sort_unstable();
});
let ys = self.ys.clone();
let points = self.ypoints.clone();
let yhandle = thread::spawn(move || {
let mut ys = ys.write().unwrap();
ys.sort_unstable();
let mut points = points.write().unwrap();
for (i, y) in ys.iter().enumerate() {
let p = y.point_index;
points[p] = i;
}
s.spawn(|| {
let mut ys = self.ys.write().unwrap();
ys.sort_unstable();
});
});
xhandle.join().unwrap();
yhandle.join().unwrap();
}
}