@rbxts/hash 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/out/index.d.ts +9 -0
- package/out/init.luau +111 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# Package template
|
|
2
|
+
|
|
3
|
+
[](https://github.com/R-unic/package-template/actions/workflows)
|
|
4
|
+
[](https://coveralls.io/github/R-unic/package-template)
|
|
5
|
+
|
|
6
|
+
Package template for roblox-ts with rUnit testing, coverage tracking, and file structure already set up
|
package/out/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type Hash = number;
|
|
2
|
+
export interface Hashable {
|
|
3
|
+
hashCode(): number;
|
|
4
|
+
}
|
|
5
|
+
export type HashableType = number | string | boolean | Hashable | undefined;
|
|
6
|
+
export declare namespace Hash {
|
|
7
|
+
function of(hashable: HashableType): Hash;
|
|
8
|
+
function combine(...hashables: HashableType[]): Hash;
|
|
9
|
+
}
|
package/out/init.luau
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
-- Compiled with roblox-ts v3.0.0
|
|
2
|
+
local _binding = bit32
|
|
3
|
+
local lrotate = _binding.lrotate
|
|
4
|
+
local band = _binding.band
|
|
5
|
+
local bxor = _binding.bxor
|
|
6
|
+
local rshift = _binding.rshift
|
|
7
|
+
local lshift = _binding.lshift
|
|
8
|
+
local extract = _binding.extract
|
|
9
|
+
local PRIME1 = 0x9E3779B1
|
|
10
|
+
local PRIME2 = 0x85EBCA77
|
|
11
|
+
local PRIME3 = 0xC2B2AE3D
|
|
12
|
+
local PRIME4 = 0x27D4EB2F
|
|
13
|
+
local PRIME5 = 0x165667B1
|
|
14
|
+
local MAX32 = 0xFFFFFFFF
|
|
15
|
+
local Hash = {}
|
|
16
|
+
do
|
|
17
|
+
local _container = Hash
|
|
18
|
+
local hashString
|
|
19
|
+
local function of(hashable)
|
|
20
|
+
local _hashable = hashable
|
|
21
|
+
local _exp = typeof(_hashable)
|
|
22
|
+
repeat
|
|
23
|
+
if _exp == "nil" then
|
|
24
|
+
return 0
|
|
25
|
+
end
|
|
26
|
+
if _exp == "boolean" then
|
|
27
|
+
return if hashable ~= 0 and hashable == hashable and hashable ~= "" and hashable then 1 else 0
|
|
28
|
+
end
|
|
29
|
+
if _exp == "number" then
|
|
30
|
+
return band(hashable, MAX32)
|
|
31
|
+
end
|
|
32
|
+
if _exp == "string" then
|
|
33
|
+
return hashString(hashable)
|
|
34
|
+
end
|
|
35
|
+
return hashable:hashCode()
|
|
36
|
+
until true
|
|
37
|
+
end
|
|
38
|
+
_container.of = of
|
|
39
|
+
local round, mixState, mul32, queueRound, mixFinal
|
|
40
|
+
local function combine(...)
|
|
41
|
+
local hashables = { ... }
|
|
42
|
+
local length = #hashables
|
|
43
|
+
local i = 0
|
|
44
|
+
local hash
|
|
45
|
+
if length >= 4 then
|
|
46
|
+
local v1 = PRIME1 + PRIME2
|
|
47
|
+
local v2 = PRIME2
|
|
48
|
+
local v3 = 0
|
|
49
|
+
local v4 = -PRIME1
|
|
50
|
+
while i + 3 <= length do
|
|
51
|
+
v1 = round(v1, of(hashables[i + 1]))
|
|
52
|
+
v2 = round(v2, of(hashables[i + 2]))
|
|
53
|
+
v3 = round(v3, of(hashables[i + 3]))
|
|
54
|
+
v4 = round(v4, of(hashables[i + 4]))
|
|
55
|
+
i += 4
|
|
56
|
+
end
|
|
57
|
+
hash = mixState(v1, v2, v3, v4)
|
|
58
|
+
else
|
|
59
|
+
hash = PRIME5
|
|
60
|
+
end
|
|
61
|
+
hash = band(hash + mul32(length, 4), MAX32)
|
|
62
|
+
while i <= length do
|
|
63
|
+
hash = queueRound(hash, of(hashables[i + 1]))
|
|
64
|
+
i += 1
|
|
65
|
+
end
|
|
66
|
+
return mixFinal(hash)
|
|
67
|
+
end
|
|
68
|
+
_container.combine = combine
|
|
69
|
+
function round(acc, input)
|
|
70
|
+
acc = band(acc + mul32(input, PRIME2), MAX32)
|
|
71
|
+
acc = lrotate(acc, 13)
|
|
72
|
+
acc = band(mul32(acc, PRIME1), MAX32)
|
|
73
|
+
return acc
|
|
74
|
+
end
|
|
75
|
+
function queueRound(hash, queuedValue)
|
|
76
|
+
hash = band(hash + mul32(queuedValue, PRIME3), MAX32)
|
|
77
|
+
hash = lrotate(hash, 17)
|
|
78
|
+
hash = band(mul32(hash, PRIME4), MAX32)
|
|
79
|
+
return hash
|
|
80
|
+
end
|
|
81
|
+
function mul32(a, b)
|
|
82
|
+
local aLo = extract(a, 0, 16)
|
|
83
|
+
local aHi = extract(a, 16, 16)
|
|
84
|
+
local bLo = extract(b, 0, 16)
|
|
85
|
+
local bHi = extract(b, 16, 16)
|
|
86
|
+
local lo = aLo * bLo
|
|
87
|
+
local mid = aHi * bLo + aLo * bHi
|
|
88
|
+
return band(lo + lshift(band(mid, 0xFFFF), 16), 0xFFFFFFFF)
|
|
89
|
+
end
|
|
90
|
+
function mixState(v1, v2, v3, v4)
|
|
91
|
+
return band(lrotate(v1, 1) + lrotate(v2, 7) + lrotate(v3, 12) + lrotate(v4, 18), MAX32)
|
|
92
|
+
end
|
|
93
|
+
function mixFinal(hash)
|
|
94
|
+
hash = bxor(hash, rshift(hash, 15))
|
|
95
|
+
hash = band(mul32(hash, PRIME2), MAX32)
|
|
96
|
+
hash = bxor(hash, rshift(hash, 13))
|
|
97
|
+
hash = band(mul32(hash, PRIME3), MAX32)
|
|
98
|
+
hash = bxor(hash, rshift(hash, 16))
|
|
99
|
+
return band(hash, MAX32)
|
|
100
|
+
end
|
|
101
|
+
function hashString(value)
|
|
102
|
+
local hash = 0
|
|
103
|
+
for i = 1, #value do
|
|
104
|
+
hash = band(mul32(hash, 31) + (string.byte(value, i)), MAX32)
|
|
105
|
+
end
|
|
106
|
+
return hash
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
return {
|
|
110
|
+
Hash = Hash,
|
|
111
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rbxts/hash",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A utility for generating hash codes for objects",
|
|
5
|
+
"author": "me",
|
|
6
|
+
"main": "out/init.lua",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"setup-rokit": "rokit trust lune-org/lune && rokit trust rojo-rbx/rojo && rokit install",
|
|
9
|
+
"build": "npm run setup-rokit && rbxtsc ",
|
|
10
|
+
"dev": "npm run build -- -w",
|
|
11
|
+
"prepublishOnly": "npm test",
|
|
12
|
+
"test": "npm run build && npm run build -- -p tests && rojo build tests -o tests/test-environment.rbxl && lune run tests tests/test-environment.rbxl",
|
|
13
|
+
"coverage": "nyc report && lune run fix-lcov-paths"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"roblox",
|
|
17
|
+
"rbxts"
|
|
18
|
+
],
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/R-unic/hash.git"
|
|
22
|
+
},
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"types": "out/index.d.ts",
|
|
25
|
+
"files": [
|
|
26
|
+
"out",
|
|
27
|
+
"!**/*.tsbuildinfo"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@rbxts/compiler-types": "^3.0.0-types.0",
|
|
34
|
+
"@rbxts/types": "^1.0.918",
|
|
35
|
+
"nyc": "^17.1.0",
|
|
36
|
+
"roblox-ts": "=3.0.0",
|
|
37
|
+
"typescript": "=5.5.3"
|
|
38
|
+
}
|
|
39
|
+
}
|