Skip to content

Commit b34732c

Browse files
aayush0325kgryte
andauthored
feat: add at method to array/fixed-endian-factory
PR-URL: #3184 Closes: #3135 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent e1e57ad commit b34732c

File tree

4 files changed

+362
-0
lines changed

4 files changed

+362
-0
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,38 @@ var v = arr.get( 0 );
306306
// returns 1.0
307307
```
308308

309+
<a name="method-at"></a>
310+
311+
#### TypedArrayFE.prototype.at( i )
312+
313+
Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer positions.
314+
315+
```javascript
316+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
317+
318+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
319+
320+
var out = arr.at( 0 );
321+
// returns 1.0
322+
323+
out = arr.at( -1 );
324+
// returns 3.0
325+
```
326+
327+
If provided an out-of-bounds index, the method returns `undefined`.
328+
329+
```javascript
330+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
331+
332+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
333+
334+
var v = arr.at( 100 );
335+
// returns undefined
336+
337+
v = arr.at( -100 );
338+
// returns undefined
339+
```
340+
309341
<a name="method-for-each"></a>
310342

311343
#### TypedArrayFE.prototype.forEach( callbackFn\[, thisArg] )
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
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+'::nonnegative_indices:at:endianness=little-endian', function benchmark( b ) {
37+
var arr;
38+
var N;
39+
var v;
40+
var i;
41+
42+
arr = [];
43+
for ( i = 0; i < 10; i++ ) {
44+
arr.push( i );
45+
}
46+
arr = new Float64ArrayFE( 'little-endian', arr );
47+
N = arr.length;
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
v = arr.at( i % N );
52+
if ( typeof v !== 'number' ) {
53+
b.fail( 'should return a number' );
54+
}
55+
}
56+
b.toc();
57+
if ( !isNumber( v ) ) {
58+
b.fail( 'should return a number' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
63+
64+
bench( pkg+'::negative_indices:at:endianness=little-endian', function benchmark( b ) {
65+
var arr;
66+
var N;
67+
var v;
68+
var i;
69+
70+
arr = [];
71+
for ( i = 0; i < 10; i++ ) {
72+
arr.push( i );
73+
}
74+
arr = new Float64ArrayFE( 'little-endian', arr );
75+
N = arr.length;
76+
77+
b.tic();
78+
for ( i = 0; i < b.iterations; i++ ) {
79+
v = arr.at( -( i % N ) - 1 );
80+
if ( typeof v !== 'number' ) {
81+
b.fail( 'should return a number' );
82+
}
83+
}
84+
b.toc();
85+
if ( !isNumber( v ) ) {
86+
b.fail( 'should return a number' );
87+
}
88+
b.pass( 'benchmark finished' );
89+
b.end();
90+
});
91+
92+
bench( pkg+'::nonnegative_indices:at:endianness=big-endian', function benchmark( b ) {
93+
var arr;
94+
var N;
95+
var v;
96+
var i;
97+
98+
arr = [];
99+
for ( i = 0; i < 1000; i++ ) {
100+
arr.push( i );
101+
}
102+
arr = new Float64ArrayFE( 'big-endian', arr );
103+
N = arr.length;
104+
105+
b.tic();
106+
for ( i = 0; i < b.iterations; i++ ) {
107+
v = arr.at( i % N );
108+
if ( typeof v !== 'number' ) {
109+
b.fail( 'should return a number' );
110+
}
111+
}
112+
b.toc();
113+
if ( !isNumber( v ) ) {
114+
b.fail( 'should return a number' );
115+
}
116+
b.pass( 'benchmark finished' );
117+
b.end();
118+
});
119+
120+
bench( pkg+'::negative_indices:at:endianness=big-endian', function benchmark( b ) {
121+
var arr;
122+
var N;
123+
var v;
124+
var i;
125+
126+
arr = [];
127+
for ( i = 0; i < 1000; i++ ) {
128+
arr.push( i );
129+
}
130+
arr = new Float64ArrayFE( 'big-endian', arr );
131+
N = arr.length;
132+
133+
b.tic();
134+
for ( i = 0; i < b.iterations; i++ ) {
135+
v = arr.at( -( i % N ) - 1 );
136+
if ( typeof v !== 'number' ) {
137+
b.fail( 'should return a number' );
138+
}
139+
}
140+
b.toc();
141+
if ( !isNumber( v ) ) {
142+
b.fail( 'should return a number' );
143+
}
144+
b.pass( 'benchmark finished' );
145+
b.end();
146+
});

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// MODULES //
2424

2525
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
26+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
2627
var isCollection = require( '@stdlib/assert/is-collection' );
2728
var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
2829
var isObject = require( '@stdlib/assert/is-object' );
@@ -457,6 +458,36 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
457458
return new this( order, args );
458459
});
459460

461+
/**
462+
* Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer indices.
463+
*
464+
* @private
465+
* @name at
466+
* @memberof TypedArray.prototype
467+
* @type {Function}
468+
* @param {integer} idx - element index
469+
* @throws {TypeError} `this` must be a typed array instance
470+
* @throws {TypeError} must provide an integer
471+
* @returns {(*|void)} array element
472+
*/
473+
setReadOnly( TypedArray.prototype, 'at', function at( idx ) {
474+
var len;
475+
if ( !isTypedArray( this ) ) {
476+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
477+
}
478+
if ( !isInteger( idx ) ) {
479+
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', idx ) );
480+
}
481+
len = this._length;
482+
if ( idx < 0 ) {
483+
idx += len;
484+
}
485+
if ( idx < 0 || idx >= len ) {
486+
return;
487+
}
488+
return this._buffer[ GETTER ]( idx * BYTES_PER_ELEMENT, this._isLE );
489+
});
490+
460491
/**
461492
* Pointer to the underlying data buffer.
462493
*
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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 `at` method', function test( t ) {
44+
var ctor = factory( 'float64' );
45+
t.strictEqual( hasOwnProp( ctor.prototype, 'at' ), true, 'returns expected value' );
46+
t.strictEqual( isFunction( ctor.prototype.at ), 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.at.call( value, 0 );
79+
};
80+
}
81+
});
82+
83+
tape( 'the method throws an error if provided an index 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.at( value );
112+
};
113+
}
114+
});
115+
116+
tape( 'the method returns `undefined` if provided an index 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, 10.0 ] );
124+
125+
for ( i = -arr.length; i < arr.length; i++ ) {
126+
if ( i < 0 ) {
127+
v = arr.at( i - arr.length );
128+
t.strictEqual( v, void 0, 'returns expected value for index '+(i-arr.length) );
129+
} else {
130+
v = arr.at( arr.length+i );
131+
t.strictEqual( v, void 0, 'returns expected value for index '+(arr.length+i) );
132+
}
133+
}
134+
t.end();
135+
});
136+
137+
tape( 'the method returns an array element', function test( t ) {
138+
var expected;
139+
var ctor;
140+
var arr;
141+
var v;
142+
var i;
143+
144+
ctor = factory( 'float64' );
145+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
146+
expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0 ];
147+
148+
for ( i = -arr.length; i < arr.length; i++ ) {
149+
v = arr.at( i );
150+
t.strictEqual( v, expected[ i + arr.length ], 'returns expected value' );
151+
}
152+
t.end();
153+
});

0 commit comments

Comments
 (0)