use crate::distance::*;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Gate {
pub target: u32,
pub distance: f32,
}
impl Gate {
#[inline]
pub fn target(&self) -> usize {
self.target as usize
}
}
impl Gate {
pub fn new(target: usize, distance: f32) -> Self {
Self {
target: target as u32,
distance,
}
}
}
impl PartialEq for Gate {
fn eq(&self, other: &Gate) -> bool {
if self.target != other.target {
return false;
}
let diff = f32::abs(self.distance - other.distance);
diff < DISTANCE_PRECISION
}
}
impl Eq for Gate {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_eq() {
let a = Gate::new(1, 1.0);
let b = Gate::new(1, 1.001); let c = Gate::new(1, 1.000_000_1); let d = Gate::new(2, 1.0);
assert_eq!(a, a);
assert_eq!(b, b);
assert_eq!(c, c);
assert_eq!(c, c);
assert_ne!(a, b); assert_eq!(a, c); assert_ne!(a, d); }
#[test]
#[cfg(feature = "serde")]
fn test_serde() {
let gate = Gate::new(42, 1.5);
let json = serde_json::to_string(&gate).unwrap();
let deserialized: Gate = serde_json::from_str(&json).unwrap();
assert_eq!(gate, deserialized);
}
}