@Immutable public abstract class SortedKeyIndex extends java.lang.Object implements KeyIndex, java.io.Serializable
Implement an index of long keys, sorted by key. Keys can be mapped back to integer indexes (contiguous and 0-based) and vice versa.
Modifier and Type | Method and Description |
---|---|
boolean |
containsKey(long key)
Query whether this set contains the specified key in its domain.
|
static SortedKeyIndex |
create(long... keys)
Create a key set with some keys.
|
static SortedKeyIndex |
empty()
Create an empty key domain.
|
int |
findLowerBound(long key)
Get the lower bound, the first index whose key is greater than or equal to the specified key.
|
int |
findUpperBound(long key)
Get the upper bound, the first index whose key is greater than the specified key.
|
static SortedKeyIndex |
fromCollection(java.util.Collection<java.lang.Long> keys)
Create a key set from a collection of keys.
|
SortedKeyIndex |
frozenCopy()
Get a frozen copy of this key index.
|
int |
getIndex(long key)
Get the index of a key.
|
abstract long |
getKey(int idx)
Get the key at an index.
|
LongList |
getKeyList()
Get the key set’s list of keys (domain) as a list.
|
int |
getLowerBound()
Get the lower bound of this index.
|
int |
getUpperBound()
Get the upper bound of this index.
|
LongBidirectionalIterator |
keyIterator()
Create an iterator over keys.
|
LongBidirectionalIterator |
keyIterator(int initial)
Create an iterator over keys.
|
LongSortedArraySet |
keySet()
Get a view of the index as a set.
|
int |
size()
Get the domain size of this set.
|
abstract SortedKeyIndex |
subIndex(int lb,
int ub)
Create a view of a subset of this index.
|
abstract int |
tryGetIndex(long key)
Get the index for a key.
|
static SortedKeyIndex |
wrap(long[] keys,
int size)
Wrap a key array (with a specified size) into a key set.
|
public static SortedKeyIndex wrap(long[] keys, int size)
Wrap a key array (with a specified size) into a key set.
keys
- The key array. This array must be sorted, and must not contain duplicates. For efficiency, this condition is not checked unless assertions are enabled. Since this method is only intended to be used when implementing test cases or other data structures, callers of this method should ensure sortedness and throw the appropriate exception.size
- The length of the array to actually use.public static SortedKeyIndex fromCollection(java.util.Collection<java.lang.Long> keys)
Create a key set from a collection of keys.
keys
- The key collection.public static SortedKeyIndex create(long... keys)
Create a key set with some keys. All keys are initially active.
keys
- The keys.public static SortedKeyIndex empty()
Create an empty key domain.
public int size()
Get the domain size of this set.
public int getLowerBound()
Get the lower bound of this index.
getLowerBound
in interface KeyIndex
public int getUpperBound()
Get the upper bound of this index.
getUpperBound
in interface KeyIndex
public abstract SortedKeyIndex subIndex(int lb, int ub)
Create a view of a subset of this index.
lb
- The index of the lower bound of the subset (inclusive).ub
- The index of the upper bound of the subset (exclusive);public abstract int tryGetIndex(long key)
Get the index for a key.
tryGetIndex
in interface KeyIndex
key
- The key.Arrays.binarySearch(long[], int, int, long)
.public int getIndex(long key)
KeyIndex
Get the index of a key.
public int findUpperBound(long key)
Get the upper bound, the first index whose key is greater than the specified key.
key
- The key to search for.getUpperBound()
if the key is the last key in the domain.public int findLowerBound(long key)
Get the lower bound, the first index whose key is greater than or equal to the specified key. This method is paired with findUpperBound(long)
; the interval [findLowerBound(k),findUpperBound(k))
contains the index of k
, if the key is in the domain, and is empty if the key is not in the domain.
key
- The key to search for.key
; will be getLowerBound()
if key
is less than or equal to the lowest key.public boolean containsKey(long key)
Query whether this set contains the specified key in its domain.
containsKey
in interface KeyIndex
key
- The key.true
if the key is in the domain.public abstract long getKey(int idx)
Get the key at an index.
public LongBidirectionalIterator keyIterator()
Create an iterator over keys.
public LongBidirectionalIterator keyIterator(int initial)
Create an iterator over keys.
initial
- The initial index (the first thing to be returned by Iterator.next()
).public LongSortedArraySet keySet()
Get a view of the index as a set.
public LongList getKeyList()
Get the key set’s list of keys (domain) as a list.
getKeyList
in interface KeyIndex
public SortedKeyIndex frozenCopy()
KeyIndex
Get a frozen copy of this key index. If the key index is mutable, then this method will return an immutable copy of it. If the key index is already immutable, it may just return itself without copying.
frozenCopy
in interface KeyIndex