diff --git a/CHANGELOG.md b/CHANGELOG.md index d290e434..245b7c70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -480,6 +480,7 @@ This release closes the following issue: ##### Features +- [`9db1815`](https://github.com/stdlib-js/stdlib/commit/9db181520dc74f28aab70a73aac64202cf7560ce) - add `indexOf` method to `array/fixed-endian-factory` [(#3279)](https://github.com/stdlib-js/stdlib/pull/3279) - [`1d217ba`](https://github.com/stdlib-js/stdlib/commit/1d217bad1d35210a57d65c8be67c596815589082) - add `map` method to `array/fixed-endian-factory` [(#3270)](https://github.com/stdlib-js/stdlib/pull/3270) - [`2df0e9b`](https://github.com/stdlib-js/stdlib/commit/2df0e9bb08b8ad1c573b7c2383ef39e492dd5436) - add `some` method to `array/fixed-endian-factory` [(#3241)](https://github.com/stdlib-js/stdlib/pull/3241) - [`e3a2173`](https://github.com/stdlib-js/stdlib/commit/e3a2173a24bd8634f333cace626fc2d71740ebd3) - add `every` method to `array/fixed-endian-factory` [(#3200)](https://github.com/stdlib-js/stdlib/pull/3200) @@ -494,9 +495,9 @@ This release closes the following issue: ##### Closed Issues -A total of 4 issues were closed in this release: +A total of 5 issues were closed in this release: -[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3150](https://github.com/stdlib-js/stdlib/issues/3150), [#3155](https://github.com/stdlib-js/stdlib/issues/3155) +[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3146](https://github.com/stdlib-js/stdlib/issues/3146), [#3150](https://github.com/stdlib-js/stdlib/issues/3150), [#3155](https://github.com/stdlib-js/stdlib/issues/3155) @@ -550,9 +551,9 @@ A total of 4 issues were closed in this release: ### Closed Issues -A total of 11 issues were closed in this release: +A total of 12 issues were closed in this release: -[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3150](https://github.com/stdlib-js/stdlib/issues/3150), [#3155](https://github.com/stdlib-js/stdlib/issues/3155), [#3164](https://github.com/stdlib-js/stdlib/issues/3164), [#3165](https://github.com/stdlib-js/stdlib/issues/3165), [#3176](https://github.com/stdlib-js/stdlib/issues/3176), [#3177](https://github.com/stdlib-js/stdlib/issues/3177), [#3178](https://github.com/stdlib-js/stdlib/issues/3178), [#3179](https://github.com/stdlib-js/stdlib/issues/3179), [#3180](https://github.com/stdlib-js/stdlib/issues/3180) +[#3135](https://github.com/stdlib-js/stdlib/issues/3135), [#3138](https://github.com/stdlib-js/stdlib/issues/3138), [#3146](https://github.com/stdlib-js/stdlib/issues/3146), [#3150](https://github.com/stdlib-js/stdlib/issues/3150), [#3155](https://github.com/stdlib-js/stdlib/issues/3155), [#3164](https://github.com/stdlib-js/stdlib/issues/3164), [#3165](https://github.com/stdlib-js/stdlib/issues/3165), [#3176](https://github.com/stdlib-js/stdlib/issues/3176), [#3177](https://github.com/stdlib-js/stdlib/issues/3177), [#3178](https://github.com/stdlib-js/stdlib/issues/3178), [#3179](https://github.com/stdlib-js/stdlib/issues/3179), [#3180](https://github.com/stdlib-js/stdlib/issues/3180) @@ -562,10 +563,11 @@ A total of 11 issues were closed in this release: ### Contributors -A total of 7 people contributed to this release. Thank you to the following contributors: +A total of 8 people contributed to this release. Thank you to the following contributors: - Aayush Khanna - Abhijit Raut +- Aditya Sapra - Athan Reines - Kshitij-Dale - Philipp Burckhardt @@ -582,6 +584,7 @@ A total of 7 people contributed to this release. Thank you to the following cont
+- [`9db1815`](https://github.com/stdlib-js/stdlib/commit/9db181520dc74f28aab70a73aac64202cf7560ce) - **feat:** add `indexOf` method to `array/fixed-endian-factory` [(#3279)](https://github.com/stdlib-js/stdlib/pull/3279) _(by Aditya Sapra, Athan Reines)_ - [`1d217ba`](https://github.com/stdlib-js/stdlib/commit/1d217bad1d35210a57d65c8be67c596815589082) - **feat:** add `map` method to `array/fixed-endian-factory` [(#3270)](https://github.com/stdlib-js/stdlib/pull/3270) _(by Aayush Khanna, Athan Reines)_ - [`2df0e9b`](https://github.com/stdlib-js/stdlib/commit/2df0e9bb08b8ad1c573b7c2383ef39e492dd5436) - **feat:** add `some` method to `array/fixed-endian-factory` [(#3241)](https://github.com/stdlib-js/stdlib/pull/3241) _(by Kshitij-Dale, Athan Reines)_ - [`abb0dc3`](https://github.com/stdlib-js/stdlib/commit/abb0dc38783210623e67f19a5bb95b3998f75ff7) - **docs:** update examples and descriptions _(by Athan Reines)_ diff --git a/CONTRIBUTORS b/CONTRIBUTORS index fd0e7292..a8d0bb6e 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -48,6 +48,7 @@ Karthik Prakash <116057817+skoriop@users.noreply.github.com> Khaldon Kohantika Nath <145763549+kohantikanath@users.noreply.github.com> Krishnendu Das <86651039+itskdhere@users.noreply.github.com> +Kshitij-Dale <152467202+Kshitij-Dale@users.noreply.github.com> Lovelin <100030865+lovelindhoni@users.noreply.github.com> Manik Sharma Marcus Fantham diff --git a/fixed-endian-factory/README.md b/fixed-endian-factory/README.md index d3b46a91..15724ee3 100644 --- a/fixed-endian-factory/README.md +++ b/fixed-endian-factory/README.md @@ -483,6 +483,38 @@ var v = arr.get( 100 ); // returns undefined ``` + + +#### TypedArrayFE.prototype.indexOf( searchElement\[, fromIndex] ) + +Returns the first index at which a given element can be found. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + +var idx = arr.indexOf( 2.0 ); +// returns 1 + +idx = arr.indexOf( 2.0, 2 ); +// returns 4 + +idx = arr.indexOf( 2.0, -4 ); +// returns 1 +``` + +If `searchElement` is not present in the array, the method returns `-1`. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + +var idx = arr.indexOf( 5.0 ); +// returns -1 +``` + #### TypedArray.prototype.map( callbackFn\[, thisArg] ) diff --git a/fixed-endian-factory/benchmark/benchmark.index_of.js b/fixed-endian-factory/benchmark/benchmark.index_of.js new file mode 100644 index 00000000..04944e3c --- /dev/null +++ b/fixed-endian-factory/benchmark/benchmark.index_of.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var factory = require( './../lib' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// MAIN // + +bench( pkg+':indexOf', function benchmark( b ) { + var arr; + var idx; + var i; + + arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + idx = arr.indexOf( 5.0, 0 ); + if ( typeof idx !== 'number' ) { + b.fail( 'should return an integer' ); + } + } + b.toc(); + if ( !isInteger( idx ) ) { + b.fail( 'should return an integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fixed-endian-factory/benchmark/benchmark.index_of.length.js b/fixed-endian-factory/benchmark/benchmark.index_of.length.js new file mode 100644 index 00000000..24938940 --- /dev/null +++ b/fixed-endian-factory/benchmark/benchmark.index_of.length.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var zeroTo = require( './../../zero-to' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var factory = require( './../lib' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var idx; + var v; + var i; + + v = len - 1; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + idx = arr.indexOf( v ); + if ( typeof idx !== 'number' ) { + b.fail( 'should return an integer' ); + } + } + b.toc(); + if ( !isInteger( idx ) ) { + b.fail( 'should return an integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':indexOf:len='+len, f ); + } +} + +main(); diff --git a/fixed-endian-factory/lib/main.js b/fixed-endian-factory/lib/main.js index c3c03326..ccee59e7 100644 --- a/fixed-endian-factory/lib/main.js +++ b/fixed-endian-factory/lib/main.js @@ -634,6 +634,48 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli return this._buffer[ GETTER ]( idx*BYTES_PER_ELEMENT, this._isLE ); }); + /** + * Returns the index of the first occurrence of a given element. + * + * @private + * @name indexOf + * @memberof TypedArray.prototype + * @type {Function} + * @param {*} searchElement - element to search for + * @param {integer} [fromIndex=0] - starting index (inclusive) + * @throws {TypeError} `this` must be a typed array instance + * @throws {TypeError} second argument must be an integer + * @returns {integer} index or -1 + */ + setReadOnly( TypedArray.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) { + var buf; + var i; + + if ( !isTypedArray( this ) ) { + throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) ); + } + if ( arguments.length > 1 ) { + if ( !isInteger( fromIndex ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); + } + if ( fromIndex < 0 ) { + fromIndex += this._length; + if ( fromIndex < 0 ) { + fromIndex = 0; + } + } + } else { + fromIndex = 0; + } + buf = this._buffer; + for ( i = fromIndex; i < this._length; i++ ) { + if ( buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ) === searchElement ) { + return i; + } + } + return -1; + }); + /** * Number of array elements. * diff --git a/fixed-endian-factory/test/test.index_of.js b/fixed-endian-factory/test/test.index_of.js new file mode 100644 index 00000000..ea6b8c2c --- /dev/null +++ b/fixed-endian-factory/test/test.index_of.js @@ -0,0 +1,226 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var factory = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a function', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( isFunction( ctor ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'attached to the prototype of the returned function is an `indexOf` method', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( hasOwnProp( ctor.prototype, 'indexOf' ), true, 'returns expected value' ); + t.strictEqual( isFunction( ctor.prototype.indexOf ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.indexOf.call( value, 0 ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.indexOf( 1.0, value ); + }; + } +}); + +tape( 'the method returns `-1` if provided a second argument which exceeds array dimensions', function test( t ) { + var ctor; + var arr; + var v; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + + for ( i = arr.length; i < arr.length+5; i++ ) { + v = arr.indexOf( 1.0, i ); + t.strictEqual( v, -1, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the method returns `-1` if operating on an empty array', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [] ); + + v = arr.indexOf( 1.0 ); + t.strictEqual( v, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method returns `-1` if a search element is not found', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + + v = arr.indexOf( 10.0 ); + t.strictEqual( v, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method returns the index of the first match if an element is found', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + + v = arr.indexOf( 2.0 ); + t.strictEqual( v, 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method supports specifying a starting search index', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + + v = arr.indexOf( 2.0, 0 ); + t.strictEqual( v, 1, 'returns expected value' ); + + v = arr.indexOf( 2.0, 2 ); + t.strictEqual( v, 4, 'returns expected value' ); + + v = arr.indexOf( 1.0, 4 ); + t.strictEqual( v, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the method supports specifying a starting search index (negative)', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + + v = arr.indexOf( 2.0, -3 ); + t.strictEqual( v, 4, 'returns expected value' ); + + v = arr.indexOf( 1.0, -3 ); + t.strictEqual( v, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'when provided a starting index which resolves to an index less than zero, the method searches from the first array element', function test( t ) { + var ctor; + var arr; + var v; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] ); + + v = arr.indexOf( 2.0, -10 ); + t.strictEqual( v, 1, 'returns expected value' ); + + v = arr.indexOf( 1.0, -10 ); + t.strictEqual( v, 0, 'returns expected value' ); + + t.end(); +});