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
use ordered_float::OrderedFloat;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cmp::Ordering;
use std::fmt::{self, Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
#[derive(Debug, Copy, Clone, Default)]
pub struct DoubleKey(pub f64);
impl Display for DoubleKey {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl Deref for DoubleKey {
type Target = f64;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for DoubleKey {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl PartialOrd for DoubleKey {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
OrderedFloat(self.0).partial_cmp(&OrderedFloat(other.0))
}
}
impl PartialEq for DoubleKey {
#[inline]
fn eq(&self, other: &Self) -> bool {
OrderedFloat(self.0) == OrderedFloat(other.0)
}
}
impl Ord for DoubleKey {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
OrderedFloat(self.0).cmp(&OrderedFloat(other.0))
}
}
impl Eq for DoubleKey {}
impl Hash for DoubleKey {
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
OrderedFloat(self.0).hash(state)
}
}
impl<'de> Deserialize<'de> for DoubleKey {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
f64::deserialize(deserializer).map(DoubleKey)
}
}
impl Serialize for DoubleKey {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.serialize(serializer)
}
}