time-to-botec

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

ndarray.native.js (3826B)


      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 isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
     25 var addon = require( './trunc.native.js' );
     26 var js = require( './ndarray.js' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Rounds each element in a strided array `x` toward zero and assigns the results to elements in a strided array `y`.
     33 *
     34 * @param {integer} N - number of indexed elements
     35 * @param {Collection} x - input array
     36 * @param {integer} strideX - `x` stride length
     37 * @param {NonNegativeInteger} offsetX - starting `x` index
     38 * @param {Collection} y - destination array
     39 * @param {integer} strideY - `y` stride length
     40 * @param {NonNegativeInteger} offsetY - starting `y` index
     41 * @throws {TypeError} first argument must be an integer
     42 * @throws {TypeError} second argument must be an array-like object
     43 * @throws {TypeError} third argument must be an integer
     44 * @throws {TypeError} fourth argument must be a nonnegative integer
     45 * @throws {TypeError} fifth argument must be an array-like object
     46 * @throws {TypeError} sixth argument must be an integer
     47 * @throws {TypeError} seventh argument must be a nonnegative integer
     48 * @throws {Error} insufficient arguments
     49 * @throws {Error} too many arguments
     50 * @throws {RangeError} second argument has insufficient elements based on the associated stride and the number of indexed elements
     51 * @throws {RangeError} fifth argument has insufficient elements based on the associated stride and the number of indexed elements
     52 * @throws {TypeError} unable to resolve a strided array function supporting the provided array argument data types
     53 * @returns {Collection} `y`
     54 *
     55 * @example
     56 * var Float64Array = require( '@stdlib/array/float64' );
     57 *
     58 * var x = new Float64Array( [ 1.1, 2.5, -3.5, 4.0, -5.9 ] );
     59 * var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] );
     60 *
     61 * trunc( x.length, x, 1, 0, y, 1, 0 );
     62 * // y => <Float64Array>[ 1.0, 2.0, -3.0, 4.0, -5.0 ]
     63 */
     64 function trunc( N, x, strideX, offsetX, y, strideY, offsetY ) {
     65 	var viewX;
     66 	var viewY;
     67 
     68 	// WARNING: we assume that, if we're provided something resembling a typed array, we're provided a typed array; however, this can lead to potential unintended errors as the native add-on cannot work with non-typed array objects (e.g., generic arrays)...
     69 	if ( !isTypedArrayLike( x ) || !isTypedArrayLike( y ) ) {
     70 		return js( N, x, strideX, offsetX, y, strideY, offsetY );
     71 	}
     72 	if ( !isNonNegativeInteger( offsetX ) ) {
     73 		throw new TypeError( 'invalid argument. Input array offset argument must be a nonnegative integer.' );
     74 	}
     75 	if ( !isNonNegativeInteger( offsetY ) ) {
     76 		throw new TypeError( 'invalid argument. Output array offset argument must be a nonnegative integer.' );
     77 	}
     78 	if ( strideX < 0 ) {
     79 		offsetX += (N-1) * strideX;
     80 	}
     81 	if ( strideY < 0 ) {
     82 		offsetY += (N-1) * strideY;
     83 	}
     84 	viewX = new x.constructor( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offsetX), x.length-offsetX ); // eslint-disable-line max-len
     85 	viewY = new y.constructor( y.buffer, y.byteOffset+(y.BYTES_PER_ELEMENT*offsetY), y.length-offsetY ); // eslint-disable-line max-len
     86 	addon( N, viewX, strideX, viewY, strideY );
     87 	return y;
     88 }
     89 
     90 
     91 // EXPORTS //
     92 
     93 module.exports = trunc;