Skip to content

Commit d60c44a

Browse files
committed
Add logic for determining layout order
1 parent c44d900 commit d60c44a

File tree

1 file changed

+60
-17
lines changed
  • lib/node_modules/@stdlib/ndarray/lib

1 file changed

+60
-17
lines changed

lib/node_modules/@stdlib/ndarray/lib/main.js

+60-17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var isArray = require( '@stdlib/assert/is-array' );
2727
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
2828
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
2929
var strides2offset = require( '@stdlib/ndarray/base/strides2offset' );
30+
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
3031
var numel = require( '@stdlib/ndarray/base/numel' );
3132
var ctor = require( '@stdlib/ndarray/memoized-ctor' );
3233
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
@@ -49,7 +50,7 @@ var castBuffer = require( './cast_buffer.js' );
4950
* @param {Options} [options] - function options
5051
* @param {(ArrayLikeObject|TypedArrayLike|Buffer|ndarrayLike)} [options.buffer] - data source
5152
* @param {string} [options.dtype="float64"] - underlying storage data type (if the input data is not of the same type, this option specifies the data type to which to cast the input data)
52-
* @param {string} [options.order="row-major"] - specifies whether an array should be row-major (C-style) or column-major (Fortran-style)
53+
* @param {string} [options.order="row-major"] - specifies the memory layout of the array as either row-major (C-style) or column-major (Fortran-style)
5354
* @param {NonNegativeIntegerArray} [options.shape] - array shape
5455
* @param {boolean} [options.codegen=true] - boolean indicating whether to use code generation
5556
* @param {string} [options.mode="throw"] - specifies how to handle indices which exceed array dimensions
@@ -107,6 +108,7 @@ function array() {
107108
var nopts;
108109
var opts;
109110
var len;
111+
var ord;
110112
var FLG;
111113

112114
if ( arguments.length === 1 ) {
@@ -144,7 +146,27 @@ function array() {
144146
}
145147
if ( hasOwnProp( options, 'order' ) ) {
146148
order = options.order;
147-
if ( !isOrder( order ) && order !== 'any' && order !== 'keep' ) {
149+
if ( order === 'any' || order === 'same' ) {
150+
if ( FLG ) {
151+
// If the user indicated that "any" order suffices (meaning the user does not care about ndarray order), then we default to the default order, unless the input ndarray is unequivocally either "row-major" or "column-major"....
152+
if ( order === 'any' ) {
153+
ord = strides2order( buffer.strides );
154+
if ( ord === 'column-major' ) {
155+
order = ord;
156+
} else if ( ord === 'row-major' ) {
157+
order = ord;
158+
} else {
159+
order = defaults.order;
160+
}
161+
}
162+
// If the user indicated to keep the same order, then TODO...
163+
else if ( order === 'same' ) {
164+
165+
}
166+
} else {
167+
order = defaults.order;
168+
}
169+
} else if ( !isOrder( order ) ) {
148170
throw new TypeError( 'invalid option. `order` option must be a recognized order. Option: `' + order + '`.' );
149171
}
150172
} else {
@@ -212,27 +234,48 @@ function array() {
212234
} else {
213235
throw new Error( 'invalid input arguments. Must provide either a data source, array shape, or both.' );
214236
}
215-
// TODO: if provided an ndarray, determine order
216-
217-
// Compute the array strides:
218-
strides = shape2strides( shape, order );
219-
220-
// Determine the index offset (i.e., the pointer to the first array element):
221-
offset = strides2offset( shape, strides );
237+
// If provided an ndarray, we need to take special care to ensure we determine the correct order...
238+
if ( FLG ) {
239+
// If we are not copying the ndarray buffer, then we are simply creating another ndarray wrapper around the same underlying buffer...
240+
if ( opts.copy === false ) {
241+
strides = buffer.strides;
242+
offset = buffer.offset;
243+
order = buffer.order;
244+
}
245+
// If we are copying the ndarray buffer, then we need to perform some analysis to determine order of the output ndarray...
246+
else {
222247

248+
}
249+
}
250+
// For all other data source inputs, we assume we've been provided a contiguous data source for which we can simply compute the strides and index offset pointing to the first indexed element...
251+
else {
252+
strides = shape2strides( shape, order );
253+
offset = strides2offset( shape, strides );
254+
}
223255
// If not provided a data buffer, create it; otherwise, see if we need to cast a provided data buffer to another data type or perform a copy...
224256
if ( buffer ) {
225257
// TODO: determine how to handle ndarray data sources (for non-copy, we need to compute strides based on the order taking into account that a provided ndarray may have "scrambled" strides; for copy, we need to "pluck" the values from the ndarray buffer)
226258

227-
btype = getType( buffer );
228-
if ( btype === 'generic' && opts.flatten ) {
229-
buffer = flattenArray( buffer );
230-
}
231-
if ( btype !== dtype || opts.copy ) {
232-
if ( buffer.length < len ) {
233-
throw new RangeError( 'invalid input arguments. Array shape is incompatible with provided data source. To accommodate the requested shape, provide a bigger data source.' );
259+
if ( FLG ) {
260+
if ( buffer.length !== len ) {
261+
throw new RangeError( 'invalid input arguments. Array shape is incompatible with provided data source. Number of data source elements does not match array shape.' );
262+
}
263+
if ( opts.copy ) {
264+
// TODO: pluck data
265+
} else {
266+
buffer = buffer.data;
267+
}
268+
} else {
269+
btype = getType( buffer );
270+
if ( btype === 'generic' && opts.flatten ) {
271+
buffer = flattenArray( buffer );
272+
}
273+
if ( buffer.length !== len ) {
274+
throw new RangeError( 'invalid input arguments. Array shape is incompatible with provided data source. Number of data source elements does not match array shape.' );
275+
}
276+
if ( btype !== dtype || opts.copy ) {
277+
buffer = castBuffer( buffer, len, dtype );
234278
}
235-
buffer = castBuffer( buffer, len, dtype );
236279
}
237280
} else {
238281
buffer = createBuffer( dtype, len );

0 commit comments

Comments
 (0)