77*/
88
99'use strict' ;
10+ var b64 = require ( 'base64-arraybuffer' ) ;
11+ var isPlainObject = require ( './is_plain_object' ) ;
1012
1113var isArray = Array . isArray ;
1214
@@ -39,7 +41,7 @@ exports.isArrayOrTypedArray = isArrayOrTypedArray;
3941 * not consistent we won't figure that out here.
4042 */
4143function isArray1D ( a ) {
42- return ! isArrayOrTypedArray ( a [ 0 ] ) ;
44+ return ! ( isArrayOrTypedArray ( a [ 0 ] ) || ( isTypedArraySpec ( a ) && a . ndims === 1 ) ) ;
4345}
4446exports . isArray1D = isArray1D ;
4547
@@ -63,6 +65,113 @@ exports.ensureArray = function(out, n) {
6365 return out ;
6466} ;
6567
68+ var typedArrays = {
69+ int8 : typeof Int8Array !== 'undefined' ? Int8Array : null ,
70+ uint8 : typeof Uint8Array !== 'undefined' ? Uint8Array : null ,
71+ uint8clamped : typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : null ,
72+ int16 : typeof Int16Array !== 'undefined' ? Int16Array : null ,
73+ uint16 : typeof Uint16Array !== 'undefined' ? Uint16Array : null ,
74+ int32 : typeof Int32Array !== 'undefined' ? Int32Array : null ,
75+ uint32 : typeof Uint32Array !== 'undefined' ? Uint32Array : null ,
76+ float32 : typeof Float32Array !== 'undefined' ? Float32Array : null ,
77+ float64 : typeof Float64Array !== 'undefined' ? Float64Array : null ,
78+ bigint64 : typeof BigInt64Array !== 'undefined' ? BigInt64Array : null ,
79+ biguint64 : typeof BigUint64Array !== 'undefined' ? BigUint64Array : null
80+ } ;
81+ exports . typedArrays = typedArrays ;
82+
83+
84+ exports . decodeTypedArraySpec = function ( v ) {
85+ // Assume processed by coerceTypedArraySpec
86+ var T = typedArrays [ v . dtype ] ;
87+ var buffer ;
88+ if ( v . bvals . constructor === ArrayBuffer ) {
89+ // Already an ArrayBuffer
90+ buffer = v . bvals ;
91+ } else {
92+ // Decode, assuming a string
93+ buffer = b64 . decode ( v . bvals ) ;
94+ }
95+
96+ // Check if 1d shape. If so, we're done
97+ if ( v . ndims === 1 ) {
98+ // Construct single Typed array over entire buffer
99+ return new T ( buffer ) ;
100+ } else {
101+ // Reshape into nested plain arrays with innermost
102+ // level containing typed arrays
103+ // We could eventually adopt an ndarray library
104+
105+ // Build cumulative product of dimensions
106+ var cumulativeShape = v . shape . map ( function ( a , i ) {
107+ return a * ( v . shape [ i - 1 ] || 1 ) ;
108+ } ) ;
109+
110+ // Loop of dimensions in reverse order
111+ var nestedArray = [ ] ;
112+ for ( var dimInd = v . ndims - 1 ; dimInd > 0 ; dimInd -- ) {
113+ var subArrayLength = v . shape [ dimInd ] ;
114+ var numSubArrays = cumulativeShape [ dimInd - 1 ] ;
115+ var nextArray = [ ] ;
116+
117+ if ( dimInd === v . ndims - 1 ) {
118+ // First time through, we build the
119+ // inner most typed arrays
120+ for ( var typedInd = 0 ; typedInd < numSubArrays ; typedInd ++ ) {
121+ var typedOffset = typedInd * subArrayLength ;
122+ nextArray . push (
123+ new T ( buffer , typedOffset * T . BYTES_PER_ELEMENT , subArrayLength )
124+ ) ;
125+ }
126+ } else {
127+ // Following times through, build
128+ // next layer of nested arrays
129+ for ( var i = 0 ; i < numSubArrays ; i ++ ) {
130+ var offset = i * subArrayLength ;
131+ nextArray . push ( nextArray . slice ( offset , offset + subArrayLength - 1 ) ) ;
132+ }
133+ }
134+
135+ // Update nested array with next nesting level
136+ nestedArray = nextArray ;
137+ }
138+
139+ return nestedArray ;
140+ }
141+ } ;
142+
143+ function isTypedArraySpec ( v ) {
144+ // Assume v has not passed through
145+ return isPlainObject ( v ) && typedArrays [ v . dtype ] && v . bvals && (
146+ Number . isInteger ( v . shape ) ||
147+ ( isArrayOrTypedArray ( v . shape ) &&
148+ v . shape . length > 0 &&
149+ v . shape . every ( function ( d ) { return Number . isInteger ( d ) ; } ) )
150+ ) ;
151+ }
152+ exports . isTypedArraySpec = isTypedArraySpec ;
153+
154+ function coerceTypedArraySpec ( v ) {
155+ // Assume isTypedArraySpec passed
156+ var coerced = { dtype : v . dtype , bvals : v . bvals } ;
157+
158+ // Normalize shape to a list
159+ if ( Number . isInteger ( v . shape ) ) {
160+ coerced . shape = [ v . shape ] ;
161+ } else {
162+ coerced . shape = v . shape ;
163+ }
164+
165+ // Add length property
166+ coerced . length = v . shape . reduce ( function ( a , b ) { return a * b ; } ) ;
167+
168+ // Add ndims
169+ coerced . ndims = v . shape . length ;
170+
171+ return coerced ;
172+ }
173+ exports . coerceTypedArraySpec = coerceTypedArraySpec ;
174+
66175/*
67176 * TypedArray-compatible concatenation of n arrays
68177 * if all arrays are the same type it will preserve that type,
@@ -150,6 +259,8 @@ function _rowLength(z, fn, len0) {
150259 } else {
151260 return z . length ;
152261 }
262+ } else if ( isTypedArraySpec ( z ) ) {
263+ return z . shape [ z . shape . length - 1 ] ;
153264 }
154265 return 0 ;
155266}
0 commit comments