Package icyllis.modernui.util
Class AlgorithmUtils
java.lang.Object
icyllis.modernui.util.AlgorithmUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic doubleaverageStable(double[] a) Calculate arithmetic mean without intermediate overflow or underflow.static doubleaverageStable(double[] a, int start, int limit) Calculate arithmetic mean without intermediate overflow or underflow.static intceil(int[] a, int value) Returns an index of the first element in the range[0, a.length)such thata[index] >= value, ora.lengthif no such element is found.static intceil(int[] a, int first, int last, int value) Returns an index of the first element in the range[first, last)such thata[index] >= value, orlastif no such element is found.static intceil(long[] a, int first, int last, long value) Returns an index of the first element in the range[first, last)such thata[index] >= value, orlastif no such element is found.static intceil(long[] a, long value) Returns an index of the first element in the range[0, a.length)such thata[index] >= value, ora.lengthif no such element is found.static <T> intceil(T[] a, int first, int last, T value, Comparator<? super T> c) Returns an index of the first element in the range[first, last)such thata[index] >= value, orlastif no such element is found.static <T> intceil(T[] a, T value, Comparator<? super T> c) Returns an index of the first element in the range[0, a.length)such thata[index] >= value, ora.lengthif no such element is found.static intfloor(int[] a, int value) Returns an index of the last element in the range[0, a.length)such thata[index] <= value, or-1if no such element is found.static intfloor(int[] a, int first, int last, int value) Returns an index of the last element in the range[first, last)such thata[index] <= value, orfirst - 1if no such element is found.static intfloor(long[] a, int first, int last, long value) Returns an index of the last element in the range[first, last)such thata[index] <= value, orfirst - 1if no such element is found.static intfloor(long[] a, long value) Returns an index of the last element in the range[0, a.length)such thata[index] <= value, or-1if no such element is found.static <T> intfloor(T[] a, int first, int last, T value, Comparator<? super T> c) Returns an index of the last element in the range[first, last)such thata[index] <= value, orfirst - 1if no such element is found.static <T> intfloor(T[] a, T value, Comparator<? super T> c) Returns an index of the last element in the range[0, a.length)such thata[index] <= value, or-1if no such element is found.static intgcd(int a, int b) Returns the greatest common divisor ofa, b.static longgcd(long a, long b) Returns the greatest common divisor ofa, b.static inthigher(int[] a, int value) Returns an index of the first element in the range[0, a.length)such thata[index] > value, ora.lengthif no such element is found.static inthigher(int[] a, int first, int last, int value) Returns an index of the first element in the range[first, last)such thata[index] > value, orlastif no such element is found.static inthigher(long[] a, int first, int last, long value) Returns an index of the first element in the range[first, last)such thata[index] > value, orlastif no such element is found.static inthigher(long[] a, long value) Returns an index of the first element in the range[0, a.length)such thata[index] > value, ora.lengthif no such element is found.static <T> inthigher(T[] a, int first, int last, T value, Comparator<? super T> c) Returns an index of the first element in the range[first, last)such thata[index] > value, orlastif no such element is found.static <T> inthigher(T[] a, T value, Comparator<? super T> c) Returns an index of the first element in the range[0, a.length)such thata[index] > value, ora.lengthif no such element is found.static intlengthOfLIS(int[] a, int n) Returns the length of the longest increasing subsequence (non-strictly).static intlengthOfLIS(int[] a, int n, boolean strict) Returns the length of the longest increasing subsequence.static intlengthOfLIS(long[] a, int n) Returns the length of the longest increasing subsequence (non-strictly).static intlengthOfLIS(long[] a, int n, boolean strict) Returns the length of the longest increasing subsequence.static intlower(int[] a, int value) Returns an index of the last element in the range[0, a.length)such thata[index] < value, or-1if no such element is found.static intlower(int[] a, int first, int last, int value) Returns an index of the last element in the range[0, a.length)such thata[index] < value, orfirst - 1if no such element is found.static intlower(long[] a, int first, int last, long value) Returns an index of the last element in the range[0, a.length)such thata[index] < value, orfirst - 1if no such element is found.static intlower(long[] a, long value) Returns an index of the last element in the range[0, a.length)such thata[index] < value, or-1if no such element is found.static <T> intlower(T[] a, int first, int last, T value, Comparator<? super T> c) Returns an index of the last element in the range[0, a.length)such thata[index] < value, orfirst - 1if no such element is found.static <T> intlower(T[] a, T value, Comparator<? super T> c) Returns an index of the last element in the range[0, a.length)such thata[index] < value, or-1if no such element is found.static intquickModPow(int a, int b, int m) Returnsa^b mod m, no overflow checks.static longquickModPow(long a, long b, int m) Returnsa^b mod m, no overflow checks.static intquickPow(int a, int b) Returnsa^b, no overflow checks.static longquickPow(long a, long b) Returnsa^b, no overflow checks.
-
Method Details
-
gcd
public static int gcd(int a, int b) Returns the greatest common divisor ofa, b. Assertsa >= 0 && b >= 0.- See Also:
-
gcd
public static long gcd(long a, long b) Returns the greatest common divisor ofa, b. Assertsa >= 0 && b >= 0.- See Also:
-
quickPow
public static int quickPow(int a, int b) Returnsa^b, no overflow checks. Assertsb >= 0.- Parameters:
a- the baseb- the exponent
-
quickPow
public static long quickPow(long a, long b) Returnsa^b, no overflow checks. Assertsb >= 0.- Parameters:
a- the baseb- the exponent
-
quickModPow
public static int quickModPow(int a, int b, int m) Returnsa^b mod m, no overflow checks. Assertsb >= 0 && m > 0. -
quickModPow
public static long quickModPow(long a, long b, int m) Returnsa^b mod m, no overflow checks. Assertsb >= 0 && m > 0. -
lower
Returns an index of the last element in the range[0, a.length)such thata[index] < value, or-1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
-1 - See Also:
-
lower
Returns an index of the last element in the range[0, a.length)such thata[index] < value, orfirst - 1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
first - 1 - See Also:
-
lower
Returns an index of the last element in the range[0, a.length)such thata[index] < value, or-1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
-1 - See Also:
-
lower
Returns an index of the last element in the range[0, a.length)such thata[index] < value, orfirst - 1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
first - 1 - See Also:
-
lower
Returns an index of the last element in the range[0, a.length)such thata[index] < value, or-1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedvalue- the value to be searched forc- the comparator by which the array is ordered. Anullvalue indicates that the elements' natural ordering should be used.- Returns:
- index of the search value, or
-1 - See Also:
-
lower
public static <T> int lower(@NonNull T[] a, int first, int last, T value, @Nullable Comparator<? super T> c) Returns an index of the last element in the range[0, a.length)such thata[index] < value, orfirst - 1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched forc- the comparator by which the array is ordered. Anullvalue indicates that the elements' natural ordering should be used.- Returns:
- index of the search value, or
first - 1 - See Also:
-
floor
Returns an index of the last element in the range[0, a.length)such thata[index] <= value, or-1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
-1 - See Also:
-
floor
Returns an index of the last element in the range[first, last)such thata[index] <= value, orfirst - 1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
first - 1 - See Also:
-
floor
Returns an index of the last element in the range[0, a.length)such thata[index] <= value, or-1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
-1 - See Also:
-
floor
Returns an index of the last element in the range[first, last)such thata[index] <= value, orfirst - 1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
first - 1 - See Also:
-
floor
Returns an index of the last element in the range[0, a.length)such thata[index] <= value, or-1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedvalue- the value to be searched forc- the comparator by which the array is ordered. Anullvalue indicates that the elements' natural ordering should be used.- Returns:
- index of the search value, or
-1 - See Also:
-
floor
public static <T> int floor(@NonNull T[] a, int first, int last, T value, @Nullable Comparator<? super T> c) Returns an index of the last element in the range[first, last)such thata[index] <= value, orfirst - 1if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched forc- the comparator by which the array is ordered. Anullvalue indicates that the elements' natural ordering should be used.- Returns:
- index of the search value, or
first - 1 - See Also:
-
ceil
Returns an index of the first element in the range[0, a.length)such thata[index] >= value, ora.lengthif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
lower_bound.- Parameters:
a- the array to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
a.length - See Also:
-
ceil
Returns an index of the first element in the range[first, last)such thata[index] >= value, orlastif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
lower_bound.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
last - See Also:
-
ceil
Returns an index of the first element in the range[0, a.length)such thata[index] >= value, ora.lengthif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
lower_bound.- Parameters:
a- the array to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
a.length - See Also:
-
ceil
Returns an index of the first element in the range[first, last)such thata[index] >= value, orlastif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
lower_bound.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
last - See Also:
-
ceil
Returns an index of the first element in the range[0, a.length)such thata[index] >= value, ora.lengthif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
lower_bound.- Parameters:
a- the array to be searchedvalue- the value to be searched forc- the comparator by which the array is ordered. Anullvalue indicates that the elements' natural ordering should be used.- Returns:
- index of the search value, or
a.length - See Also:
-
ceil
public static <T> int ceil(@NonNull T[] a, int first, int last, T value, @Nullable Comparator<? super T> c) Returns an index of the first element in the range[first, last)such thata[index] >= value, orlastif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
lower_bound.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched forc- the comparator by which the array is ordered. Anullvalue indicates that the elements' natural ordering should be used.- Returns:
- index of the search value, or
last - See Also:
-
higher
Returns an index of the first element in the range[0, a.length)such thata[index] > value, ora.lengthif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
upper_bound.- Parameters:
a- the array to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
a.length - See Also:
-
higher
Returns an index of the first element in the range[first, last)such thata[index] > value, orlastif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
upper_bound.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
last - See Also:
-
higher
Returns an index of the first element in the range[0, a.length)such thata[index] > value, ora.lengthif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
upper_bound.- Parameters:
a- the array to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
a.length - See Also:
-
higher
Returns an index of the first element in the range[first, last)such thata[index] > value, orlastif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
upper_bound.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched for- Returns:
- index of the search value, or
last - See Also:
-
higher
Returns an index of the first element in the range[0, a.length)such thata[index] > value, ora.lengthif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
upper_bound.- Parameters:
a- the array to be searchedvalue- the value to be searched forc- the comparator by which the array is ordered. Anullvalue indicates that the elements' natural ordering should be used.- Returns:
- index of the search value, or
a.length - See Also:
-
higher
public static <T> int higher(@NonNull T[] a, int first, int last, T value, @Nullable Comparator<? super T> c) Returns an index of the first element in the range[first, last)such thata[index] > value, orlastif no such element is found. The elements in the range shall already be sorted or at least partitioned with respect tovalue.Equivalent to C++
upper_bound.- Parameters:
a- the array to be searchedfirst- the index of the first element (inclusive) to be searchedlast- the index of the last element (exclusive) to be searchedvalue- the value to be searched forc- the comparator by which the array is ordered. Anullvalue indicates that the elements' natural ordering should be used.- Returns:
- index of the search value, or
last - See Also:
-
lengthOfLIS
Returns the length of the longest increasing subsequence (non-strictly). This algorithm has a time complexity of O(n log(n)). -
lengthOfLIS
Returns the length of the longest increasing subsequence. This algorithm has a time complexity of O(n log(n)).- Parameters:
strict- strictly increasing or not
-
lengthOfLIS
Returns the length of the longest increasing subsequence (non-strictly). This algorithm has a time complexity of O(n log(n)). -
lengthOfLIS
Returns the length of the longest increasing subsequence. This algorithm has a time complexity of O(n log(n)).- Parameters:
strict- strictly increasing or not
-
averageStable
Calculate arithmetic mean without intermediate overflow or underflow. -
averageStable
Calculate arithmetic mean without intermediate overflow or underflow.
-