Skip to content

Commit 74d2527

Browse files
committed
feat: add specialized support for complex number and boolean arrays
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent b27b960 commit 74d2527

File tree

5 files changed

+206
-17
lines changed

5 files changed

+206
-17
lines changed

lib/node_modules/@stdlib/array/base/last-index-of-same-value/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# lastIndexOfSameValue
2222

23-
> Return the index of the last element which equals a provided search element according to the [same value algorithm][@stdlib/assert/is-same-value].
23+
> Return the index of the last element which equals a provided search element according to the [SameValue Algorithm][@stdlib/assert/is-same-value].
2424
2525
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
2626

@@ -42,7 +42,7 @@ var lastIndexOfSameValue = require( '@stdlib/array/base/last-index-of-same-value
4242

4343
#### lastIndexOfSameValue( x, searchElement, fromIndex )
4444

45-
Returns the index of the last element which equals a provided search element according to the [same value algorithm][@stdlib/assert/is-same-value].
45+
Returns the index of the last element which equals a provided search element according to the [SameValue Algorithm][@stdlib/assert/is-same-value].
4646

4747
```javascript
4848
var x = [ 1, 2, 3, 4, 5, 6 ];
@@ -89,7 +89,7 @@ var idx = lastIndexOfSameValue( x, 2, -4 );
8989
## Notes
9090

9191
- The function scans an input array from the starting index to the beginning of the array (i.e., backward).
92-
- When searching for a search element, the function checks for equality using the [same value algorithm][@stdlib/assert/is-same-value]. As a consequence, `NaN` values are considered equal, and `-0` and `+0` are considered distinct.
92+
- When searching for a search element, the function checks for equality using the [SameValue Algorithm][@stdlib/assert/is-same-value] as specified in ECMAScript 5. As a consequence, `NaN` values are considered equal, and `-0` and `+0` are considered distinct.
9393

9494
</section>
9595

lib/node_modules/@stdlib/array/base/last-index-of-same-value/docs/repl.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
The function scans an input array from the starting index to the beginning
1313
of the array (i.e., backward).
1414

15+
The function treats `-0` and `+0` as distinct and `NaNs` as the same.
16+
1517
Parameters
1618
----------
1719
x: ArrayLikeObject

lib/node_modules/@stdlib/array/base/last-index-of-same-value/docs/types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ import { Collection, AccessorArrayLike } from '@stdlib/types/array';
2727
*
2828
* ## Notes
2929
*
30+
* - The function uses the [SameValue Algorithm][ecma-262-same-value-algorithm], as specified in ECMAScript 5.
31+
* - In contrast to the strict equality operator `===`, `-0` and `+0` are distinguishable and `NaNs` are the same.
3032
* - The function scans an input array from the starting index to the beginning of the array (i.e., backward).
3133
* - If unable to find an element which equals a provided search element, the function returns `-1`.
3234
* - If `fromIndex` is less than zero, the starting index is resolved relative to the last array element, with the last array element corresponding to `fromIndex = -1`.
3335
*
36+
* [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12
37+
*
3438
* @param x - input array
3539
* @param searchElement - search element
3640
* @param fromIndex - starting index (inclusive)

lib/node_modules/@stdlib/array/base/last-index-of-same-value/lib/main.js

Lines changed: 98 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@
2020

2121
// MODULES //
2222

23-
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
23+
var isAccessorArray = require( '@stdlib/assert/is-accessor-array' );
24+
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
27+
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
28+
var isComplexTypedArray = require( '@stdlib/array/base/assert/is-complex-typed-array' );
29+
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
30+
var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
2431
var isSameValue = require( '@stdlib/assert/is-same-value' );
32+
var real = require( '@stdlib/complex/float64/real' );
33+
var imag = require( '@stdlib/complex/float64/imag' );
2534

2635

2736
// FUNCTIONS //
@@ -38,10 +47,10 @@ var isSameValue = require( '@stdlib/assert/is-same-value' );
3847
* @example
3948
* var x = [ 1, 2, 3, 4 ];
4049
*
41-
* var idx = internal( x, 2, 3 );
50+
* var idx = indexed( x, 2, 3 );
4251
* // returns 1
4352
*/
44-
function internal( x, searchElement, fromIndex ) {
53+
function indexed( x, searchElement, fromIndex ) {
4554
var i;
4655
for ( i = fromIndex; i >= 0; i-- ) {
4756
if ( isSameValue( searchElement, x[ i ] ) ) {
@@ -62,23 +71,90 @@ function internal( x, searchElement, fromIndex ) {
6271
*
6372
* @example
6473
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
65-
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
6674
*
67-
* var x = arraylike2object( toAccessorArray( [ 1, 2, 3, 4 ] ) );
75+
* var x = toAccessorArray( [ 1, 2, 3, 4 ] );
6876
*
6977
* var idx = accessors( x, 2, 3 );
7078
* // returns 1
7179
*/
7280
function accessors( x, searchElement, fromIndex ) {
73-
var data;
7481
var get;
7582
var i;
7683

77-
data = x.data;
78-
get = x.accessors[ 0 ];
84+
get = resolveGetter( x );
85+
for ( i = fromIndex; i >= 0; i-- ) {
86+
if ( isSameValue( searchElement, get( x, i ) ) ) {
87+
return i;
88+
}
89+
}
90+
return -1;
91+
}
92+
93+
/**
94+
* Returns the index of the last element which equals a provided search element according to the same value algorithm.
95+
*
96+
* @private
97+
* @param {Collection} x - input array
98+
* @param {*} searchElement - search element
99+
* @param {NonNegativeInteger} fromIndex - starting index (inclusive)
100+
* @returns {integer} index
101+
*
102+
* @example
103+
* var Complex128Array = require( '@stdlib/array/complex128' );
104+
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
105+
*
106+
* var x = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ] );
107+
*
108+
* var idx = complex( x, new Complex128( 3.0, 4.0 ), 3 );
109+
* // returns 2
110+
*/
111+
function complex( x, searchElement, fromIndex ) {
112+
var view;
113+
var re;
114+
var im;
115+
var i;
116+
if ( !isComplexLike( searchElement ) ) {
117+
return -1;
118+
}
119+
view = reinterpret( x, 0 );
120+
re = real( searchElement );
121+
im = imag( searchElement );
122+
for ( i = fromIndex*2; i >= 0; i -= 2 ) {
123+
if ( isSameValue( view[ i ], re ) && isSameValue( view[ i+1 ], im ) ) {
124+
return i / 2;
125+
}
126+
}
127+
return -1;
128+
}
79129

130+
/**
131+
* Returns the index of the last element which equals a provided search element according to the same value algorithm.
132+
*
133+
* @private
134+
* @param {Collection} x - input array
135+
* @param {*} searchElement - search element
136+
* @param {NonNegativeInteger} fromIndex - starting index (inclusive)
137+
* @returns {integer} index
138+
*
139+
* @example
140+
* var BooleanArray = require( '@stdlib/array/bool' );
141+
*
142+
* var x = new BooleanArray( [ true, false, true, false, true ] );
143+
*
144+
* var idx = boolean( x, true, 3 );
145+
* // returns 2
146+
*/
147+
function boolean( x, searchElement, fromIndex ) {
148+
var view;
149+
var v;
150+
var i;
151+
if ( !isBoolean( searchElement ) ) {
152+
return -1;
153+
}
154+
view = reinterpretBoolean( x, 0 );
155+
v = ( searchElement ) ? 1 : 0;
80156
for ( i = fromIndex; i >= 0; i-- ) {
81-
if ( isSameValue( searchElement, get( data, i ) ) ) {
157+
if ( view[ i ] === v ) {
82158
return i;
83159
}
84160
}
@@ -93,8 +169,12 @@ function accessors( x, searchElement, fromIndex ) {
93169
*
94170
* ## Notes
95171
*
172+
* - The function uses the [SameValue Algorithm][ecma-262-same-value-algorithm], as specified in ECMAScript 5.
173+
* - In contrast to the strict equality operator `===`, `-0` and `+0` are distinguishable and `NaNs` are the same.
96174
* - If unable to find an element which equals a provided search element, the function returns `-1`.
97175
*
176+
* [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12
177+
*
98178
* @param {Collection} x - input array
99179
* @param {*} searchElement - search element
100180
* @param {integer} fromIndex - starting index (inclusive)
@@ -115,7 +195,6 @@ function accessors( x, searchElement, fromIndex ) {
115195
* // returns 1
116196
*/
117197
function lastIndexOfSameValue( x, searchElement, fromIndex ) {
118-
var obj;
119198
if ( fromIndex < 0 ) {
120199
fromIndex += x.length;
121200
if ( fromIndex < 0 ) {
@@ -124,11 +203,16 @@ function lastIndexOfSameValue( x, searchElement, fromIndex ) {
124203
} else if ( fromIndex > x.length ) {
125204
fromIndex = x.length - 1;
126205
}
127-
obj = arraylike2object( x );
128-
if ( obj.accessorProtocol ) {
129-
return accessors( obj, searchElement, fromIndex );
206+
if ( isAccessorArray( x ) ) {
207+
if ( isComplexTypedArray( x ) ) {
208+
return complex( x, searchElement, fromIndex );
209+
}
210+
if ( isBooleanArray( x ) ) {
211+
return boolean( x, searchElement, fromIndex );
212+
}
213+
return accessors( x, searchElement, fromIndex );
130214
}
131-
return internal( x, searchElement, fromIndex );
215+
return indexed( x, searchElement, fromIndex );
132216
}
133217

134218

lib/node_modules/@stdlib/array/base/last-index-of-same-value/test/test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222

2323
var tape = require( 'tape' );
2424
var AccessorArray = require( '@stdlib/array/base/accessor' );
25+
var Complex128Array = require( '@stdlib/array/complex128' );
26+
var BooleanArray = require( '@stdlib/array/bool' );
2527
var Float64Array = require( '@stdlib/array/float64' );
2628
var Int32Array = require( '@stdlib/array/int32' );
29+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
2730
var lastIndexOfSameValue = require( './../lib' );
2831

2932

@@ -129,6 +132,102 @@ tape( 'the function returns the last index of an element which equals a provided
129132
t.end();
130133
});
131134

135+
tape( 'the function returns the last index of an element which equals a provided search element (complex128)', function test( t ) {
136+
var actual;
137+
var x;
138+
139+
x = new Complex128Array( [ 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0 ] ); // eslint-disable-line max-len
140+
141+
actual = lastIndexOfSameValue( x, new Complex128( 1.0, 1.0 ), 5 );
142+
t.strictEqual( actual, 1, 'returns expected value' );
143+
144+
actual = lastIndexOfSameValue( x, new Complex128( 2.0, 2.0 ), 5 );
145+
t.strictEqual( actual, 3, 'returns expected value' );
146+
147+
actual = lastIndexOfSameValue( x, new Complex128( 3.0, 3.0 ), 5 );
148+
t.strictEqual( actual, 5, 'returns expected value' );
149+
150+
actual = lastIndexOfSameValue( x, new Complex128( 2.0, 2.0 ), 1 );
151+
t.strictEqual( actual, -1, 'returns expected value' );
152+
153+
// Nonnegative starting index...
154+
actual = lastIndexOfSameValue( x, new Complex128( 1.0, 1.0 ), 0 );
155+
t.strictEqual( actual, 0, 'returns expected value' );
156+
157+
actual = lastIndexOfSameValue( x, new Complex128( 2.0, 2.0 ), 2 );
158+
t.strictEqual( actual, 2, 'returns expected value' );
159+
160+
actual = lastIndexOfSameValue( x, new Complex128( 3.0, 3.0 ), 4 );
161+
t.strictEqual( actual, 4, 'returns expected value' );
162+
163+
actual = lastIndexOfSameValue( x, new Complex128( 3.0, 3.0 ), 100 );
164+
t.strictEqual( actual, 5, 'returns expected value' );
165+
166+
// Negative starting index...
167+
actual = lastIndexOfSameValue( x, new Complex128( 1.0, 1.0 ), -1 );
168+
t.strictEqual( actual, 1, 'returns expected value' );
169+
170+
actual = lastIndexOfSameValue( x, new Complex128( 3.0, 3.0 ), -2 );
171+
t.strictEqual( actual, 4, 'returns expected value' );
172+
173+
actual = lastIndexOfSameValue( x, new Complex128( 1.0, 1.0 ), -5 );
174+
t.strictEqual( actual, 1, 'returns expected value' );
175+
176+
actual = lastIndexOfSameValue( x, new Complex128( 2.0, 2.0 ), -3 );
177+
t.strictEqual( actual, 3, 'returns expected value' );
178+
179+
actual = lastIndexOfSameValue( x, new Complex128( 3.0, 3.0 ), -1 );
180+
t.strictEqual( actual, 5, 'returns expected value' );
181+
182+
// Non-complex values:
183+
actual = lastIndexOfSameValue( x, 1.0, 0 );
184+
t.strictEqual( actual, -1, 'returns expected value' );
185+
186+
t.end();
187+
});
188+
189+
tape( 'the function returns the last index of an element which equals a provided search element (bool)', function test( t ) {
190+
var actual;
191+
var x;
192+
193+
x = new BooleanArray( [ true, true, false, false, true, true ] );
194+
195+
actual = lastIndexOfSameValue( x, true, 5 );
196+
t.strictEqual( actual, 5, 'returns expected value' );
197+
198+
actual = lastIndexOfSameValue( x, false, 2 );
199+
t.strictEqual( actual, 2, 'returns expected value' );
200+
201+
// Nonnegative starting index...
202+
actual = lastIndexOfSameValue( x, true, 0 );
203+
t.strictEqual( actual, 0, 'returns expected value' );
204+
205+
actual = lastIndexOfSameValue( x, false, 2 );
206+
t.strictEqual( actual, 2, 'returns expected value' );
207+
208+
actual = lastIndexOfSameValue( x, true, 4 );
209+
t.strictEqual( actual, 4, 'returns expected value' );
210+
211+
// Negative starting index...
212+
actual = lastIndexOfSameValue( x, true, -1 );
213+
t.strictEqual( actual, 5, 'returns expected value' );
214+
215+
actual = lastIndexOfSameValue( x, true, -3 );
216+
t.strictEqual( actual, 1, 'returns expected value' );
217+
218+
actual = lastIndexOfSameValue( x, false, -5 );
219+
t.strictEqual( actual, -1, 'returns expected value' );
220+
221+
actual = lastIndexOfSameValue( x, false, -3 );
222+
t.strictEqual( actual, 3, 'returns expected value' );
223+
224+
// Non-boolean values:
225+
actual = lastIndexOfSameValue( x, 0, 0 );
226+
t.strictEqual( actual, -1, 'returns expected value' );
227+
228+
t.end();
229+
});
230+
132231
tape( 'the function returns the last index of an element which equals a provided search element (int32)', function test( t ) {
133232
var actual;
134233
var x;

0 commit comments

Comments
 (0)