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