forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashCode.cs
More file actions
153 lines (139 loc) · 5.05 KB
/
HashCode.cs
File metadata and controls
153 lines (139 loc) · 5.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
namespace MLAPI.Hashing
{
/// <summary>
/// Provides extension methods for getting hashes
/// </summary>
internal static class HashCode
{
private const uint k_FNV_offset_basis32 = 2166136261;
private const uint k_FNV_prime32 = 16777619;
private const ulong k_FNV_offset_basis64 = 14695981039346656037;
private const ulong k_FNV_prime64 = 1099511628211;
/// <summary>
/// non cryptographic stable hash code,
/// it will always return the same hash for the same
/// string.
///
/// This is simply an implementation of FNV-1 32 bit xor folded to 16 bit
/// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <returns>The stable hash32.</returns>
/// <param name="txt">Text.</param>
internal static ushort GetStableHash16(this string txt)
{
uint hash32 = txt.GetStableHash32();
return (ushort)((hash32 >> 16) ^ hash32);
}
/// <summary>
/// non cryptographic stable hash code,
/// it will always return the same hash for the same
/// string.
///
/// This is simply an implementation of FNV-1 32 bit
/// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <returns>The stable hash32.</returns>
/// <param name="txt">Text.</param>
internal static uint GetStableHash32(this string txt)
{
unchecked
{
uint hash = k_FNV_offset_basis32;
for (int i = 0; i < txt.Length; i++)
{
uint ch = txt[i];
hash = hash * k_FNV_prime32;
hash = hash ^ ch;
}
return hash;
}
}
/// <summary>
/// non cryptographic stable hash code,
/// it will always return the same hash for the same
/// string.
///
/// This is simply an implementation of FNV-1 64 bit
/// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <returns>The stable hash32.</returns>
/// <param name="txt">Text.</param>
internal static ulong GetStableHash64(this string txt)
{
unchecked
{
ulong hash = k_FNV_offset_basis64;
for (int i = 0; i < txt.Length; i++)
{
ulong ch = txt[i];
hash = hash * k_FNV_prime64;
hash = hash ^ ch;
}
return hash;
}
}
/// <summary>
/// non cryptographic stable hash code,
/// it will always return the same hash for the same
/// string.
///
/// This is simply an implementation of FNV-1 32 bit xor folded to 16 bit
/// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <returns>The stable hash32.</returns>
/// <param name="bytes">Text.</param>
internal static ushort GetStableHash16(this byte[] bytes)
{
uint hash32 = bytes.GetStableHash32();
return (ushort)((hash32 >> 16) ^ hash32);
}
/// <summary>
/// non cryptographic stable hash code,
/// it will always return the same hash for the same
/// string.
///
/// This is simply an implementation of FNV-1 32 bit
/// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <returns>The stable hash32.</returns>
/// <param name="bytes">Text.</param>
internal static uint GetStableHash32(this byte[] bytes)
{
unchecked
{
uint hash = k_FNV_offset_basis32;
for (int i = 0; i < bytes.Length; i++)
{
uint bt = bytes[i];
hash = hash * k_FNV_prime32;
hash = hash ^ bt;
}
return hash;
}
}
/// <summary>
/// non cryptographic stable hash code,
/// it will always return the same hash for the same
/// string.
///
/// This is simply an implementation of FNV-1 64 bit
/// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <returns>The stable hash32.</returns>
/// <param name="bytes">Text.</param>
internal static ulong GetStableHash64(this byte[] bytes)
{
unchecked
{
ulong hash = k_FNV_offset_basis64;
for (int i = 0; i < bytes.Length; i++)
{
ulong bt = bytes[i];
hash = hash * k_FNV_prime64;
hash = hash ^ bt;
}
return hash;
}
}
}
}