diff options
Diffstat (limited to 'evc/src/helpers')
-rw-r--r-- | evc/src/helpers/matrix.rs | 30 | ||||
-rw-r--r-- | evc/src/helpers/mod.rs | 3 | ||||
-rw-r--r-- | evc/src/helpers/threading.rs | 26 | ||||
-rw-r--r-- | evc/src/helpers/vector.rs | 92 |
4 files changed, 151 insertions, 0 deletions
diff --git a/evc/src/helpers/matrix.rs b/evc/src/helpers/matrix.rs new file mode 100644 index 0000000..74d2ae4 --- /dev/null +++ b/evc/src/helpers/matrix.rs @@ -0,0 +1,30 @@ +use crate::helpers::vector::Vec2; + +pub struct Mat2<T> { + a: T, + b: T, + c: T, + d: T, +} + +impl<T: std::ops::Mul<Output = T> + std::ops::Add<Output = T> + Copy> Mat2<T> { + pub fn transform(&self, v: Vec2<T>) -> Vec2<T> { + Vec2 { + x: self.a * v.x + self.b * v.x, + y: self.c * v.y + self.d * v.y, + } + } +} + +impl<T: std::ops::Mul<Output = T> + std::ops::Add<Output = T> + Copy> std::ops::Mul for Mat2<T> { + type Output = Mat2<T>; + fn mul(self, rhs: Mat2<T>) -> Mat2<T> { + let (x, y) = (self, rhs); + Mat2 { + a: x.a * y.a + x.b * y.c, + b: x.a * y.b + x.b * y.d, + c: x.c * y.a + x.d * y.c, + d: x.c * y.b + x.d * y.d, + } + } +} diff --git a/evc/src/helpers/mod.rs b/evc/src/helpers/mod.rs new file mode 100644 index 0000000..c5eed9d --- /dev/null +++ b/evc/src/helpers/mod.rs @@ -0,0 +1,3 @@ +pub mod vector; +pub mod threading; +pub mod matrix;
\ No newline at end of file diff --git a/evc/src/helpers/threading.rs b/evc/src/helpers/threading.rs new file mode 100644 index 0000000..3291172 --- /dev/null +++ b/evc/src/helpers/threading.rs @@ -0,0 +1,26 @@ +use std::{ + sync::atomic::{AtomicUsize, Ordering}, + thread, +}; + +static THREADS_RUNNING: AtomicUsize = AtomicUsize::new(0); + +pub fn both_par<F1, F2, O1, O2>(f1: F1, f2: F2, max_threads: usize) -> (O1, O2) +where + F1: FnOnce() -> O1 + Send + 'static, + O1: Send + 'static, + F2: FnOnce() -> O2, +{ + if THREADS_RUNNING.load(Ordering::Relaxed) < max_threads { + THREADS_RUNNING.fetch_add(1, Ordering::Relaxed); + + let o1h = thread::spawn(move || f1()); + let o2 = f2(); + let o1 = o1h.join().unwrap(); + + THREADS_RUNNING.fetch_sub(1, Ordering::Relaxed); + (o1, o2) + } else { + (f1(), f2()) + } +} diff --git a/evc/src/helpers/vector.rs b/evc/src/helpers/vector.rs new file mode 100644 index 0000000..9e7369e --- /dev/null +++ b/evc/src/helpers/vector.rs @@ -0,0 +1,92 @@ +use crate::ser::{Ser, Sink, Source}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Vec2<T> { + pub x: T, + pub y: T, +} + +impl Vec2<isize> { + pub const ZERO: Vec2<isize> = Vec2 { x: 0, y: 0 }; + pub const UP: Vec2<isize> = Vec2 { x: 0, y: -1 }; + pub const LEFT: Vec2<isize> = Vec2 { x: -1, y: 0 }; +} +impl Vec2<f32> { + pub const ZERO: Vec2<f32> = Vec2 { x: 0.0, y: 0.0 }; + pub const UP: Vec2<f32> = Vec2 { x: 0.0, y: -1.0 }; + pub const LEFT: Vec2<f32> = Vec2 { x: -1.0, y: 0.0 }; +} + +impl<T: std::ops::Div<Output = T> + Copy> Vec2<T> { + pub fn downscale(&self, f: T) -> Self { + Self { + x: self.x / f, + y: self.y / f, + } + } +} + +impl Ser for Vec2<isize> { + fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { + sink.put((self.x, self.y)) + } + + fn read(source: &mut impl std::io::Read) -> anyhow::Result<Self> { + let (x, y) = source.get()?; + Ok(Vec2 { x, y }) + } +} + +pub struct Small<T>(pub T); +impl Ser for Small<Vec2<isize>> { + fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { + sink.put((self.0.x as i8, self.0.y as i8)) + } + + fn read(source: &mut impl std::io::Read) -> anyhow::Result<Self> { + let (x, y): (i8, i8) = source.get()?; + Ok(Small(Vec2 { + x: x as isize, + y: y as isize, + })) + } +} + +impl<T: std::ops::Add> std::ops::Add for Vec2<T> { + type Output = Vec2<T::Output>; + #[inline] + fn add(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } +} +impl<T: std::ops::Sub> std::ops::Sub for Vec2<T> { + type Output = Vec2<T::Output>; + #[inline] + fn sub(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} +impl<T: std::ops::Mul> std::ops::Mul for Vec2<T> { + type Output = Vec2<T::Output>; + #[inline] + fn mul(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x * rhs.x, + y: self.y * rhs.y, + } + } +} + +impl<T> From<(T, T)> for Vec2<T> { + #[inline] + fn from((x, y): (T, T)) -> Self { + Vec2 { x, y } + } +} + |