|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 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 | +// MAIN // |
| 22 | + |
| 23 | +/** |
| 24 | +* Adds a scalar constant to each element in a strided array. |
| 25 | +* |
| 26 | +* @private |
| 27 | +* @param {PositiveInteger} N - number of indexed elements |
| 28 | +* @param {number} alpha - scalar constant |
| 29 | +* @param {Object} x - input array object |
| 30 | +* @param {Collection} x.data - input array data |
| 31 | +* @param {Array<Function>} x.accessors - array element accessors |
| 32 | +* @param {integer} strideX - stride length |
| 33 | +* @param {NonNegativeInteger} offsetX - starting index |
| 34 | +* @returns {Object} input array object |
| 35 | +* |
| 36 | +* @example |
| 37 | +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 38 | +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); |
| 39 | +* |
| 40 | +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 41 | +* |
| 42 | +* var v = gapx( 4, 5.0, arraylike2object( x ), 2, 1 ); |
| 43 | +* // returns {...} |
| 44 | +*/ |
| 45 | +function gapx( N, alpha, x, strideX, offsetX ) { |
| 46 | + var xbuf; |
| 47 | + var get; |
| 48 | + var set; |
| 49 | + var ix; |
| 50 | + var i; |
| 51 | + |
| 52 | + // Cache reference to array data: |
| 53 | + xbuf = x.data; |
| 54 | + |
| 55 | + // Cache reference to the element accessors: |
| 56 | + get = x.accessors[ 0 ]; |
| 57 | + set = x.accessors[ 1 ]; |
| 58 | + |
| 59 | + ix = offsetX; |
| 60 | + for ( i = 0; i < N; i++ ) { |
| 61 | + set( xbuf, ix, alpha + get( xbuf, ix ) ); |
| 62 | + ix += strideX; |
| 63 | + } |
| 64 | + return x; |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +// EXPORTS // |
| 69 | + |
| 70 | +module.exports = gapx; |
0 commit comments