All files main.js

100% Statements 221/221
100% Branches 27/27
100% Functions 4/4
100% Lines 221/221

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 2223x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 9x 9x 32x 10x 10x 13x 13x 13x 32x 1x 1x 12x 12x 12x 12x 12x 12x 12x 32x 3x 3x 9x 9x 32x 1x 1x 9x 9x 32x 17x 17x 9x 9x 9x 9x 9x 32x 18x 18x 18x 18x 34x 34x 18x 18x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 8x 8x 9x 9x 9x 9x 9x 9x 9x 9x 9x 34x 34x 34x 34x 34x 34x 8x 8x 8x 8x 26x 26x 26x 26x 26x 26x 26x 26x 26x 34x 12x 12x 12x 26x 26x 26x 26x 26x 34x 9x 9x 9x 9x 9x 9x 9x 9x 9x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 32x 3x 3x 3x 3x 3x  
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' );
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
var iteratorSymbol = require( '@stdlib/symbol/iterator' );
var zeros = require( '@stdlib/array/base/zeros' );
var getShape = require( '@stdlib/ndarray/shape' );
var numel = require( '@stdlib/ndarray/base/numel' );
var ndslice = require( '@stdlib/ndarray/base/slice' );
var maybeBroadcastArrays = require( '@stdlib/ndarray/maybe-broadcast-arrays' );
var nextCartesianIndex = require( '@stdlib/ndarray/base/next-cartesian-index' ).assign;
var args2multislice = require( '@stdlib/slice/base/args2multislice' );
var format = require( '@stdlib/string/format' );
 
 
// MAIN //
 
/**
* Returns an iterator which iterates over interleaved subarrays.
*
* @param {ArrayLikeObject<ndarray>} arrays - input ndarrays
* @param {PositiveInteger} ndims - number of dimensions to stack after broadcasting
* @throws {TypeError} first argument must be an array of ndarrays
* @throws {TypeError} each ndarray after broadcasting must have at least `ndims+1` dimensions
* @throws {TypeError} second argument must be a positive integer
* @throws {Error} input ndarrays must be broadcast-compatible
* @returns {Iterator} iterator
*
* @example
* var array = require( '@stdlib/ndarray/array' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
*
* var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] );
* // returns <ndarray>
*
* var iter = nditerInterleaveSubarrays( [ x, x ], 2 );
*
* var v = iter.next().value;
* // returns <ndarray>
*
* var arr = ndarray2array( v );
* // returns [ [ 1, 2 ], [ 3, 4 ] ]
*
* v = iter.next().value;
* // returns <ndarray>
*
* arr = ndarray2array( v );
* // returns [ [ 1, 2 ], [ 3, 4 ] ]
*
* // ...
*/
function nditerInterleaveSubarrays( arrays, ndims ) {
	var shape;
	var iter;
	var list;
	var FLG;
	var idx;
	var dim;
	var tmp;
	var S;
	var M;
	var N;
	var K;
	var i;
	var j;
	var k;
 
	if ( !isArrayLikeObject( arrays ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an array of ndarrays. Value: `%s`.', arrays ) );
	}
	if ( !isPositiveInteger( ndims ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be a positive integer. Value: `%s`.', ndims ) );
	}
	// Attempt to broadcast the input ndarrays...
	try {
		list = maybeBroadcastArrays( arrays );
	} catch ( err ) { // eslint-disable-line no-unused-vars
		throw new TypeError( format( 'invalid argument. First argument must be an array of ndarrays which are broadcast-compatible. Value: `%s`.', arrays ) );
	}
	K = list.length;
 
	// Retrieve input array meta data:
	shape = getShape( list[ 0 ] );
	M = shape.length;
 
	// Ensure that each broadcasted input array has sufficient dimensions...
	if ( M <= ndims ) {
		throw new TypeError( format( 'invalid argument. First argument must be an array of ndarrays having at least %d dimensions after broadcasting.', ndims+1 ) );
	}
	// Check whether the broadcasted shape is empty...
	N = numel( shape );
	if ( N === 0 ) {
		FLG = true;
	}
	// Compute the number of subarrays across all stacks of subarrays:
	dim = M - ndims - 1;
	for ( i = dim+1; i < M; i++ ) {
		N /= shape[ i ];
	}
	N *= K;
	S = shape[ dim ];
 
	// Initialize index arrays for generating slices...
	idx = [];
	for ( i = 0; i < K; i++ ) {
		tmp = zeros( M );
 
		// Set the last `ndims` elements to `null` to indicate that we want a full "slice" for the last `ndims` dimensions:
		for ( j = dim+1; j < M; j++ ) {
			tmp[ j ] = null;
		}
		idx.push( tmp );
	}
	// Initialize counters:
	i = -1;
	k = -1;
 
	// Create an iterator protocol-compliant object:
	iter = {};
	setReadOnly( iter, 'next', next );
	setReadOnly( iter, 'return', end );
 
	// If an environment supports `Symbol.iterator`, make the iterator iterable:
	if ( iteratorSymbol ) {
		setReadOnly( iter, iteratorSymbol, factory );
	}
	return iter;
 
	/**
	* Returns an iterator protocol-compliant object containing the next iterated value.
	*
	* @private
	* @returns {Object} iterator protocol-compliant object
	*/
	function next() {
		var ibuf;
		var s;
		var j;
 
		i += 1;
		if ( FLG || i >= N ) {
			return {
				'done': true
			};
		}
		k = ( k+1 ) % K;
		ibuf = idx[ k ];
 
		// Create a multi-slice for the current view:
		s = args2multislice( ibuf );
 
		// Update the index array:
		j = ( ibuf[ dim ] + 1 ) % S;
		ibuf[ dim ] = j;
		if ( j === 0 ) {
			// If we've iterated over all the subarrays in the current stack, move on to the next set of subarrays:
			ibuf = nextCartesianIndex( shape, 'row-major', ibuf, dim-1, ibuf );
		}
		// Return the next slice:
		return {
			'value': ndslice( list[ k ], s, true, false ),
			'done': false
		};
	}
 
	/**
	* Finishes an iterator.
	*
	* @private
	* @param {*} [value] - value to return
	* @returns {Object} iterator protocol-compliant object
	*/
	function end( value ) {
		FLG = true;
		if ( arguments.length ) {
			return {
				'value': value,
				'done': true
			};
		}
		return {
			'done': true
		};
	}
 
	/**
	* Returns a new iterator.
	*
	* @private
	* @returns {Iterator} iterator
	*/
	function factory() {
		return nditerInterleaveSubarrays( arrays, ndims );
	}
}
 
 
// EXPORTS //
 
module.exports = nditerInterleaveSubarrays;