Skip to content

Commit ce7ccf7

Browse files
committed
refactor: special case handling of complex number and index arrays
1 parent dd9c4bc commit ce7ccf7

File tree

1 file changed

+189
-33
lines changed
  • lib/node_modules/@stdlib/array/base/put/lib

1 file changed

+189
-33
lines changed

lib/node_modules/@stdlib/array/base/put/lib/main.js

Lines changed: 189 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,166 @@
2020

2121
// MODULES //
2222

23-
var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
24-
var resolveSetter = require( '@stdlib/array/base/resolve-setter' );
23+
var isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' );
24+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
25+
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
2526
var ind = require( '@stdlib/ndarray/base/ind' ).factory;
2627
var format = require( '@stdlib/string/format' );
2728

2829

30+
// FUNCTIONS //
31+
32+
/**
33+
* Replaces elements in an indexed array with provided values.
34+
*
35+
* @private
36+
* @param {Collection} x - input array
37+
* @param {IntegerArray} indices - list of indices
38+
* @param {Collection} values - values to set
39+
* @param {NonNegativeInteger} stride - index stride for accessing elements in `values`
40+
* @param {Function} getIndex - function for resolving an array index
41+
* @param {NonNegativeInteger} maxIndex - maximum array index (inclusive)
42+
* @returns {Collection} input array
43+
*
44+
* @example
45+
* var ind = require( '@stdlib/ndarray/base/ind' ).factory;
46+
*
47+
* var x = [ 1, 2, 3, 4 ];
48+
*
49+
* var indices = [ 3, 1, 2, 0 ];
50+
* var values = [ 5, 6, 7, 8 ];
51+
*
52+
* var getIndex = ind( 'throw' );
53+
*
54+
* var out = indexed( x, indices, values, 1, getIndex, x.length-1 );
55+
* // returns [ 8, 6, 7, 5 ]
56+
*/
57+
function indexed( x, indices, values, stride, getIndex, maxIndex ) {
58+
var iv;
59+
var i;
60+
var j;
61+
62+
iv = 0;
63+
for ( i = 0; i < indices.length; i++ ) {
64+
j = getIndex( indices[ i ], maxIndex );
65+
x[ j ] = values[ iv ];
66+
iv += stride;
67+
}
68+
return x;
69+
}
70+
71+
/**
72+
* Replaces specified elements of an accessor array with provided values.
73+
*
74+
* @private
75+
* @param {Object} x - input array object
76+
* @param {Object} indices - index object
77+
* @param {Object} values - values object
78+
* @param {NonNegativeInteger} stride - index stride for accessing elements in `values`
79+
* @param {Function} getIndex - function for resolving an array index
80+
* @param {NonNegativeInteger} maxIndex - maximum array index (inclusive)
81+
* @returns {Collection} input array
82+
*
83+
* @example
84+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
85+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
86+
* var ind = require( '@stdlib/ndarray/base/ind' ).factory;
87+
*
88+
* var x = toAccessorArray( [ 1, 2, 3, 4 ] );
89+
*
90+
* var indices = toAccessorArray( [ 1, 2 ] );
91+
* var values = toAccessorArray( [ 20, 30 ] );
92+
*
93+
* var getIndex = ind( 'throw' );
94+
*
95+
* var out = accessors( arraylike2object( x ), arraylike2object( indices ), arraylike2object( values ), 1, getIndex, x.length-1 );
96+
*
97+
* var v = x.get( 0 );
98+
* // returns 1
99+
*
100+
* v = x.get( 1 );
101+
* // returns 20
102+
*/
103+
function accessors( x, indices, values, stride, getIndex, maxIndex ) {
104+
var xdata;
105+
var idata;
106+
var vdata;
107+
var xset;
108+
var iget;
109+
var vget;
110+
var iv;
111+
var i;
112+
var j;
113+
114+
xdata = x.data;
115+
idata = indices.data;
116+
vdata = values.data;
117+
118+
xset = x.accessors[ 1 ];
119+
iget = indices.accessors[ 0 ];
120+
vget = values.accessors[ 0 ];
121+
122+
iv = 0;
123+
for ( i = 0; i < idata.length; i++ ) {
124+
j = getIndex( iget( idata, i ), maxIndex );
125+
xset( xdata, j, vget( vdata, iv ) );
126+
iv += stride;
127+
}
128+
return xdata;
129+
}
130+
131+
/**
132+
* Replaces elements in a complex array with provided values.
133+
*
134+
* @private
135+
* @param {Collection} x - real-valued floating-point input array view
136+
* @param {Object} indices - index array object
137+
* @param {Collection} values - real-valued floating-point values array view
138+
* @param {NonNegativeInteger} stride - index stride for accessing elements in `values`
139+
* @param {Function} getIndex - function for resolving an array index
140+
* @param {NonNegativeInteger} maxIndex - maximum array index (inclusive)
141+
* @returns {Collection} input array view
142+
*
143+
* @example
144+
* var Float64Array = require( '@stdlib/array/float64' );
145+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
146+
* var ind = require( '@stdlib/ndarray/base/ind' ).factory;
147+
*
148+
* var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
149+
*
150+
* var indices = [ 0, 2 ];
151+
* var values = new Float64Array( [ 10.0, 20.0, 50.0, 60.0 ] );
152+
*
153+
* var getIndex = ind( 'throw' );
154+
*
155+
* var out = complex( x, arraylike2object( indices ), values, 1, getIndex, x.length-1 );
156+
* // returns <Float64Array>[ 10.0, 20.0, 3.0, 4.0, 50.0, 60.0, 7.0, 8.0 ]
157+
*/
158+
function complex( x, indices, values, stride, getIndex, maxIndex ) {
159+
var idata;
160+
var iget;
161+
var iv;
162+
var sv;
163+
var i;
164+
var j;
165+
var k;
166+
167+
idata = indices.data;
168+
iget = indices.accessors[ 0 ];
169+
170+
sv = stride * 2; // note: multiply by 2, as real-valued values array consists of interleaved real and imaginary components
171+
iv = 0;
172+
for ( i = 0; i < idata.length; i++ ) {
173+
j = getIndex( iget( idata, i ), maxIndex );
174+
k = j * 2;
175+
x[ k ] = values[ iv ];
176+
x[ k+1 ] = values[ iv+1 ];
177+
iv += sv;
178+
}
179+
return x;
180+
}
181+
182+
29183
// MAIN //
30184

31185
/**
@@ -36,7 +190,7 @@ var format = require( '@stdlib/string/format' );
36190
* @param {Collection} values - values to set
37191
* @param {string} mode - index mode
38192
* @throws {Error} third argument must be broadcast compatible with the second argument
39-
* @returns {Array} output array
193+
* @returns {Collection} input array
40194
*
41195
* @example
42196
* var x = [ 1, 2, 3, 4 ];
@@ -64,47 +218,49 @@ var format = require( '@stdlib/string/format' );
64218
*/
65219
function put( x, indices, values, mode ) {
66220
var getIndex;
67-
var xset;
68-
var iget;
69-
var vget;
221+
var stride;
70222
var max;
71-
var vs;
223+
var xo;
224+
var io;
72225
var vo;
73-
var N;
74-
var i;
75-
var j;
76-
77-
// Resolve accessors for accessing array elements:
78-
xset = resolveSetter( x );
79-
iget = resolveGetter( indices );
80-
vget = resolveGetter( values );
81-
82-
// Resolve a function for returning an index according to the specified index mode:
83-
getIndex = ind( mode );
84-
85-
// Resolve the maximum index:
86-
max = x.length - 1;
87226

88227
// Broadcast the `values` array...
89-
N = indices.length;
90-
if ( N > 0 ) { // note: this allows `indices` to be empty and `values` to be non-empty (and not broadcast compatible with `indices`) to allow the potential use case where having an empty `indices` array is expected behavior and you don't want to trigger an exception simply because `values` has elements
228+
if ( indices.length > 0 ) { // note: this allows `indices` to be empty and `values` to be non-empty (and not broadcast compatible with `indices`) to allow the potential use case where having an empty `indices` array is expected behavior and you don't want to trigger an exception simply because `values` has elements
91229
// Note that this effectively in-lines logic from `@stdlib/array/base/broadcast-array` in order to avoid unnecessary object creation...
92-
if ( values.length === N ) {
93-
vs = 1;
230+
if ( values.length === indices.length ) {
231+
stride = 1;
94232
} else if ( values.length === 1 ) {
95-
vs = 0;
233+
stride = 0;
96234
} else {
97-
throw new Error( format( 'invalid argument. The third argument must be broadcast compatible with the second argument. Array shape: (%d). Desired shape: (%d).', values.length, N ) );
235+
throw new Error( format( 'invalid argument. The third argument must be broadcast compatible with the second argument. Array shape: (%d). Desired shape: (%d).', values.length, indices.length ) );
98236
}
99237
}
100-
vo = 0;
238+
// Resolve a function for returning an index according to the specified index mode:
239+
getIndex = ind( mode );
101240

102-
// Replace each desired element in the provided array...
103-
for ( i = 0; i < N; i++ ) {
104-
j = getIndex( iget( indices, i ), max );
105-
xset( x, j, vget( values, vo ) );
106-
vo += vs;
241+
// Resolve the maximum index:
242+
max = x.length - 1;
243+
244+
xo = arraylike2object( x );
245+
io = arraylike2object( indices );
246+
vo = arraylike2object( values );
247+
if (
248+
xo.accessorProtocol ||
249+
io.accessorProtocol ||
250+
vo.accessorProtocol
251+
) {
252+
// Note: we only explicitly support complex-to-complex, as this function should not be concerned with casting rules, etc. That is left to userland...
253+
if (
254+
isComplexDataType( xo.dtype ) &&
255+
isComplexDataType( vo.dtype )
256+
) {
257+
complex( reinterpret( x, 0 ), io, reinterpret( values, 0 ), stride, getIndex, max ); // eslint-disable-line max-len
258+
return x;
259+
}
260+
accessors( xo, io, vo, stride, getIndex, max );
261+
return x;
107262
}
263+
indexed( x, indices, values, stride, getIndex, max );
108264
return x;
109265
}
110266

0 commit comments

Comments
 (0)