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
use std::num::Wrapping as w;
#[derive(Copy, Clone)]
pub struct SplitMix64(pub u64);
impl SplitMix64 {
pub fn new(seed: u64) -> Self { Self(seed) }
#[inline]
pub fn next_u64(&mut self) -> u64 {
let mut z = w(self.0) + w(0x9E37_79B9_7F4A_7C15_u64);
self.0 = z.0;
z = (z ^ (z >> 30)) * w(0xBF58_476D_1CE4_E5B9_u64);
z = (z ^ (z >> 27)) * w(0x94D0_49BB_1331_11EB_u64);
(z ^ (z >> 31)).0
}
#[inline]
pub fn next_i64(&mut self) -> i64 {
i64::from_be_bytes(
self.next_u64().to_be_bytes())
}
}
pub fn u64_to_open01(u: u64) -> f64 {
use core::f64::EPSILON;
let float_size = std::mem::size_of::<f64>() as u32 * 8;
let fraction = u >> (float_size - 52);
let exponent_bits: u64 = (1023 as u64) << 52;
f64::from_bits(fraction | exponent_bits) - (1.0 - EPSILON / 2.0)
}
#[derive(Copy, Clone, Debug)]
pub struct FnvHasher(u64);
impl Default for FnvHasher {
#[inline]
fn default() -> FnvHasher {
FnvHasher(0xcbf2_9ce4_8422_2325)
}
}
impl FnvHasher {
#[inline]
#[allow(dead_code)]
pub fn with_key(key: u64) -> FnvHasher {
FnvHasher(key)
}
#[inline]
fn finish(self) -> u64 {
self.0
}
#[inline]
pub fn finish_i64(self) -> i64 {
i64::from_be_bytes(
self.finish().to_be_bytes())
}
#[inline]
pub fn write_f64(&mut self, f: f64) {
self.write(&f.to_bits().to_be_bytes());
}
#[inline]
pub fn write_i64(&mut self, i: i64) {
self.write(&i.to_be_bytes());
}
#[inline]
#[allow(clippy::cast_lossless)]
pub fn write(&mut self, bytes: &[u8]) {
let FnvHasher(mut hash) = *self;
for byte in bytes.iter() {
hash ^= *byte as u64;
hash = hash.wrapping_mul(0x0100_0000_01b3);
}
*self = FnvHasher(hash);
}
}
pub fn now_timestamp() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as u64
}