-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathhash_rounded_float.rs
80 lines (70 loc) · 3.08 KB
/
hash_rounded_float.rs
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
use std::hash::{Hash, Hasher};
use uom::si::{
acceleration::meter_per_second_squared,
f64::{
Acceleration, Force, Frequency, Length, LinearMassDensity, LinearNumberDensity, Mass,
MassRate, Velocity,
},
force::newton,
frequency::hertz,
length::meter,
linear_mass_density::kilogram_per_meter,
linear_number_density::per_meter,
mass::kilogram,
mass_rate::kilogram_per_second,
velocity::meter_per_second,
};
/// Hash a float through a rounded integer value
/// `hash_float<3,_>` means that the value is rounded to the nearest thousandth
pub fn hash_float<const T: u8, H: Hasher>(value: &f64, state: &mut H) {
((value * 10i64.pow(T as u32) as f64).round() as i64).hash(state);
}
/// Hash a length through a rounded integer value
/// `hash_length<3,_>` means that the value is rounded to the nearest thousandth
pub fn hash_length<const T: u8, H: Hasher>(value: &Length, state: &mut H) {
hash_float::<T, H>(&value.get::<meter>(), state);
}
/// Hash a acceleration through a rounded integer value
/// `hash_acceleration<3,_>` means that the value is rounded to the nearest thousandth
pub fn hash_acceleration<const T: u8, H: Hasher>(value: &Acceleration, state: &mut H) {
hash_float::<T, H>(&value.get::<meter_per_second_squared>(), state);
}
/// Hash a velocity through a rounded integer value
/// `hash_velocity<3,_>` means that the value is rounded to the nearest thousandth
pub fn hash_velocity<const T: u8, H: Hasher>(value: &Velocity, state: &mut H) {
hash_float::<T, H>(&value.get::<meter_per_second>(), state);
}
/// Hash a force through a rounded integer value
/// `hash_force<3,_>` means that the value is rounded to the nearest thousandth
pub fn hash_force<const T: u8, H: Hasher>(value: &Force, state: &mut H) {
hash_float::<T, H>(&value.get::<newton>(), state);
}
pub fn hash_mass<const T: u8, H: Hasher>(value: &Mass, state: &mut H) {
hash_float::<T, H>(&value.get::<kilogram>(), state);
}
/// Hash a mass rate through a rounded integer value
/// `hash_mass_rate<3,_>` means that the value is rounded to the nearest thousandth
pub fn hash_mass_rate<const T: u8, H: Hasher>(value: &MassRate, state: &mut H) {
hash_float::<T, H>(&value.get::<kilogram_per_second>(), state);
}
pub fn hash_frequency<const T: u8, H: Hasher>(value: &Frequency, state: &mut H) {
hash_float::<T, H>(&value.get::<hertz>(), state);
}
pub fn hash_linear_mass_density<const T: u8, H: Hasher>(value: &LinearMassDensity, state: &mut H) {
hash_float::<T, H>(&value.get::<kilogram_per_meter>(), state);
}
pub fn hash_linear_number_density<const T: u8, H: Hasher>(
value: &LinearNumberDensity,
state: &mut H,
) {
hash_float::<T, H>(&value.get::<per_meter>(), state);
}
/// Hash a list of floats through a list of rounded integer value
/// `hash_float_slice<3,_>` means that the values are rounded to the nearest thousandth
pub fn hash_float_slice<const T: u8, H: Hasher>(value: &[f64], state: &mut H) {
let slice: Vec<_> = value
.iter()
.map(|v| ((v * 10i64.pow(T as u32) as f64).round() as i64))
.collect();
slice.hash(state);
}