BadScript 2
Loading...
Searching...
No Matches
BadScript2.Utility.BadHashCode Class Reference

Implements Combination of HashCode Functions Taken from decompiled source of System.HashCode This exists because the System.HashCode implementation is not available in .NET Standard 2.0. More...

Static Public Member Functions

static uint RotateLeft (uint value, int offset)
 Rotates the specified value left by the specified number of bits. Similar in behavior to the x86 instruction ROL.
 
static ulong RotateLeft (ulong value, int offset)
 Rotates the specified value left by the specified number of bits. Similar in behavior to the x86 instruction ROL.
 
static uint RotateRight (uint value, int offset)
 Rotates the specified value right by the specified number of bits. Similar in behavior to the x86 instruction ROR.
 
static ulong RotateRight (ulong value, int offset)
 Rotates the specified value right by the specified number of bits. Similar in behavior to the x86 instruction ROR.
 
static int Combine< T1 > (T1 value1)
 
static int Combine< T1, T2 > (T1 value1, T2 value2)
 
static int Combine< T1, T2, T3 > (T1 value1, T2 value2, T3 value3)
 
static int Combine< T1, T2, T3, T4 > (T1 value1, T2 value2, T3 value3, T4 value4)
 
static int Combine< T1, T2, T3, T4, T5 > (T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
 
static int Combine< T1, T2, T3, T4, T5, T6 > (T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
 
static int Combine< T1, T2, T3, T4, T5, T6, T7 > (T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
 
static int Combine< T1, T2, T3, T4, T5, T6, T7, T8 > (T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8)
 

Static Private Member Functions

static uint GenerateGlobalSeed ()
 
static void Initialize (out uint v1, out uint v2, out uint v3, out uint v4)
 
static uint Round (uint hash, uint input)
 
static uint QueueRound (uint hash, uint queuedValue)
 
static uint MixState (uint v1, uint v2, uint v3, uint v4)
 
static uint MixEmptyState ()
 
static uint MixFinal (uint hash)
 

Static Private Attributes

const uint PRIME1 = 2654435761U
 
const uint PRIME2 = 2246822519U
 
const uint PRIME3 = 3266489917U
 
const uint PRIME4 = 668265263U
 
const uint PRIME5 = 374761393U
 
static readonly uint s_Seed = GenerateGlobalSeed()
 

Detailed Description

Implements Combination of HashCode Functions Taken from decompiled source of System.HashCode This exists because the System.HashCode implementation is not available in .NET Standard 2.0.

Definition at line 10 of file BadHashCode.cs.

Member Function Documentation

◆ Combine< T1 >()

static int BadScript2.Utility.BadHashCode.Combine< T1 > ( T1  value1)
static

Definition at line 88 of file BadHashCode.cs.

89 {
90 // Provide a way of diffusing bits from something with a limited
91 // input hash space. For example, many enums only have a few
92 // possible hashes, only using the bottom few bits of the code. Some
93 // collections are built on the assumption that hashes are spread
94 // over a larger space, so diffusing the bits may help the
95 // collection work more efficiently.
96
97 uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
98
99 uint hash = MixEmptyState();
100 hash += 4;
101
102 hash = QueueRound(hash, hc1);
103
104 hash = MixFinal(hash);
105
106 return (int)hash;
107 }
static uint QueueRound(uint hash, uint queuedValue)
static uint MixFinal(uint hash)

◆ Combine< T1, T2 >()

static int BadScript2.Utility.BadHashCode.Combine< T1, T2 > ( T1  value1,
T2  value2 
)
static

Definition at line 109 of file BadHashCode.cs.

110 {
111 uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
112 uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
113
114 uint hash = MixEmptyState();
115 hash += 8;
116
117 hash = QueueRound(hash, hc1);
118 hash = QueueRound(hash, hc2);
119
120 hash = MixFinal(hash);
121
122 return (int)hash;
123 }

◆ Combine< T1, T2, T3 >()

static int BadScript2.Utility.BadHashCode.Combine< T1, T2, T3 > ( T1  value1,
T2  value2,
T3  value3 
)
static

Definition at line 125 of file BadHashCode.cs.

126 {
127 uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
128 uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
129 uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
130
131 uint hash = MixEmptyState();
132 hash += 12;
133
134 hash = QueueRound(hash, hc1);
135 hash = QueueRound(hash, hc2);
136 hash = QueueRound(hash, hc3);
137
138 hash = MixFinal(hash);
139
140 return (int)hash;
141 }

◆ Combine< T1, T2, T3, T4 >()

static int BadScript2.Utility.BadHashCode.Combine< T1, T2, T3, T4 > ( T1  value1,
T2  value2,
T3  value3,
T4  value4 
)
static

Definition at line 143 of file BadHashCode.cs.

144 {
145 uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
146 uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
147 uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
148 uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
149
150 Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
151
152 v1 = Round(v1, hc1);
153 v2 = Round(v2, hc2);
154 v3 = Round(v3, hc3);
155 v4 = Round(v4, hc4);
156
157 uint hash = MixState(v1, v2, v3, v4);
158 hash += 16;
159
160 hash = MixFinal(hash);
161
162 return (int)hash;
163 }
static uint MixState(uint v1, uint v2, uint v3, uint v4)
static uint Round(uint hash, uint input)
static void Initialize(out uint v1, out uint v2, out uint v3, out uint v4)

◆ Combine< T1, T2, T3, T4, T5 >()

static int BadScript2.Utility.BadHashCode.Combine< T1, T2, T3, T4, T5 > ( T1  value1,
T2  value2,
T3  value3,
T4  value4,
T5  value5 
)
static

Definition at line 165 of file BadHashCode.cs.

166 {
167 uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
168 uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
169 uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
170 uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
171 uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
172
173 Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
174
175 v1 = Round(v1, hc1);
176 v2 = Round(v2, hc2);
177 v3 = Round(v3, hc3);
178 v4 = Round(v4, hc4);
179
180 uint hash = MixState(v1, v2, v3, v4);
181 hash += 20;
182
183 hash = QueueRound(hash, hc5);
184
185 hash = MixFinal(hash);
186
187 return (int)hash;
188 }

◆ Combine< T1, T2, T3, T4, T5, T6 >()

static int BadScript2.Utility.BadHashCode.Combine< T1, T2, T3, T4, T5, T6 > ( T1  value1,
T2  value2,
T3  value3,
T4  value4,
T5  value5,
T6  value6 
)
static

Definition at line 190 of file BadHashCode.cs.

197 {
198 uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
199 uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
200 uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
201 uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
202 uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
203 uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
204
205 Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
206
207 v1 = Round(v1, hc1);
208 v2 = Round(v2, hc2);
209 v3 = Round(v3, hc3);
210 v4 = Round(v4, hc4);
211
212 uint hash = MixState(v1, v2, v3, v4);
213 hash += 24;
214
215 hash = QueueRound(hash, hc5);
216 hash = QueueRound(hash, hc6);
217
218 hash = MixFinal(hash);
219
220 return (int)hash;
221 }

◆ Combine< T1, T2, T3, T4, T5, T6, T7 >()

static int BadScript2.Utility.BadHashCode.Combine< T1, T2, T3, T4, T5, T6, T7 > ( T1  value1,
T2  value2,
T3  value3,
T4  value4,
T5  value5,
T6  value6,
T7  value7 
)
static

Definition at line 223 of file BadHashCode.cs.

231 {
232 uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
233 uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
234 uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
235 uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
236 uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
237 uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
238 uint hc7 = (uint)(value7?.GetHashCode() ?? 0);
239
240 Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
241
242 v1 = Round(v1, hc1);
243 v2 = Round(v2, hc2);
244 v3 = Round(v3, hc3);
245 v4 = Round(v4, hc4);
246
247 uint hash = MixState(v1, v2, v3, v4);
248 hash += 28;
249
250 hash = QueueRound(hash, hc5);
251 hash = QueueRound(hash, hc6);
252 hash = QueueRound(hash, hc7);
253
254 hash = MixFinal(hash);
255
256 return (int)hash;
257 }

◆ Combine< T1, T2, T3, T4, T5, T6, T7, T8 >()

static int BadScript2.Utility.BadHashCode.Combine< T1, T2, T3, T4, T5, T6, T7, T8 > ( T1  value1,
T2  value2,
T3  value3,
T4  value4,
T5  value5,
T6  value6,
T7  value7,
T8  value8 
)
static

Definition at line 259 of file BadHashCode.cs.

268 {
269 uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
270 uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
271 uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
272 uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
273 uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
274 uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
275 uint hc7 = (uint)(value7?.GetHashCode() ?? 0);
276 uint hc8 = (uint)(value8?.GetHashCode() ?? 0);
277
278 Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
279
280 v1 = Round(v1, hc1);
281 v2 = Round(v2, hc2);
282 v3 = Round(v3, hc3);
283 v4 = Round(v4, hc4);
284
285 v1 = Round(v1, hc5);
286 v2 = Round(v2, hc6);
287 v3 = Round(v3, hc7);
288 v4 = Round(v4, hc8);
289
290 uint hash = MixState(v1, v2, v3, v4);
291 hash += 32;
292
293 hash = MixFinal(hash);
294
295 return (int)hash;
296 }

◆ GenerateGlobalSeed()

static uint BadScript2.Utility.BadHashCode.GenerateGlobalSeed ( )
staticprivate

Definition at line 83 of file BadHashCode.cs.

84 {
85 return (uint)new Random().Next();
86 }

◆ Initialize()

static void BadScript2.Utility.BadHashCode.Initialize ( out uint  v1,
out uint  v2,
out uint  v3,
out uint  v4 
)
staticprivate

Definition at line 299 of file BadHashCode.cs.

300 {
301 v1 = s_Seed + PRIME1 + PRIME2;
302 v2 = s_Seed + PRIME2;
303 v3 = s_Seed;
304 v4 = s_Seed - PRIME1;
305 }
static readonly uint s_Seed

◆ MixEmptyState()

static uint BadScript2.Utility.BadHashCode.MixEmptyState ( )
staticprivate

Definition at line 325 of file BadHashCode.cs.

326 {
327 return s_Seed + PRIME5;
328 }

◆ MixFinal()

static uint BadScript2.Utility.BadHashCode.MixFinal ( uint  hash)
staticprivate

Definition at line 331 of file BadHashCode.cs.

332 {
333 hash ^= hash >> 15;
334 hash *= PRIME2;
335 hash ^= hash >> 13;
336 hash *= PRIME3;
337 hash ^= hash >> 16;
338
339 return hash;
340 }

◆ MixState()

static uint BadScript2.Utility.BadHashCode.MixState ( uint  v1,
uint  v2,
uint  v3,
uint  v4 
)
staticprivate

Definition at line 320 of file BadHashCode.cs.

321 {
322 return RotateLeft(v1, 1) + RotateLeft(v2, 7) + RotateLeft(v3, 12) + RotateLeft(v4, 18);
323 }
static uint RotateLeft(uint value, int offset)
Rotates the specified value left by the specified number of bits. Similar in behavior to the x86 inst...

◆ QueueRound()

static uint BadScript2.Utility.BadHashCode.QueueRound ( uint  hash,
uint  queuedValue 
)
staticprivate

Definition at line 314 of file BadHashCode.cs.

315 {
316 return RotateLeft(hash + queuedValue * PRIME3, 17) * PRIME4;
317 }

◆ RotateLeft() [1/2]

static uint BadScript2.Utility.BadHashCode.RotateLeft ( uint  value,
int  offset 
)
static

Rotates the specified value left by the specified number of bits. Similar in behavior to the x86 instruction ROL.

Parameters
valueThe value to rotate.
offsetThe number of bits to rotate by. Any value outside the range [0..31] is treated as congruent mod 32.
Returns
The rotated value.

Definition at line 30 of file BadHashCode.cs.

31 {
32 return value << offset | value >> 32 - offset;
33 }

◆ RotateLeft() [2/2]

static ulong BadScript2.Utility.BadHashCode.RotateLeft ( ulong  value,
int  offset 
)
static

Rotates the specified value left by the specified number of bits. Similar in behavior to the x86 instruction ROL.

Parameters
valueThe value to rotate.
offsetThe number of bits to rotate by. Any value outside the range [0..63] is treated as congruent mod 64.
Returns
The rotated value.

Definition at line 46 of file BadHashCode.cs.

47 {
48 return value << offset | value >> 64 - offset;
49 }

◆ RotateRight() [1/2]

static uint BadScript2.Utility.BadHashCode.RotateRight ( uint  value,
int  offset 
)
static

Rotates the specified value right by the specified number of bits. Similar in behavior to the x86 instruction ROR.

Parameters
valueThe value to rotate.
offsetThe number of bits to rotate by. Any value outside the range [0..31] is treated as congruent mod 32.
Returns
The rotated value.

Definition at line 62 of file BadHashCode.cs.

63 {
64 return value >> offset | value << 32 - offset;
65 }

◆ RotateRight() [2/2]

static ulong BadScript2.Utility.BadHashCode.RotateRight ( ulong  value,
int  offset 
)
static

Rotates the specified value right by the specified number of bits. Similar in behavior to the x86 instruction ROR.

Parameters
valueThe value to rotate.
offsetThe number of bits to rotate by. Any value outside the range [0..63] is treated as congruent mod 64.
Returns
The rotated value.

Definition at line 78 of file BadHashCode.cs.

79 {
80 return value >> offset | value << 64 - offset;
81 }

◆ Round()

static uint BadScript2.Utility.BadHashCode.Round ( uint  hash,
uint  input 
)
staticprivate

Definition at line 308 of file BadHashCode.cs.

309 {
310 return RotateLeft(hash + input * PRIME2, 13) * PRIME1;
311 }

Member Data Documentation

◆ PRIME1

const uint BadScript2.Utility.BadHashCode.PRIME1 = 2654435761U
staticprivate

Definition at line 12 of file BadHashCode.cs.

◆ PRIME2

const uint BadScript2.Utility.BadHashCode.PRIME2 = 2246822519U
staticprivate

Definition at line 13 of file BadHashCode.cs.

◆ PRIME3

const uint BadScript2.Utility.BadHashCode.PRIME3 = 3266489917U
staticprivate

Definition at line 14 of file BadHashCode.cs.

◆ PRIME4

const uint BadScript2.Utility.BadHashCode.PRIME4 = 668265263U
staticprivate

Definition at line 15 of file BadHashCode.cs.

◆ PRIME5

const uint BadScript2.Utility.BadHashCode.PRIME5 = 374761393U
staticprivate

Definition at line 16 of file BadHashCode.cs.

◆ s_Seed

readonly uint BadScript2.Utility.BadHashCode.s_Seed = GenerateGlobalSeed()
staticprivate

Definition at line 17 of file BadHashCode.cs.


The documentation for this class was generated from the following file: