Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.common.util;
import java.lang.reflect.Array;
import java.util.Arrays;
public class ArrayUtils {
private ArrayUtils() {}
/**
* Return the index of value in array, or {@code -1} if there is no such index.
* If there are several values that are within tolerance or less of value, this method will return the
* index of the closest value. In case of several values being as close ot value, there is no guarantee which index
* will be returned.
* Results are undefined if the array is not sorted.
*/
public static int binarySearch(double[] array, double value, double tolerance) {
if (array.length == 0) {
return -1;
}
return binarySearch(array, 0, array.length, value, tolerance);
}
private static int binarySearch(double[] array, int fromIndex, int toIndex, double value, double tolerance) {
int index = Arrays.binarySearch(array, fromIndex, toIndex, value);
if (index < 0) {
final int highIndex = -1 - index; // first index of a value that is > value
final int lowIndex = highIndex - 1; // last index of a value that is < value
double lowError = Double.POSITIVE_INFINITY;
double highError = Double.POSITIVE_INFINITY;
if (lowIndex >= 0) {
lowError = value - array[lowIndex];
}
if (highIndex < array.length) {
highError = array[highIndex] - value;
}
if (highError < lowError) {
if (highError < tolerance) {
index = highIndex;
}
} else if (lowError < tolerance) {
index = lowIndex;
} else {
index = -1;
}
}
return index;
}
/**
* Concatenates 2 arrays
*/
public static String[] concat(String[] one, String[] other) {
return concat(one, other, String.class);
}
/**
* Concatenates 2 arrays
*/
public static T[] concat(T[] one, T[] other, Class clazz) {
@SuppressWarnings("unchecked")
T[] target = (T[]) Array.newInstance(clazz, one.length + other.length);
System.arraycopy(one, 0, target, 0, one.length);
System.arraycopy(other, 0, target, one.length, other.length);
return target;
}
}