Skip to content

Commit 3f08fb8

Browse files
committed
refactor: implementation
--- 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: passed - 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: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 55d5bf4 commit 3f08fb8

2 files changed

Lines changed: 106 additions & 61 deletions

File tree

lib/node_modules/@stdlib/ndarray/to-unflattened/lib/main.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@
2020

2121
// MODULES //
2222

23-
var unflatten = require( '@stdlib/ndarray/unflatten' );
23+
var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives;
24+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
25+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
27+
var getShape = require( '@stdlib/ndarray/shape' );
28+
var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' );
29+
var unflatten = require( '@stdlib/ndarray/base/unflatten' );
2430
var emptyLike = require( '@stdlib/ndarray/empty-like' );
2531
var assign = require( '@stdlib/ndarray/base/assign' );
32+
var format = require( '@stdlib/string/format' );
2633

2734

2835
// MAIN //
@@ -51,17 +58,28 @@ var assign = require( '@stdlib/ndarray/base/assign' );
5158
*/
5259
function toUnflattened( x, dim, sizes ) {
5360
var out;
54-
var xv;
55-
56-
// Create an unflattened view:
57-
xv = unflatten( x, dim, sizes );
58-
59-
// Create an output ndarray with the unflattened shape:
60-
out = emptyLike( xv );
61-
62-
// Assign the elements of the unflattened view to the output ndarray:
63-
assign( [ xv, out ] );
61+
var bv;
62+
var sh;
63+
var d;
6464

65+
if ( !isndarrayLike( x ) ) {
66+
throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) );
67+
}
68+
if ( !isInteger( dim ) ) {
69+
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', dim ) );
70+
}
71+
if ( !isNonNegativeIntegerArray( sizes ) ) {
72+
throw new TypeError( format( 'invalid argument. Third argument must be an array of nonnegative integers. Value: `%s`.', sizes ) );
73+
}
74+
sh = getShape( x );
75+
d = normalizeIndex( dim, sh.length - 1 );
76+
if ( d === -1 ) {
77+
throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', sh.length, dim ) );
78+
}
79+
bv = ndarraylike2ndarray( x );
80+
bv = unflatten( bv, d, sizes, false );
81+
out = emptyLike( bv );
82+
assign( [ bv, out ] );
6583
return out;
6684
}
6785

lib/node_modules/@stdlib/ndarray/to-unflattened/test/test.js

Lines changed: 77 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24-
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
2524
var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' );
26-
var zeroTo = require( '@stdlib/array/zero-to' );
27-
var ndarray = require( '@stdlib/ndarray/ctor' );
28-
var array = require( '@stdlib/ndarray/array' );
25+
var zeroTo = require( '@stdlib/blas/ext/zero-to' );
2926
var getShape = require( '@stdlib/ndarray/shape' );
3027
var getDType = require( '@stdlib/ndarray/dtype' );
3128
var getData = require( '@stdlib/ndarray/data-buffer' );
@@ -72,10 +69,14 @@ tape( 'the function throws an error if provided a first argument which is not an
7269

7370
tape( 'the function throws an error if provided a second argument which is not an integer', function test( t ) {
7471
var values;
72+
var opts;
7573
var x;
7674
var i;
7775

78-
x = new ndarray( 'float64', [ 1, 2, 3, 4, 5, 6 ], [ 6 ], [ 1 ], 0, 'row-major' );
76+
opts = {
77+
'dtype': 'float64'
78+
};
79+
x = zeroTo( [ 6 ], opts );
7980

8081
values = [
8182
'5',
@@ -104,10 +105,14 @@ tape( 'the function throws an error if provided a second argument which is not a
104105

105106
tape( 'the function throws an error if provided a third argument which is not an array of nonnegative integers', function test( t ) {
106107
var values;
108+
var opts;
107109
var x;
108110
var i;
109111

110-
x = new ndarray( 'float64', [ 1, 2, 3, 4, 5, 6 ], [ 6 ], [ 1 ], 0, 'row-major' );
112+
opts = {
113+
'dtype': 'float64'
114+
};
115+
x = zeroTo( [ 6 ], opts );
111116

112117
values = [
113118
'5',
@@ -137,41 +142,70 @@ tape( 'the function throws an error if provided a third argument which is not an
137142
});
138143

139144
tape( 'the function throws an error if provided an invalid dimension index', function test( t ) {
145+
var values;
146+
var opts;
147+
var i;
140148
var x;
141149

142-
x = new ndarray( 'float64', [ 1, 2, 3, 4, 5, 6 ], [ 6 ], [ 1 ], 0, 'row-major' );
143-
144-
t.throws( function badValue() {
145-
toUnflattened( x, 5, [ 2, 3 ] );
146-
}, RangeError, 'throws an error when dimension index is out-of-bounds (positive)' );
150+
opts = {
151+
'dtype': 'float64'
152+
};
153+
x = zeroTo( [ 6 ], opts );
147154

148-
t.throws( function badValue() {
149-
toUnflattened( x, -3, [ 2, 3 ] );
150-
}, RangeError, 'throws an error when dimension index is out-of-bounds (negative)' );
155+
values = [
156+
5,
157+
-3
158+
];
151159

160+
for ( i = 0; i < values.length; i++ ) {
161+
t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] );
162+
}
152163
t.end();
164+
165+
function badValue( dim ) {
166+
return function test() {
167+
toUnflattened( x, dim, [ 2, 3 ] );
168+
};
169+
}
153170
});
154171

155172
tape( 'the function throws an error if the product of `sizes` does not match the dimension size', function test( t ) {
173+
var values;
174+
var opts;
175+
var i;
156176
var x;
157177

158-
x = new ndarray( 'float64', [ 1, 2, 3, 4, 5, 6 ], [ 6 ], [ 1 ], 0, 'row-major' );
178+
opts = {
179+
'dtype': 'float64'
180+
};
181+
x = zeroTo( [ 6 ], opts );
159182

160-
t.throws( function badValue() {
161-
toUnflattened( x, 0, [ 2, 4 ] );
162-
}, RangeError, 'throws an error when product of sizes does not match dimension size' );
183+
values = [
184+
[ 2, 4 ]
185+
];
163186

187+
for ( i = 0; i < values.length; i++ ) {
188+
t.throws( badValue( values[ i ] ), RangeError, 'throws an error when product of sizes does not match dimension size' );
189+
}
164190
t.end();
191+
192+
function badValue( sizes ) {
193+
return function test() {
194+
toUnflattened( x, 0, sizes );
195+
};
196+
}
165197
});
166198

167199
tape( 'the function returns a new ndarray with the unflattened dimension (1D input)', function test( t ) {
168200
var expected;
169-
var buf;
201+
var opts;
170202
var x;
171203
var y;
172204

173-
buf = zeroTo( 6 );
174-
x = ndarray( 'generic', buf, [ 6 ], [ 1 ], 0, 'row-major' );
205+
opts = {
206+
'dtype': 'generic'
207+
};
208+
x = zeroTo( [ 6 ], opts );
175209

176210
expected = [
177211
[ 0, 1, 2 ],
@@ -189,12 +223,14 @@ tape( 'the function returns a new ndarray with the unflattened dimension (1D inp
189223

190224
tape( 'the function returns a new ndarray with the unflattened dimension (1D input, unflattened to 3D)', function test( t ) {
191225
var expected;
192-
var buf;
226+
var opts;
193227
var x;
194228
var y;
195229

196-
buf = zeroTo( 12 );
197-
x = ndarray( 'generic', buf, [ 12 ], [ 1 ], 0, 'row-major' );
230+
opts = {
231+
'dtype': 'generic'
232+
};
233+
x = zeroTo( [ 12 ], opts );
198234

199235
expected = [
200236
[ [ 0, 1, 2 ], [ 3, 4, 5 ] ],
@@ -212,12 +248,14 @@ tape( 'the function returns a new ndarray with the unflattened dimension (1D inp
212248

213249
tape( 'the function supports negative dimension indices', function test( t ) {
214250
var expected;
215-
var buf;
251+
var opts;
216252
var x;
217253
var y;
218254

219-
buf = zeroTo( 6 );
220-
x = ndarray( 'generic', buf, [ 6 ], [ 1 ], 0, 'row-major' );
255+
opts = {
256+
'dtype': 'generic'
257+
};
258+
x = zeroTo( [ 6 ], opts );
221259

222260
expected = [
223261
[ 0, 1, 2 ],
@@ -233,31 +271,17 @@ tape( 'the function supports negative dimension indices', function test( t ) {
233271
t.end();
234272
});
235273

236-
tape( 'the function returns a new ndarray that is ndarray-like', function test( t ) {
237-
var x;
238-
var y;
239-
240-
x = array( zeroTo( 6 ), {
241-
'shape': [ 6 ],
242-
'dtype': 'float64'
243-
});
244-
245-
y = toUnflattened( x, 0, [ 2, 3 ] );
246-
t.strictEqual( isndarrayLike( y ), true, 'returns ndarray-like object' );
247-
t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected dtype' );
248-
t.notEqual( getData( y ), getData( x ), 'does not share the same data buffer' );
249-
250-
t.end();
251-
});
252-
253274
tape( 'the function supports column-major order', function test( t ) {
254275
var expected;
255-
var buf;
276+
var opts;
256277
var x;
257278
var y;
258279

259-
buf = zeroTo( 6 );
260-
x = ndarray( 'generic', buf, [ 6 ], [ 1 ], 0, 'column-major' );
280+
opts = {
281+
'dtype': 'generic',
282+
'order': 'column-major'
283+
};
284+
x = zeroTo( [ 6 ], opts );
261285

262286
expected = [
263287
[ 0, 2, 4 ],
@@ -275,12 +299,15 @@ tape( 'the function supports column-major order', function test( t ) {
275299

276300
tape( 'the function unflattens a middle dimension in a multidimensional input', function test( t ) {
277301
var expected;
278-
var buf;
302+
var opts;
279303
var x;
280304
var y;
281305

282-
buf = zeroTo( 12 );
283-
x = ndarray( 'generic', buf, [ 3, 4 ], [ 4, 1 ], 0, 'row-major' );
306+
opts = {
307+
'dtype': 'generic',
308+
'dims': [ 0, 1 ]
309+
};
310+
x = zeroTo( [ 3, 4 ], opts );
284311

285312
expected = [
286313
[ [ 0, 1 ], [ 2, 3 ] ],

0 commit comments

Comments
 (0)