public class PackedIntegerArrayList extends java.lang.Object
minValue
.
All elements are stored in an array of long values. If the number of bits in this array is not sufficient anymore to take up the values of this list, a new array is created automatically. The initial number of elements (not equal to the number of longs in the underlying array object) the list can take up before creating a new array object can be specified in the constructor.
This class can be used to store large lists of integer values in a specific range in a memory efficient way.
Modifier and Type | Field and Description |
---|---|
protected long[] |
array
The array containing the bits used to store values contained in this list.
|
static int |
BLOCK_BITS |
static int |
BLOCK_SIZE |
static int |
MAX_ARRAY_LENGTH |
static int |
MOD_MASK |
Constructor and Description |
---|
PackedIntegerArrayList(int bitsPerValue,
long minValue,
long initialCapacity)
Creates a new instance of this class.
|
Modifier and Type | Method and Description |
---|---|
void |
add(long value)
Adds the specified element to the end of this list.
|
void |
add(long index,
long value)
Adds the specified element to this list and moves other elements to the right if necessary.
|
protected int |
calculateArrayLength(long capacity)
Calculates the number of
long array elements necessary to store the specified
number of elements. |
static int |
calculateBitsPerValue(int elementCount)
Calculates how many bits are necessary to represent the specified number of different values.
|
void |
ensureCapacity(long newCapacity)
Makes sure that the underlying array can take up at least the specified number of elements.
|
long |
get(long index)
Returns the element stored at the specified index.
|
long |
getBitsPerValue()
Returns the length in bits of an entry in this list.
|
long |
getMaxValue()
Returns the maximum value that can be represented by an element of this list.
|
long |
getMinValue()
Returns the minimum value that can be represented by an element of this list.
|
protected void |
insertRange(long index,
long length)
Reserves space for the specified number of elements at the specified position.
|
void |
remove(long index)
Removes the element at the specified position from this list.
|
protected void |
removeRange(long index,
long length)
Removes the specified elements from this list.
|
void |
set(long index,
long value)
Replaces the value at the specified position with the new value.
|
long |
size()
Returns the number of elements currently contained in this list.
|
public static final int MAX_ARRAY_LENGTH
public static final int BLOCK_SIZE
public static final int BLOCK_BITS
public static final int MOD_MASK
protected long[] array
public PackedIntegerArrayList(int bitsPerValue, long minValue, long initialCapacity)
bitsPerValue
- the number of bit each value will consume in memoryminValue
- the minimum value to be stored in the listinitialCapacity
- the number of values the underlying array can take up initiallycalculateBitsPerValue(int)
public static int calculateBitsPerValue(int elementCount)
elementCount
- the number of elements to be representedelementCount
different values/objectspublic long getMinValue()
public long getMaxValue()
getMinValue()
and the specified number of
bits per valuepublic long getBitsPerValue()
protected int calculateArrayLength(long capacity)
long
array elements necessary to store the specified
number of elements.capacity
- the number of list elements to be storedpublic void ensureCapacity(long newCapacity)
newCapacity
- the number of elements that need to be stored in the arrayprotected void insertRange(long index, long length)
index
- the index of the first element to be insertedlength
- the number of elements to be insertedprotected void removeRange(long index, long length)
index
- the index of the first element to be removedlength
- the number of elements to be removedjava.lang.IllegalArgumentException
- if the specified range is outside the current size of the listpublic void add(long index, long value)
index
- the index where the new element shall be insertedvalue
- the value to be insertedjava.lang.IndexOutOfBoundsException
- if index
is lower than 0 or greater than the current size of
the listjava.lang.IllegalArgumentException
- if value
does not fit into the range specified by the constructor
parameters bitsPerValue
and minValue
public void add(long value)
value
- the value to be appendedjava.lang.IllegalArgumentException
- if value
does not fit into the range specified by the constructor
parameters bitsPerValue
and minValue
public long get(long index)
index
- the index of the element to be readjava.lang.IndexOutOfBoundsException
- if index
is lower than 0 or greater or equal to the current size
of the listpublic void remove(long index)
index
- the index of the element to be removedjava.lang.IndexOutOfBoundsException
- if index
is lower than 0 or greater or equal to the current size
of the listpublic void set(long index, long value)
index
- the index of the value to be replacedvalue
- the new value for the specified positionjava.lang.IndexOutOfBoundsException
- if index
is lower than 0 or greater or equal to the current size
of the listjava.lang.IllegalArgumentException
- if value
does not fit into the range specified by the constructor
parameters bitsPerValue
and minValue
public long size()
long
array.