generated from tipragot/rust
Add utils for hexagonal grild #50
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::ops;
|
use std::ops::{Add, AddAssign, Sub, SubAssign};
|
||||||
|
|
||||||
use num::Signed;
|
use num::{FromPrimitive, Signed};
|
||||||
use partial_min_max::{max, min};
|
use partial_min_max::{max, min};
|
||||||
|
|
||||||
/// Represents a number that can be used in a hexagonal grid.
|
/// Represents a number that can be used in a hexagonal grid.
|
||||||
|
@ -88,7 +88,7 @@ impl<T: HexNumber + Eq + Hash + std::cmp::PartialOrd + num::ToPrimitive> HexPosi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: HexNumber> ops::Add<Self> for HexPosition<T> {
|
impl<T: HexNumber> Add<Self> for HexPosition<T> {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn add(self, other: Self) -> Self::Output {
|
fn add(self, other: Self) -> Self::Output {
|
||||||
|
@ -99,13 +99,14 @@ impl<T: HexNumber> ops::Add<Self> for HexPosition<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: HexNumber> ops::AddAssign<Self> for HexPosition<T> {
|
impl<T: HexNumber + AddAssign> AddAssign<Self> for HexPosition<T> {
|
||||||
fn add_assign(&mut self, other: Self) {
|
fn add_assign(&mut self, other: Self) {
|
||||||
*self = *self + other;
|
self.q += other.q;
|
||||||
|
self.r += other.r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
|
|||||||
impl<T: HexNumber> ops::Sub<Self> for HexPosition<T> {
|
impl<T: HexNumber> Sub<Self> for HexPosition<T> {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn sub(self, other: Self) -> Self {
|
fn sub(self, other: Self) -> Self {
|
||||||
|
@ -116,8 +117,9 @@ impl<T: HexNumber> ops::Sub<Self> for HexPosition<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: HexNumber> ops::SubAssign<Self> for HexPosition<T> {
|
impl<T: HexNumber + SubAssign> SubAssign<Self> for HexPosition<T> {
|
||||||
fn sub_assign(&mut self, other: Self) {
|
fn sub_assign(&mut self, other: Self) {
|
||||||
*self = *self - other;
|
self.q -= other.q;
|
||||||
|
self.r -= other.r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Don't make a new allocation. Use the += operator Don't make a new allocation. Use the += operator
|
|||||||
|
|
Loading…
Reference in a new issue
Don't make a new allocation. Use the += operator