Skip to content

Latest commit

 

History

History

README.md

sindexOf

Return the first index of a specified search element in a single-precision floating-point strided array.

Usage

var sindexOf = require( '@stdlib/blas/ext/base/sindex-of' );

sindexOf( N, searchElement, x, strideX )

Returns the first index of a specified search element in a single-precision floating-point strided array.

var Float32Array = require( '@stdlib/array/float32' );

var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] );

var idx = sindexOf( x.length, 3.0, x, 1 );
// returns 2

The function has the following parameters:

  • N: number of indexed elements.
  • searchElement: search element.
  • x: input Float32Array.
  • strideX: stride length.

If the function is unable to find a search element, the function returns -1.

var Float32Array = require( '@stdlib/array/float32' );

var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] );

var idx = sindexOf( x.length, 8.0, x, 1 );
// returns -1

The N and stride parameters determine which elements in the strided array are accessed at runtime. For example, to search every other element:

var Float32Array = require( '@stdlib/array/float32' );

var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] );

var idx = sindexOf( 4, -1.0, x, 2 );
// returns 3

Note that indexing is relative to the first index. To introduce an offset, use typed array views.

var Float32Array = require( '@stdlib/array/float32' );

// Initial array...
var x0 = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );

// Create an offset view...
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

// Find index...
var idx = sindexOf( 3, -6.0, x1, 2 );
// returns 2

sindexOf.ndarray( N, searchElement, x, strideX, offsetX )

Returns the first index of a specified search element in a single-precision floating-point strided array using alternative indexing semantics.

var Float32Array = require( '@stdlib/array/float32' );

var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] );

var idx = sindexOf.ndarray( x.length, 3.0, x, 1, 0 );
// returns 2

The function has the following additional parameters:

  • offsetX: starting index.

While typed array views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of the strided array

var Float32Array = require( '@stdlib/array/float32' );

var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, 3.0 ] );

var idx = sindexOf.ndarray( 3, 3.0, x, 1, x.length-3 );
// returns 2

Notes

  • When searching for a search element, the function checks for equality using the strict equality operator ===. As a consequence, NaN values are considered distinct, and -0 and +0 are considered the same.

Examples

var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var sindexOf = require( '@stdlib/blas/ext/base/sindex-of' );

var x = discreteUniform( 10, -100, 100, {
    'dtype': 'float32'
});
console.log( x );

var idx = sindexOf( x.length, 80.0, x, 1 );
console.log( idx );

C APIs

Usage

#include "stdlib/blas/ext/base/sindex_of.h"

stdlib_strided_sindex_of( N, searchElement, *X, strideX )

Returns the first index of a specified search element in a single-precision floating-point strided array.

float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };

int idx = stdlib_strided_sindex_of( 4, 3.0f, x, 1 );
// returns 2

The function accepts the following arguments:

  • N: [in] CBLAS_INT number of indexed elements.
  • searchElement: [in] float search element.
  • X: [in] float* input array.
  • strideX: [in] CBLAS_INT stride length.
CBLAS_INT N stdlib_strided_sindex_of( const CBLAS_INT N, const float searchElement, const float *X, const CBLAS_INT strideX );

stdlib_strided_sindex_of_ndarray( N, searchElement, *X, strideX, offsetX )

Returns the first index of a specified search element in a single-precision floating-point strided array using alternative indexing semantics.

float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };

int idx = stdlib_strided_sindex_of_ndarray( 4, 3.0f, x, 1, 0 );
// returns 2

The function accepts the following arguments:

  • N: [in] CBLAS_INT number of indexed elements.
  • searchElement: [in] float search element.
  • X: [in] float* input array.
  • strideX: [in] CBLAS_INT stride length.
  • offsetX: [in] CBLAS_INT starting index.
CBLAS_INT stdlib_strided_sindex_of_ndarray( const CBLAS_INT N, const float searchElement, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );

Examples

#include "stdlib/blas/ext/base/sindex_of.h"
#include <stdio.h>

int main( void ) {
    // Create a strided array:
    float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };

    // Specify the number of indexed elements:
    const int N = 8;

    // Specify a stride:
    const int strideX = 1;

    // Perform a search:
    int idx = stdlib_strided_sindex_of( N, 5.0f, x, strideX );

    // Print the result:
    printf( "index value: %d\n", idx );
}