-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndhash.js
45 lines (40 loc) · 888 Bytes
/
ndhash.js
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
"use strict"
var ndarray = require("ndarray")
var useMaps = !(typeof Map === "undefined")
function HashMap(n) {
this.length = n
this.store = useMaps ? new Map() : {}
}
if (useMaps) {
HashMap.prototype.get = function(i) {
return this.store.get(i) || 0
}
HashMap.prototype.set = function(i,v) {
if (v===0) {
this.store.delete(i)
} else {
this.store.set(i, v)
}
return v
}
} else { // Using a polyfill would be neater, but this works as well
HashMap.prototype.get = function(i) {
return this.store[i] || 0
}
HashMap.prototype.set = function(i,v) {
if (v===0) {
delete this.store[i]
} else {
this.store[i] = v
}
return v
}
}
function createNDHash(shape) {
var sz = 1
for(var i=0; i<shape.length; ++i) {
sz *= shape[i]
}
return ndarray(new HashMap(sz), shape)
}
module.exports = createNDHash