forked from YaccConstructor/QuickGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashCodeHelper.cs
More file actions
69 lines (62 loc) · 2.05 KB
/
HashCodeHelper.cs
File metadata and controls
69 lines (62 loc) · 2.05 KB
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
using System;
using System.Collections.Generic;
using System.Text;
namespace QuickGraph
{
static class HashCodeHelper
{
const Int32 FNV1_prime_32 = 16777619;
const Int32 FNV1_basis_32 = unchecked((int)2166136261);
const Int64 FNV1_prime_64 = 1099511628211;
const Int64 FNV1_basis_64 = unchecked((int)14695981039346656037);
public static Int32 GetHashCode(Int64 x)
{
return Combine((Int32)x, (Int32)(((UInt64)x) >> 32));
}
private static Int32 Fold(Int32 hash, byte value)
{
return (hash * FNV1_prime_32) ^ (Int32)value;
}
private static Int32 Fold(Int32 hash, Int32 value)
{
return Fold(Fold(Fold(Fold(hash,
(byte)value),
(byte)(((UInt32)value) >> 8)),
(byte)(((UInt32)value) >> 16)),
(byte)(((UInt32)value) >> 24));
}
/// <summary>
/// Combines two hashcodes in a strong way.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static Int32 Combine(Int32 x, Int32 y)
{
return Fold(Fold(FNV1_basis_32, x), y);
}
/// <summary>
/// Combines three hashcodes in a strong way.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <returns></returns>
public static Int32 Combine(Int32 x, Int32 y, Int32 z)
{
return Fold(Fold(Fold(FNV1_basis_32, x), y), z);
}
/// <summary>
/// Combines four hashcodes in a strong way.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <param name="w"></param>
/// <returns></returns>
public static Int32 Combine(Int32 x, Int32 y, Int32 z, Int32 w)
{
return Fold(Fold(Fold(Fold(FNV1_basis_32, x), y), z), w);
}
}
}