time-to-botec

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

iset.js (1723B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 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 /* eslint-disable no-invalid-this */
     20 
     21 'use strict';
     22 
     23 // METHODS //
     24 
     25 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
     26 var getIndex = require( './../../base/ind' );
     27 var parent = require( './../../base/ctor' ); // eslint-disable-line stdlib/no-redeclare
     28 
     29 
     30 // VARIABLES //
     31 
     32 var base = parent.prototype.iset;
     33 
     34 
     35 // MAIN //
     36 
     37 /**
     38 * Sets an array element located at a specified linear index.
     39 *
     40 * ## Notes
     41 *
     42 * -   For zero-dimensional arrays, the first, and only, argument should be the value to set.
     43 *
     44 * @private
     45 * @param {integer} [idx] - linear view index
     46 * @param {*} v - value to set
     47 * @throws {TypeError} index must be an integer value
     48 * @throws {RangeError} index exceeds array dimensions
     49 * @returns {ndarray} ndarray instance
     50 */
     51 function iset( idx, v ) {
     52 	if ( this._ndims > 0 ) {
     53 		if ( !isInteger( idx ) ) {
     54 			throw new TypeError( 'invalid argument. Index must be an integer value. Value: `'+idx+'`.' );
     55 		}
     56 		idx = getIndex( idx, this._length-1, this._mode );
     57 		base.call( this, idx, v );
     58 	} else {
     59 		base.call( this, idx );
     60 	}
     61 	return this;
     62 }
     63 
     64 
     65 // EXPORTS //
     66 
     67 module.exports = iset;