forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashUtility.cs
More file actions
executable file
·102 lines (88 loc) · 2.67 KB
/
Copy pathHashUtility.cs
File metadata and controls
executable file
·102 lines (88 loc) · 2.67 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
using Unity.Mathematics;
namespace Samples.Common
{
public struct GridHash
{
public readonly static int3[] cellOffsets =
{
new int3(0, 0, 0),
new int3(-1, 0, 0),
new int3(0, -1, 0),
new int3(0, 0, -1),
new int3(1, 0, 0),
new int3(0, 1, 0),
new int3(0, 0, 1)
};
public readonly static int2[] cell2DOffsets =
{
new int2(0, 0),
new int2(-1, 0),
new int2(0, -1),
new int2(1, 0),
new int2(0, 1),
};
public static int Hash(float3 v, float cellSize)
{
return Hash(Quantize(v, cellSize));
}
public static int3 Quantize(float3 v, float cellSize)
{
return new int3(math.floor(v / cellSize));
}
public static int Hash(float2 v, float cellSize)
{
return Hash(Quantize(v, cellSize));
}
public static int2 Quantize(float2 v, float cellSize)
{
return new int2(math.floor(v / cellSize));
}
public static int Hash(int3 grid)
{
unchecked
{
// Simple int3 hash based on a pseudo mix of :
// 1) https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
// 2) https://en.wikipedia.org/wiki/Jenkins_hash_function
int hash = grid.x;
hash = (hash * 397) ^ grid.y;
hash = (hash * 397) ^ grid.z;
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
}
public static int Hash(int2 grid)
{
unchecked
{
// Simple int3 hash based on a pseudo mix of :
// 1) https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
// 2) https://en.wikipedia.org/wiki/Jenkins_hash_function
int hash = grid.x;
hash = (hash * 397) ^ grid.y;
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
}
public static ulong Hash(ulong hash, ulong key)
{
const ulong m = 0xc6a4a7935bd1e995UL;
const int r = 47;
ulong h = hash;
ulong k = key;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
h ^= h >> r;
h *= m;
h ^= h >> r;
return h;
}
}
}