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
use std::{
collections::HashMap,
hash::{Hash, Hasher},
};
pub fn to_words(line: &str) -> Vec<&str> {
let split = line.trim().split(" ");
split.collect::<Vec<&str>>()
}
pub fn safe_append_at<T>(vec: &mut Vec<Vec<T>>, i: usize, val: T)
where
T: Clone,
{
if i >= vec.len() {
vec.resize(i + 1, Vec::new());
}
vec[i].push(val);
}
pub fn max_f64(a: f64, b: f64) -> f64 {
if a > b {
return a;
}
b
}
pub fn max_f64_3(a: f64, b: f64, c: f64) -> f64 {
max_f64(max_f64(a, b), c)
}
pub fn hash_f64<H>(x: f64, state: &mut H)
where
H: Hasher,
{
let (m, e, s) = integer_decode(x);
m.hash(state);
e.hash(state);
s.hash(state);
}
fn integer_decode(x: f64) -> (u64, i16, i8) {
let bits: u64 = unsafe { std::mem::transmute(x) };
let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
let mantissa = if exponent == 0 {
(bits & 0xfffffffffffff) << 1
} else {
(bits & 0xfffffffffffff) | 0x10000000000000
};
exponent -= 1023 + 52;
(mantissa, exponent, sign)
}
pub fn pack_dupes_indexed<'a, I, T>(idata: I) -> (Vec<T>, Vec<usize>)
where
I: Iterator<Item = &'a T>,
T: 'a + Eq + Hash + Clone,
{
let mut map = HashMap::new();
let mut packed_data = Vec::new();
let mut ids = Vec::new();
for x in idata {
let id = map.entry(x).or_insert_with(|| {
let value = packed_data.len();
packed_data.push(x.clone());
value
});
ids.push(*id);
}
(packed_data, ids)
}