back to fast construction
This commit is contained in:
parent
9dc0c4b9aa
commit
f0b5244a1c
1 changed files with 23 additions and 22 deletions
45
src/lib.rs
45
src/lib.rs
|
@ -1,26 +1,25 @@
|
|||
use std::{
|
||||
sync::{
|
||||
atomic::{AtomicUsize, Ordering::Relaxed},
|
||||
atomic::{AtomicU64, AtomicUsize, Ordering::Relaxed},
|
||||
Arc, RwLock,
|
||||
},
|
||||
thread,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
|
||||
pub struct Point2d {
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
#[derive(Debug, Default)]
|
||||
pub struct PVal {
|
||||
point: usize,
|
||||
val: f64,
|
||||
pub point: usize,
|
||||
pub val: AtomicU64,
|
||||
}
|
||||
|
||||
impl PartialEq for PVal {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.val == other.val
|
||||
self.val.load(Relaxed) == other.val.load(Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,22 +27,23 @@ impl Eq for PVal {}
|
|||
|
||||
impl PartialOrd for PVal {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
let a = f64::from_bits(self.val.load(Relaxed));
|
||||
let b = f64::from_bits(other.val.load(Relaxed));
|
||||
a.partial_cmp(&b)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for PVal {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.val.partial_cmp(&other.val).unwrap()
|
||||
self.partial_cmp(other).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Spindex2d {
|
||||
xpoints: Arc<Vec<AtomicUsize>>,
|
||||
ypoints: Arc<Vec<AtomicUsize>>,
|
||||
xs: Arc<RwLock<Vec<PVal>>>,
|
||||
ys: Arc<RwLock<Vec<PVal>>>,
|
||||
pub xpoints: Arc<Vec<AtomicUsize>>,
|
||||
pub ypoints: Arc<Vec<AtomicUsize>>,
|
||||
pub xs: Arc<RwLock<Vec<PVal>>>,
|
||||
pub ys: Arc<RwLock<Vec<PVal>>>,
|
||||
}
|
||||
|
||||
impl Spindex2d {
|
||||
|
@ -55,22 +55,22 @@ impl Spindex2d {
|
|||
for (i, point) in points.iter().enumerate() {
|
||||
let x = PVal {
|
||||
point: i,
|
||||
val: point.x,
|
||||
val: AtomicU64::new(point.x.to_bits()),
|
||||
};
|
||||
xs.push(x);
|
||||
let y = PVal {
|
||||
point: i,
|
||||
val: point.y,
|
||||
val: AtomicU64::new(point.y.to_bits()),
|
||||
};
|
||||
ys.push(y);
|
||||
|
||||
xpoints.push(i.into());
|
||||
ypoints.push(i.into());
|
||||
xpoints.push(AtomicUsize::new(i));
|
||||
ypoints.push(AtomicUsize::new(i));
|
||||
}
|
||||
|
||||
let index = Self {
|
||||
xpoints: Arc::new(xpoints),
|
||||
ypoints: Arc::new(ypoints),
|
||||
xpoints: xpoints.into(),
|
||||
ypoints: ypoints.into(),
|
||||
xs: Arc::new(RwLock::new(xs)),
|
||||
ys: Arc::new(RwLock::new(ys)),
|
||||
};
|
||||
|
@ -90,15 +90,17 @@ impl Spindex2d {
|
|||
for (i, point) in points.iter().enumerate() {
|
||||
let xi = self.xpoints[i].load(Relaxed);
|
||||
let yi = self.ypoints[i].load(Relaxed);
|
||||
let x = point.x.to_bits();
|
||||
let x = PVal {
|
||||
point: i,
|
||||
val: point.x,
|
||||
val: x.into(),
|
||||
};
|
||||
xs[xi] = x;
|
||||
|
||||
let y = point.y.to_bits();
|
||||
let y = PVal {
|
||||
point: i,
|
||||
val: point.y,
|
||||
val: y.into(),
|
||||
};
|
||||
ys[yi] = y;
|
||||
}
|
||||
|
@ -123,7 +125,6 @@ impl Spindex2d {
|
|||
let yhandle = thread::spawn(move || {
|
||||
let mut ys = ys.write().unwrap();
|
||||
ys.sort_unstable();
|
||||
|
||||
for (i, y) in ys.iter().enumerate() {
|
||||
let p = y.point;
|
||||
points[p].store(i, Relaxed);
|
||||
|
|
Loading…
Reference in a new issue