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
use ::json::{number::Number, JsonValue};
use std::collections::{hash_map::DefaultHasher, HashMap, HashSet};
use std::hash::{Hash, Hasher};
mod json;
pub use self::json::*;
pub fn as_array(json: &JsonValue) -> &[JsonValue] {
match json {
JsonValue::Array(ary) => ary,
_ => unsafe { std::mem::transmute::<&JsonValue, &[JsonValue; 1]>(json) as &[JsonValue] },
}
}
pub fn hash_json_number<H: Hasher>(number: &Number, hasher: &mut H) {
let (positive, mantissa, exponent) = number.as_parts();
positive.hash(hasher);
mantissa.hash(hasher);
exponent.hash(hasher);
}
pub fn hash_json<H: Hasher>(value: &JsonValue, hasher: &mut H) {
match value {
JsonValue::Null => (),
JsonValue::Boolean(b) => b.hash(hasher),
JsonValue::Number(n) => hash_json_number(n, hasher),
JsonValue::Short(str) => str.hash(hasher),
JsonValue::String(str) => str.hash(hasher),
JsonValue::Array(ary) => {
for item in ary {
hash_json(item, hasher)
}
}
JsonValue::Object(obj) => {
for (key, value) in obj.iter() {
key.hash(hasher);
hash_json(value, hasher);
}
}
}
}
pub fn hash_set<T: Hash, H: Hasher>(set: &HashSet<T>, hasher: &mut H) {
let mut hash = 0;
for item in set {
let mut h = DefaultHasher::new();
item.hash(&mut h);
hash = u64::wrapping_add(hash, h.finish());
}
hasher.write_u64(hash);
}
pub fn hash_set_opt<T: Hash, H: Hasher>(set_opt: &Option<HashSet<T>>, hasher: &mut H) {
if let Some(set) = set_opt.as_ref() {
hash_set(set, hasher)
}
}
pub fn hash_map<K: Hash, V: Hash, H: Hasher>(map: &HashMap<K, V>, hasher: &mut H) {
let mut hash = 0;
for entry in map {
let mut h = DefaultHasher::new();
entry.hash(&mut h);
hash = u64::wrapping_add(hash, h.finish());
}
hasher.write_u64(hash);
}