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.

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

◆ 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 222 of file BadHashCode.cs.

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

◆ 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 257 of file BadHashCode.cs.

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

◆ 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 296 of file BadHashCode.cs.

297 {
298 v1 = s_Seed + PRIME1 + PRIME2;
299 v2 = s_Seed + PRIME2;
300 v3 = s_Seed;
301 v4 = s_Seed - PRIME1;
302 }
static readonly uint s_Seed

◆ MixEmptyState()

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

Definition at line 322 of file BadHashCode.cs.

323 {
324 return s_Seed + PRIME5;
325 }

◆ MixFinal()

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

Definition at line 328 of file BadHashCode.cs.

329 {
330 hash ^= hash >> 15;
331 hash *= PRIME2;
332 hash ^= hash >> 13;
333 hash *= PRIME3;
334 hash ^= hash >> 16;
335
336 return hash;
337 }

◆ MixState()

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

Definition at line 317 of file BadHashCode.cs.

318 {
319 return RotateLeft(v1, 1) + RotateLeft(v2, 7) + RotateLeft(v3, 12) + RotateLeft(v4, 18);
320 }
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 311 of file BadHashCode.cs.

312 {
313 return RotateLeft(hash + queuedValue * PRIME3, 17) * PRIME4;
314 }

◆ 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 305 of file BadHashCode.cs.

306 {
307 return RotateLeft(hash + input * PRIME2, 13) * PRIME1;
308 }

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: