Skip to content

Commit 939797c

Browse files
committed
refactor: add implementation and examples
1 parent cec422b commit 939797c

File tree

15 files changed

+842
-12
lines changed

15 files changed

+842
-12
lines changed

lib/node_modules/@stdlib/ndarray/nested/examples/0d.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
'use strict';
2222

23-
var S = require( '@stdlib/slice/ctor' );
23+
var E = require( '@stdlib/slice/multi' );
2424
var NestedArray = require( './../lib' );
2525

2626
var buffer = [ 6 ];
@@ -47,16 +47,16 @@ console.log( 'x[] = %d', x.get() );
4747
// => 'x[] = 10'
4848

4949
// Use an empty slice to create a separate array view:
50-
var y1 = x[ S() ];
50+
var y1 = x[ E() ];
5151
console.log( y1.get() );
52-
// throws <TypeError>
52+
// => 10
5353

5454
// Use alternative syntax:
5555
var y2 = x[ [] ];
5656
console.log( y2.get() );
57-
// throws <TypeError>
57+
// => 10
5858

5959
// Use alternative syntax:
6060
var y3 = x[ '' ];
6161
console.log( y3.get() );
62-
// throws <TypeError>
62+
// => 10

lib/node_modules/@stdlib/ndarray/nested/examples/1d.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,15 @@ console.log( 'x[2] = %d', x.get( 2 ) );
5151
var _ = void 0;
5252

5353
// Use a slice to create a view on the original ndarray:
54-
var y1 = x[ [ S(0,_,2) ] ];
54+
var y1 = x[ S(0,_,2) ];
5555
console.log( toArray( y1 ) );
56-
// throws <TypeError>
56+
// => [ 1, 10, 5 ]
5757

5858
// Use alternative syntax:
5959
var y2 = x[ '::-2' ];
6060
console.log( toArray( y2 ) );
61-
// throws <TypeError>
61+
// => [ 6, 4, 2 ]
62+
63+
var y3 = x[ 3 ];
64+
console.log( y3.get() );
65+
// => 4

lib/node_modules/@stdlib/ndarray/nested/examples/3d.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ var _ = void 0;
6767
// Use slices to create a view on the original ndarray:
6868
var y1 = x[ 1 ][ S(0,_,2) ];
6969
console.log( toArray( y1 ) );
70-
// => [ [ [ 9, 10 ], [ 13, 100 ] ] ]
70+
// => [ [ 9, 10 ], [ 13, 100 ] ]
7171

7272
// Use alternative syntax:
7373
var y2 = x[ 1 ][ '0::2' ];
7474
console.log( toArray( y2 ) );
75-
// => [ [ [ 9, 10 ], [ 13, 100 ] ] ]
75+
// => [ [ 9, 10 ], [ 13, 100 ] ]

lib/node_modules/@stdlib/ndarray/nested/examples/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ console.log( toArray( y2 ) );
7070
var y3 = x[ S(_,_,-2) ];
7171
console.log( toArray( y3 ) );
7272
// => [ [ 7, 20 ], [ 3, 4 ] ]
73+
74+
// Get the second row:
75+
var y4 = x[ 1 ];
76+
console.log( toArray( y4 ) );
77+
// => [ 5, 6 ]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
// MAIN //
22+
23+
/**
24+
* Returns array creation options.
25+
*
26+
* @private
27+
* @returns {Object} options
28+
*/
29+
function options() {
30+
return {
31+
// Default to always returning read-only arrays:
32+
'readonly': true
33+
};
34+
}
35+
36+
37+
// EXPORTS //
38+
39+
module.exports = options;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 buffer = require( '@stdlib/ndarray/base/buffer' );
24+
var zeros = require( '@stdlib/array/base/zeros' );
25+
var options = require( './array_options.js' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Returns an empty n-dimensional ndarray.
32+
*
33+
* @private
34+
* @param {Function} ctor - ndarray constructor
35+
* @param {string} dtype - array data type
36+
* @param {NonNegativeIntegerArray} shape - array shape
37+
* @param {string} order - layout order
38+
* @returns {ndarray} empty ndarray
39+
*/
40+
function empty( ctor, dtype, shape, order ) {
41+
var strides;
42+
var ndims;
43+
44+
ndims = shape.length;
45+
if ( ndims === 0 ) {
46+
strides = [ 0 ];
47+
} else {
48+
strides = zeros( ndims );
49+
}
50+
return new ctor( dtype, buffer( dtype, 0 ), shape, strides, 0, order, options() ); // eslint-disable-line max-len
51+
}
52+
53+
54+
// EXPORTS //
55+
56+
module.exports = empty;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 isFunction = require( '@stdlib/assert/is-function' );
24+
var trim = require( '@stdlib/string/base/trim' );
25+
var str2multislice = require( '@stdlib/slice/base/str2multislice' );
26+
var format = require( '@stdlib/string/format' );
27+
var hasProperty = require( './has_property.js' );
28+
var options = require( './array_options.js' );
29+
var RE_INTEGER = require( './re_integer.js' );
30+
31+
32+
// MAIN //
33+
34+
/**
35+
* Trap for retrieving property values.
36+
*
37+
* @private
38+
* @param {Object} target - target object
39+
* @param {(string|symbol)} property - property name
40+
* @param {Object} receiver - the proxy object or an object inheriting from the proxy
41+
* @throws {Error} invalid slice operation
42+
* @throws {RangeError} number of slice dimensions must match the number of array dimensions
43+
* @returns {*} result
44+
*/
45+
function get( target, property, receiver ) {
46+
var dtype;
47+
var value;
48+
var prop;
49+
var ch;
50+
var sh;
51+
var s;
52+
if ( hasProperty( property ) ) {
53+
value = target[ property ];
54+
if ( isFunction( value ) ) {
55+
return wrapper;
56+
}
57+
return value;
58+
}
59+
prop = trim( property );
60+
61+
// Resolve target meta data:
62+
dtype = target.dtype;
63+
sh = target.shape;
64+
65+
// Retrieve the first character in order to to detect how a slice operation was specified:
66+
ch = prop[ 0 ];
67+
68+
// Case: multi-slice
69+
if ( ch === 'M' ) {
70+
// Convert the string to a slice object:
71+
s = str2multislice( prop );
72+
if ( s === null ) {
73+
throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) );
74+
}
75+
// TODO: => @stdlib/ndarray/base/slice: return slice( receiver, s.data );
76+
77+
// Ensure that we were provided an empty multi-slice:
78+
if ( s.ndims !== sh.length ) {
79+
throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', sh.join( ',' ), s.ndims ) );
80+
}
81+
}
82+
// Case: non-empty string
83+
else if ( prop.length !== 0 ) {
84+
// TODO: the following can be generalized by going ahead and parsing the slice string or integer and passing to a functional API which then verifies that s.ndims !== sh.length. We need only retain the error raised for an invalid operation.
85+
86+
// Case: slice or an integer
87+
if ( ch === 'S' || RE_INTEGER.test( prop ) ) {
88+
throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', sh.join( ',' ), 1 ) );
89+
}
90+
throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) );
91+
}
92+
// TODO: => @stdlib/ndarray/base/slice: return slice( receiver, [] );
93+
94+
// Return an zero-dimensional array view:
95+
return new receiver.constructor( dtype, target.data, sh, target.strides, target.offset, target.order, options() ); // eslint-disable-line max-len
96+
97+
/**
98+
* Method wrapper.
99+
*
100+
* @private
101+
* @returns {*} results
102+
*/
103+
function wrapper() {
104+
var args;
105+
var i;
106+
107+
args = [];
108+
for ( i = 0; i < arguments.length; i++ ) {
109+
args.push( arguments[ i ] );
110+
}
111+
return value.apply( ( this === receiver ) ? target : this, args ); // eslint-disable-line no-invalid-this
112+
}
113+
}
114+
115+
116+
// EXPORTS //
117+
118+
module.exports = get;

0 commit comments

Comments
 (0)