/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.arrays;

import java.util.Comparator;
import java.util.Optional;
import java.util.function.Predicate;

import net.ranides.assira.collection.IntComparator;

@SuppressWarnings({
    "PMD.AvoidReassigningParameters"
})
public final class ArraySearch {
    
    private ArraySearch() {
        // utility class
    }
    
    public static <A,T> boolean contains(A array, T value) {
        return NativeArraySearch.contains(NativeArray.wrap(array), value);
    }
    
    public static <A,T> boolean contains(A array, int begin, int end, T value) {
        return NativeArraySearch.contains(NativeArray.wrap(array), begin, end, value);
    }
    
    public static <A,T> Optional<T> first(A array, Predicate<T> predicate) {
        return NativeArraySearch.first(NativeArray.wrap(array), predicate);
    }
    
    public static <A,T> Optional<T> first(A array, int begin, int end, Predicate<T> predicate) {
        return NativeArraySearch.first(NativeArray.wrap(array), begin, end, predicate);
    }
    
    public static <A,T> Optional<T> last(A array, Predicate<T> predicate) {
        return NativeArraySearch.last(NativeArray.wrap(array), predicate);
    }
    
    public static <A,T> Optional<T> last(A array, int begin, int end, Predicate<T> predicate) {
        return NativeArraySearch.last(NativeArray.wrap(array), begin, end, predicate);
    }
    
    public static <A, T> int indexOf(A array, T value) {
        return NativeArraySearch.indexOf(NativeArray.wrap(array), value);
    }
    
    public static <A,T> int indexOf(A array, int begin, int end, T value) {
        return NativeArraySearch.indexOf(NativeArray.wrap(array), begin, end, value);
    }
    
    public static <A,T> int lastIndexOf(A array, T value) {
        return NativeArraySearch.lastIndexOf(NativeArray.wrap(array), value);
    }
    
    public static <A,T> int lastIndexOf(A array, int begin, int end, T value) {
        return NativeArraySearch.lastIndexOf(NativeArray.wrap(array), begin, end, value);
    }
    
    /**
     * Searches an array for the specified value using the binary search
     * algorithm. The range must be sorted prior to making this call. If it is
     * not sorted, the results are undefined. If the range contains multiple
     * elements with the specified value, there is no guarantee which one will
     * be found.
     *
     * @param array the array to be searched.
     * @param key the value to be searched for.
     * @return index of the search key, if it is contained in the array;
     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
     * point</i> is defined as the the point at which the value would be
     * inserted into the array: the index of the first element greater than the
     * key, or the length of the array, if all elements in the array are less
     * than the specified key. Note that this guarantees that the return value
     * will be &gt;= 0 if and only if the key is found.
     * @see java.util.Arrays
     */
    public static <A,T> int binarySearch(A array, T key) {
        if($isint(array)) {
            return $binarySearch((int[])array, (Integer)key);
        } else {
            return NativeArraySearch.binarySearch(NativeArray.wrap(array), key);
        }
    }
    
    /**
     * Searches a range of the specified array for the specified value using the
     * binary search algorithm. The range must be sorted prior to making this
     * call. If it is not sorted, the results are undefined. If the range
     * contains multiple elements with the specified value, there is no
     * guarantee which one will be found.
     *
     * @param array the array to be searched.
     * @param begin the index of the first element (inclusive) to be searched.
     * @param end the index of the last element (exclusive) to be searched.
     * @param key the value to be searched for.
     * @return index of the search key, if it is contained in the array;
     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
     * point</i> is defined as the the point at which the value would be
     * inserted into the array: the index of the first element greater than the
     * key, or the length of the array, if all elements in the array are less
     * than the specified key. Note that this guarantees that the return value
     * will be &gt;= 0 if and only if the key is found.
     * @see java.util.Arrays
     */
    public static <A, T> int binarySearch(A array, int begin, int end, T key) {
        if($isint(array)) {
            return $binarySearch((int[])array, begin, end, (Integer)key);
        } else {
            return NativeArraySearch.binarySearch(NativeArray.wrap(array), begin, end, key);
        }
    }
    
    /**
     * Searches an array for the specified value using the binary search
     * algorithm and a specified comparator. The range must be sorted following
     * the comparator prior to making this call. If it is not sorted, the
     * results are undefined. If the range contains multiple elements with the
     * specified value, there is no guarantee which one will be found.
     *
     * @param array the array to be searched.
     * @param key the value to be searched for.
     * @param cmp a comparator.
     * @return index of the search key, if it is contained in the array;
     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
     * point</i> is defined as the the point at which the value would be
     * inserted into the array: the index of the first element greater than the
     * key, or the length of the array, if all elements in the array are less
     * than the specified key. Note that this guarantees that the return value
     * will be &gt;= 0 if and only if the key is found.
     * @see java.util.Arrays
     */
    public static <A,T> int binarySearch(A array, T key, Comparator<? super T> cmp) {
        if($isint(array)) {
            return $binarySearch((int[])array, (Integer)key, IntComparator.$wrap(cmp));
        } else {
            return NativeArraySearch.binarySearch(NativeArray.wrap(array), key, cmp);
        }
    }

    /**
     * Searches a range of the specified array for the specified value using the
     * binary search algorithm and a specified comparator. The range must be
     * sorted following the comparator prior to making this call. If it is not
     * sorted, the results are undefined. If the range contains multiple
     * elements with the specified value, there is no guarantee which one will
     * be found.
     *
     * @param array the array to be searched.
     * @param begin the index of the first element (inclusive) to be searched.
     * @param end the index of the last element (exclusive) to be searched.
     * @param key the value to be searched for.
     * @param comp a comparator.
     * @return index of the search key, if it is contained in the array;
     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
     * point</i> is defined as the the point at which the value would be
     * inserted into the array: the index of the first element greater than the
     * key, or the length of the array, if all elements in the array are less
     * than the specified key. Note that this guarantees that the return value
     * will be &gt;= 0 if and only if the key is found.
     * @see java.util.Arrays
     */
    public static <A,T> int binarySearch(A array, int begin, int end, T key, Comparator<? super T> comp) {
        if($isint(array)) {
            return $binarySearch((int[])array, begin, end, (Integer)key, IntComparator.$wrap(comp));
        } else {
            return NativeArraySearch.binarySearch(NativeArray.wrap(array), begin, end, key, comp);
        }
    }

    private static <A> boolean $isint(A array) {
        return null!=array && int[].class.equals(array.getClass());
    }
    
    private static int $binarySearch(int[] array, int key) {
        return $binarySearch(array, 0, array.length, key);
    }
    
    private static int $binarySearch(int[] array, int begin, int end, int key) {
        int midVal;
        end--;
        while (begin <= end) {
            int mid = (begin + end) >>> 1;
            midVal = array[ mid];
            if (midVal < key) {
                begin = mid + 1;
            } else if (midVal > key) {
                end = mid - 1;
            } else {
                return mid;
            }
        }
        return -(begin + 1);
    }
    
    private static int $binarySearch(int[] array, int key, IntComparator comp) {
        return $binarySearch(array, 0, array.length, key, comp);
    }
    
    private static int $binarySearch(int[] array, int begin, int end, int key, IntComparator comp) {
        int midVal;
        end--;
        while (begin <= end) {
            int mid = (begin + end) >>> 1;
            midVal = array[ mid];
            int cmp = comp.compare(midVal, key);
            if (cmp < 0) {
                begin = mid + 1;
            } else if (cmp > 0) {
                end = mid - 1;
            } else {
                return mid; // key found
            }
        }
        return -(begin + 1);
    }

}
