Package org.threadly.util
Class SortUtils
- java.lang.Object
-
- org.threadly.util.SortUtils
-
public class SortUtils extends java.lang.Object
A collection of utilities for searching and sorting against collections and other data providers.- Since:
- 5.0
-
-
Constructor Summary
Constructors Constructor Description SortUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
binarySearch(java.util.function.Function<java.lang.Integer,java.lang.Long> valueProvider, int absoluteMax, long insertionValue, boolean randomAccessList)
A faster binary search algorithm for sorting a list.static int
binarySearch(java.util.List<java.lang.Long> values, long insertionValue, boolean randomAccessList)
A faster binary search algorithm for sorting a list.static int
getInsertionEndIndex(java.util.function.Function<java.lang.Integer,java.lang.Long> valueProvider, int absoluteMax, long insertionValue, boolean randomAccessList)
This function uses the binary search and adds a small amount of logic such that it determines the placement index for a given value.static int
getInsertionEndIndex(java.util.List<java.lang.Long> values, long insertionValue, boolean randomAccessList)
This function uses the binary search and adds a small amount of logic such that it determines the placement index for a given item.
-
-
-
Method Detail
-
getInsertionEndIndex
public static int getInsertionEndIndex(java.util.List<java.lang.Long> values, long insertionValue, boolean randomAccessList)
This function uses the binary search and adds a small amount of logic such that it determines the placement index for a given item. It is designed to always return the index after any values which equal the provided insertion value.- Parameters:
values
- List of values to search over and find desired valueinsertionValue
- value in relation to functions provided values to search for insertion pointrandomAccessList
- boolean for optimization with binary search- Returns:
- the index to insert the key into the list
-
getInsertionEndIndex
public static int getInsertionEndIndex(java.util.function.Function<java.lang.Integer,java.lang.Long> valueProvider, int absoluteMax, long insertionValue, boolean randomAccessList)
This function uses the binary search and adds a small amount of logic such that it determines the placement index for a given value. It is designed to always return the index after any values which equal the provided insertion value.- Parameters:
valueProvider
- Function which will provide values for requested indexesabsoluteMax
- maximum index (inclusive) to search withininsertionValue
- value in relation to functions provided values to search for insertion pointrandomAccessList
- boolean for optimization with binary search- Returns:
- the index to insert the key into the list
-
binarySearch
public static int binarySearch(java.util.List<java.lang.Long> values, long insertionValue, boolean randomAccessList)
A faster binary search algorithm for sorting a list. This algorithm works by actually knowing the values and making smart decisions about how far to jump in the list based on those values. Which is why this can not take in a comparable interface like Collections does. This was adapted from code posted from this blog post: http://ochafik.com/blog/?p=106- Parameters:
values
- List of values to search over and find desired valueinsertionValue
- value in relation to functions provided values to search for insertion pointrandomAccessList
-true
to optimize for list that have cheap random access- Returns:
- index where found, or -(insertion point) - 1 if not found
-
binarySearch
public static int binarySearch(java.util.function.Function<java.lang.Integer,java.lang.Long> valueProvider, int absoluteMax, long insertionValue, boolean randomAccessList)
A faster binary search algorithm for sorting a list. This algorithm works by actually knowing the values and making smart decisions about how far to jump in the list based on those values. Which is why this can not take in a comparable interface like Collections does. This was adapted from code posted from this blog post: http://ochafik.com/blog/?p=106- Parameters:
valueProvider
- Function which will provide values for requested indexesabsoluteMax
- maximum index (inclusive) to search withininsertionValue
- value in relation to functions provided values to search for insertion pointrandomAccessList
-true
to optimize for list that have cheap random access- Returns:
- index where found, or -(insertion point) - 1 if not found
-
-