use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
use super::Position;
impl Position {
fn significant(&self) -> (&[u16], &[u16]) {
let n = self.n();
let n = &n[n.iter().position(|d| *d != 0).unwrap_or(n.len())..];
let z = self.z();
let z = &z[..z.iter().rposition(|d| *d != 0).map_or(0, |i| i + 1)];
(n, z)
}
}
impl Ord for Position {
fn cmp(&self, other: &Self) -> Ordering {
let (left, right) = (self.n(), other.n());
let width = left.len().max(right.len());
let digit = |section: &[u16], i: usize| {
let missing = width - section.len();
if i < missing { 0 } else { section[i - missing] }
};
for i in 0..width {
match digit(left, i).cmp(&digit(right, i)) {
Ordering::Equal => {}
ordering => return ordering,
}
}
let (left, right) = (self.z(), other.z());
for i in 0..left.len().max(right.len()) {
let l = left.get(i).copied().unwrap_or(0);
let r = right.get(i).copied().unwrap_or(0);
match l.cmp(&r) {
Ordering::Equal => {}
ordering => return ordering,
}
}
Ordering::Equal
}
}
impl PartialOrd for Position {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Position {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Eq for Position {}
impl Hash for Position {
fn hash<H: Hasher>(&self, state: &mut H) {
let (n, z) = self.significant();
n.hash(state);
z.hash(state);
}
}