@rbxts/hash 1.0.1 → 1.0.2
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 +32 -1
- package/out/index.d.ts +10 -2
- package/out/init.luau +32 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
Utility for computing and combining hash codes.
|
|
7
7
|
|
|
8
|
+
### Example
|
|
9
|
+
|
|
8
10
|
```ts
|
|
9
11
|
import { Hash } from "@rbxts/hash";
|
|
10
12
|
|
|
@@ -15,7 +17,7 @@ class User {
|
|
|
15
17
|
public readonly age: number
|
|
16
18
|
) { }
|
|
17
19
|
|
|
18
|
-
public hashCode():
|
|
20
|
+
public hashCode(): Hash {
|
|
19
21
|
return Hash.combine(this.name, this.age);
|
|
20
22
|
}
|
|
21
23
|
}
|
|
@@ -25,3 +27,32 @@ const alice = new User("Alice", 21);
|
|
|
25
27
|
print(bob.hashCode() === bob.hashCode()); // true
|
|
26
28
|
print(bob.hashCode() === alice.hashCode()); // false
|
|
27
29
|
```
|
|
30
|
+
|
|
31
|
+
### Incremental combining
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { Hash } from "@rbxts/hash";
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class Employee {
|
|
38
|
+
public constructor(
|
|
39
|
+
public readonly name: string,
|
|
40
|
+
public readonly reviews: string[]
|
|
41
|
+
) { }
|
|
42
|
+
|
|
43
|
+
public hashCode(): Hash {
|
|
44
|
+
const hash = Hash.incremental();
|
|
45
|
+
hash.add(name);
|
|
46
|
+
hash.add(reviews.size());
|
|
47
|
+
for (const review of reviews)
|
|
48
|
+
hash.add(review)
|
|
49
|
+
|
|
50
|
+
return hash.toHash();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const bob = new Employee("Bob", ["great dude"]);
|
|
55
|
+
const alice = new User("Alice", ["polite woman"]);
|
|
56
|
+
print(bob.hashCode() === bob.hashCode()); // true
|
|
57
|
+
print(bob.hashCode() === alice.hashCode()); // false
|
|
58
|
+
```
|
package/out/index.d.ts
CHANGED
|
@@ -4,6 +4,14 @@ export interface Hashable {
|
|
|
4
4
|
}
|
|
5
5
|
export type HashableType = number | string | boolean | Hashable | undefined;
|
|
6
6
|
export declare namespace Hash {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
class IncrementalHash {
|
|
8
|
+
private hash;
|
|
9
|
+
private count;
|
|
10
|
+
add(value: HashableType): this;
|
|
11
|
+
toHash(): Hash;
|
|
12
|
+
}
|
|
13
|
+
export function incremental(): IncrementalHash;
|
|
14
|
+
export function of(hashable: HashableType): Hash;
|
|
15
|
+
export function combine(...hashables: HashableType[]): Hash;
|
|
16
|
+
export {};
|
|
9
17
|
}
|
package/out/init.luau
CHANGED
|
@@ -15,8 +15,38 @@ local MAX32 = 0xFFFFFFFF
|
|
|
15
15
|
local Hash = {}
|
|
16
16
|
do
|
|
17
17
|
local _container = Hash
|
|
18
|
+
local queueRound, of, mixFinal, mul32
|
|
19
|
+
local IncrementalHash
|
|
20
|
+
do
|
|
21
|
+
IncrementalHash = setmetatable({}, {
|
|
22
|
+
__tostring = function()
|
|
23
|
+
return "IncrementalHash"
|
|
24
|
+
end,
|
|
25
|
+
})
|
|
26
|
+
IncrementalHash.__index = IncrementalHash
|
|
27
|
+
function IncrementalHash.new(...)
|
|
28
|
+
local self = setmetatable({}, IncrementalHash)
|
|
29
|
+
return self:constructor(...) or self
|
|
30
|
+
end
|
|
31
|
+
function IncrementalHash:constructor()
|
|
32
|
+
self.hash = PRIME5
|
|
33
|
+
self.count = 0
|
|
34
|
+
end
|
|
35
|
+
function IncrementalHash:add(value)
|
|
36
|
+
self.hash = queueRound(self.hash, of(value))
|
|
37
|
+
self.count += 1
|
|
38
|
+
return self
|
|
39
|
+
end
|
|
40
|
+
function IncrementalHash:toHash()
|
|
41
|
+
return mixFinal(band(self.hash + mul32(self.count, 4), MAX32))
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
local function incremental()
|
|
45
|
+
return IncrementalHash.new()
|
|
46
|
+
end
|
|
47
|
+
_container.incremental = incremental
|
|
18
48
|
local hashString
|
|
19
|
-
|
|
49
|
+
function of(hashable)
|
|
20
50
|
local _hashable = hashable
|
|
21
51
|
local _exp = typeof(_hashable)
|
|
22
52
|
repeat
|
|
@@ -36,7 +66,7 @@ do
|
|
|
36
66
|
until true
|
|
37
67
|
end
|
|
38
68
|
_container.of = of
|
|
39
|
-
local round, mixState
|
|
69
|
+
local round, mixState
|
|
40
70
|
local function combine(...)
|
|
41
71
|
local hashables = { ... }
|
|
42
72
|
local length = #hashables
|