Skip to content

Commit 8bb576b

Browse files
committed
feat: add 10d kernels
--- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 1392a91 commit 8bb576b

File tree

6 files changed

+1578
-0
lines changed

6 files changed

+1578
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/* eslint-disable max-depth */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Tests whether an ndarray contains a specified value.
32+
*
33+
* @private
34+
* @param {Object} x - object containing ndarray meta data
35+
* @param {string} x.dtype - data type
36+
* @param {Collection} x.data - data buffer
37+
* @param {NonNegativeIntegerArray} x.shape - dimensions
38+
* @param {IntegerArray} x.strides - stride lengths
39+
* @param {NonNegativeInteger} x.offset - index offset
40+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
41+
* @param {*} value - search element
42+
* @returns {boolean} result
43+
*
44+
* @example
45+
* var Float64Array = require( '@stdlib/array/float64' );
46+
*
47+
* // Create a data buffer:
48+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
49+
*
50+
* // Define the shape of the input array:
51+
* var shape = [ 1, 1, 1, 1, 1, 1, 1, 3, 1, 2 ];
52+
*
53+
* // Define the array strides:
54+
* var sx = [ 12, 12, 12, 12, 12, 12, 12, 4, 4, 1 ];
55+
*
56+
* // Define the index offset:
57+
* var ox = 0;
58+
*
59+
* // Create the input ndarray-like object:
60+
* var x = {
61+
* 'dtype': 'float64',
62+
* 'data': xbuf,
63+
* 'shape': shape,
64+
* 'strides': sx,
65+
* 'offset': ox,
66+
* 'order': 'row-major'
67+
* };
68+
*
69+
* // Perform reduction:
70+
* var out = includes10d( x, 6.0 );
71+
* // returns true
72+
*
73+
* out = includes10d( x, 100.0 );
74+
* // returns false
75+
*/
76+
function includes10d( x, value ) { // eslint-disable-line max-statements
77+
var xbuf;
78+
var dx0;
79+
var dx1;
80+
var dx2;
81+
var dx3;
82+
var dx4;
83+
var dx5;
84+
var dx6;
85+
var dx7;
86+
var dx8;
87+
var dx9;
88+
var sh;
89+
var S0;
90+
var S1;
91+
var S2;
92+
var S3;
93+
var S4;
94+
var S5;
95+
var S6;
96+
var S7;
97+
var S8;
98+
var S9;
99+
var sx;
100+
var ix;
101+
var i0;
102+
var i1;
103+
var i2;
104+
var i3;
105+
var i4;
106+
var i5;
107+
var i6;
108+
var i7;
109+
var i8;
110+
var i9;
111+
112+
// Note on variable naming convention: S#, dx#, dy#, i# where # corresponds to the loop number, with `0` being the innermost loop...
113+
114+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
115+
sh = x.shape;
116+
sx = x.strides;
117+
if ( isRowMajor( x.order ) ) {
118+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
119+
S0 = sh[ 9 ];
120+
S1 = sh[ 8 ];
121+
S2 = sh[ 7 ];
122+
S3 = sh[ 6 ];
123+
S4 = sh[ 5 ];
124+
S5 = sh[ 4 ];
125+
S6 = sh[ 3 ];
126+
S7 = sh[ 2 ];
127+
S8 = sh[ 1 ];
128+
S9 = sh[ 0 ];
129+
dx0 = sx[ 9 ]; // offset increment for innermost loop
130+
dx1 = sx[ 8 ] - ( S0*sx[9] );
131+
dx2 = sx[ 7 ] - ( S1*sx[8] );
132+
dx3 = sx[ 6 ] - ( S2*sx[7] );
133+
dx4 = sx[ 5 ] - ( S3*sx[6] );
134+
dx5 = sx[ 4 ] - ( S4*sx[5] );
135+
dx6 = sx[ 3 ] - ( S5*sx[4] );
136+
dx7 = sx[ 2 ] - ( S6*sx[3] );
137+
dx8 = sx[ 1 ] - ( S7*sx[2] );
138+
dx9 = sx[ 0 ] - ( S8*sx[1] ); // offset increment for outermost loop
139+
} else { // order === 'column-major'
140+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
141+
S0 = sh[ 0 ];
142+
S1 = sh[ 1 ];
143+
S2 = sh[ 2 ];
144+
S3 = sh[ 3 ];
145+
S4 = sh[ 4 ];
146+
S5 = sh[ 5 ];
147+
S6 = sh[ 6 ];
148+
S7 = sh[ 7 ];
149+
S8 = sh[ 8 ];
150+
S9 = sh[ 9 ];
151+
dx0 = sx[ 0 ]; // offset increment for innermost loop
152+
dx1 = sx[ 1 ] - ( S0*sx[0] );
153+
dx2 = sx[ 2 ] - ( S1*sx[1] );
154+
dx3 = sx[ 3 ] - ( S2*sx[2] );
155+
dx4 = sx[ 4 ] - ( S3*sx[3] );
156+
dx5 = sx[ 5 ] - ( S4*sx[4] );
157+
dx6 = sx[ 6 ] - ( S5*sx[5] );
158+
dx7 = sx[ 7 ] - ( S6*sx[6] );
159+
dx8 = sx[ 8 ] - ( S7*sx[7] );
160+
dx9 = sx[ 9 ] - ( S8*sx[8] ); // offset increment for outermost loop
161+
}
162+
// Set a pointer to the first indexed element:
163+
ix = x.offset;
164+
165+
// Cache a reference to the input ndarray buffer:
166+
xbuf = x.data;
167+
168+
// Iterate over the ndarray dimensions...
169+
for ( i9 = 0; i9 < S9; i9++ ) {
170+
for ( i8 = 0; i8 < S8; i8++ ) {
171+
for ( i7 = 0; i7 < S7; i7++ ) {
172+
for ( i6 = 0; i6 < S6; i6++ ) {
173+
for ( i5 = 0; i5 < S5; i5++ ) {
174+
for ( i4 = 0; i4 < S4; i4++ ) {
175+
for ( i3 = 0; i3 < S3; i3++ ) {
176+
for ( i2 = 0; i2 < S2; i2++ ) {
177+
for ( i1 = 0; i1 < S1; i1++ ) {
178+
for ( i0 = 0; i0 < S0; i0++ ) {
179+
if ( xbuf[ ix ] === value ) {
180+
return true;
181+
}
182+
ix += dx0;
183+
}
184+
ix += dx1;
185+
}
186+
ix += dx2;
187+
}
188+
ix += dx3;
189+
}
190+
ix += dx4;
191+
}
192+
ix += dx5;
193+
}
194+
ix += dx6;
195+
}
196+
ix += dx7;
197+
}
198+
ix += dx8;
199+
}
200+
ix += dx9;
201+
}
202+
return false;
203+
}
204+
205+
206+
// EXPORTS //
207+
208+
module.exports = includes10d;

0 commit comments

Comments
 (0)