public class MinMaxNumberList extends AbstractLongList
Numbers can be compressed when minimum and maximum of all values ever to be stored in the list are known. For example, if min=16, max=27, only 4 bits are needed to store a value. No compression is achieved for float and double values.
You can add, get and set elements quite similar to java.util.ArrayList.
Applicability: Applicable if the data is non floating point, highly skewed without "outliers" and minimum and maximum known in advance.
Performance: Basic operations like add(), get(), set(), size() and clear() are O(1), i.e. run in constant time.
This class can, for example, be useful when making large lists of numbers persistent. Also useful when very large lists would otherwise consume too much main memory.
Upon instantiation a contract is signed that defines the interval values may fall into. It is not legal to store values not contained in that interval. WARNING: The contract is not checked. Be sure you do not store illegal values. If you need to store float or double values, you must set the minimum and maximum to [Integer.MIN_VALUE,Integer.MAX_VALUE] or [Long.MIN_VALUE,Long.MAX_VALUE], respectively.
Although access methods are only defined on long values you can also store all other primitive data types: boolean, byte, short, int, long, float, double and char. You can do this by explicitly representing them as long values. Use casts for discrete data types. Use the methods of java.lang.Float and java.lang.Double for floating point data types: Recall that with those methods you can convert any floating point value to a long value and back without losing any precision:
Example usage:
MinMaxNumberList list = ... instantiation goes here double d1 = 1.234; list.add(Double.doubleToLongBits(d1)); double d2 = Double.longBitsToDouble(list.get(0)); if (d1!=d2) System.out.println("This is impossible!"); MinMaxNumberList list2 = ... instantiation goes here float f1 = 1.234f; list2.add((long) Float.floatToIntBits(f1)); float f2 = Float.intBitsToFloat((int)list2.get(0)); if (f1!=f2) System.out.println("This is impossible!");
LongArrayList
,
DistinctNumberList
,
Float
,
Double
,
Serialized FormConstructor and Description |
---|
MinMaxNumberList(long minimum,
long maximum,
int initialCapacity)
Constructs an empty list with the specified initial capacity and the
specified range of values allowed to be hold in this list.
|
Modifier and Type | Method and Description |
---|---|
void |
add(long element)
Appends the specified element to the end of this list.
|
void |
addAllOfFromTo(long[] elements,
int from,
int to)
Appends the elements elements[from] (inclusive), ...,
elements[to] (inclusive) to the receiver.
|
int |
bitsPerElement()
Returns the number of bits necessary to store a single element.
|
static int |
bitsPerElement(long minimum,
long maximum)
Returns the number of bits necessary to store values in the range
[minimum,maximum].
|
void |
ensureCapacity(int minCapacity)
Ensures that the receiver can hold at least the specified number of
elements without needing to allocate new internal memory.
|
long |
getQuick(int index)
Returns the element at the specified position in the receiver;
WARNING: Does not check preconditions.
|
void |
partFromTo(int from,
int to,
BitVector qualificants,
int qualificantsFrom,
long[] part,
int partFrom)
Copies all elements between index from (inclusive) and
to (inclusive) into part, starting at index
partFrom within part.
|
void |
setQuick(int index,
long element)
Replaces the element at the specified position in the receiver with the
specified element; WARNING: Does not check preconditions.
|
void |
setSizeRaw(int newSize)
Sets the size of the receiver without modifying it otherwise.
|
BitVector |
toBitVector()
Returns the receiver seen as bitvector.
|
void |
trimToSize()
Trims the capacity of the receiver to be the receiver's current size.
|
long |
xminimum()
Deprecated.
|
addAllOfFromTo, beforeInsert, beforeInsertAllOfFromTo, binarySearch, binarySearchFromTo, clone, contains, delete, elements, elements, equals, fillFromToWith, forEach, get, indexOf, indexOfFromTo, lastIndexOf, lastIndexOfFromTo, mergeSortFromTo, mergeSortFromTo, partFromTo, quickSortFromTo, quickSortFromTo, removeAll, removeFromTo, replaceFromToWithFrom, replaceFromToWithFromTo, replaceFromWith, retainAll, reverse, set, shuffleFromTo, size, times, toList, toString
addAllOf, beforeInsertAllOf, clear, mergeSort, quickSort, remove, setSize, shuffle, sort, sortFromTo
isEmpty
public MinMaxNumberList(long minimum, long maximum, int initialCapacity)
minimum
- the minimum of values allowed to be hold in this list.maximum
- the maximum of values allowed to be hold in this list.initialCapacity
- the number of elements the receiver can hold without
auto-expanding itself by allocating new internal memory.public void add(long element)
add
in class AbstractLongList
element
- element to be appended to this list.public void addAllOfFromTo(long[] elements, int from, int to)
elements
- the elements to be appended to the receiver.from
- the index of the first element to be appended (inclusive)to
- the index of the last element to be appended (inclusive)public int bitsPerElement()
public static int bitsPerElement(long minimum, long maximum)
public void ensureCapacity(int minCapacity)
ensureCapacity
in class AbstractLongList
minCapacity
- the desired minimum capacity.public long getQuick(int index)
index
- index of element to return.public void partFromTo(int from, int to, BitVector qualificants, int qualificantsFrom, long[] part, int partFrom)
for (; from <= to; from++, partFrom++, qualificantsFrom++) { if (qualificants == null || qualificants.get(qualificantsFrom)) { part[partFrom] = this.get(from); } }
public void setQuick(int index, long element)
index
- index of element to replace.element
- element to be stored at the specified position.public void setSizeRaw(int newSize)
setSizeRaw
in class AbstractLongList
public BitVector toBitVector()
public void trimToSize()
trimToSize
in class AbstractList
@Deprecated public long xminimum()
Jump to the Parallel Colt Homepage