|
1 | 1 | 'use strict';
|
| 2 | +var b64decode = require('base64-arraybuffer').decode; |
| 3 | + |
| 4 | +var isPlainObject = require('./is_plain_object'); |
2 | 5 |
|
3 | 6 | var isArray = Array.isArray;
|
4 | 7 |
|
@@ -48,6 +51,140 @@ exports.ensureArray = function(out, n) {
|
48 | 51 | return out;
|
49 | 52 | };
|
50 | 53 |
|
| 54 | +var typedArrays = { |
| 55 | + u1c: typeof Uint8ClampedArray === 'undefined' ? undefined : |
| 56 | + Uint8ClampedArray, // not supported in numpy? |
| 57 | + |
| 58 | + i1: typeof Int8Array === 'undefined' ? undefined : |
| 59 | + Int8Array, |
| 60 | + |
| 61 | + u1: typeof Uint8Array === 'undefined' ? undefined : |
| 62 | + Uint8Array, |
| 63 | + |
| 64 | + i2: typeof Int16Array === 'undefined' ? undefined : |
| 65 | + Int16Array, |
| 66 | + |
| 67 | + u2: typeof Uint16Array === 'undefined' ? undefined : |
| 68 | + Uint16Array, |
| 69 | + |
| 70 | + i4: typeof Int32Array === 'undefined' ? undefined : |
| 71 | + Int32Array, |
| 72 | + |
| 73 | + u4: typeof Uint32Array === 'undefined' ? undefined : |
| 74 | + Uint32Array, |
| 75 | + |
| 76 | + f4: typeof Float32Array === 'undefined' ? undefined : |
| 77 | + Float32Array, |
| 78 | + |
| 79 | + f8: typeof Float64Array === 'undefined' ? undefined : |
| 80 | + Float64Array, |
| 81 | + |
| 82 | + /* TODO: potentially add Big Int |
| 83 | +
|
| 84 | + i8: typeof BigInt64Array === 'undefined' ? undefined : |
| 85 | + BigInt64Array, |
| 86 | +
|
| 87 | + u8: typeof BigUint64Array === 'undefined' ? undefined : |
| 88 | + BigUint64Array, |
| 89 | + */ |
| 90 | +}; |
| 91 | + |
| 92 | +typedArrays.uint8c = typedArrays.u1c; |
| 93 | +typedArrays.uint8 = typedArrays.u1; |
| 94 | +typedArrays.int8 = typedArrays.i1; |
| 95 | +typedArrays.uint16 = typedArrays.u2; |
| 96 | +typedArrays.int16 = typedArrays.i2; |
| 97 | +typedArrays.uint32 = typedArrays.u4; |
| 98 | +typedArrays.int32 = typedArrays.i4; |
| 99 | +typedArrays.float32 = typedArrays.f4; |
| 100 | +typedArrays.float64 = typedArrays.f8; |
| 101 | + |
| 102 | +function isArrayBuffer(a) { |
| 103 | + return a.constructor === ArrayBuffer; |
| 104 | +} |
| 105 | +exports.isArrayBuffer = isArrayBuffer; |
| 106 | + |
| 107 | +exports.decodeTypedArraySpec = function(vIn) { |
| 108 | + var out = []; |
| 109 | + var v = coerceTypedArraySpec(vIn); |
| 110 | + var dtype = v.dtype; |
| 111 | + |
| 112 | + var T = typedArrays[dtype]; |
| 113 | + if(!T) throw new Error('Error in dtype: "' + dtype + '"'); |
| 114 | + var BYTES_PER_ELEMENT = T.BYTES_PER_ELEMENT; |
| 115 | + |
| 116 | + var buffer = v.bdata; |
| 117 | + if(!isArrayBuffer(buffer)) { |
| 118 | + buffer = b64decode(buffer); |
| 119 | + } |
| 120 | + var shape = v.shape === undefined ? |
| 121 | + // detect 1-d length |
| 122 | + [buffer.byteLength / BYTES_PER_ELEMENT] : |
| 123 | + // convert number to string and split to array |
| 124 | + ('' + v.shape).split(','); |
| 125 | + |
| 126 | + shape.reverse(); // i.e. to match numpy order |
| 127 | + var ndim = shape.length; |
| 128 | + |
| 129 | + var nj, j; |
| 130 | + var ni = +shape[0]; |
| 131 | + |
| 132 | + var rowBytes = BYTES_PER_ELEMENT * ni; |
| 133 | + var pos = 0; |
| 134 | + |
| 135 | + if(ndim === 1) { |
| 136 | + out = new T(buffer); |
| 137 | + } else if(ndim === 2) { |
| 138 | + nj = +shape[1]; |
| 139 | + for(j = 0; j < nj; j++) { |
| 140 | + out[j] = new T(buffer, pos, ni); |
| 141 | + pos += rowBytes; |
| 142 | + } |
| 143 | + } else if(ndim === 3) { |
| 144 | + nj = +shape[1]; |
| 145 | + var nk = +shape[2]; |
| 146 | + for(var k = 0; k < nk; k++) { |
| 147 | + out[k] = []; |
| 148 | + for(j = 0; j < nj; j++) { |
| 149 | + out[k][j] = new T(buffer, pos, ni); |
| 150 | + pos += rowBytes; |
| 151 | + } |
| 152 | + } |
| 153 | + } else { |
| 154 | + throw new Error('ndim: ' + ndim + 'is not supported with the shape:"' + v.shape + '"'); |
| 155 | + } |
| 156 | + |
| 157 | + // attach bdata, dtype & shape to array for json export |
| 158 | + out.bdata = v.bdata; |
| 159 | + out.dtype = v.dtype; |
| 160 | + out.shape = shape.reverse().join(','); |
| 161 | + |
| 162 | + vIn._inputArray = out; |
| 163 | + |
| 164 | + return out; |
| 165 | +}; |
| 166 | + |
| 167 | +exports.isTypedArraySpec = function(v) { |
| 168 | + return ( |
| 169 | + isPlainObject(v) && |
| 170 | + v.hasOwnProperty('dtype') && (typeof v.dtype === 'string') && |
| 171 | + |
| 172 | + v.hasOwnProperty('bdata') && (typeof v.bdata === 'string' || isArrayBuffer(v.bdata)) && |
| 173 | + |
| 174 | + (v.shape === undefined || ( |
| 175 | + v.hasOwnProperty('shape') && (typeof v.shape === 'string' || typeof v.shape === 'number') |
| 176 | + )) |
| 177 | + ); |
| 178 | +}; |
| 179 | + |
| 180 | +function coerceTypedArraySpec(v) { |
| 181 | + return { |
| 182 | + bdata: v.bdata, |
| 183 | + dtype: v.dtype, |
| 184 | + shape: v.shape |
| 185 | + }; |
| 186 | +} |
| 187 | + |
51 | 188 | /*
|
52 | 189 | * TypedArray-compatible concatenation of n arrays
|
53 | 190 | * if all arrays are the same type it will preserve that type,
|
|
0 commit comments