time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

table.native.js (1807B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
     24 var serialize = require( '@stdlib/ndarray/base/serialize-meta-data' );
     25 var copy = require( '@stdlib/utils/copy' );
     26 var addon = require( './../src/addon.node' );
     27 var TABLE = require( './table.js' );
     28 
     29 
     30 // VARIABLES //
     31 
     32 var table = copy( TABLE, 1 );
     33 var js = table.ndarray;
     34 table.ndarray = unary;
     35 
     36 
     37 // FUNCTIONS //
     38 
     39 /**
     40 * Applies a unary function to an input ndarray and assigns results to an output ndarray.
     41 *
     42 * @private
     43 * @param {ndarray} x - input array
     44 * @param {ndarray} y - output array
     45 * @returns {ndarray} output array
     46 */
     47 function unary( x, y ) {
     48 	var xdata = x.data;
     49 	var ydata = y.data;
     50 
     51 	// WARNING: we assume that, if we're provided something which has a data buffer resembling a typed array, we're provided a typed ndarray buffer; however, this can lead to potential unintended errors as the native add-on cannot work with non-typed array objects (e.g., generic arrays)...
     52 	if ( !( isTypedArrayLike( xdata ) && isTypedArrayLike( ydata ) ) ) {
     53 		return js( x, y );
     54 	}
     55 	addon( xdata, serialize( x ), ydata, serialize( y ) );
     56 	return y;
     57 }
     58 
     59 
     60 // EXPORTS //
     61 
     62 module.exports = table;