Skip to content

Commit accee60

Browse files
committed
Add base array utility to return an accessor for setting elements in arrays supporting the get/set protocol
1 parent 32cc6fc commit accee60

File tree

10 files changed

+1137
-0
lines changed

10 files changed

+1137
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# accessorSetter
22+
23+
> Return an accessor function for setting an element in an array-like object supporting the get/set protocol.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var accessorSetter = require( '@stdlib/array/base/accessor-setter' );
41+
```
42+
43+
#### accessorSetter( dtype )
44+
45+
Returns an accessor function for setting an element in an array-like object supporting the get/set protocol.
46+
47+
```javascript
48+
var Complex64Array = require( '@stdlib/array/complex64' );
49+
var Complex64 = require( '@stdlib/complex/float32' );
50+
var realf = require( '@stdlib/complex/realf' );
51+
var imagf = require( '@stdlib/complex/imagf' );
52+
53+
var arr = new Complex64Array( [ 1, 2, 3, 4 ] );
54+
55+
var set = accessorSetter( 'complex64' );
56+
set( arr, 1, new Complex64( 10.0, 11.0 ) );
57+
58+
var v = arr.get( 1 );
59+
// returns <Complex64>
60+
61+
var re = realf( v );
62+
// returns 10.0
63+
64+
var im = imagf( v );
65+
// returns 11.0
66+
```
67+
68+
The returned accessor function accepts the following arguments:
69+
70+
- **arr**: input array.
71+
- **idx**: element index.
72+
- **value**: value to set.
73+
74+
</section>
75+
76+
<!-- /.usage -->
77+
78+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
79+
80+
<section class="notes">
81+
82+
## Notes
83+
84+
- If provided an unsupported [`dtype`][@stdlib/array/dtypes], the function returns a default accessor function for accessing elements from any indexed array-like object supporting the get/set protocol; otherwise, the function returns an accessor function which should **only** be provided an array instance corresponding to `dtype` (e.g., if `dtype` is `'complex64'`, the returned accessor function should only be provided instances of `Complex64Array`).
85+
- Accessor functions do **not** verify that provided input arrays are array instances corresponding to `dtype`, as doing so would introduce performance overhead. If array instances corresponding to other data types are provided to an accessor function, JavaScript runtimes will consider the function polymorphic, potentially triggering de-optimization. In order to ensure maximum performance, **always** ensure that an accessor function is monomorphic.
86+
- Accessor functions do **not** perform bounds checking.
87+
- Accessor functions do **not** validate input values.
88+
- Accessor functions do **not** verify that provided input arrays actually implement the get/set protocol.
89+
- An array-like object supporting the get/set protocol is a data structure in which one accesses elements using explicit `get` and `set` methods (e.g., `Complex64Array` and `Complex128Array`).
90+
91+
</section>
92+
93+
<!-- /.notes -->
94+
95+
<!-- Package usage examples. -->
96+
97+
<section class="examples">
98+
99+
## Examples
100+
101+
<!-- eslint no-undef: "error" -->
102+
103+
```javascript
104+
var Complex128Array = require( '@stdlib/array/complex128' );
105+
var Complex64Array = require( '@stdlib/array/complex64' );
106+
var Complex128 = require( '@stdlib/complex/float64' );
107+
var Complex64 = require( '@stdlib/complex/float32' );
108+
var zeroTo = require( '@stdlib/array/base/zero-to' );
109+
var dtype = require( '@stdlib/array/dtype' );
110+
var accessorSetter = require( '@stdlib/array/base/accessor-setter' );
111+
112+
var arr = new Complex128Array( zeroTo( 10 ) );
113+
accessorSetter( dtype( arr ) )( arr, 2, new Complex128( 100.0, 101.0 ) );
114+
115+
var v = arr.get( 2 );
116+
// returns <Complex128>
117+
118+
console.log( '%s', v.toString() );
119+
// => '100 + 101i'
120+
121+
arr = new Complex64Array( zeroTo( 10 ) );
122+
accessorSetter( dtype( arr ) )( arr, 4, new Complex64( 102.0, 103.0 ) );
123+
124+
v = arr.get( 4 );
125+
// returns <Complex64>
126+
127+
console.log( '%s', v.toString() );
128+
// => '102 + 103i'
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="references">
138+
139+
</section>
140+
141+
<!-- /.references -->
142+
143+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib
156+
157+
</section>
158+
159+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
25+
var isFunction = require( '@stdlib/assert/is-function' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
28+
var filledBy = require( '@stdlib/array/filled-by' );
29+
var Complex128Array = require( '@stdlib/array/complex128' );
30+
var Complex64Array = require( '@stdlib/array/complex64' );
31+
var Complex128 = require( '@stdlib/complex/float64' );
32+
var Complex64 = require( '@stdlib/complex/float32' );
33+
var real = require( '@stdlib/complex/real' );
34+
var imag = require( '@stdlib/complex/imag' );
35+
var realf = require( '@stdlib/complex/realf' );
36+
var imagf = require( '@stdlib/complex/imagf' );
37+
var dtype = require( '@stdlib/array/dtype' );
38+
var pkg = require( './../package.json' ).name;
39+
var setter = require( './../lib' );
40+
41+
42+
// VARIABLES //
43+
44+
var rand = discreteUniform( 0, 127 );
45+
46+
47+
// MAIN //
48+
49+
bench( pkg, function benchmark( b ) {
50+
var set;
51+
var dt;
52+
var i;
53+
54+
dt = [
55+
'complex128',
56+
'complex64',
57+
'foo'
58+
];
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
set = setter( dt[ i%dt.length ] );
63+
if ( typeof set !== 'function' ) {
64+
b.fail( 'should return a function' );
65+
}
66+
}
67+
b.toc();
68+
if ( !isFunction( set ) ) {
69+
b.fail( 'should return a function' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
74+
75+
bench( pkg+':dtype=complex128', function benchmark( b ) {
76+
var values;
77+
var arr;
78+
var buf;
79+
var set;
80+
var i;
81+
var j;
82+
var v;
83+
84+
values = [
85+
new Complex128( 1.0, 2.0 ),
86+
new Complex128( 3.0, 4.0 ),
87+
new Complex128( 5.0, 6.0 )
88+
];
89+
90+
buf = filledBy( 100, 'float64', rand );
91+
arr = new Complex128Array( buf.buffer );
92+
set = setter( dtype( arr ) );
93+
94+
b.tic();
95+
for ( i = 0; i < b.iterations; i++ ) {
96+
j = i % arr.length;
97+
set( arr, j, values[ i%values.length ] );
98+
v = arr.get( j );
99+
if ( typeof v !== 'object' ) {
100+
b.fail( 'should return an object' );
101+
}
102+
}
103+
b.toc();
104+
if ( isnan( real( v ) ) || isnan( imag( v ) ) ) {
105+
b.fail( 'should not return NaN' );
106+
}
107+
b.pass( 'benchmark finished' );
108+
b.end();
109+
});
110+
111+
bench( pkg+':dtype=complex64', function benchmark( b ) {
112+
var values;
113+
var arr;
114+
var buf;
115+
var set;
116+
var i;
117+
var j;
118+
var v;
119+
120+
values = [
121+
new Complex64( 1.0, 2.0 ),
122+
new Complex64( 3.0, 4.0 ),
123+
new Complex64( 5.0, 6.0 )
124+
];
125+
126+
buf = filledBy( 100, 'float32', rand );
127+
arr = new Complex64Array( buf.buffer );
128+
set = setter( dtype( arr ) );
129+
130+
b.tic();
131+
for ( i = 0; i < b.iterations; i++ ) {
132+
j = i % arr.length;
133+
set( arr, j, values[ i%values.length ] );
134+
v = arr.get( j );
135+
if ( typeof v !== 'object' ) {
136+
b.fail( 'should return an object' );
137+
}
138+
}
139+
b.toc();
140+
if ( isnanf( realf( v ) ) || isnanf( imagf( v ) ) ) {
141+
b.fail( 'should not return NaN' );
142+
}
143+
b.pass( 'benchmark finished' );
144+
b.end();
145+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
{{alias}}( dtype )
3+
Returns an accessor function for setting an element in an array-like object
4+
supporting the get/set protocol.
5+
6+
An accessor function accepts the following arguments:
7+
8+
- arr: input array
9+
- idx: element index
10+
- value: value to set
11+
12+
If provided an unsupported `dtype`, the function returns a default accessor
13+
function for accessing elements in any indexed array-like object supporting
14+
the get/set protocol.
15+
16+
Otherwise, the function returns an accessor function which should *only* be
17+
provided an array instance corresponding to `dtype` (e.g., if `dtype` is
18+
'complex64', the returned accessor function should only be provided
19+
instances of Complex64Array).
20+
21+
Accessor functions do *not* verify that provided input arrays are array
22+
instances corresponding to `dtype`, as doing so would introduce performance
23+
overhead. If array instances corresponding to other data types are provided
24+
to an accessor function, JavaScript runtimes will consider the function
25+
polymorphic, potentially triggering de-optimization. In order to ensure
26+
maximum performance, *always* ensure that an accessor function is
27+
monomorphic.
28+
29+
Accessor functions do *not* perform bounds checking.
30+
31+
Accessor functions do *not* validate input values.
32+
33+
Accessor functions do *not* verify that provided input arrays actually
34+
implement the get/set protocol.
35+
36+
Parameters
37+
----------
38+
dtype: string
39+
Array data type.
40+
41+
Returns
42+
-------
43+
f: Function
44+
Accessor function.
45+
46+
Examples
47+
--------
48+
> var f = {{alias}}( 'complex64' );
49+
> var x = {{alias:@stdlib/array/complex64}}( [ 1, 2, 3, 4 ] );
50+
> f( x, 1, new {{alias:@stdlib/complex/float32}}( 10.0, 11.0 ) );
51+
> var v = x.get( 1 )
52+
<Complex64>
53+
> var r = {{alias:@stdlib/complex/realf}}( v )
54+
10.0
55+
> var i = {{alias:@stdlib/complex/imagf}}( v )
56+
11.0
57+
58+
See Also
59+
--------
60+

0 commit comments

Comments
 (0)