Skip to content

Commit 259b682

Browse files
committed
feat: add support for specifying an array index cache and add support for linear indexing
--- 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: na - task: lint_repl_help status: passed - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: passed - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: passed - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: passed - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed ---
1 parent 66ea937 commit 259b682

40 files changed

+1755
-249
lines changed

lib/node_modules/@stdlib/ndarray/to-fancy/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ The function supports the following options:
9292

9393
- **data**: the underlying index ndarray.
9494
- **type**: the index type. Must be either `'mask'`, `'bool'`, or `'int'`.
95+
- **kind**: the index kind. Must be either `''`, `'cartesian'`, or `'linear'`.
9596
- **dtype**: the [data type][@stdlib/ndarray/dtypes] of the underlying ndarray.
9697

9798
If an ndarray index is not associated with a provided identifier, the `get` method should return `null`.
@@ -132,6 +133,7 @@ The function supports the following options:
132133

133134
- **data**: the underlying index ndarray.
134135
- **type**: the index type. Must be either `'mask'`, `'bool'`, or `'int'`.
136+
- **kind**: the index kind. Must be either `''`, `'cartesian'`, or `'linear'`.
135137
- **dtype**: the [data type][@stdlib/ndarray/dtypes] of the underlying ndarray.
136138

137139
If an ndarray index is not associated with a provided identifier, the `get` method should return `null`.
@@ -195,6 +197,10 @@ For documentation and usage, see [`ndindex`][@stdlib/ndarray/index].
195197

196198
// TODO: see array/to-fancy
197199

200+
### Linear Indexing
201+
202+
// TODO: only applies to non-zero-dimensional ndarrays. In non-strict mode, out-of-bounds indices return `undefined` and fail to assign.
203+
198204
### Broadcasting
199205

200206
// TODO: see array/to-fancy

lib/node_modules/@stdlib/ndarray/to-fancy/docs/repl.txt

+16-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
numeric literals and strings), Slice and MultiSlice instances, subsequence
1616
expressions, mask ndarrays, boolean ndarrays, and integer ndarrays.
1717

18-
A fancy array supports all properties and methods of the input array, and,
19-
thus, a fancy array can be consumed by any API which supports array-like
20-
objects.
18+
A fancy ndarray supports all properties and methods of the input ndarray,
19+
and, thus, a fancy ndarray can be consumed by any API which supports
20+
ndarray-like objects.
2121

2222
For operations returning a new ndarray (e.g., when slicing or invoking an
2323
instance method), a fancy ndarray returns a new fancy ndarray having the
@@ -72,6 +72,7 @@
7272

7373
- data: the underlying index ndarray.
7474
- type: the index type. Must be either 'mask', 'bool', or 'int'.
75+
- kind: the index kind. Must be either '', 'cartesian', or 'linear'.
7576
- dtype: the data type of the underlying ndarray.
7677

7778
If an ndarray index is not associated with a provided identifier, the
@@ -137,6 +138,7 @@
137138

138139
- data: the underlying index ndarray.
139140
- type: the index type. Must be either 'mask', 'bool', or 'int'.
141+
- kind: the index kind. Must be either '', 'cartesian', or 'linear'.
140142
- dtype: the data type of the underlying ndarray.
141143

142144
If an ndarray index is not associated with a provided identifier, the
@@ -183,6 +185,17 @@
183185
Boolean indicating whether to continue persisting an index object after
184186
first usage. Default: false.
185187

188+
options.kind: string (optional)
189+
Specifies whether a provided ndarray is a specialized input ndarray
190+
"kind". This option is only applicable for integer input ndarrays. Must
191+
be one of the following:
192+
193+
- cartesian: a provided input ndarray contains Cartesian indices.
194+
- linear: a provided input ndarray contains indices representing
195+
locations in linear memory.
196+
197+
Default: ''.
198+
186199
Returns
187200
-------
188201
out: ndarrayIndex

lib/node_modules/@stdlib/ndarray/to-fancy/lib/ctor.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @param {Function} ndarray2fancy - function for creating a proxied ndarray
2828
* @param {Object} opts - options
2929
* @param {boolean} opts.strict - boolean indicating whether to perform strict bounds checking
30+
* @param {Function} opts.cache - cache for resolving ndarray index objects
3031
* @returns {Function} handler
3132
*/
3233
function factory( ndarray2fancy, opts ) {

lib/node_modules/@stdlib/ndarray/to-fancy/lib/defaults.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var ndindex = require( '@stdlib/ndarray/index' );
24+
25+
2126
// MAIN //
2227

2328
/**
@@ -32,7 +37,7 @@
3237
*/
3338
function defaults() {
3439
return {
35-
'cache': null, // FIXME: default index cache
40+
'cache': ndindex,
3641
'strict': false
3742
};
3843
}

lib/node_modules/@stdlib/ndarray/to-fancy/lib/error_message.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var replace = require( '@stdlib/string/base/replace' );
3333
* @returns {string} updated message
3434
*/
3535
function errMessage( msg ) {
36-
return replace( msg, /^invalid argument/, 'invalid operation' );
36+
return replace( msg, /^invalid arguments?/, 'invalid operation' );
3737
}
3838

3939

lib/node_modules/@stdlib/ndarray/to-fancy/lib/factory.js

+6-24
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,14 @@ var getArrayWrapper = require( './get_ndarray_wrapper.js' );
3232
var defaults = require( './defaults.js' );
3333
var validate = require( './validate.js' );
3434
var validator = require( './validator.js' );
35-
var prop2slice0d = require( './prop2slice.0d.js' );
36-
var prop2slice1d = require( './prop2slice.1d.js' );
37-
var prop2slicend = require( './prop2slice.nd.js' );
35+
var prop2slice = require( './prop2slice.js' );
3836
var ctor = require( './ctor.js' );
37+
var getter = require( './getter.js' );
38+
var setter = require( './setter.js' );
3939
var get = require( './get.js' );
4040
var set = require( './set.js' );
4141

4242

43-
// FUNCTIONS //
44-
45-
/**
46-
* Returns a function for converting a property string to a slice according to a specified dimensionality.
47-
*
48-
* @private
49-
* @param {NonNegativeInteger} ndims - number of dimensions
50-
* @returns {Function} function for converting a property string to a slice
51-
*/
52-
function prop2slice( ndims ) {
53-
if ( ndims === 0 ) {
54-
return prop2slice0d;
55-
}
56-
if ( ndims === 1 ) {
57-
return prop2slice1d;
58-
}
59-
return prop2slicend;
60-
}
61-
62-
6343
// MAIN //
6444

6545
/**
@@ -140,6 +120,8 @@ function factory() {
140120
o = {
141121
'ref': x,
142122
'dtype': dt,
123+
'getter': getter( x ),
124+
'setter': setter( x ),
143125
'preSetElement': setElementWrapper( dt ),
144126
'postGetArray': getArrayWrapper( ndarray2fancy, opts ),
145127
'cache': opts.cache,
@@ -149,7 +131,7 @@ function factory() {
149131
'ctor': new Proxy( x.constructor || ndarray, {
150132
'construct': ctor( ndarray2fancy, opts )
151133
}),
152-
'prop2slice': prop2slice( arr.shape.length )
134+
'prop2slice': prop2slice( arr.shape.length ) // WARNING: we assume a fixed number of dimensions!
153135
};
154136
return new Proxy( x, {
155137
'get': get( o ),

lib/node_modules/@stdlib/ndarray/to-fancy/lib/get.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020

2121
// MODULES //
2222

23-
var hasProperty = require( './has_property.js' );
23+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
24+
var hasProperty = require( '@stdlib/assert/has-property' );
25+
var isIntegerString = require( './is_integer_string.js' );
26+
var isArrayIndexString = require( './is_ndindex_string.js' );
27+
var isCartesianIndexString = require( './is_cartesian_index_string.js' );
28+
var getElement = require( './get_element.js' );
29+
var getCartesian = require( './get_cartesian.js' );
2430
var getValue = require( './get_value.js' );
2531
var getSlice = require( './get_slice.js' );
2632

@@ -54,10 +60,20 @@ function factory( ctx ) {
5460
* @returns {*} result
5561
*/
5662
function get( target, property, receiver ) {
57-
if ( hasProperty( property ) ) {
58-
return getValue( target, property, receiver );
63+
if ( isIntegerString( property ) ) {
64+
return getElement( target, property, ctx );
5965
}
60-
return getSlice( target, property, receiver, ctx.prop2slice );
66+
if ( hasProperty( target, property ) || !isString( property ) ) {
67+
return getValue( target, property, receiver, ctx );
68+
}
69+
if ( isCartesianIndexString( property ) ) {
70+
return getCartesian( target, property, ctx );
71+
}
72+
if ( isArrayIndexString( property ) ) {
73+
// FIXME
74+
return;
75+
}
76+
return getSlice( target, property, ctx );
6177
}
6278
}
6379

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 scalar2ndarrayLike = require( '@stdlib/ndarray/base/from-scalar-like' );
24+
var getShape = require( '@stdlib/ndarray/base/shape' );
25+
var resolveSubscripts = require( './resolve_subscripts.js' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Returns the element associated with a specified set of subscripts.
32+
*
33+
* @private
34+
* @param {ndarrayLike} target - target object
35+
* @param {string} property - index string
36+
* @param {Object} ctx - context object
37+
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
38+
* @param {Function} ctx.postGetArray - function to process a retrieved ndarray
39+
* @throws {RangeError} index exceeds ndarray bounds
40+
* @throws {RangeError} number of indices must match the number of ndarray dimensions
41+
* @returns {(ndarrayLike|void)} result
42+
*/
43+
function getCartesian( target, property, ctx ) {
44+
var sub;
45+
var v;
46+
47+
sub = resolveSubscripts( property, getShape( target, false ), ctx.strict );
48+
if ( sub === void 0 ) {
49+
return;
50+
}
51+
// Use the `get` method, which we expect every ndarray-like object to have, in order to resolve a single element:
52+
v = target.get.apply( target, sub );
53+
if ( v === void 0 ) {
54+
return;
55+
}
56+
// Return the element as a 0-D ndarray to ensure consistency with linear indexing and slicing:
57+
return ctx.postGetArray( scalar2ndarrayLike( target, v ) );
58+
}
59+
60+
61+
// EXPORTS //
62+
63+
module.exports = getCartesian;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 scalar2ndarrayLike = require( '@stdlib/ndarray/base/from-scalar-like' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Returns the element associated with a specified index.
30+
*
31+
* @private
32+
* @param {ndarrayLike} target - target object
33+
* @param {string} property - index string
34+
* @param {Object} ctx - context object
35+
* @param {Function} ctx.getter - accessor for retrieving array elements
36+
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
37+
* @param {Function} ctx.postGetArray - function to process a retrieved ndarray
38+
* @throws {RangeError} index exceeds ndarray bounds
39+
* @returns {(ndarrayLike|void)} result
40+
*/
41+
function getElement( target, property, ctx ) {
42+
var v = ctx.getter( target, property, ctx.strict );
43+
if ( v === void 0 ) {
44+
return;
45+
}
46+
return ctx.postGetArray( scalar2ndarrayLike( target, v ) );
47+
}
48+
49+
50+
// EXPORTS //
51+
52+
module.exports = getElement;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 take = require( '@stdlib/ndarray/take' );
24+
25+
// var mskfilter = require( '@stdlib/ndarray/mskfilter' );
26+
27+
// var mskreject = require( '@stdlib/ndarray/mskreject' );
28+
29+
var format = require( '@stdlib/string/format' );
30+
var prop2array = require( './prop2array.js' );
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* Returns the elements specified by an ndarray index.
37+
*
38+
* @private
39+
* @param {ndarrayLike} target - target object
40+
* @param {string} property - index string
41+
* @param {Object} ctx - context object
42+
* @param {Object} ctx.cache - cache for resolving ndarray index objects
43+
* @param {Function} ctx.postGetArray - function to process a retrieved ndarray
44+
* @throws {Error} invalid ndarray index
45+
* @throws {RangeError} index exceeds ndarray bounds
46+
* @returns {ndarrayLike} result
47+
*/
48+
function getElements( target, property, ctx ) {
49+
var idx = prop2array( property, ctx.cache );
50+
if ( idx.type === 'int' ) {
51+
// FIXME: handle the various "kinds" of index arrays (e.g., "linear" and "cartesian")
52+
53+
// return ctx.postGetArray( take( target, idx.data ) );
54+
return NaN; // FIXME
55+
}
56+
if ( idx.type === 'bool' ) {
57+
// return ctx.postGetArray( mskfilter( target, idx.data ) );
58+
return NaN; // FIXME
59+
}
60+
if ( idx.type === 'mask' ) {
61+
// return ctx.postGetArray( mskreject( target, idx.data ) );
62+
return NaN; // FIXME
63+
}
64+
throw new Error( format( 'invalid operation. Unrecognized ndarray index type. Value: `%s`.', idx.type ) );
65+
}
66+
67+
68+
// EXPORTS //
69+
70+
module.exports = getElements;

0 commit comments

Comments
 (0)