1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use app_units::Au;
use euclid::Size2D;
use std::iter::Sum;
use std::ops::Add;
pub trait ComputeSquaredDistance {
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()>;
}
#[derive(Clone, Copy, Debug)]
pub enum SquaredDistance {
Sqrt(f64),
Value(f64),
}
impl ComputeSquaredDistance for u16 {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(SquaredDistance::Sqrt(((*self as f64) - (*other as f64)).abs()))
}
}
impl ComputeSquaredDistance for i32 {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(SquaredDistance::Sqrt((*self - *other).abs() as f64))
}
}
impl ComputeSquaredDistance for f32 {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(SquaredDistance::Sqrt((*self - *other).abs() as f64))
}
}
impl ComputeSquaredDistance for f64 {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(SquaredDistance::Sqrt((*self - *other).abs()))
}
}
impl ComputeSquaredDistance for Au {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
self.0.compute_squared_distance(&other.0)
}
}
impl<T> ComputeSquaredDistance for Option<T>
where T: ComputeSquaredDistance
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
match (self.as_ref(), other.as_ref()) {
(Some(this), Some(other)) => this.compute_squared_distance(other),
(None, None) => Ok(SquaredDistance::Value(0.)),
_ => Err(()),
}
}
}
impl<T> ComputeSquaredDistance for Size2D<T>
where T: ComputeSquaredDistance
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(self.width.compute_squared_distance(&other.width)? + self.height.compute_squared_distance(&other.height)?)
}
}
impl SquaredDistance {
pub fn sqrt(self) -> f64 {
match self {
SquaredDistance::Sqrt(this) => this,
SquaredDistance::Value(this) => this.sqrt(),
}
}
}
impl From<SquaredDistance> for f64 {
#[inline]
fn from(distance: SquaredDistance) -> Self {
match distance {
SquaredDistance::Sqrt(this) => this * this,
SquaredDistance::Value(this) => this,
}
}
}
impl Add for SquaredDistance {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self {
SquaredDistance::Value(f64::from(self) + f64::from(rhs))
}
}
impl Sum for SquaredDistance {
fn sum<I>(mut iter: I) -> Self
where
I: Iterator<Item = Self>,
{
let first = match iter.next() {
Some(first) => first,
None => return SquaredDistance::Value(0.),
};
iter.fold(first, Add::add)
}
}