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 | 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) 2025 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 arraylike2object = require( '@stdlib/array/base/arraylike2object' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
// FUNCTIONS //
/**
* Returns an array of indices corresponding to the elements of an input array which pass a test implemented by a predicate function.
*
* @private
* @param {Collection} x - input array
* @param {Array<number>} idx - list of indices
* @param {Function} predicate - predicate function
* @returns {Array<number>} updated list of indices
*
* @example
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
* var zeroTo = require( '@stdlib/array/base/zero-to' );
*
* function predicate( v ) {
* return ( v > 0 );
* }
*
* var x = arraylike2object( toAccessorArray( [ -1, 2, 3, -4, 5 ] ) );
* var idx = zeroTo( 5 );
*
* var out = accessors( x, idx, predicate );
* // returns [ 1, 2, 4 ]
*/
function accessors( x, idx, predicate ) {
var buf;
var get;
var i;
var j;
var k;
buf = x.data;
get = x.accessors[ 0 ];
k = 0;
for ( i = 0; i < idx.length; i++ ) {
j = idx[ i ];
if ( predicate( get( buf, j ), j, buf ) ) {
idx[ k ] = j;
k += 1;
}
}
idx.length = k;
return idx;
}
/**
* Returns an array of indices corresponding to the elements of an input array which pass a test implemented by a predicate function.
*
* @private
* @param {Collection} x - input array
* @param {Array<number>} idx - list of indices
* @param {Function} predicate - predicate function
* @returns {Array<number>} updated list of indices
*
* @example
* var zeroTo = require( '@stdlib/array/base/zero-to' );
*
* function predicate( v ) {
* return ( v > 0 );
* }
*
* var x = [ -1, 2, 3, -4, 5 ];
* var idx = zeroTo( 5 );
*
* var out = indexed( x, idx, predicate );
* // returns [ 1, 2, 4 ]
*/
function indexed( x, idx, predicate ) {
var i;
var j;
var k;
k = 0;
for ( i = 0; i < idx.length; i++ ) {
j = idx[ i ];
if ( predicate( x[ j ], j, x ) ) {
idx[ k ] = j;
k += 1;
}
}
idx.length = k;
return idx;
}
// MAIN //
/**
* Performs element-wise evaluation of one or more input arrays according to provided predicate functions and counts the number of elements for which all predicates respectively return `true`.
*
* @param {Collection} x0 - first input array
* @param {Function} predicate0 - first predicate function
* @param {Collection} [x1] - second input array
* @param {Function} [predicate1] - second predicate function
* @param {Collection} [x2] - third input array
* @param {Function} [predicate2] - third predicate function
* @param {...(Collection|Function)} [args] - additional input arrays and predicate functions
* @returns {NonNegativeInteger} result
*
* @example
* function predicate0( value ) {
* return ( value > 0 );
* }
*
* function predicate1( value ) {
* return ( value < 3 );
* }
*
* var x0 = [ 0, 1, 0, 1, 2 ];
* var x1 = [ 2, 3, 1, 2, 5 ];
*
* var n = countIfs( x0, predicate0, x1, predicate1 );
* // returns 1
*/
function countIfs() {
var predicates;
var arrays;
var len;
var idx;
var x;
var i;
arrays = [];
predicates = [];
for ( i = 0; i < arguments.length; i += 2 ) {
arrays.push( arraylike2object( arguments[ i ] ) );
predicates.push( arguments[ i+1 ] );
}
len = arguments[ 0 ].length;
idx = zeroTo( len );
for ( i = 0; i < arrays.length; i++ ) {
x = arrays[ i ];
if ( x.accessorProtocol ) {
idx = accessors( x, idx, predicates[ i ] );
} else {
idx = indexed( x.data, idx, predicates[ i ] );
}
}
return idx.length;
}
// EXPORTS //
module.exports = countIfs;
|