forked from purescript/purescript-prelude
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashable.js
More file actions
42 lines (38 loc) · 821 Bytes
/
Hashable.js
File metadata and controls
42 lines (38 loc) · 821 Bytes
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
"use strict";
// Same as immutable.js, except for not dropping the highest bit.
exports.hashNumber = function (f) {
var o = f;
if (o !== o || o === Infinity) {
return 0;
}
var h = o | 0;
if (h !== o) {
h ^= o * 0xffffffff;
}
while (o > 0xffffffff) {
o /= 0xffffffff;
h ^= o;
}
return h;
};
// Same as Java. Improvements welcome.
exports.hashString = function (s) {
var h = 0;
for (var i = 0; i < s.length; i++) {
h = (31 * h + s.charCodeAt(i)) | 0;
}
return h;
};
// Almost the same as Java. Improvements welcome.
exports.hashArray = function (hash) {
return function (as) {
var h = as.length;
for (var i = 0; i < as.length; i++) {
h = (31 * h + hash(as[i])) | 0;
}
return h;
};
};
exports.hashChar = function (c) {
return c.charCodeAt(0);
};