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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Apache License, Version 2.0. See LICENSE.txt in the project root for license information.
* This software incorporates material from third parties. See NOTICE.txt for details.
*--------------------------------------------------------------------------------------------*/
#![allow(dead_code)]
use std::num::Wrapping;
/// used for debugging when there are divergences between encoder and decoder
pub struct SimpleHash {
hash: u64,
}
pub trait SimpleHashProvider {
fn get_u64(&self) -> u64;
}
impl SimpleHashProvider for i32 {
fn get_u64(&self) -> u64 {
return *self as u64;
}
}
impl SimpleHashProvider for u32 {
fn get_u64(&self) -> u64 {
return *self as u64;
}
}
impl SimpleHashProvider for u64 {
fn get_u64(&self) -> u64 {
return *self as u64;
}
}
impl SimpleHash {
pub fn new() -> Self {
return SimpleHash { hash: 0 };
}
pub fn hash<T: SimpleHashProvider>(&mut self, v: T) {
self.hash = (Wrapping(self.hash as u64) * Wrapping(13u64) + Wrapping(v.get_u64())).0;
}
pub fn get(&self) -> u32 {
return self.hash as u32;
}
}