|
20 | 20 |
|
21 | 21 | // MODULES //
|
22 | 22 |
|
23 |
| -var isFloat32ndarrayLike = require( '@stdlib/assert/is-float32ndarray-like' ); |
24 |
| -var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; |
25 |
| -var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); |
26 |
| -var hasEqualValues = require( '@stdlib/array/base/assert/has-equal-values-indexed' ); |
27 |
| -var min = require( '@stdlib/math/base/special/fast/min' ); |
28 |
| -var without = require( '@stdlib/array/base/without' ); |
29 |
| -var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); |
30 |
| -var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); |
31 |
| -var nditerStacks = require( '@stdlib/ndarray/iter/stacks' ); |
32 |
| -var numel = require( '@stdlib/ndarray/base/numel' ); |
33 | 23 | var base = require( '@stdlib/blas/base/sswap' ).ndarray;
|
34 |
| -var format = require( '@stdlib/string/format' ); |
| 24 | +var factory = require( '@stdlib/blas/tools/swap-factory' ); |
35 | 25 |
|
36 | 26 |
|
37 | 27 | // MAIN //
|
38 | 28 |
|
39 | 29 | /**
|
40 | 30 | * Interchanges two single-precision floating-point vectors.
|
41 | 31 | *
|
| 32 | +* @name sswap |
| 33 | +* @type {Function} |
42 | 34 | * @param {ndarrayLike} x - first input array
|
43 | 35 | * @param {ndarrayLike} y - second input array
|
44 | 36 | * @param {NegativeInteger} [dim=-1] - dimension along which to interchange elements
|
@@ -66,86 +58,7 @@ var format = require( '@stdlib/string/format' );
|
66 | 58 | * var ybuf = y.data;
|
67 | 59 | * // returns <Float32Array>[ 4.0, 2.0, -3.0, 5.0, -1.0 ]
|
68 | 60 | */
|
69 |
| -function sswap( x, y ) { |
70 |
| - var dim; |
71 |
| - var xsh; |
72 |
| - var ysh; |
73 |
| - var xit; |
74 |
| - var yit; |
75 |
| - var xc; |
76 |
| - var yc; |
77 |
| - var vx; |
78 |
| - var vy; |
79 |
| - var dm; |
80 |
| - var S; |
81 |
| - var N; |
82 |
| - var i; |
83 |
| - if ( !isFloat32ndarrayLike( x ) ) { |
84 |
| - throw new TypeError( format( 'invalid argument. First argument must be an ndarray containing single-precision floating-point numbers. Value: `%s`.', x ) ); |
85 |
| - } |
86 |
| - if ( !isFloat32ndarrayLike( y ) ) { |
87 |
| - throw new TypeError( format( 'invalid argument. Second argument must be an ndarray containing single-precision floating-point numbers. Value: `%s`.', y ) ); |
88 |
| - } |
89 |
| - if ( isReadOnly( x ) || isReadOnly( y ) ) { |
90 |
| - throw new Error( 'invalid argument. Cannot write to read-only array.' ); |
91 |
| - } |
92 |
| - // Convert the input arrays to "base" ndarrays: |
93 |
| - xc = ndarraylike2ndarray( x ); |
94 |
| - yc = ndarraylike2ndarray( y ); |
95 |
| - |
96 |
| - // Resolve the input array shapes: |
97 |
| - xsh = xc.shape; |
98 |
| - ysh = yc.shape; |
99 |
| - |
100 |
| - // Validate that we've been provided non-zero-dimensional arrays... |
101 |
| - if ( xsh.length < 1 ) { |
102 |
| - throw new TypeError( format( 'invalid argument. First argument must have at least one dimension.' ) ); |
103 |
| - } |
104 |
| - if ( ysh.length < 1 ) { |
105 |
| - throw new TypeError( format( 'invalid argument. Second argument must have at least one dimension.' ) ); |
106 |
| - } |
107 |
| - // Validate that the arrays have the same shape... |
108 |
| - if ( !hasEqualValues( xsh, ysh ) ) { |
109 |
| - throw new Error( 'invalid arguments. The first and second arguments must have the same shape.' ); |
110 |
| - } |
111 |
| - // Validate that the dimension argument is a negative integer... |
112 |
| - if ( arguments.length > 2 ) { |
113 |
| - dim = arguments[ 2 ]; |
114 |
| - if ( !isNegativeInteger( dim ) ) { |
115 |
| - throw new TypeError( format( 'invalid argument. Third argument must be a negative integer. Value: `%s`.', dim ) ); |
116 |
| - } |
117 |
| - } else { |
118 |
| - dim = -1; |
119 |
| - } |
120 |
| - // Validate that a provided dimension index is within bounds... |
121 |
| - dm = min( xsh.length, ysh.length ) - 1; |
122 |
| - dim = normalizeIndex( dim, dm ); |
123 |
| - if ( dim === -1 ) { |
124 |
| - throw new RangeError( format( 'invalid argument. Third argument must be a value on the interval: [%d,%d]. Value: `%d`.', -dm, -1, arguments[ 2 ] ) ); |
125 |
| - } |
126 |
| - // Resolve the size of the interchange dimension: |
127 |
| - S = xsh[ dim ]; |
128 |
| - |
129 |
| - // If we are only provided one-dimensional input arrays, we can skip iterating over stacks... |
130 |
| - if ( xsh.length === 1 ) { |
131 |
| - base( S, xc.data, xc.strides[0], xc.offset, yc.data, yc.strides[0], yc.offset ); // eslint-disable-line max-len |
132 |
| - return y; |
133 |
| - } |
134 |
| - // Resolve the number of stacks: |
135 |
| - N = numel( without( xsh, dim ) ); |
136 |
| - |
137 |
| - // Create iterators for iterating over stacks of vectors: |
138 |
| - xit = nditerStacks( xc, [ dim ] ); |
139 |
| - yit = nditerStacks( yc, [ dim ] ); |
140 |
| - |
141 |
| - // Interchange each pair of vectors... |
142 |
| - for ( i = 0; i < N; i++ ) { |
143 |
| - vx = xit.next().value; |
144 |
| - vy = yit.next().value; |
145 |
| - base( S, vx.data, vx.strides[0], vx.offset, vy.data, vy.strides[0], vy.offset ); // eslint-disable-line max-len |
146 |
| - } |
147 |
| - return y; |
148 |
| -} |
| 61 | +var sswap = factory( base, 'float32' ); |
149 | 62 |
|
150 | 63 |
|
151 | 64 | // EXPORTS //
|
|
0 commit comments