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 | 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 61x 61x 61x 101x 54x 54x 101x 7x 61x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 26x 14x 14x 26x 2x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 1x 1x 13x 13x 13x 14x 24x 12x 12x 24x 1x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 1x 1x 9x 10x 10x 12x 8x 8x 12x 1x 10x 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 106x 106x 39x 39x 5x 5x 106x 6x 6x 106x 40x 14x 14x 40x 10x 10x 16x 16x 61x 106x 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 isAccessorArray = require( '@stdlib/assert/is-accessor-array' );
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var isComplexTypedArray = require( '@stdlib/array/base/assert/is-complex-typed-array' );
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
var isSameValue = require( '@stdlib/assert/is-same-value' );
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );
// FUNCTIONS //
/**
* Returns the index of the last element which equals a provided search element according to the same value algorithm.
*
* @private
* @param {Collection} x - input array
* @param {*} searchElement - search element
* @param {NonNegativeInteger} fromIndex - starting index (inclusive)
* @returns {integer} index
*
* @example
* var x = [ 1, 2, 3, 4 ];
*
* var idx = indexed( x, 2, 3 );
* // returns 1
*/
function indexed( x, searchElement, fromIndex ) {
var i;
for ( i = fromIndex; i >= 0; i-- ) {
if ( isSameValue( searchElement, x[ i ] ) ) {
return i;
}
}
return -1;
}
/**
* Returns the index of the last element which equals a provided search element according to the same value algorithm.
*
* @private
* @param {Object} x - input array object
* @param {*} searchElement - search element
* @param {NonNegativeInteger} fromIndex - starting index (inclusive)
* @returns {integer} index
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
*
* var x = toAccessorArray( [ 1, 2, 3, 4 ] );
*
* var idx = accessors( x, 2, 3 );
* // returns 1
*/
function accessors( x, searchElement, fromIndex ) {
var get;
var i;
get = resolveGetter( x );
for ( i = fromIndex; i >= 0; i-- ) {
if ( isSameValue( searchElement, get( x, i ) ) ) {
return i;
}
}
return -1;
}
/**
* Returns the index of the last element which equals a provided search element according to the same value algorithm.
*
* @private
* @param {Collection} x - input array
* @param {*} searchElement - search element
* @param {NonNegativeInteger} fromIndex - starting index (inclusive)
* @returns {integer} index
*
* @example
* var Complex128Array = require( '@stdlib/array/complex128' );
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
*
* var x = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ] );
*
* var idx = complex( x, new Complex128( 3.0, 4.0 ), 3 );
* // returns 2
*/
function complex( x, searchElement, fromIndex ) {
var view;
var re;
var im;
var i;
if ( !isComplexLike( searchElement ) ) {
return -1;
}
view = reinterpret( x, 0 );
re = real( searchElement );
im = imag( searchElement );
for ( i = fromIndex*2; i >= 0; i -= 2 ) {
if ( isSameValue( view[ i ], re ) && isSameValue( view[ i+1 ], im ) ) {
return i / 2;
}
}
return -1;
}
/**
* Returns the index of the last element which equals a provided search element according to the same value algorithm.
*
* @private
* @param {Collection} x - input array
* @param {*} searchElement - search element
* @param {NonNegativeInteger} fromIndex - starting index (inclusive)
* @returns {integer} index
*
* @example
* var BooleanArray = require( '@stdlib/array/bool' );
*
* var x = new BooleanArray( [ true, false, true, false, true ] );
*
* var idx = boolean( x, true, 3 );
* // returns 2
*/
function boolean( x, searchElement, fromIndex ) {
var view;
var v;
var i;
if ( !isBoolean( searchElement ) ) {
return -1;
}
view = reinterpretBoolean( x, 0 );
v = ( searchElement ) ? 1 : 0;
for ( i = fromIndex; i >= 0; i-- ) {
if ( view[ i ] === v ) {
return i;
}
}
return -1;
}
// MAIN //
/**
* Returns the index of the last element which equals a provided search element according to the same value algorithm.
*
* ## Notes
*
* - The function uses the [SameValue Algorithm][ecma-262-same-value-algorithm], as specified in ECMAScript 5.
* - In contrast to the strict equality operator `===`, `-0` and `+0` are distinguishable and `NaNs` are the same.
* - If unable to find an element which equals a provided search element, the function returns `-1`.
*
* [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12
*
* @param {Collection} x - input array
* @param {*} searchElement - search element
* @param {integer} fromIndex - starting index (inclusive)
* @returns {integer} index
*
* @example
* var x = [ 1, 2, 3, 4 ];
*
* var idx = lastIndexOfSameValue( x, 2, 3 );
* // returns 1
*
* @example
* var Int32Array = require( '@stdlib/array/int32' );
*
* var x = new Int32Array( [ 1, 2, 3, 4 ] );
*
* var idx = lastIndexOfSameValue( x, 2, 3 );
* // returns 1
*/
function lastIndexOfSameValue( x, searchElement, fromIndex ) {
if ( fromIndex < 0 ) {
fromIndex += x.length;
if ( fromIndex < 0 ) {
return -1;
}
} else if ( fromIndex > x.length ) {
fromIndex = x.length - 1;
}
if ( isAccessorArray( x ) ) {
if ( isComplexTypedArray( x ) ) {
return complex( x, searchElement, fromIndex );
}
if ( isBooleanArray( x ) ) {
return boolean( x, searchElement, fromIndex );
}
return accessors( x, searchElement, fromIndex );
}
return indexed( x, searchElement, fromIndex );
}
// EXPORTS //
module.exports = lastIndexOfSameValue;
|