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 | 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 172x 172x 172x 112x 112x 112x 172x 172x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 246x 246x 246x 246x 246x 246x 246x 246x 246x 246x 427x 427x 427x 427x 16x 16x 16x 411x 427x 196x 196x 22x 22x 22x 196x 427x 208x 246x 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 69x 69x 69x 69x 69x 9x 9x 60x 60x 60x 60x 60x 60x 60x 60x 69x 50x 50x 50x 60x 69x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2018 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 isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' );
var format = require( '@stdlib/string/format' );
// FUNCTIONS //
/**
* Recursively (and eagerly) attempts to resolve nested array dimensions.
*
* @private
* @param {Array} shape - output array
* @param {ArrayLikeObject} arr - array
* @returns {Array} shape array
*/
function recurse( shape, arr ) {
var v = arr[ 0 ];
if ( isArrayLikeObject( v ) ) {
shape.push( v.length );
recurse( shape, v );
}
return shape;
}
/**
* Recursively verifies that all nested arrays have consistent dimensions.
*
* @private
* @param {PositiveInteger} ndims - number of dimensions
* @param {Array} shape - shape array
* @param {NonNegativeInteger} d - dimension
* @param {ArrayLikeObject} arr - array element to verify
* @param {boolean} flg - boolean indicating whether to continue recursing
* @returns {NonNegativeInteger} number of consistent dimensions
*/
function check( ndims, shape, d, arr, flg ) {
var len;
var v;
var i;
// Get the size of the current dimension:
len = shape[ d ];
// Ensure that each array element is an array of the same size:
for ( i = 0; i < arr.length; i++ ) {
v = arr[ i ];
// If the array element is not an array or is not the same size, we have found an inconsistent dimension:
if ( !isArrayLikeObject( v ) || v.length !== len ) {
// `d` is one more than the index of the last consistent dimension and thus equal to the number of consistent dimensions:
return d;
}
// Recursively examine nested elements:
if ( flg ) {
v = check( ndims, shape, d+1, v, d+1 < ndims-1 );
if ( v < ndims ) {
// Propagate the number of consistent dimensions up the recursion chain...
return v;
}
}
}
return ndims;
}
// MAIN //
/**
* Determines (nested) array dimensions.
*
* @param {ArrayLikeObject} arr - array
* @throws {TypeError} must provide an array
* @returns {Array} array shape
*
* @example
* var arr = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
*
* var shape = arrayShape( arr );
* // returns [ 3, 3 ]
*
* @example
* var arr = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8 ] ];
*
* var shape = arrayShape( arr );
* // returns [ 3 ]
*
* @example
* var arr = [ [ 1, 2, 3 ], [ 4, 5, 6 ], null ];
*
* var shape = arrayShape( arr );
* // returns [ 3 ]
*/
function arrayShape( arr ) {
var shape;
var ndims;
if ( !isArrayLikeObject( arr ) ) {
throw new TypeError( format( 'invalid argument. Must provide an array-like object. Value: `%s`.', arr ) );
}
// Initialize the shape/dimensions array:
shape = [ arr.length ];
// Eagerly determine array dimensions:
recurse( shape, arr );
ndims = shape.length;
// Check that all array element dimensions are consistent:
if ( ndims > 1 ) {
// If `check()` returns a value less than `ndims`, trim off the inconsistent dimensions:
shape.length = check( ndims, shape, 1, arr, ndims > 2 );
}
return shape;
}
// EXPORTS //
module.exports = arrayShape;
|