ukebox/
distance.rs

1use std::{iter::Sum, ops::Add};
2
3#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
4/// The distance between two voicings combining semitone distance
5/// and fingering distance as a tuple.
6pub struct Distance(u8, u8);
7
8impl Distance {
9    pub fn new(semitone_distance: u8, fingering_distance: u8) -> Self {
10        Self(semitone_distance, fingering_distance)
11    }
12
13    pub fn semitone_distance(&self) -> u8 {
14        self.0
15    }
16
17    pub fn fingering_distance(&self) -> u8 {
18        self.1
19    }
20}
21
22impl Add for Distance {
23    type Output = Self;
24
25    fn add(self, other: Self) -> Self {
26        Self(self.0 + other.0, self.1 + other.1)
27    }
28}
29
30impl<'a> Sum<&'a Self> for Distance {
31    fn sum<I>(iter: I) -> Self
32    where
33        I: Iterator<Item = &'a Self>,
34    {
35        iter.fold(Self(0, 0), |a, b| Self(a.0 + b.0, a.1 + b.1))
36    }
37}