Package icyllis.modernui.util
Class AlgorithmUtils
java.lang.Object
icyllis.modernui.util.AlgorithmUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic double
averageStable
(double[] a) Calculate arithmetic mean without intermediate overflow or underflow.static double
averageStable
(double[] a, int start, int limit) Calculate arithmetic mean without intermediate overflow or underflow.static int
ceil
(int[] a, int value) Returns an index of the first element in the range[0, a.length)
such thata[index] >= value
, ora.length
if no such element is found.static int
ceil
(int[] a, int first, int last, int value) Returns an index of the first element in the range[first, last)
such thata[index] >= value
, orlast
if no such element is found.static int
ceil
(long[] a, int first, int last, long value) Returns an index of the first element in the range[first, last)
such thata[index] >= value
, orlast
if no such element is found.static int
ceil
(long[] a, long value) Returns an index of the first element in the range[0, a.length)
such thata[index] >= value
, ora.length
if no such element is found.static <T> int
ceil
(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
, orlast
if no such element is found.static <T> int
ceil
(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.length
if no such element is found.static int
floor
(int[] a, int value) Returns an index of the last element in the range[0, a.length)
such thata[index] <= value
, or-1
if no such element is found.static int
floor
(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 - 1
if no such element is found.static int
floor
(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 - 1
if no such element is found.static int
floor
(long[] a, long value) Returns an index of the last element in the range[0, a.length)
such thata[index] <= value
, or-1
if no such element is found.static <T> int
floor
(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 - 1
if no such element is found.static <T> int
floor
(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-1
if no such element is found.static int
gcd
(int a, int b) Returns the greatest common divisor ofa, b
.static long
gcd
(long a, long b) Returns the greatest common divisor ofa, b
.static int
higher
(int[] a, int value) Returns an index of the first element in the range[0, a.length)
such thata[index] > value
, ora.length
if no such element is found.static int
higher
(int[] a, int first, int last, int value) Returns an index of the first element in the range[first, last)
such thata[index] > value
, orlast
if no such element is found.static int
higher
(long[] a, int first, int last, long value) Returns an index of the first element in the range[first, last)
such thata[index] > value
, orlast
if no such element is found.static int
higher
(long[] a, long value) Returns an index of the first element in the range[0, a.length)
such thata[index] > value
, ora.length
if no such element is found.static <T> int
higher
(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
, orlast
if no such element is found.static <T> int
higher
(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.length
if no such element is found.static int
lengthOfLIS
(int[] a, int n) Returns the length of the longest increasing subsequence (non-strictly).static int
lengthOfLIS
(int[] a, int n, boolean strict) Returns the length of the longest increasing subsequence.static int
lengthOfLIS
(long[] a, int n) Returns the length of the longest increasing subsequence (non-strictly).static int
lengthOfLIS
(long[] a, int n, boolean strict) Returns the length of the longest increasing subsequence.static int
lower
(int[] a, int value) Returns an index of the last element in the range[0, a.length)
such thata[index] < value
, or-1
if no such element is found.static int
lower
(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 - 1
if no such element is found.static int
lower
(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 - 1
if no such element is found.static int
lower
(long[] a, long value) Returns an index of the last element in the range[0, a.length)
such thata[index] < value
, or-1
if no such element is found.static <T> int
lower
(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 - 1
if no such element is found.static <T> int
lower
(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-1
if no such element is found.static int
quickModPow
(int a, int b, int m) Returnsa^b mod m
, no overflow checks.static long
quickModPow
(long a, long b, int m) Returnsa^b mod m
, no overflow checks.static int
quickPow
(int a, int b) Returnsa^b
, no overflow checks.static long
quickPow
(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-1
if 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 - 1
if 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-1
if 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 - 1
if 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-1
if 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. Anull
value 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 - 1
if 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. Anull
value 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-1
if 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 - 1
if 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-1
if 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 - 1
if 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-1
if 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. Anull
value 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 - 1
if 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. Anull
value 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.length
if 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
, orlast
if 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.length
if 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
, orlast
if 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.length
if 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. Anull
value 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
, orlast
if 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. Anull
value 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.length
if 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
, orlast
if 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.length
if 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
, orlast
if 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.length
if 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. Anull
value 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
, orlast
if 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. Anull
value 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.
-