Skip to content

Commit f040210

Browse files
committed
Auto-generated commit
1 parent e3bc68d commit f040210

File tree

6 files changed

+197
-6
lines changed

6 files changed

+197
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ This release closes the following issue:
765765

766766
##### Features
767767

768+
- [`b68b283`](https://github.com/stdlib-js/stdlib/commit/b68b28358b19ba7e4d23d9a87acfb283c3c0ea3d) - add specialized support for complex number and boolean arrays
768769
- [`c7f571e`](https://github.com/stdlib-js/stdlib/commit/c7f571ef00c124a120847f0c8fa59053c7c27dba) - add `array/base/index-of-same-value`
769770

770771
</section>
@@ -1623,6 +1624,8 @@ A total of 13 people contributed to this release. Thank you to the following con
16231624

16241625
<details>
16251626

1627+
- [`c7f060f`](https://github.com/stdlib-js/stdlib/commit/c7f060f6549d45a4c7faa06b25ff55a882ee6299) - **test:** add test cases _(by Athan Reines)_
1628+
- [`b68b283`](https://github.com/stdlib-js/stdlib/commit/b68b28358b19ba7e4d23d9a87acfb283c3c0ea3d) - **feat:** add specialized support for complex number and boolean arrays _(by Athan Reines)_
16261629
- [`e8bb580`](https://github.com/stdlib-js/stdlib/commit/e8bb580f445dcbeeb03e458def428d9e820ab808) - **feat:** add `lastIndexOfSameValue` to namespace _(by Athan Reines)_
16271630
- [`25fd7dd`](https://github.com/stdlib-js/stdlib/commit/25fd7dd71923e473865bb8c814fdef3981b17593) - **feat:** add `array/base/last-index-of-same-value` _(by Athan Reines)_
16281631
- [`e8a96a1`](https://github.com/stdlib-js/stdlib/commit/e8a96a17652f957f69c041a2f7292bee1b827aeb) - **feat:** add `indexOfSameValue` to namespace _(by Athan Reines)_

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

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

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)

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( './../../../base/assert/is-complex-typed-array' );
28+
var isBooleanArray = require( './../../../base/assert/is-booleanarray' );
2329
var arraylike2object = require( './../../../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

base/index-of-same-value/test/test.js

Lines changed: 93 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( './../../../base/accessor' );
25+
var Complex128Array = require( './../../../complex128' );
26+
var BooleanArray = require( './../../../bool' );
2527
var Float64Array = require( './../../../float64' );
2628
var Int32Array = require( './../../../int32' );
29+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
2730
var indexOfSameValue = require( './../lib' );
2831

2932

@@ -123,6 +126,96 @@ 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+
// Non-complex values:
171+
actual = indexOfSameValue( x, 1.0, 0 );
172+
t.strictEqual( actual, -1, 'returns expected value' );
173+
174+
t.end();
175+
});
176+
177+
tape( 'the function returns the first index of an element which equals a provided search element (bool)', function test( t ) {
178+
var actual;
179+
var x;
180+
181+
x = new BooleanArray( [ true, true, false, false, true, true ] );
182+
183+
actual = indexOfSameValue( x, true, 0 );
184+
t.strictEqual( actual, 0, 'returns expected value' );
185+
186+
actual = indexOfSameValue( x, false, 0 );
187+
t.strictEqual( actual, 2, 'returns expected value' );
188+
189+
// Nonnegative starting index...
190+
actual = indexOfSameValue( x, true, 1 );
191+
t.strictEqual( actual, 1, 'returns expected value' );
192+
193+
actual = indexOfSameValue( x, false, 3 );
194+
t.strictEqual( actual, 3, 'returns expected value' );
195+
196+
actual = indexOfSameValue( x, true, 5 );
197+
t.strictEqual( actual, 5, 'returns expected value' );
198+
199+
// Negative starting index...
200+
actual = indexOfSameValue( x, true, -10 );
201+
t.strictEqual( actual, 0, 'returns expected value' );
202+
203+
actual = indexOfSameValue( x, true, -5 );
204+
t.strictEqual( actual, 1, 'returns expected value' );
205+
206+
actual = indexOfSameValue( x, false, -3 );
207+
t.strictEqual( actual, 3, 'returns expected value' );
208+
209+
actual = indexOfSameValue( x, true, -1 );
210+
t.strictEqual( actual, 5, 'returns expected value' );
211+
212+
// Non-boolean values:
213+
actual = indexOfSameValue( x, 0, 0 );
214+
t.strictEqual( actual, -1, 'returns expected value' );
215+
216+
t.end();
217+
});
218+
126219
tape( 'the function returns the first index of an element which equals a provided search element (int32)', function test( t ) {
127220
var actual;
128221
var x;

0 commit comments

Comments
 (0)