All files main.js

41.69% Statements 118/283
100% Branches 1/1
0% Functions 0/7
41.69% Lines 118/283

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 2841x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x                                 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                         1x 1x 1x 1x 1x 1x 1x 1x 1x                                 1x 1x 1x 1x 1x 1x 1x 1x                               1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                         1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var iterationOrder = require( '@stdlib/ndarray/base/iteration-order' );
var minmaxViewBufferIndex = require( '@stdlib/ndarray/base/minmax-view-buffer-index' ).assign;
var ndarraylike2descriptor = require( '@stdlib/ndarray/base/ndarraylike2descriptor' );
var assign = require( '@stdlib/ndarray/base/assign' );
var descriptor = require( '@stdlib/ndarray/base/descriptor' );
var empty = require( '@stdlib/ndarray/base/empty' );
 
 
// FUNCTIONS //
 
/**
* Returns an input ndarray.
*
* @private
* @param {Object} x - input ndarray descriptor
* @returns {Object} input ndarray descriptor
*/
function identity( x ) {
	return x;
}
 
/**
* Broadcasts a zero-dimensional ndarray to a one-dimensional ndarray view containing a single element.
*
* @private
* @param {Object} x - input ndarray descriptor
* @returns {Object} broadcasted ndarray view descriptor
*/
function broadcast( x ) {
	return descriptor( x.dtype, x.data, [ 1 ], [ 0 ], x.offset, x.order );
}
 
/**
* Returns a function which returns an ndarray view in which the singleton dimensions are removed from an input ndarray having only one non-singleton dimension.
*
* @private
* @param {Object} arr - original ndarray descriptor
* @param {integer} index - index of the non-singleton dimension
* @returns {Function} function for returning an ndarray view
*/
function squeeze( arr, index ) {
	var sh = [ arr.shape[ index ] ];
	var sx = [ arr.strides[ index ] ];
	return reshape;

	/**
	* Returns an ndarray view in which the singleton dimensions are removed from an input ndarray having only one non-singleton dimension.
	*
	* @private
	* @param {Object} x - input ndarray descriptor
	* @returns {Object} a squeezed ndarray view descriptor
	*/
	function reshape( x ) {
		return descriptor( x.dtype, x.data, sh, sx, x.offset, x.order );
	}
}
 
/**
* Returns a function which returns a one-dimensional ndarray view of a contiguous input ndarray having more than one dimension.
*
* @private
* @param {integer} len - number of elements in an ndarray
* @param {integer} iox - iteration order
* @returns {Function} function for returning a one-dimensional ndarray view
*/
function contiguous( len, iox ) {
	var xmmv;
	var ind;
	var sh;
	var sx;

	// Resolve the index of the min/max view buffer element which is the first indexed element...
	if ( iox === 1 ) {
		ind = 0;
	} else {
		ind = 1;
	}
	// Initialize an array for storing the min/max view buffer elements:
	xmmv = [ 0, 0 ]; // [ min, max ]

	// Initialize the output one-dimensional view's shape and strides:
	sh = [ len ];
	sx = [ iox ];

	return reshape;

	/**
	* Returns a one-dimensional ndarray view of a contiguous input ndarray having more than one dimension.
	*
	* @private
	* @param {Object} x - input ndarray descriptor
	* @returns {Object} a one-dimensional ndarray view descriptor
	*/
	function reshape( x ) {
		// Resolve the minimum and maximum linear indices in the underlying data buffer which are accessible to the input ndarray view:
		minmaxViewBufferIndex( x.shape, x.strides, x.offset, xmmv );

		// Note: `xmmv[ ind ]` corresponds to the index of the first indexed element
		return descriptor( x.dtype, x.data, sh, sx, xmmv[ ind ], x.order );
	}
}
 
/**
* Returns a function which copies an input ndarray to a contiguous ndarray workspace.
*
* @private
* @param {integer} len - number of elements in an ndarray
* @param {Object} workspace - ndarray workspace descriptor
* @returns {Function} function which copies an input ndarray to a contiguous ndarray workspace
*/
function copyToWorkspace( len, workspace ) {
	var view = descriptor( workspace.dtype, workspace.data, [ len ], [ 1 ], workspace.offset, workspace.order ); // eslint-disable-line max-len
	return reshape;

	/**
	* Copies an input ndarray to a contiguous ndarray workspace and returns a one-dimensional workspace view.
	*
	* @private
	* @param {Object} x - input ndarray descriptor
	* @returns {Object} one-dimensional workspace view descriptor
	*/
	function reshape( x ) {
		assign( [ x, workspace ] );
		return view;
	}
}
 
/**
* Returns a function which copies from a contiguous ndarray workspace to an input ndarray.
*
* @private
* @param {Object} workspace - ndarray workspace descriptor
* @returns {Function} function which copies from a contiguous ndarray workspace to an input ndarray
*/
function copyFromWorkspace( workspace ) {
	return copy;

	/**
	* Copies from a contiguous ndarray workspace to an input ndarray.
	*
	* @private
	* @param {Object} x - input ndarray descriptor
	* @returns {Object} input ndarray descriptor
	*/
	function copy( x ) {
		assign( [ workspace, x ] );
		return x;
	}
}
 
 
// MAIN //
 
/**
* Returns an object containing methods for reshaping an ndarray as a one-dimensional strided array view.
*
* @param {Object} x - ndarray descriptor
* @param {*} x.dtype - ndarray data type
* @param {Collection} x.data - ndarray data buffer
* @param {Array<integer>} x.shape - ndarray shape
* @param {Array<integer>} x.strides - ndarray strides
* @param {integer} x.offset - ndarray index offset
* @param {string} x.order - ndarray memory layout
* @returns {Object} object containing methods implementing a reshape strategy
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var x = {
*     'dtype': 'float64',
*     'data': new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ),
*     'shape': [ 1 ],
*     'strides': [ 1 ],
*     'offset': 0,
*     'order': 'row-major'
* };
*
* var out = strategy( x );
* // returns {...}
*/
function strategy( x ) {
	var workspace;
	var ndims;
	var xmmv;
	var len;
	var iox;
	var sh;
	var ns;
	var i;

	// Resolve the number of array dimensions:
	sh = x.shape;
	ndims = sh.length;

	// Check whether the ndarray is zero-dimensional...
	if ( ndims === 0 ) {
		return {
			'input': broadcast,
			'output': identity
		};
	}
	// Check whether the ndarray is already one-dimensional...
	if ( ndims === 1 ) {
		return {
			'input': identity,
			'output': identity
		};
	}
	// Determine the number of singleton dimensions...
	len = 1; // number of elements
	ns = 0;  // number of singleton dimensions
	for ( i = 0; i < ndims; i++ ) {
		// Check whether the current dimension is a singleton dimension...
		if ( sh[ i ] === 1 ) {
			ns += 1;
		}
		len *= sh[ i ];
	}
	// Determine whether the ndarray has only **one** non-singleton dimension (e.g., ndims=4, shape=[10,1,1,1]) so that we can simply create an ndarray view without the singleton dimensions...
	if ( ns === ndims-1 ) {
		// Get the index of the non-singleton dimension...
		for ( i = 0; i < ndims; i++ ) {
			if ( sh[ i ] !== 1 ) {
				break;
			}
		}
		return {
			'input': squeeze( x, i ),
			'output': identity
		};
	}
	iox = iterationOrder( x.strides ); // +/-1

	// Determine whether we can avoid copying data...
	if ( iox !== 0 ) {
		// Determine the minimum and maximum linear indices which are accessible by the ndarray view:
		xmmv = minmaxViewBufferIndex( sh, x.strides, x.offset, [ 0, 0 ] );

		// Determine whether we can ignore shape (and strides) and create a new one-dimensional ndarray view...
		if ( len === ( xmmv[1]-xmmv[0]+1 ) ) {
			return {
				'input': contiguous( len, iox ),
				'output': identity
			};
		}
		// The ndarray is non-contiguous, so we cannot directly interpret as a one-dimensional ndarray...

		// Fall-through to copying to a workspace ndarray...
	}
	// At this point, we're dealing with a non-contiguous multi-dimensional ndarray, so we need to copy to a contiguous workspace:
	workspace = ndarraylike2descriptor( empty( x.dtype, x.shape, x.order ) );
	return {
		'input': copyToWorkspace( len, workspace ),
		'output': copyFromWorkspace( workspace )
	};
}
 
 
// EXPORTS //
 
module.exports = strategy;