/*
 * @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.NoSuchElementException;
import java.util.Optional;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
import net.ranides.assira.collection.arrays.NativeArray.NativeComparator;

@SuppressWarnings({
    "PMD.AvoidReassigningParameters"
})
public final class NativeArraySearch {
    
    private NativeArraySearch() {
        // utility class
    }
    
    public static <T> Optional<T> first(NativeArray array, Predicate<T> predicate) {
        return first(array, 0, array.size(), predicate);
    }
    
    @SuppressWarnings("unchecked")
    public static <T> Optional<T>  first(NativeArray array, int begin, int end, Predicate<T> predicate) {
        for (int i=begin; i<end; i++) {
            T v = (T)array.get(i);
            if( predicate.test(v)) {
                return Optional.of(v);
            }
        }
        return Optional.empty();
    }
    
    public static <T> Optional<T> last(NativeArray array, Predicate<T> predicate) {
        return last(array, 0, array.size(), predicate);
    }
    
    @SuppressWarnings("unchecked")
    public static <T> Optional<T> last(NativeArray array, int begin, int end, Predicate<T> predicate) {
        for (int i=end-1; i>=begin; i--) {
            T v = (T)array.get(i);
            if( predicate.test(v)) {
                return Optional.of(v);
            }
        }
        return Optional.empty();
    }
    
    public static <T> boolean contains(NativeArray array, T value) {
        return contains(array, 0, array.size(), value);
    }
    
    public static <T> boolean contains(NativeArray array, int begin, int end, T value) {
        return indexOf(array, begin, end, value) >= 0;
    }
    
    public static <T> int indexOf(NativeArray array, T value) {
        return indexOf(array, 0, array.size(), value);
    }
    
    public static <T> int indexOf(NativeArray array, int begin, int end, T value) {
        IntPredicate ip = array.cmp(value);
        for (int i=begin; i<end; i++) {
            if( ip.test(i)) {
                return i;
            }
        }
        return -1;
    }
    
    public static <T> int lastIndexOf(NativeArray array, T value) {
        return lastIndexOf(array, 0, array.size(), value);
    }
    
    public static <T> int lastIndexOf(NativeArray array, int begin, int end, T value) {
        IntPredicate ip = array.cmp(value);
        for (int i=end-1; i>=begin; i--) {
            if( ip.test(i)) {
                return i;
            }
        }
        return -1;
    }

    public static <T> int binarySearch(NativeArray array, T key) {
        return binarySearch(array.rcmp(key), 0, array.size());
    }
    
    public static <T> int binarySearch(NativeArray array, int begin, int end, T key) {
        return binarySearch(array.rcmp(key), begin, end);
    }
    
    public static <T> int binarySearch(NativeArray array, T key, Comparator<? super T> comp) {
        return binarySearch(array.rcmp(key, comp), 0, array.size());
    }
    
    public static <T> int binarySearch(NativeArray array, int begin, int end, T key, Comparator<? super T> comp) {
        return binarySearch(array.rcmp(key, comp), begin, end);
    }
    
    private static int binarySearch(NativeComparator comp, int begin, int end) {
        end--;
        while (begin <= end) {
            int mid = (begin + end) >>> 1;
            int cmp = comp.compare(mid);
            if (cmp < 0) {
                begin = mid + 1;
            } else if (cmp > 0) {
                end = mid - 1;
            } else {
                return mid; // key found
            }
        }
        return -(begin + 1);
    }
    
}
