Skip to content

Commit dd143b9

Browse files
committed
feat: add strided/base/write-dataview
1 parent 2aae52e commit dd143b9

File tree

14 files changed

+4879
-0
lines changed

14 files changed

+4879
-0
lines changed
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
<!-- lint disable maximum-heading-length -->
22+
23+
# writeDataView
24+
25+
> Copy elements from an input strided array to elements in a strided [`DataView`][@stdlib/array/dataview].
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var writeDataView = require( '@stdlib/strided/base/write-dataview' );
39+
```
40+
41+
#### writeDataView( N, x, strideX, view, strideView, littleEndian )
42+
43+
Copies elements from an input strided array to elements in a strided [`DataView`][@stdlib/array/dataview].
44+
45+
```javascript
46+
var ArrayBuffer = require( '@stdlib/array/buffer' );
47+
var DataView = require( '@stdlib/array/dataview' );
48+
49+
var x = [ 1.0, 2.0, 3.0, 4.0 ];
50+
51+
var buf = new ArrayBuffer( 32 );
52+
var view = new DataView( buf );
53+
54+
var out = writeDataView( 4, x, 1, view, 8, true );
55+
// returns <DataView>
56+
57+
var bool = ( out === view );
58+
// returns true
59+
60+
var v = view.getFloat64( 0, true );
61+
// returns 1.0
62+
63+
v = view.getFloat64( 8, true );
64+
// returns 2.0
65+
```
66+
67+
The function accepts the following arguments:
68+
69+
- **N**: number of indexed elements.
70+
- **x**: input strided array.
71+
- **strideX**: index increment for `x`.
72+
- **view**: output [`DataView`][@stdlib/array/dataview].
73+
- **strideView**: index increment (in bytes) for `y`.
74+
- **littleEndian**: boolean indicating whether to store values in little-endian format.
75+
76+
The `N` and stride parameters determine which elements in `x` and `view` are accessed at runtime. For example, to index every other value in `x` and to index the first `N` elements of `y` in reverse order,
77+
78+
```javascript
79+
var ArrayBuffer = require( '@stdlib/array/buffer' );
80+
var DataView = require( '@stdlib/array/dataview' );
81+
82+
var x = [ 1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 4.0, 0.0 ];
83+
84+
var buf = new ArrayBuffer( 64 );
85+
var view = new DataView( buf );
86+
87+
var out = writeDataView( 4, x, 2, view, -8, true );
88+
// returns <DataView>
89+
90+
var bool = ( out === view );
91+
// returns true
92+
93+
var v = view.getFloat64( 0, true );
94+
// returns 4.0
95+
96+
v = view.getFloat64( 8, true );
97+
// returns 3.0
98+
```
99+
100+
Note that indexing is relative to the first index. To introduce an offset, use typed array views.
101+
102+
```javascript
103+
var ArrayBuffer = require( '@stdlib/array/buffer' );
104+
var DataView = require( '@stdlib/array/dataview' );
105+
var Float32Array = require( '@stdlib/array/float32' );
106+
107+
// Initial array:
108+
var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
109+
110+
// Create an offset view:
111+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
112+
113+
// Create an output DataView:
114+
var buf = new ArrayBuffer( 64 );
115+
var view = new DataView( buf );
116+
117+
var out = writeDataView( 4, x, 1, view, 8, true );
118+
// returns <DataView>
119+
120+
var bool = ( out === view );
121+
// returns true
122+
123+
var v = view.getFloat32( 0, true );
124+
// returns 2.0
125+
126+
v = view.getFloat32( 8, true );
127+
// returns 3.0
128+
```
129+
130+
#### writeDataView.ndarray( N, x, strideX, offsetX, view, strideView, offsetView, littleEndian )
131+
132+
Copies elements from an input strided array to elements in a strided [`DataView`][@stdlib/array/dataview] using alternative indexing semantics.
133+
134+
```javascript
135+
var ArrayBuffer = require( '@stdlib/array/buffer' );
136+
var DataView = require( '@stdlib/array/dataview' );
137+
138+
var x = [ 1.0, 2.0, 3.0, 4.0 ];
139+
140+
var buf = new ArrayBuffer( 32 );
141+
var view = new DataView( buf );
142+
143+
var out = writeDataView.ndarray( 4, x, 1, 0, view, 8, 0, true );
144+
// returns <DataView>
145+
146+
var bool = ( out === view );
147+
// returns true
148+
149+
var v = view.getFloat64( 0, true );
150+
// returns 1.0
151+
```
152+
153+
The function accepts the following additional arguments:
154+
155+
- **offsetX**: starting index for `x`.
156+
- **offsetView**: starting index (in bytes) for `view`.
157+
158+
While typed array views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to index every other value in `x` starting from the second value and to index the last `N` elements in `view` in reverse order,
159+
160+
```javascript
161+
var ArrayBuffer = require( '@stdlib/array/buffer' );
162+
var DataView = require( '@stdlib/array/dataview' );
163+
164+
var x = [ 0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 4.0 ];
165+
166+
var buf = new ArrayBuffer( 64 );
167+
var view = new DataView( buf );
168+
169+
var out = writeDataView( 4, x, 2, 1, view, -8, 56, true );
170+
// returns <DataView>
171+
172+
var bool = ( out === view );
173+
// returns true
174+
175+
var v = view.getFloat64( 32, true );
176+
// returns 4.0
177+
178+
v = view.getFloat64( 40, true );
179+
// returns 3.0
180+
```
181+
182+
</section>
183+
184+
<!-- /.usage -->
185+
186+
<section class="notes">
187+
188+
</section>
189+
190+
<!-- /.notes -->
191+
192+
<section class="examples">
193+
194+
## Examples
195+
196+
<!-- eslint no-undef: "error" -->
197+
198+
```javascript
199+
var ArrayBuffer = require( '@stdlib/array/buffer' );
200+
var DataView = require( '@stdlib/array/dataview' );
201+
var typedarray = require( '@stdlib/array/typed' );
202+
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
203+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
204+
var IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' );
205+
var logEach = require( '@stdlib/console/log-each' );
206+
var writeDataView = require( '@stdlib/strided/base/write-dataview' );
207+
208+
// Specify the array data type:
209+
var dtype = 'float64';
210+
211+
// Resolve the number of bytes per element:
212+
var nbytes = bytesPerElement( dtype );
213+
214+
// Generate an array of random numbers:
215+
var x = discreteUniform( 10, 0, 100, {
216+
'dtype': dtype
217+
});
218+
219+
// Create a DataView:
220+
var buf = new ArrayBuffer( x.length*nbytes );
221+
var view = new DataView( buf );
222+
223+
// Copy the numbers to the DataView:
224+
writeDataView( x.length, x, 1, view, nbytes, IS_LITTLE_ENDIAN );
225+
226+
// Create a view of the DataView:
227+
var y = typedarray( view.buffer, dtype );
228+
229+
// Print the results:
230+
logEach( '%d -> %d', x, y );
231+
```
232+
233+
</section>
234+
235+
<!-- /.examples -->
236+
237+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
238+
239+
<section class="related">
240+
241+
</section>
242+
243+
<!-- /.related -->
244+
245+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
246+
247+
<section class="links">
248+
249+
[@stdlib/array/dataview]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dataview
250+
251+
</section>
252+
253+
<!-- /.links -->
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var typedarray = require( '@stdlib/array/typed' );
28+
var ArrayBuffer = require( '@stdlib/array/buffer' );
29+
var DataView = require( '@stdlib/array/dataview' );
30+
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
31+
var pkg = require( './../package.json' ).name;
32+
var writeDataView = require( './../lib/main.js' );
33+
34+
35+
// VARIABLES //
36+
37+
var opts = {
38+
'dtype': 'float64'
39+
};
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Creates a benchmark function.
46+
*
47+
* @private
48+
* @param {PositiveInteger} len - array length
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( len ) {
52+
var view;
53+
var buf;
54+
var x;
55+
var y;
56+
57+
x = discreteUniform( len, -100, 100, opts );
58+
59+
buf = new ArrayBuffer( len*bytesPerElement( opts.dtype ) );
60+
view = new DataView( buf );
61+
y = typedarray( buf, opts.dtype );
62+
63+
return benchmark;
64+
65+
/**
66+
* Benchmark function.
67+
*
68+
* @private
69+
* @param {Benchmark} b - benchmark instance
70+
*/
71+
function benchmark( b ) {
72+
var out;
73+
var i;
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
out = writeDataView( x.length, x, 1, view, 1, true );
78+
if ( out === null || isnan( y[ i%len ] ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
}
82+
b.toc();
83+
if ( out === null || isnan( y[ i%len ] ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
}
89+
}
90+
91+
92+
// MAIN //
93+
94+
/**
95+
* Main execution sequence.
96+
*
97+
* @private
98+
*/
99+
function main() {
100+
var len;
101+
var min;
102+
var max;
103+
var f;
104+
var i;
105+
106+
min = 1; // 10^min
107+
max = 6; // 10^max
108+
109+
for ( i = min; i <= max; i++ ) {
110+
len = pow( 10, i );
111+
f = createBenchmark( len );
112+
bench( pkg+':dtype='+opts.dtype+',len='+len, f );
113+
}
114+
}
115+
116+
main();

0 commit comments

Comments
 (0)