removed atomics, still fast
This commit is contained in:
parent
6190a676e7
commit
dec3ea0e60
1 changed files with 33 additions and 35 deletions
68
src/lib.rs
68
src/lib.rs
|
@ -1,11 +1,9 @@
|
||||||
use std::{
|
use std::{
|
||||||
sync::{
|
sync::{Arc, RwLock},
|
||||||
atomic::{AtomicUsize, Ordering::Relaxed},
|
|
||||||
Arc, RwLock,
|
|
||||||
},
|
|
||||||
thread,
|
thread,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
|
||||||
pub struct Point2d {
|
pub struct Point2d {
|
||||||
pub x: f64,
|
pub x: f64,
|
||||||
pub y: f64,
|
pub y: f64,
|
||||||
|
@ -13,8 +11,8 @@ pub struct Point2d {
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Copy)]
|
#[derive(Debug, Default, Clone, Copy)]
|
||||||
pub struct PVal {
|
pub struct PVal {
|
||||||
pub point: usize,
|
point_index: usize,
|
||||||
pub val: f64,
|
val: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for PVal {
|
impl PartialEq for PVal {
|
||||||
|
@ -41,21 +39,25 @@ impl Ord for PVal {
|
||||||
|
|
||||||
impl PVal {
|
impl PVal {
|
||||||
pub fn new(point: usize, val: f64) -> Self {
|
pub fn new(point: usize, val: f64) -> Self {
|
||||||
Self { point, val }
|
Self {
|
||||||
|
point_index: point,
|
||||||
|
val,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn point_index(&self) -> usize {
|
pub fn point_index(&self) -> usize {
|
||||||
self.point
|
self.point_index
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn val(&self) -> f64 {
|
pub fn val(&self) -> f64 {
|
||||||
// f64::from_bits(self.val)
|
self.val
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct Spindex2d {
|
pub struct Spindex2d {
|
||||||
pub xpoints: Arc<Vec<AtomicUsize>>,
|
pub xpoints: Arc<RwLock<Vec<usize>>>,
|
||||||
pub ypoints: Arc<Vec<AtomicUsize>>,
|
pub ypoints: Arc<RwLock<Vec<usize>>>,
|
||||||
pub xs: Arc<RwLock<Vec<PVal>>>,
|
pub xs: Arc<RwLock<Vec<PVal>>>,
|
||||||
pub ys: Arc<RwLock<Vec<PVal>>>,
|
pub ys: Arc<RwLock<Vec<PVal>>>,
|
||||||
}
|
}
|
||||||
|
@ -67,24 +69,18 @@ impl Spindex2d {
|
||||||
let mut xs = Vec::with_capacity(points.len());
|
let mut xs = Vec::with_capacity(points.len());
|
||||||
let mut ys = Vec::with_capacity(points.len());
|
let mut ys = Vec::with_capacity(points.len());
|
||||||
for (i, point) in points.iter().enumerate() {
|
for (i, point) in points.iter().enumerate() {
|
||||||
let x = PVal {
|
let x = PVal::new(i, point.x);
|
||||||
point: i,
|
|
||||||
val: point.x,
|
|
||||||
};
|
|
||||||
xs.push(x);
|
xs.push(x);
|
||||||
let y = PVal {
|
let y = PVal::new(i, point.y);
|
||||||
point: i,
|
|
||||||
val: point.y,
|
|
||||||
};
|
|
||||||
ys.push(y);
|
ys.push(y);
|
||||||
|
|
||||||
xpoints.push(AtomicUsize::new(i));
|
xpoints.push(i);
|
||||||
ypoints.push(AtomicUsize::new(i));
|
ypoints.push(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
let index = Self {
|
let index = Self {
|
||||||
xpoints: xpoints.into(),
|
xpoints: Arc::new(xpoints.into()),
|
||||||
ypoints: ypoints.into(),
|
ypoints: Arc::new(ypoints.into()),
|
||||||
xs: Arc::new(RwLock::new(xs)),
|
xs: Arc::new(RwLock::new(xs)),
|
||||||
ys: Arc::new(RwLock::new(ys)),
|
ys: Arc::new(RwLock::new(ys)),
|
||||||
};
|
};
|
||||||
|
@ -101,15 +97,15 @@ impl Spindex2d {
|
||||||
{
|
{
|
||||||
let mut xs = self.xs.write().unwrap();
|
let mut xs = self.xs.write().unwrap();
|
||||||
let mut ys = self.ys.write().unwrap();
|
let mut ys = self.ys.write().unwrap();
|
||||||
|
let xpoints = self.xpoints.read().unwrap();
|
||||||
|
let ypoints = self.ypoints.read().unwrap();
|
||||||
for (i, point) in points.iter().enumerate() {
|
for (i, point) in points.iter().enumerate() {
|
||||||
let xi = self.xpoints[i].load(Relaxed);
|
let xi = xpoints[i];
|
||||||
let yi = self.ypoints[i].load(Relaxed);
|
let yi = ypoints[i];
|
||||||
let x = point.x;
|
let x = PVal::new(i, point.x);
|
||||||
let x = PVal { point: i, val: x };
|
|
||||||
xs[xi] = x;
|
xs[xi] = x;
|
||||||
|
|
||||||
let y = point.y;
|
let y = PVal::new(i, point.y);
|
||||||
let y = PVal { point: i, val: y };
|
|
||||||
ys[yi] = y;
|
ys[yi] = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,9 +118,10 @@ impl Spindex2d {
|
||||||
let xhandle = thread::spawn(move || {
|
let xhandle = thread::spawn(move || {
|
||||||
let mut xs = 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() {
|
for (i, x) in xs.iter().enumerate() {
|
||||||
let p = x.point;
|
let p = x.point_index;
|
||||||
points[p].store(i, Relaxed);
|
points[p] = i;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -133,9 +130,10 @@ impl Spindex2d {
|
||||||
let yhandle = thread::spawn(move || {
|
let yhandle = thread::spawn(move || {
|
||||||
let mut ys = ys.write().unwrap();
|
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() {
|
for (i, y) in ys.iter().enumerate() {
|
||||||
let p = y.point;
|
let p = y.point_index;
|
||||||
points[p].store(i, Relaxed);
|
points[p] = i;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue