@ParametersAreNonnullByDefault public final class LongUtils extends Object
Utilities for working with longs and collections of them from Fastutil.
Modifier and Type | Method and Description |
---|---|
static Long2DoubleFunction |
asLong2DoubleFunction(Map<Long,Double> map)
Create a long-to-double function from a map, casting if appropriate.
|
static LongCollection |
asLongCollection(Collection<Long> longs)
Get a Fastutil
LongCollection from a Collection of longs. |
static LongList |
asLongList(List<Long> longs) |
static LongSet |
asLongSet(Collection<Long> longs)
Get a Fastutil
LongSet from a Collection of longs. |
static LongSet |
asLongSet(Set<Long> longs)
|
static Long2DoubleMap |
constantDoubleMap(Set<Long> keys,
double value)
Create a map that maps a group of items to the same value.
|
static Long2DoubleSortedMap |
frozenMap(Map<Long,Double> map)
Create a frozen long-to-double map.
|
static LongSortedSet |
frozenSet(Collection<Long> longs)
Create a frozen long set.
|
static boolean |
hasNCommonItems(LongSortedSet a,
LongSortedSet b,
int n)
Check if two sets have at least a given number of common items.
|
static int |
intersectSize(LongSortedSet a,
LongSortedSet b)
Compute the size of the intersection of two sets.
|
static Long2IntMap |
itemRanks(LongList results)
Compute the ranks for a list of longs.
|
static LongComparator |
keyValueComparator(Long2DoubleFunction vals)
Create a comparator that compares long keys by associated double values.
|
static LongSortedSet |
packedSet(Collection<Long> longs)
Pack longs into a sorted set.
|
static LongSortedSet |
packedSet(long... longs)
Pack longs into a sorted set.
|
static LongSortedSet |
randomSubset(LongSet set,
int num,
LongSet exclude,
Random rng)
Selects a random subset of
n longs from a given set of longs such that no selected items is in a second set of longs. |
static LongSet |
randomSubset(LongSet set,
int num,
Random random)
Selects a random subset of
n longs from a given set of longs. |
static LongSortedSet |
setDifference(LongSet items,
LongSet exclude)
Compute the set difference of two sets.
|
static LongSortedSet |
setIntersect(LongSet a,
LongSet b)
Compute the intersection of two sets.
|
static LongSortedSet |
setIntersect(LongSortedSet a,
LongSortedSet b)
Compute the intersection of two sets.
|
static LongSortedSet |
setUnion(LongSet a,
LongSet b)
Compute the union of two sets.
|
static LongSortedSet |
setUnion(LongSortedSet a,
LongSortedSet b)
Compute the union of two sets.
|
static int |
unionSize(LongSortedSet a,
LongSortedSet b)
Compute the size of the union of two sets.
|
public static Long2DoubleSortedMap frozenMap(Map<Long,Double> map)
Create a frozen long-to-double map. This effectively creates a copy of a map, but if the provided map is an instance of Long2DoubleSortedArrayMap
, which is immutable, it is returned as-is for efficiency.
map
- The source map.map
.public static LongSortedSet frozenSet(Collection<Long> longs)
Create a frozen long set. If the underlying collection is already an immutable sorted set (specifically, a LongSortedArraySet
, it is used as-is. Otherwise, it is copied into a sorted array set.
This is equivalent to packedSet(Collection)
.
longs
- The collection.packedSet(Collection)
public static LongSortedSet packedSet(Collection<Long> longs)
Pack longs into a sorted set.
longs
- A collection of longs.longs
.public static LongSortedSet packedSet(long... longs)
Pack longs into a sorted set.
longs
- An array of longs. This array is copied, not wrapped.longs
.public static LongComparator keyValueComparator(Long2DoubleFunction vals)
Create a comparator that compares long keys by associated double values.
vals
- The value map.public static LongCollection asLongCollection(Collection<Long> longs)
Get a Fastutil LongCollection
from a Collection
of longs. This method simply casts the collection, if possible, and returns a wrapper otherwise.
longs
- A collection of longs.LongCollection
.public static Long2DoubleFunction asLong2DoubleFunction(Map<Long,Double> map)
Create a long-to-double function from a map, casting if appropriate. Useful to allow unboxed access to maps that are really fastutil maps.
map
- The map.map
, or map
if it is a Fastutil map.public static Long2DoubleMap constantDoubleMap(Set<Long> keys, double value)
Create a map that maps a group of items to the same value.
keys
- The keys.value
- The value.keys
, mapping each of them to value
.public static LongSet asLongSet(@Nullable Set<Long> longs)
longs
- The set of longs.longs
as a fastutil LongSet
. If longs
is already a LongSet, it is cast.public static LongSet asLongSet(@Nullable Collection<Long> longs)
Get a Fastutil LongSet
from a Collection
of longs.
longs
- The set of longs.longs
as a fastutil LongSet
. If longs
is already a LongSet, it is cast.public static Long2IntMap itemRanks(LongList results)
Compute the ranks for a list of longs.
results
- The list of longs.public static LongSortedSet setDifference(LongSet items, LongSet exclude)
Compute the set difference of two sets.
items
- The initial setexclude
- The items to removepublic static int intersectSize(LongSortedSet a, LongSortedSet b)
Compute the size of the intersection of two sets.
a
- The first set.b
- The second set.public static boolean hasNCommonItems(LongSortedSet a, LongSortedSet b, int n)
Check if two sets have at least a given number of common items.
a
- The first set.b
- The second set.n
- The number of common items to require.true
if the two sets have at least n
common items.public static int unionSize(LongSortedSet a, LongSortedSet b)
Compute the size of the union of two sets.
a
- The first set.b
- The second set.public static LongSortedSet setUnion(LongSet a, LongSet b)
Compute the union of two sets.
a
- The first set.b
- The second set.public static LongSortedSet setUnion(LongSortedSet a, LongSortedSet b)
Compute the union of two sets.
a
- The first set.b
- The second set.public static LongSortedSet setIntersect(LongSet a, LongSet b)
Compute the intersection of two sets.
a
- The first set.b
- The second set.public static LongSortedSet setIntersect(LongSortedSet a, LongSortedSet b)
Compute the intersection of two sets.
a
- The first set.b
- The second set.public static LongSet randomSubset(LongSet set, int num, Random random)
Selects a random subset of n
longs from a given set of longs. If fewer than n
items can be selected the whole set is returned.
set
- the set of items to select fromnum
- The number of random items to add.random
- a random number generator to be used.base
plus an additional nRandom
items.public static LongSortedSet randomSubset(LongSet set, int num, LongSet exclude, Random rng)
Selects a random subset of n
longs from a given set of longs such that no selected items is in a second set of longs. If fewer than n
items can be selected the whole set is returned.
set
- the set of items to select fromnum
- The number of random items to add.exclude
- a set of longs which must not be returnedrng
- a random number generator to be used.base
plus an additional nRandom
items.