-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray.js
79 lines (61 loc) · 2.59 KB
/
array.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
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
/*************************************************************************************************/
/* Array */
/*************************************************************************************************/
/*************************************************************************************************/
/* Constants */
/*************************************************************************************************/
_DTYPE_MAPPING = {
"uint8": [Uint8Array, 1],
"uint16": [Uint16Array, 2],
"int16": [Int16Array, 2],
"uint32": [Uint32Array, 4],
"int32": [Int32Array, 4],
"float32": [Float32Array, 4],
"float64": [Float64Array, 8],
};
/*************************************************************************************************/
/* Utils */
/*************************************************************************************************/
function tob64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
/*************************************************************************************************/
/* Array class */
/*************************************************************************************************/
function StructArray(count, dtype) {
this.count = count;
this.dtype = dtype;
this.fields = {};
this.itemsize = 0;
for (let i = 0; i < dtype.length; i++) {
let name = dtype[i][0];
let dt = dtype[i][1];
let count = dtype[i][2];
let size = _DTYPE_MAPPING[dt][1];
this.fields[name] = {
type: dt,
count: count,
itemsize: size,
offset: this.itemsize
};
this.itemsize += count * size;
}
this.buffer = new ArrayBuffer(count * this.itemsize);
};
StructArray.prototype.set = function (idx, name, values) {
let field = this.fields[name];
let vt = _DTYPE_MAPPING[field.type][0];
let view = new vt(this.buffer, this.itemsize * idx + field.offset, field.count);
for (let i = 0; i < field.count; i++) {
view[i] = values[i];
}
};
StructArray.prototype.b64 = function () {
return tob64(this.buffer);
}