Skip to content

Commit 1809375

Browse files
committed
feat: indexOf implementation
1 parent 5b17c89 commit 1809375

File tree

4 files changed

+376
-0
lines changed

4 files changed

+376
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
25+
var factory = require( './../lib' );
26+
var pkg = require( './../package.json' ).name;
27+
28+
29+
// VARIABLES //
30+
31+
var Float64ArrayFE = factory( 'float64' );
32+
33+
34+
// MAIN //
35+
36+
bench( pkg+':indexOf', function benchmark( b ) {
37+
var arr;
38+
var idx;
39+
var i;
40+
41+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
idx = arr.indexOf( 5.0, 0 ); // Start searching from index 0
46+
if ( typeof idx !== 'number' ) {
47+
b.fail( 'should return an integer' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isInteger( idx ) ) {
52+
b.fail( 'should return an integer' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/zero-to' );
26+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
27+
var factory = require( './../lib' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var Float64ArrayFE = factory( 'float64' );
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Creates a benchmark function.
40+
*
41+
* @private
42+
* @param {PositiveInteger} len - array length
43+
* @returns {Function} benchmark function
44+
*/
45+
function createBenchmark( len ) {
46+
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
47+
return benchmark;
48+
49+
/**
50+
* Benchmark function.
51+
*
52+
* @private
53+
* @param {Benchmark} b - benchmark instance
54+
*/
55+
function benchmark( b ) {
56+
var idx;
57+
var v;
58+
var i;
59+
60+
v = len - 1;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
idx = arr.indexOf( v );
65+
if ( typeof idx !== 'number' ) {
66+
b.fail( 'should return an integer' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isInteger( idx ) ) {
71+
b.fail( 'should return an integer' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( pkg+':indexOf:len='+len, f );
100+
}
101+
}
102+
103+
main();

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,9 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
653653
fromIndex = 0;
654654
}
655655
}
656+
if ( fromIndex >= len ) {
657+
return -1;
658+
}
656659
} else {
657660
fromIndex = 0;
658661
}
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var tape = require( 'tape' );
24+
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25+
var isFunction = require( '@stdlib/assert/is-function' );
26+
var factory = require( './../lib' );
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.strictEqual( typeof factory, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'the function returns a function', function test( t ) {
38+
var ctor = factory( 'float64' );
39+
t.strictEqual( isFunction( ctor ), true, 'returns expected value' );
40+
t.end();
41+
});
42+
43+
tape( 'attached to the prototype of the returned function is an `indexOf` method', function test( t ) {
44+
var ctor = factory( 'float64' );
45+
t.strictEqual( hasOwnProp( ctor.prototype, 'indexOf' ), true, 'returns expected value' );
46+
t.strictEqual( isFunction( ctor.prototype.indexOf ), true, 'returns expected value' );
47+
t.end();
48+
});
49+
50+
tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) {
51+
var values;
52+
var ctor;
53+
var arr;
54+
var i;
55+
56+
ctor = factory( 'float64' );
57+
arr = new ctor( 'little-endian', 5 );
58+
59+
values = [
60+
'5',
61+
5,
62+
NaN,
63+
true,
64+
false,
65+
null,
66+
void 0,
67+
{},
68+
[],
69+
function noop() {}
70+
];
71+
for ( i = 0; i < values.length; i++ ) {
72+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
73+
}
74+
t.end();
75+
76+
function badValue( value ) {
77+
return function badValue() {
78+
return arr.indexOf.call( value, 0 );
79+
};
80+
}
81+
});
82+
83+
tape( 'the method throws an error if provided `fromIndex` argument which is not an integer', function test( t ) {
84+
var values;
85+
var ctor;
86+
var arr;
87+
var i;
88+
89+
ctor = factory( 'float64' );
90+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] );
91+
92+
values = [
93+
'5',
94+
3.14,
95+
NaN,
96+
true,
97+
false,
98+
null,
99+
void 0,
100+
{},
101+
[],
102+
function noop() {}
103+
];
104+
for ( i = 0; i < values.length; i++ ) {
105+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
106+
}
107+
t.end();
108+
109+
function badValue( value ) {
110+
return function badValue() {
111+
return arr.indexOf( 1.0, value );
112+
};
113+
}
114+
});
115+
116+
tape( 'the method returns `-1` if provided fromIndex argument which exceeds array dimensions', function test( t ) {
117+
var ctor;
118+
var arr;
119+
var v;
120+
var i;
121+
122+
ctor = factory( 'float64' );
123+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] );
124+
125+
for ( i = arr.length; i < arr.length + 5; i++ ) {
126+
v = arr.indexOf( 1.0, i );
127+
t.strictEqual( v, -1, 'returns expected value' );
128+
}
129+
t.end();
130+
});
131+
132+
tape( 'the method returns `-1` if operating on an empty array', function test( t ) {
133+
var ctor;
134+
var arr;
135+
var v;
136+
137+
ctor = factory( 'float64' );
138+
arr = new ctor( 'little-endian', [] );
139+
140+
v = arr.indexOf( 1.0 );
141+
t.strictEqual( v, -1, 'returns -1 for an empty array' );
142+
143+
t.end();
144+
});
145+
146+
tape( 'the method returns `-1` if element not found', function test( t ) {
147+
var ctor;
148+
var arr;
149+
var v;
150+
151+
ctor = factory( 'float64' );
152+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
153+
154+
v = arr.indexOf( 10.0 );
155+
t.strictEqual( v, -1, 'returns -1 when element is not found' );
156+
157+
t.end();
158+
});
159+
160+
tape( 'the method returns the index of the first match if element is found', function test( t ) {
161+
var ctor;
162+
var arr;
163+
var v;
164+
165+
ctor = factory( 'float64' );
166+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );
167+
168+
v = arr.indexOf( 2.0 );
169+
t.strictEqual( v, 1, 'returns index of the first match when the element is found' );
170+
171+
t.end();
172+
});
173+
174+
tape( 'the method supports specifying a starting search index', function test( t ) {
175+
var ctor;
176+
var arr;
177+
var v;
178+
179+
ctor = factory( 'float64' );
180+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );
181+
182+
v = arr.indexOf( 2.0, 2 );
183+
t.strictEqual( v, 4, 'returns index of the match starting from the specified index' );
184+
185+
t.end();
186+
});
187+
188+
tape( 'the method supports specifying a starting search index (negative)', function test( t ) {
189+
var ctor;
190+
var arr;
191+
var v;
192+
193+
ctor = factory( 'float64' );
194+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );
195+
196+
v = arr.indexOf( 2.0, -3 );
197+
t.strictEqual( v, 4, 'returns index of the match starting from the resolved negative index' );
198+
199+
t.end();
200+
});
201+
202+
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 ) {
203+
var ctor;
204+
var arr;
205+
var v;
206+
207+
ctor = factory( 'float64' );
208+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );
209+
210+
v = arr.indexOf( 2.0, -10 );
211+
t.strictEqual( v, 1, 'returns index of the match starting from the first array element when starting index resolves to less than zero' );
212+
213+
t.end();
214+
});

0 commit comments

Comments
 (0)