Skip to content

Commit f335956

Browse files
committed
feat: add ndarray/base/until-each
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent e64ea31 commit f335956

97 files changed

Lines changed: 19613 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# untilEach
22+
23+
> Invoke a callback function for each element in an ndarray until a predicate function returns a truthy value.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var untilEach = require( '@stdlib/ndarray/base/until-each' );
37+
```
38+
39+
#### untilEach( arrays, predicate, fcn\[, thisArg] )
40+
41+
Invokes a callback function for each element in an ndarray until a predicate function returns a truthy value.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var naryFunction = require( '@stdlib/utils/nary-function' );
46+
var log = require( '@stdlib/console/log' );
47+
48+
function predicate( value ) {
49+
return ( value !== value );
50+
}
51+
52+
// Create data buffers:
53+
var xbuf = new Float64Array( 12 );
54+
55+
// Define the shape of the array:
56+
var shape = [ 3, 1, 2 ];
57+
58+
// Define the array strides:
59+
var sx = [ 4, 4, 1 ];
60+
61+
// Define the index offset:
62+
var ox = 1;
63+
64+
// Create an ndarray-like object:
65+
var x = {
66+
'dtype': 'float64',
67+
'data': xbuf,
68+
'shape': shape,
69+
'strides': sx,
70+
'offset': ox,
71+
'order': 'row-major'
72+
};
73+
74+
// Apply the callback function:
75+
untilEach( [ x ], predicate, naryFunction( log, 1 ) );
76+
```
77+
78+
The function accepts the following arguments:
79+
80+
- **arrays**: array-like object containing an input ndarray.
81+
- **predicate**: predicate function.
82+
- **fcn**: callback to apply.
83+
- **thisArg**: callback execution context.
84+
85+
The predicate and callback functions are both provided the following arguments:
86+
87+
- **value**: current array element.
88+
- **indices**: current array element indices.
89+
- **arr**: the input ndarray.
90+
91+
</section>
92+
93+
<!-- /.usage -->
94+
95+
<section class="notes">
96+
97+
## Notes
98+
99+
- The provided ndarray should be an object with the following properties:
100+
101+
- **dtype**: data type.
102+
- **data**: data buffer.
103+
- **shape**: dimensions.
104+
- **strides**: stride lengths.
105+
- **offset**: index offset.
106+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
107+
108+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before applying a callback function in order to achieve better performance.
109+
110+
</section>
111+
112+
<!-- /.notes -->
113+
114+
<section class="examples">
115+
116+
## Examples
117+
118+
<!-- eslint no-undef: "error" -->
119+
120+
```javascript
121+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
122+
var zeroTo = require( '@stdlib/array/base/zero-to' );
123+
var naryFunction = require( '@stdlib/utils/nary-function' );
124+
var log = require( '@stdlib/console/log' );
125+
var untilEach = require( '@stdlib/ndarray/base/until-each' );
126+
127+
function predicate( value ) {
128+
return ( value > 5 );
129+
}
130+
131+
var x = {
132+
'dtype': 'generic',
133+
'data': zeroTo( 10 ),
134+
'shape': [ 5, 2 ],
135+
'strides': [ -2, 1 ],
136+
'offset': 8,
137+
'order': 'row-major'
138+
};
139+
140+
log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
141+
untilEach( [ x ], predicate, naryFunction( log, 2 ) );
142+
143+
x = {
144+
'dtype': 'generic',
145+
'data': zeroTo( 10 ),
146+
'shape': [ 5, 2 ],
147+
'strides': [ 1, -5 ],
148+
'offset': 5,
149+
'order': 'column-major'
150+
};
151+
152+
log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
153+
untilEach( [ x ], predicate, naryFunction( log, 2 ) );
154+
155+
x = {
156+
'dtype': 'generic',
157+
'data': zeroTo( 18 ),
158+
'shape': [ 2, 3, 3 ],
159+
'strides': [ 9, 3, 1 ],
160+
'offset': 0,
161+
'order': 'row-major'
162+
};
163+
164+
log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
165+
untilEach( [ x ], predicate, naryFunction( log, 2 ) );
166+
167+
x = {
168+
'dtype': 'generic',
169+
'data': zeroTo( 18 ),
170+
'shape': [ 2, 3, 3 ],
171+
'strides': [ -1, -2, -6 ],
172+
'offset': 17,
173+
'order': 'column-major'
174+
};
175+
176+
log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
177+
untilEach( [ x ], predicate, naryFunction( log, 2 ) );
178+
```
179+
180+
</section>
181+
182+
<!-- /.examples -->
183+
184+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
185+
186+
<section class="related">
187+
188+
</section>
189+
190+
<!-- /.related -->
191+
192+
<section class="links">
193+
194+
<!-- <related-links> -->
195+
196+
<!-- </related-links> -->
197+
198+
</section>
199+
200+
<!-- /.links -->
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var floor = require( '@stdlib/math/base/special/floor' );
27+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
28+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var untilEach = require( './../lib/10d_blocked.js' );
32+
33+
34+
// VARIABLES //
35+
36+
var types = [ 'float64' ];
37+
var order = 'column-major';
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Predicate function invoked for each element in an ndarray.
44+
*
45+
* @private
46+
* @returns {boolean} boolean indicating whether to stop iterating
47+
*/
48+
function predicate() {
49+
return false;
50+
}
51+
52+
/**
53+
* Callback invoked for each element in an ndarray.
54+
*
55+
* @private
56+
* @param {number} value - array element
57+
* @throws {Error} unexpected error
58+
*/
59+
function fcn( value ) {
60+
if ( isnan( value ) ) {
61+
throw new Error( 'unexpected error' );
62+
}
63+
}
64+
65+
/**
66+
* Creates a benchmark function.
67+
*
68+
* @private
69+
* @param {PositiveInteger} len - ndarray length
70+
* @param {NonNegativeIntegerArray} shape - ndarray shape
71+
* @param {string} xtype - input ndarray data type
72+
* @returns {Function} benchmark function
73+
*/
74+
function createBenchmark( len, shape, xtype ) {
75+
var x;
76+
77+
x = discreteUniform( len, -100, 100 );
78+
x = {
79+
'dtype': xtype,
80+
'data': x,
81+
'shape': shape,
82+
'strides': shape2strides( shape, order ),
83+
'offset': 0,
84+
'order': order
85+
};
86+
return benchmark;
87+
88+
/**
89+
* Benchmark function.
90+
*
91+
* @private
92+
* @param {Benchmark} b - benchmark instance
93+
*/
94+
function benchmark( b ) {
95+
var i;
96+
97+
b.tic();
98+
for ( i = 0; i < b.iterations; i++ ) {
99+
untilEach( x, predicate, fcn );
100+
if ( isnan( x.data[ i%len ] ) ) {
101+
b.fail( 'should not return NaN' );
102+
}
103+
}
104+
b.toc();
105+
if ( isnan( x.data[ i%len ] ) ) {
106+
b.fail( 'should not return NaN' );
107+
}
108+
b.pass( 'benchmark finished' );
109+
b.end();
110+
}
111+
}
112+
113+
114+
// MAIN //
115+
116+
/**
117+
* Main execution sequence.
118+
*
119+
* @private
120+
*/
121+
function main() {
122+
var len;
123+
var min;
124+
var max;
125+
var sh;
126+
var t1;
127+
var f;
128+
var i;
129+
var j;
130+
131+
min = 1; // 10^min
132+
max = 6; // 10^max
133+
134+
for ( j = 0; j < types.length; j++ ) {
135+
t1 = types[ j ];
136+
for ( i = min; i <= max; i++ ) {
137+
len = pow( 10, i );
138+
139+
sh = [ len/2, 2, 1, 1, 1, 1, 1, 1, 1, 1 ];
140+
f = createBenchmark( len, sh, t1 );
141+
bench( format( '%s::blocked:ndims=%d,len=%d,shape=[%s],xorder=%s,xtype=%s', pkg, sh.length, len, sh.join(','), order, t1 ), f );
142+
143+
sh = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, len/2 ];
144+
f = createBenchmark( len, sh, t1 );
145+
bench( format( '%s::blocked:ndims=%d,len=%d,shape=[%s],xorder=%s,xtype=%s', pkg, sh.length, len, sh.join(','), order, t1 ), f );
146+
147+
len = floor( pow( len, 1.0/10.0 ) );
148+
sh = [ len, len, len, len, len, len, len, len, len, len ];
149+
len *= pow( len, 9 );
150+
f = createBenchmark( len, sh, t1 );
151+
bench( format( '%s::blocked:ndims=%d,len=%d,shape=[%s],xorder=%s,xtype=%s', pkg, sh.length, len, sh.join(','), order, t1 ), f );
152+
}
153+
}
154+
}
155+
156+
main();

0 commit comments

Comments
 (0)