This commit is contained in:
Gabe Venberg 2023-12-12 14:14:56 -06:00
parent fb83813662
commit bd5d5ae901
11 changed files with 642 additions and 16 deletions

View file

@ -0,0 +1,3 @@
pub trait Distances{
fn taxicab_distance(&self, other: &Self)->usize;
}

View file

@ -1,3 +1,4 @@
pub mod points;
pub mod range;
pub mod misc;
pub mod distances;

View file

@ -1,3 +1,5 @@
use crate::distances::Distances;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub struct Point {
pub x: isize,
@ -16,6 +18,12 @@ impl Point {
}
}
impl Distances for Point {
fn taxicab_distance(&self, other: &Self) -> usize {
self.x.abs_diff(other.x) + self.y.abs_diff(other.y)
}
}
impl std::ops::Add for Point {
type Output = Point;
@ -62,7 +70,7 @@ impl std::ops::Neg for Point {
}
/// an unsigned point in 2d space
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UPoint {
pub x: usize,
pub y: usize,
@ -80,6 +88,12 @@ impl UPoint {
}
}
impl Distances for UPoint {
fn taxicab_distance(&self, other: &Self) -> usize {
self.x.abs_diff(other.x) + self.y.abs_diff(other.y)
}
}
impl std::ops::Add for UPoint {
type Output = UPoint;
@ -184,7 +198,9 @@ where
}
}
impl<T, const X: usize, const Y: usize> std::ops::IndexMut<Point> for FourQuadrantMatrix<{ X }, { Y }, T> {
impl<T, const X: usize, const Y: usize> std::ops::IndexMut<Point>
for FourQuadrantMatrix<{ X }, { Y }, T>
{
fn index_mut(&mut self, index: Point) -> &mut Self::Output {
let upoint = index
.to_upoint(&self.zero_coord)
@ -193,7 +209,9 @@ impl<T, const X: usize, const Y: usize> std::ops::IndexMut<Point> for FourQuadra
}
}
impl<T, const X: usize, const Y: usize> std::ops::Index<Point> for FourQuadrantMatrix<{ X }, { Y }, T> {
impl<T, const X: usize, const Y: usize> std::ops::Index<Point>
for FourQuadrantMatrix<{ X }, { Y }, T>
{
type Output = T;
fn index(&self, index: Point) -> &Self::Output {