Skip to content

Commit 49a9e59

Browse files
feat: add logic, tests, and benchmarks for keys method
1 parent d5575ad commit 49a9e59

File tree

4 files changed

+516
-0
lines changed

4 files changed

+516
-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 isIterator = require( '@stdlib/assert/is-iterator-like' );
25+
var pkg = require( './../package.json' ).name;
26+
var factory = require( './../lib' );
27+
28+
29+
// VARIABLES //
30+
31+
var Float64ArrayFE = factory( 'float64' );
32+
33+
34+
// MAIN //
35+
36+
bench( pkg+':keys', function benchmark( b ) {
37+
var iter;
38+
var arr;
39+
var i;
40+
41+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 2.0, 1.0 ] );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
iter = arr.keys();
46+
if ( typeof iter !== 'object' ) {
47+
b.fail( 'should return an object' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isIterator( iter ) ) {
52+
b.fail( 'should return an iterator' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 isIterator = require( '@stdlib/assert/is-iterator-like' );
27+
var pkg = require( './../package.json' ).name;
28+
var factory = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var Float64ArrayFE = factory( 'float64' );
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
44+
return benchmark;
45+
46+
/**
47+
* Benchmark function.
48+
*
49+
* @private
50+
* @param {Benchmark} b - benchmark instance
51+
*/
52+
function benchmark( b ) {
53+
var iter;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
iter = arr.keys();
59+
if ( typeof iter !== 'object' ) {
60+
b.fail( 'should return an object' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isIterator( iter ) ) {
65+
b.fail( 'should return an iterator' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
f = createBenchmark( len );
93+
bench( pkg+':keys:len='+len, f );
94+
}
95+
}
96+
97+
main();

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,92 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
501501
return this._buffer[ GETTER ]( idx * BYTES_PER_ELEMENT, this._isLE );
502502
});
503503

504+
/**
505+
* Returns an iterator for iterating over each index key in a typed array.
506+
*
507+
* @private
508+
* @name keys
509+
* @memberof TypedArray.prototype
510+
* @type {Function}
511+
* @throws {TypeError} `this` must be a typed array instance
512+
* @returns {Iterator} Iterator
513+
*/
514+
setReadOnly( TypedArray.prototype, 'keys', function keys() {
515+
var self;
516+
var iter;
517+
var len;
518+
var FLG;
519+
var i;
520+
if ( !isTypedArray( this ) ) {
521+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
522+
}
523+
524+
self = this;
525+
len = this._length;
526+
527+
// Initialize an iteration index:
528+
i = -1;
529+
530+
// Create an iterator protocol-compliant object:
531+
iter = {};
532+
setReadOnly( iter, 'next', next );
533+
setReadOnly( iter, 'return', end );
534+
535+
if ( ITERATOR_SYMBOL ) {
536+
setReadOnly( iter, ITERATOR_SYMBOL, factory );
537+
}
538+
return iter;
539+
540+
/**
541+
* Returns an iterator protocol-compliant object containing the next iterated value.
542+
*
543+
* @private
544+
* @returns {Object} iterator protocol-compliant object
545+
*/
546+
function next() {
547+
i += 1;
548+
if ( FLG || i >= len ) {
549+
return {
550+
'done': true
551+
};
552+
}
553+
return {
554+
'value': i,
555+
'done': false
556+
};
557+
}
558+
559+
/**
560+
* Finishes an iterator.
561+
*
562+
* @private
563+
* @param {*} [value] - value to return
564+
* @returns {Object} iterator protocol-compliant object
565+
*/
566+
function end( value ) {
567+
FLG = true;
568+
if ( arguments.length ) {
569+
return {
570+
'value': value,
571+
'done': true
572+
};
573+
}
574+
return {
575+
'done': true
576+
};
577+
}
578+
579+
/**
580+
* Returns a new iterator.
581+
*
582+
* @private
583+
* @returns {Iterator} iterator
584+
*/
585+
function factory() {
586+
return self.keys();
587+
}
588+
});
589+
504590
/**
505591
* Pointer to the underlying data buffer.
506592
*

0 commit comments

Comments
 (0)