/*
 * 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 <code>value</code> in <code>array</code>, or {@code -1} if there is no such index.
     * If there are several values that are within <code>tolerance</code> or less of <code>value</code>, this method will return the
     * index of the closest value. In case of several values being as close ot <code>value</code>, 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> T[] concat(T[] one, T[] other, Class<T> 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;
    }

    /**
     * Concat a string to a string array.
     */
    public static String[] append(String[] array, String added) {
        final String[] updated = new String[array.length + 1];
        System.arraycopy(array, 0, updated, 0, array.length);
        updated[array.length] = added;
        return updated;
    }

    /**
     * Reverse the {@code length} values on the array starting from {@code offset}.
     */
    public static void reverseSubArray(double[] array, int offset, int length) {
        int start = offset;
        int end = offset + length;
        while (end > start) {
            final double scratch = array[start];
            array[start] = array[end - 1];
            array[end - 1] = scratch;
            start++;
            end--;
        }
    }

    /**
     * Reverse the {@code length} values on the array starting from {@code offset}.
     */
    public static void reverseSubArray(long[] array, int offset, int length) {
        int start = offset;
        int end = offset + length;
        while (end > start) {
            final long scratch = array[start];
            array[start] = array[end - 1];
            array[end - 1] = scratch;
            start++;
            end--;
        }
    }
}
