still fast to construct

This commit is contained in:
Joe 2025-10-17 16:23:22 -07:00
parent f0b5244a1c
commit 6190a676e7

View file

@ -1,6 +1,6 @@
use std::{ use std::{
sync::{ sync::{
atomic::{AtomicU64, AtomicUsize, Ordering::Relaxed}, atomic::{AtomicUsize, Ordering::Relaxed},
Arc, RwLock, Arc, RwLock,
}, },
thread, thread,
@ -11,15 +11,15 @@ pub struct Point2d {
pub y: f64, pub y: f64,
} }
#[derive(Debug, Default)] #[derive(Debug, Default, Clone, Copy)]
pub struct PVal { pub struct PVal {
pub point: usize, pub point: usize,
pub val: AtomicU64, pub val: f64,
} }
impl PartialEq for PVal { impl PartialEq for PVal {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.val.load(Relaxed) == other.val.load(Relaxed) self.val == other.val
} }
} }
@ -27,18 +27,32 @@ impl Eq for PVal {}
impl PartialOrd for PVal { impl PartialOrd for PVal {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
let a = f64::from_bits(self.val.load(Relaxed)); Some(self.cmp(other))
let b = f64::from_bits(other.val.load(Relaxed));
a.partial_cmp(&b)
} }
} }
impl Ord for PVal { impl Ord for PVal {
fn cmp(&self, other: &Self) -> std::cmp::Ordering { fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other).unwrap() self.val
.partial_cmp(&other.val)
.unwrap_or(std::cmp::Ordering::Equal)
} }
} }
impl PVal {
pub fn new(point: usize, val: f64) -> Self {
Self { point, val }
}
pub fn point_index(&self) -> usize {
self.point
}
// pub fn val(&self) -> f64 {
// f64::from_bits(self.val)
// }
}
pub struct Spindex2d { pub struct Spindex2d {
pub xpoints: Arc<Vec<AtomicUsize>>, pub xpoints: Arc<Vec<AtomicUsize>>,
pub ypoints: Arc<Vec<AtomicUsize>>, pub ypoints: Arc<Vec<AtomicUsize>>,
@ -55,12 +69,12 @@ impl Spindex2d {
for (i, point) in points.iter().enumerate() { for (i, point) in points.iter().enumerate() {
let x = PVal { let x = PVal {
point: i, point: i,
val: AtomicU64::new(point.x.to_bits()), val: point.x,
}; };
xs.push(x); xs.push(x);
let y = PVal { let y = PVal {
point: i, point: i,
val: AtomicU64::new(point.y.to_bits()), val: point.y,
}; };
ys.push(y); ys.push(y);
@ -90,18 +104,12 @@ impl Spindex2d {
for (i, point) in points.iter().enumerate() { for (i, point) in points.iter().enumerate() {
let xi = self.xpoints[i].load(Relaxed); let xi = self.xpoints[i].load(Relaxed);
let yi = self.ypoints[i].load(Relaxed); let yi = self.ypoints[i].load(Relaxed);
let x = point.x.to_bits(); let x = point.x;
let x = PVal { let x = PVal { point: i, val: x };
point: i,
val: x.into(),
};
xs[xi] = x; xs[xi] = x;
let y = point.y.to_bits(); let y = point.y;
let y = PVal { let y = PVal { point: i, val: y };
point: i,
val: y.into(),
};
ys[yi] = y; ys[yi] = y;
} }
} }