Skip to content

Commit b68b283

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 72739f2 commit b68b283

File tree

5 files changed

+186
-6
lines changed

5 files changed

+186
-6
lines changed

lib/node_modules/@stdlib/array/base/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
# indexOfSameValue
2222

23-
> Return the index of the first element which equals a provided search element according to the [same value algorithm][@stdlib/assert/is-same-value].
23+
> Return the index of the first 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 indexOfSameValue = require( '@stdlib/array/base/index-of-same-value' );
4242

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

45-
Returns the index of the first element which equals a provided search element according to the [same value algorithm][@stdlib/assert/is-same-value].
45+
Returns the index of the first 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 = indexOfSameValue( x, 2, -4 );
8989
## Notes
9090

9191
- The function performs a linear scan and returns immediately upon finding a match.
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/index-of-same-value/docs/repl.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
If unable to find an element which equals a provided search element, the
1010
function returns -1.
1111

12+
The function treats `-0` and `+0` as distinct and `NaNs` as the same.
13+
1214
Parameters
1315
----------
1416
x: ArrayLikeObject

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ 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
* - If unable to find an element which equals a provided search element, the function returns `-1`.
3133
* - 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`.
3234
*
35+
* [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12
36+
*
3337
* @param x - input array
3438
* @param searchElement - search element
3539
* @param fromIndex - starting index (inclusive)

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

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@
2020

2121
// MODULES //
2222

23+
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
26+
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
27+
var isComplexTypedArray = require( '@stdlib/array/base/assert/is-complex-typed-array' );
28+
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
2329
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
2430
var isSameValue = require( '@stdlib/assert/is-same-value' );
31+
var real = require( '@stdlib/complex/float64/real' );
32+
var imag = require( '@stdlib/complex/float64/imag' );
2533

2634

2735
// FUNCTIONS //
@@ -38,10 +46,10 @@ var isSameValue = require( '@stdlib/assert/is-same-value' );
3846
* @example
3947
* var x = [ 1, 2, 3, 4 ];
4048
*
41-
* var idx = internal( x, 2, 0 );
49+
* var idx = indexed( x, 2, 0 );
4250
* // returns 1
4351
*/
44-
function internal( x, searchElement, fromIndex ) {
52+
function indexed( x, searchElement, fromIndex ) {
4553
var i;
4654
for ( i = fromIndex; i < x.length; i++ ) {
4755
if ( isSameValue( searchElement, x[ i ] ) ) {
@@ -85,6 +93,77 @@ function accessors( x, searchElement, fromIndex ) {
8593
return -1;
8694
}
8795

96+
/**
97+
* Returns the index of the first element which equals a provided search element according to the same value algorithm.
98+
*
99+
* @private
100+
* @param {Collection} x - input array
101+
* @param {*} searchElement - search element
102+
* @param {NonNegativeInteger} fromIndex - starting index (inclusive)
103+
* @returns {integer} index
104+
*
105+
* @example
106+
* var Complex128Array = require( '@stdlib/array/complex128' );
107+
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
108+
*
109+
* var x = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ] );
110+
*
111+
* var idx = complex( x, new Complex128( 3.0, 4.0 ), 1 );
112+
* // returns 2
113+
*/
114+
function complex( x, searchElement, fromIndex ) {
115+
var view;
116+
var re;
117+
var im;
118+
var i;
119+
if ( !isComplexLike( searchElement ) ) {
120+
return -1;
121+
}
122+
view = reinterpret( x, 0 );
123+
re = real( searchElement );
124+
im = imag( searchElement );
125+
for ( i = fromIndex*2; i < view.length; i += 2 ) {
126+
if ( isSameValue( view[ i ], re ) && isSameValue( view[ i+1 ], im ) ) {
127+
return i / 2;
128+
}
129+
}
130+
return -1;
131+
}
132+
133+
/**
134+
* Returns the index of the first element which equals a provided search element according to the same value algorithm.
135+
*
136+
* @private
137+
* @param {Collection} x - input array
138+
* @param {*} searchElement - search element
139+
* @param {NonNegativeInteger} fromIndex - starting index (inclusive)
140+
* @returns {integer} index
141+
*
142+
* @example
143+
* var BooleanArray = require( '@stdlib/array/bool' );
144+
*
145+
* var x = new BooleanArray( [ true, false, true, false, true ] );
146+
*
147+
* var idx = boolean( x, true, 1 );
148+
* // returns 2
149+
*/
150+
function boolean( x, searchElement, fromIndex ) {
151+
var view;
152+
var v;
153+
var i;
154+
if ( !isBoolean( searchElement ) ) {
155+
return -1;
156+
}
157+
view = reinterpretBoolean( x, 0 );
158+
v = ( searchElement ) ? 1 : 0;
159+
for ( i = fromIndex; i < view.length; i++ ) {
160+
if ( view[ i ] === v ) {
161+
return i;
162+
}
163+
}
164+
return -1;
165+
}
166+
88167

89168
// MAIN //
90169

@@ -93,8 +172,12 @@ function accessors( x, searchElement, fromIndex ) {
93172
*
94173
* ## Notes
95174
*
175+
* - The function uses the [SameValue Algorithm][ecma-262-same-value-algorithm], as specified in ECMAScript 5.
176+
* - In contrast to the strict equality operator `===`, `-0` and `+0` are distinguishable and `NaNs` are the same.
96177
* - If unable to find an element which equals a provided search element, the function returns `-1`.
97178
*
179+
* [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12
180+
*
98181
* @param {Collection} x - input array
99182
* @param {*} searchElement - search element
100183
* @param {integer} fromIndex - starting index (inclusive)
@@ -124,9 +207,15 @@ function indexOfSameValue( x, searchElement, fromIndex ) {
124207
}
125208
obj = arraylike2object( x );
126209
if ( obj.accessorProtocol ) {
210+
if ( isComplexTypedArray( x ) ) {
211+
return complex( x, searchElement, fromIndex );
212+
}
213+
if ( isBooleanArray( x ) ) {
214+
return boolean( x, searchElement, fromIndex );
215+
}
127216
return accessors( obj, searchElement, fromIndex );
128217
}
129-
return internal( x, searchElement, fromIndex );
218+
return indexed( x, searchElement, fromIndex );
130219
}
131220

132221

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

Lines changed: 85 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 indexOfSameValue = require( './../lib' );
2831

2932

@@ -123,6 +126,88 @@ tape( 'the function returns the first index of an element which equals a provide
123126
t.end();
124127
});
125128

129+
tape( 'the function returns the first index of an element which equals a provided search element (complex128)', function test( t ) {
130+
var actual;
131+
var x;
132+
133+
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
134+
135+
actual = indexOfSameValue( x, new Complex128( 1.0, 1.0 ), 0 );
136+
t.strictEqual( actual, 0, 'returns expected value' );
137+
138+
actual = indexOfSameValue( x, new Complex128( 2.0, 2.0 ), 0 );
139+
t.strictEqual( actual, 2, 'returns expected value' );
140+
141+
actual = indexOfSameValue( x, new Complex128( 3.0, 3.0 ), 0 );
142+
t.strictEqual( actual, 4, 'returns expected value' );
143+
144+
// Nonnegative starting index...
145+
actual = indexOfSameValue( x, new Complex128( 1.0, 1.0 ), 1 );
146+
t.strictEqual( actual, 1, 'returns expected value' );
147+
148+
actual = indexOfSameValue( x, new Complex128( 2.0, 2.0 ), 3 );
149+
t.strictEqual( actual, 3, 'returns expected value' );
150+
151+
actual = indexOfSameValue( x, new Complex128( 3.0, 3.0 ), 5 );
152+
t.strictEqual( actual, 5, 'returns expected value' );
153+
154+
// Negative starting index...
155+
actual = indexOfSameValue( x, new Complex128( 1.0, 1.0 ), -10 );
156+
t.strictEqual( actual, 0, 'returns expected value' );
157+
158+
actual = indexOfSameValue( x, new Complex128( 3.0, 3.0 ), -10 );
159+
t.strictEqual( actual, 4, 'returns expected value' );
160+
161+
actual = indexOfSameValue( x, new Complex128( 1.0, 1.0 ), -5 );
162+
t.strictEqual( actual, 1, 'returns expected value' );
163+
164+
actual = indexOfSameValue( x, new Complex128( 2.0, 2.0 ), -3 );
165+
t.strictEqual( actual, 3, 'returns expected value' );
166+
167+
actual = indexOfSameValue( x, new Complex128( 3.0, 3.0 ), -1 );
168+
t.strictEqual( actual, 5, 'returns expected value' );
169+
170+
t.end();
171+
});
172+
173+
tape( 'the function returns the first index of an element which equals a provided search element (bool)', function test( t ) {
174+
var actual;
175+
var x;
176+
177+
x = new BooleanArray( [ true, true, false, false, true, true ] );
178+
179+
actual = indexOfSameValue( x, true, 0 );
180+
t.strictEqual( actual, 0, 'returns expected value' );
181+
182+
actual = indexOfSameValue( x, false, 0 );
183+
t.strictEqual( actual, 2, 'returns expected value' );
184+
185+
// Nonnegative starting index...
186+
actual = indexOfSameValue( x, true, 1 );
187+
t.strictEqual( actual, 1, 'returns expected value' );
188+
189+
actual = indexOfSameValue( x, false, 3 );
190+
t.strictEqual( actual, 3, 'returns expected value' );
191+
192+
actual = indexOfSameValue( x, true, 5 );
193+
t.strictEqual( actual, 5, 'returns expected value' );
194+
195+
// Negative starting index...
196+
actual = indexOfSameValue( x, true, -10 );
197+
t.strictEqual( actual, 0, 'returns expected value' );
198+
199+
actual = indexOfSameValue( x, true, -5 );
200+
t.strictEqual( actual, 1, 'returns expected value' );
201+
202+
actual = indexOfSameValue( x, false, -3 );
203+
t.strictEqual( actual, 3, 'returns expected value' );
204+
205+
actual = indexOfSameValue( x, true, -1 );
206+
t.strictEqual( actual, 5, 'returns expected value' );
207+
208+
t.end();
209+
});
210+
126211
tape( 'the function returns the first index of an element which equals a provided search element (int32)', function test( t ) {
127212
var actual;
128213
var x;

0 commit comments

Comments
 (0)