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