All files main.js

100% Statements 188/188
100% Branches 29/29
100% Functions 3/3
100% Lines 188/188

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 1891x 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 34x 34x 34x 66x 8x 8x 66x 26x 34x 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 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 15x 1x 1x 15x 8x 9x 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 47x 47x 47x 47x 47x 47x 47x 47x 4x 4x 43x 43x 47x 24x 24x 24x 24x 7x 5x 5x 2x 2x 17x 24x 5x 5x 24x 5x 5x 5x 24x 5x 5x 24x 5x 5x 5x 24x 10x 10x 7x 7x 19x 47x 1x 1x 1x 1x 1x  
/**
* @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 isComplex128Array = require( '@stdlib/array/base/assert/is-complex128array' );
var isComplex64Array = require( '@stdlib/array/base/assert/is-complex64array' );
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' );
var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var isSameValue = require( '@stdlib/assert/is-same-value' );
 
 
// FUNCTIONS //
 
/**
* Tests if two arrays have the same values.
*
* @private
* @param {Collection} x - first input array
* @param {Collection} y - second input array
* @returns {boolean} boolean indicating if both arrays have the same values
*
* @example
* var x = [ 0, 0, 1, 0 ];
* var y = [ 0, 0, 1, 0 ];
*
* var out = internal( x, y );
* // returns true
*
* @example
* var x = [ 0, 0, 0, 0 ];
* var y = [ 0, 0, 1, 0 ];
*
* var out = internal( x, y );
* // returns false
*/
function internal( x, y ) {
	var i;
	for ( i = 0; i < x.length; i++ ) {
		if ( !isSameValue( x[ i ], y[ i ] ) ) {
			return false;
		}
	}
	return true;
}
 
/**
* Tests if two arrays have the same values.
*
* @private
* @param {Object} x - first input array object
* @param {Object} y - second input array object
* @returns {boolean} boolean indicating if both arrays have the same values
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
*
* var x = arraylike2object( toAccessorArray( [ 0, 0, 1, 0 ] ) );
* var y = arraylike2object( toAccessorArray( [ 0, 0, 1, 0 ] ) );
*
* var out = accessors( x, y );
* // returns true
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
*
* var x = arraylike2object( toAccessorArray( [ 0, 0, 0, 0 ] ) );
* var y = arraylike2object( toAccessorArray( [ 0, 0, 1, 0 ] ) );
*
* var out = accessors( x, y );
* // returns false
*/
function accessors( x, y ) {
	var xdata;
	var ydata;
	var xget;
	var yget;
	var i;
 
	xdata = x.data;
	ydata = y.data;
 
	xget = x.accessors[ 0 ];
	yget = y.accessors[ 0 ];
 
	for ( i = 0; i < xdata.length; i++ ) {
		if ( !isSameValue( xget( xdata, i ), yget( ydata, i ) ) ) {
			return false;
		}
	}
	return true;
}
 
 
// MAIN //
 
/**
* Tests if two arrays have the same values.
*
* @param {Collection} x - first input array
* @param {Collection} y - second input array
* @returns {boolean} boolean indicating if both arrays have the same values
*
* @example
* var x = [ 0, 0, 1, 0 ];
* var y = [ 0, 0, 1, 0 ];
*
* var out = hasSameValues( x, y );
* // returns true
*
* @example
* var x = [ 0, 0, 0, 0 ];
* var y = [ 0, 0, 1, 0 ];
*
* var out = hasSameValues( x, y );
* // returns false
*/
function hasSameValues( x, y ) {
	var FLG;
	var xo;
	var yo;
	var xr;
	var yr;
 
	if ( x.length !== y.length ) {
		return false;
	}
	xo = arraylike2object( x );
	yo = arraylike2object( y );
	if ( xo.accessorProtocol || yo.accessorProtocol ) {
		FLG = 2;
 
		// If provided boolean arrays, reinterpret the arrays to avoid using accessors to access array elements...
		if ( isBooleanArray( x ) ) {
			if ( isBooleanArray( y ) ) {
				return internal( reinterpretBoolean( x, 0 ), reinterpretBoolean( y, 0 ) );
			}
			return accessors( xo, yo );
		}
		// If provided a complex number array, reinterpret as a real typed array and test interleaved real and imaginary components...
		if ( isComplex128Array( x ) ) {
			xr = reinterpret128( x, 0 );
			FLG -= 1;
		} else if ( isComplex64Array( x ) ) {
			xr = reinterpret64( x, 0 );
			FLG -= 1;
		}
		if ( isComplex128Array( y ) ) {
			yr = reinterpret128( y, 0 );
			FLG -= 1;
		} else if ( isComplex64Array( y ) ) {
			yr = reinterpret64( y, 0 );
			FLG -= 1;
		}
		if ( FLG === 0 ) {
			return internal( xr, yr );
		}
		return accessors( xo, yo );
	}
	return internal( x, y );
}
 
 
// EXPORTS //
 
module.exports = hasSameValues;