From 13e12dd9dcb292340fcf3c14b9d591c2e5ae537e Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 11 Jan 2025 19:26:28 +0500 Subject: [PATCH] refactor: update `blas/ext/base/grev` to follow current project conventions PR-URL: https://github.com/stdlib-js/stdlib/pull/4659 Reviewed-by: Philipp Burckhardt --- .../@stdlib/blas/ext/base/grev/README.md | 25 +++--- .../blas/ext/base/grev/benchmark/benchmark.js | 12 +-- .../base/grev/benchmark/benchmark.ndarray.js | 14 ++- .../@stdlib/blas/ext/base/grev/docs/repl.txt | 33 ++++--- .../blas/ext/base/grev/docs/types/index.d.ts | 12 +-- .../blas/ext/base/grev/lib/accessors.js | 14 +-- .../@stdlib/blas/ext/base/grev/lib/main.js | 86 ++----------------- .../@stdlib/blas/ext/base/grev/lib/ndarray.js | 22 ++--- 8 files changed, 63 insertions(+), 155 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/grev/README.md b/lib/node_modules/@stdlib/blas/ext/base/grev/README.md index a5b0319ad45c..15c6b56d9c06 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/grev/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/grev/README.md @@ -30,9 +30,9 @@ limitations under the License. var grev = require( '@stdlib/blas/ext/base/grev' ); ``` -#### grev( N, x, stride ) +#### grev( N, x, strideX ) -Reverses a strided array `x` in-place. +Reverses a strided array in-place. ```javascript var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; @@ -45,17 +45,14 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input array. -- **stride**: index increment. +- **strideX**: stride length. -The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to reverse every other element +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to reverse every other element: ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; -var N = floor( x.length / 2 ); -grev( N, x, 2 ); +grev( 4, x, 2 ); // x => [ -1.0, 1.0, 4.0, -5.0, 3.0, 0.0, -2.0, -3.0 ] ``` @@ -63,23 +60,21 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```javascript var Float64Array = require( '@stdlib/array/float64' ); -var floor = require( '@stdlib/math/base/special/floor' ); // Initial array... var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); // Create an offset view... var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length/2 ); // Reverse every other element... -grev( N, x1, 2 ); +grev( 3, x1, 2 ); // x0 => [ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ] ``` -#### grev.ndarray( N, x, stride, offset ) +#### grev.ndarray( N, x, strideX, offsetX ) -Reverses a strided array `x` in-place using alternative indexing semantics. +Reverses a strided array in-place using alternative indexing semantics. ```javascript var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; @@ -90,9 +85,9 @@ grev.ndarray( x.length, x, 1, 0 ); The function has the following additional parameters: -- **offset**: starting index. +- **offsetX**: starting index. -While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of `x` +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements: ```javascript var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; diff --git a/lib/node_modules/@stdlib/blas/ext/base/grev/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/grev/benchmark/benchmark.js index c33cfd3f85b1..fc6c1be491ea 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/grev/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/grev/benchmark/benchmark.js @@ -21,7 +21,9 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ).factory; +var zeros = require( '@stdlib/array/base/zeros' ); +var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; @@ -38,13 +40,7 @@ var grev = require( './../lib/main.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = gfillBy( len, zeros( len ), 1, uniform( -100, 100 ) ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/grev/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/grev/benchmark/benchmark.ndarray.js index 58cebefa6da5..11ba5a9b4243 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/grev/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/grev/benchmark/benchmark.ndarray.js @@ -21,11 +21,13 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ).factory; +var zeros = require( '@stdlib/array/base/zeros' ); +var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; -var grev = require( './../lib/main.js' ).ndarray; +var grev = require( './../lib/ndarray.js' ); // FUNCTIONS // @@ -38,13 +40,7 @@ var grev = require( './../lib/main.js' ).ndarray; * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - } + var x = gfillBy( len, zeros( len ), 1, uniform( -100, 100 ) ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/grev/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/grev/docs/repl.txt index 0d0044d38c57..f636ca039180 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/grev/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/grev/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Reverses a strided array in-place. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use typed array views. @@ -18,8 +18,8 @@ x: ArrayLikeObject Input array. - stride: integer - Index increment for `x`. + strideX: integer + Stride length. Returns ------- @@ -33,27 +33,25 @@ > {{alias}}( x.length, x, 1 ) [ -3.0, -1.0, 4.0, -5.0, 3.0, 1.0, -2.0 ] - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}( N, x, 2 ) + > {{alias}}( 3, x, 2 ) [ 4.0, 1.0, 3.0, -5.0, -2.0, -1.0, -3.0 ] // Using view offsets: > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > {{alias}}( N, x1, 2 ) + > {{alias}}( 3, x1, 2 ) [ -6.0, 3.0, -4.0, 5.0, -2.0 ] > x0 [ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ] -{{alias}}.ndarray( N, x, stride, offset ) +{{alias}}.ndarray( N, x, strideX, offsetX ) Reverses a strided array in-place using alternative indexing semantics. While typed array views mandate a view offset based on the underlying - buffer, the `offset` parameter supports indexing semantics based on a + buffer, the offset parameter supports indexing semantics based on a starting index. Parameters @@ -64,11 +62,11 @@ x: ArrayLikeObject Input array. - stride: integer - Index increment for `x`. + strideX: integer + Stride length. - offset: integer - Starting index of `x`. + offsetX: integer + Starting index. Returns ------- @@ -84,8 +82,7 @@ // Using an index offset: > x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, x, 2, 1 ) + > {{alias}}.ndarray( 3, x, 2, 1 ) [ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ] See Also diff --git a/lib/node_modules/@stdlib/blas/ext/base/grev/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/grev/docs/types/index.d.ts index 762ecf267f56..ffa60cddb187 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/grev/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/grev/docs/types/index.d.ts @@ -31,7 +31,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @returns `x` * * @example @@ -40,15 +40,15 @@ interface Routine { * grev( x.length, x, 1 ); * // x => [ -3.0, -1.0, 0.0, 4.0, -5.0, 3.0, 1.0, -2.0 ] */ - ( N: number, x: Collection, stride: number ): Collection; + ( N: number, x: Collection, strideX: number ): Collection; /** * Reverses a strided array in-place. using alternative indexing semantics. * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns `x` * * @example @@ -57,7 +57,7 @@ interface Routine { * grev.ndarray( x.length, x, 1, 0 ); * // x => [ -3.0, -1.0, 0.0, 4.0, -5.0, 3.0, 1.0, -2.0 ] */ - ndarray( N: number, x: Collection, stride: number, offset: number ): Collection; + ndarray( N: number, x: Collection, strideX: number, offsetX: number ): Collection; } /** @@ -65,7 +65,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @returns `x` * * @example diff --git a/lib/node_modules/@stdlib/blas/ext/base/grev/lib/accessors.js b/lib/node_modules/@stdlib/blas/ext/base/grev/lib/accessors.js index 386a2858e1f9..88d17d206c3a 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/grev/lib/accessors.js +++ b/lib/node_modules/@stdlib/blas/ext/base/grev/lib/accessors.js @@ -34,8 +34,8 @@ var floor = require( '@stdlib/math/base/special/floor' ); * @param {Object} x - input array object * @param {Collection} x.data - input array data * @param {Array} x.accessors - array element accessors -* @param {integer} stride - index increment -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {Object} input array object * * @example @@ -63,7 +63,7 @@ var floor = require( '@stdlib/math/base/special/floor' ); * var view = reinterpret64( x.data, 0 ); * // view => [ -1.0, -3.0, 4.0, 0.0, 3.0, -5.0, -2.0, 1.0 ] */ -function grev( N, x, stride, offset ) { +function grev( N, x, strideX, offsetX ) { var xbuf; var set; var get; @@ -81,14 +81,14 @@ function grev( N, x, stride, offset ) { set = x.accessors[ 1 ]; n = floor( N/2 ); - ix = offset; - iy = ix + ((N-1)*stride); + ix = offsetX; + iy = ix + ( ( N - 1 ) * strideX ); for ( i = 0; i < n; i++ ) { tmp = get( xbuf, ix ); set( xbuf, ix, get( xbuf, iy ) ); set( xbuf, iy, tmp ); - ix += stride; - iy -= stride; + ix += strideX; + iy -= strideX; } return x; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/grev/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/grev/lib/main.js index 1f5fa21f832f..4b78158809ab 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/grev/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/grev/lib/main.js @@ -20,14 +20,8 @@ // MODULES // -var floor = require( '@stdlib/math/base/special/floor' ); -var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); -var accessors = require( './accessors.js' ); - - -// VARIABLES // - -var M = 3; +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -37,7 +31,7 @@ var M = 3; * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} stride - index increment +* @param {integer} strideX - stride length * @returns {NumericArray} input array * * @example @@ -46,78 +40,8 @@ var M = 3; * grev( x.length, x, 1 ); * // x => [ -3.0, -1.0, 0.0, 4.0, -5.0, 3.0, 1.0, -2.0 ] */ -function grev( N, x, stride ) { - var tmp; - var ix; - var iy; - var o; - var m; - var n; - var i; - - if ( N <= 0 ) { - return x; - } - o = arraylike2object( x ); - if ( o.accessorProtocol ) { - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - accessors( N, o, stride, ix ); - return o.data; - } - n = floor( N/2 ); - - // Use loop unrolling if the stride is equal to `1`... - if ( stride === 1 ) { - m = n % M; - iy = N - 1; - - // If we have a remainder, run a clean-up loop... - if ( m > 0 ) { - for ( ix = 0; ix < m; ix++ ) { - tmp = x[ ix ]; - x[ ix ] = x[ iy ]; - x[ iy ] = tmp; - iy -= 1; - } - } - if ( n < M ) { - return x; - } - for ( ix = m; ix < n; ix += M ) { - tmp = x[ ix ]; - x[ ix ] = x[ iy ]; - x[ iy ] = tmp; - - tmp = x[ ix+1 ]; - x[ ix+1 ] = x[ iy-1 ]; - x[ iy-1 ] = tmp; - - tmp = x[ ix+2 ]; - x[ ix+2 ] = x[ iy-2 ]; - x[ iy-2 ] = tmp; - - iy -= M; - } - return x; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - iy = ix + ((N-1)*stride); - for ( i = 0; i < n; i++ ) { - tmp = x[ ix ]; - x[ ix ] = x[ iy ]; - x[ iy ] = tmp; - ix += stride; - iy -= stride; - } - return x; +function grev( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/grev/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/grev/lib/ndarray.js index f798489b3087..e1330fa896c6 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/grev/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/grev/lib/ndarray.js @@ -37,8 +37,8 @@ var M = 3; * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} stride - index increment -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {NumericArray} input array * * @example @@ -47,7 +47,7 @@ var M = 3; * grev( 3, x, 1, x.length-3 ); * // x => [ 1.0, -2.0, 3.0, -6.0, 5.0, -4.0 ] */ -function grev( N, x, stride, offset ) { +function grev( N, x, strideX, offsetX ) { var tmp; var ix; var iy; @@ -61,14 +61,14 @@ function grev( N, x, stride, offset ) { } o = arraylike2object( x ); if ( o.accessorProtocol ) { - accessors( N, o, stride, offset ); + accessors( N, o, strideX, offsetX ); return o.data; } n = floor( N/2 ); - ix = offset; + ix = offsetX; // Use loop unrolling if the stride is equal to `1`... - if ( stride === 1 ) { + if ( strideX === 1 ) { m = n % M; iy = ix + N - 1; @@ -78,8 +78,8 @@ function grev( N, x, stride, offset ) { tmp = x[ ix ]; x[ ix ] = x[ iy ]; x[ iy ] = tmp; - ix += stride; - iy -= stride; + ix += strideX; + iy -= strideX; } } if ( n < M ) { @@ -103,13 +103,13 @@ function grev( N, x, stride, offset ) { } return x; } - iy = ix + ((N-1)*stride); + iy = ix + ( ( N - 1 ) * strideX ); for ( i = 0; i < n; i++ ) { tmp = x[ ix ]; x[ ix ] = x[ iy ]; x[ iy ] = tmp; - ix += stride; - iy -= stride; + ix += strideX; + iy -= strideX; } return x; }