Package org.apache.sis.util.collection
Class IntegerList
- All Implemented Interfaces:
Serializable
,Cloneable
,Iterable<Integer>
,Collection<Integer>
,List<Integer>
,RandomAccess
public class IntegerList
extends AbstractList<Integer>
implements RandomAccess, Serializable, Cloneable
A list of unsigned integer values. This class packs the values in the minimal amount of bits
required for storing unsigned integers of the given maximal value.
This class is not thread-safe. Synchronizations (if wanted) are user's responsibility.
- Since:
- 0.7
- Version:
- 1.1
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final class
Same asspliterator()
, but without value boxing. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private final int
The bit count for values.private final int
The mask computed as(1 << bitCount) - 1
.private static final int
private static final long
For cross-version compatibility.private int
The list size.private static final int
The size of the primitive type used for thevalues
array.private long[]
The packed values.Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorsConstructorDescriptionIntegerList
(int initialCapacity, int maximalValue) Creates an initially empty list with the given initial capacity.IntegerList
(int initialCapacity, int maximalValue, boolean fill) Creates a new list with the given initial size. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds the given element to this list.void
addInt
(int value) Adds the given element as theint
primitive type.void
clear()
Discards all elements in this list.clone()
Returns a clone of this list.boolean
Compares the content of this list with the given object.void
fill
(int value) Fills the list with the given value.get
(int index) Returns the element at the given index.int
getInt
(int index) Returns the element at the given index as theint
primitive type.private int
getUnchecked
(int index) Returns the element at the given index as theint
primitive type.iterator()
Returns an iterator over the elements in this list in increasing index order.private int
length
(int capacity) Returns the array length required for holding a list of the given size.int
Returns the maximal value that can be stored in this list.int
occurrence
(int value) Returns the occurrence of the given value in this list.remove
(int index) Removes the element at the given index.int
Retrieves and remove the last element of this list.protected void
removeRange
(int lower, int upper) Removes all values in the given range of index.void
resize
(int size) Sets the list size to the given value.Sets the element at the given index.void
setInt
(int index, int value) Sets the element at the given index as theint
primitive type.private void
setUnchecked
(int index, int value) Sets the element at the given index as theint
primitive type.int
size()
Returns the current number of values in this list.Returns an spliterator over the elements in this list in increasing index order.stream
(boolean parallel) Returns a stream of integers with thisIntegerList
as its source.void
Trims the capacity of this list to be its current size.private void
InvokestrimToSize()
before serialization in order to make the stream more compact.Methods inherited from class java.util.AbstractList
add, addAll, hashCode, indexOf, lastIndexOf, listIterator, listIterator, subList
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDFor cross-version compatibility.- See Also:
-
VALUE_SIZE
private static final int VALUE_SIZEThe size of the primitive type used for thevalues
array.- See Also:
-
BASE_SHIFT
private static final int BASE_SHIFTThe shift to apply onindex
in order to produce a result equivalent toindex
/ 64. The following relation must hold:(1 << BASE_SHIFT) == VALUE_SIZE
.- See Also:
-
OFFSET_MASK
private static final int OFFSET_MASK- See Also:
-
values
private long[] valuesThe packed values. We use thelong
type instead ofint
since 64 bits machines are common now. -
bitCount
private final int bitCountThe bit count for values. -
mask
private final int maskThe mask computed as(1 << bitCount) - 1
. -
size
private int sizeThe list size. Initially 0.- See Also:
-
-
Constructor Details
-
IntegerList
public IntegerList(int initialCapacity, int maximalValue) Creates an initially empty list with the given initial capacity.- Parameters:
initialCapacity
- the initial capacity.maximalValue
- the maximal value to be allowed, inclusive.
-
IntegerList
public IntegerList(int initialCapacity, int maximalValue, boolean fill) Creates a new list with the given initial size. The value of all elements are initialized to 0.- Parameters:
initialCapacity
- the initial capacity.maximalValue
- the maximal value to be allowed, inclusive.fill
- iftrue
, the initial size is set to the initial capacity with all values set to 0.
-
-
Method Details
-
length
private int length(int capacity) Returns the array length required for holding a list of the given size.- Parameters:
capacity
- the desired list size.- Returns:
- the array length for holding a list of the given size.
-
maximalValue
public int maximalValue()Returns the maximal value that can be stored in this list. May be slightly higher than the value given to the constructor.- Returns:
- the maximal value, inclusive.
-
size
public int size()Returns the current number of values in this list.- Specified by:
size
in interfaceCollection<Integer>
- Specified by:
size
in interfaceList<Integer>
- Specified by:
size
in classAbstractCollection<Integer>
- Returns:
- the number of values.
-
resize
public void resize(int size) Sets the list size to the given value. If the new size is lower than previous size, then the elements after the new size are discarded. If the new size is greater than the previous one, then the extra elements are initialized to 0.- Parameters:
size
- the new size.- See Also:
-
fill
public void fill(int value) Fills the list with the given value. Every existing values are overwritten from index 0 inclusive up tosize()
exclusive.- Parameters:
value
- the value to set.
-
clear
public void clear()Discards all elements in this list.- Specified by:
clear
in interfaceCollection<Integer>
- Specified by:
clear
in interfaceList<Integer>
- Overrides:
clear
in classAbstractList<Integer>
-
add
Adds the given element to this list.- Specified by:
add
in interfaceCollection<Integer>
- Specified by:
add
in interfaceList<Integer>
- Overrides:
add
in classAbstractList<Integer>
- Parameters:
value
- the value to add.- Returns:
- always
true
. - Throws:
NullPointerException
- if the given value is null.IllegalArgumentException
- if the given value is out of bounds.
-
addInt
Adds the given element as theint
primitive type.- Parameters:
value
- the value to add.- Throws:
IllegalArgumentException
- if the given value is out of bounds.- See Also:
-
get
Returns the element at the given index.- Specified by:
get
in interfaceList<Integer>
- Specified by:
get
in classAbstractList<Integer>
- Parameters:
index
- the element index.- Returns:
- the value at the given index.
- Throws:
IndexOutOfBoundsException
- if the given index is out of bounds.
-
getInt
Returns the element at the given index as theint
primitive type.- Parameters:
index
- the element index.- Returns:
- the value at the given index.
- Throws:
IndexOutOfBoundsException
- if the given index is out of bounds.
-
getUnchecked
private int getUnchecked(int index) Returns the element at the given index as theint
primitive type. This argument does not check argument validity, since the verification is assumed already done.- Parameters:
index
- the element index.- Returns:
- the value at the given index.
-
set
Sets the element at the given index.- Specified by:
set
in interfaceList<Integer>
- Overrides:
set
in classAbstractList<Integer>
- Parameters:
index
- the element index.value
- the value at the given index.- Returns:
- the previous value at the given index.
- Throws:
IndexOutOfBoundsException
- if the given index is out of bounds.IllegalArgumentException
- if the given value is out of bounds.NullPointerException
- if the given value is null.
-
setInt
Sets the element at the given index as theint
primitive type.- Parameters:
index
- the element index.value
- the value at the given index.- Throws:
IndexOutOfBoundsException
- if the given index is out of bounds.IllegalArgumentException
- if the given value is out of bounds.
-
setUnchecked
private void setUnchecked(int index, int value) Sets the element at the given index as theint
primitive type. This argument does not check argument validity, since the verification is assumed already done.- Parameters:
index
- the element index.value
- the value at the given index.
-
remove
Removes the element at the given index.- Specified by:
remove
in interfaceList<Integer>
- Overrides:
remove
in classAbstractList<Integer>
- Parameters:
index
- the index of the element to remove.- Returns:
- the previous value of the element at the given index.
- Throws:
IndexOutOfBoundsException
- if the given index is out of bounds.
-
removeLast
Retrieves and remove the last element of this list.- Returns:
- the tail of this list.
- Throws:
NoSuchElementException
- if this list is empty.
-
removeRange
protected void removeRange(int lower, int upper) Removes all values in the given range of index. Shifts any succeeding elements to the left (reduces their index).- Overrides:
removeRange
in classAbstractList<Integer>
- Parameters:
lower
- index of the first element to remove, inclusive.upper
- index after the last element to be removed.
-
occurrence
public int occurrence(int value) Returns the occurrence of the given value in this list.- Parameters:
value
- the value to search for.- Returns:
- the number of time the given value occurs in this list.
-
iterator
Returns an iterator over the elements in this list in increasing index order. The iterator is fail-fast and supports the remove operation. -
spliterator
Returns an spliterator over the elements in this list in increasing index order. The iterator is fail-fast.- Specified by:
spliterator
in interfaceCollection<Integer>
- Specified by:
spliterator
in interfaceIterable<Integer>
- Specified by:
spliterator
in interfaceList<Integer>
- Returns:
- spliterator over the integer values in this list.
- Since:
- 0.8-jdk8
-
stream
Returns a stream of integers with thisIntegerList
as its source. This method is similar toCollection.stream()
, but does not box the values. The returned stream is fail-fast, meaning that any modification to the list while using the stream will cause aConcurrentModificationException
to be thrown.The default implementation creates a parallel or sequential stream from
spliterator()
.- Parameters:
parallel
-true
for a parallel stream, orfalse
for a sequential stream.- Returns:
- a stream of values in this list as primitive types.
- Since:
- 0.8-jdk8
-
writeObject
InvokestrimToSize()
before serialization in order to make the stream more compact.- Parameters:
out
- the output stream where to serialize this list.- Throws:
IOException
- if an I/O error occurred while writing.
-
trimToSize
public void trimToSize()Trims the capacity of this list to be its current size.- See Also:
-
equals
Compares the content of this list with the given object. This method overrides thedefault implementation
for performance reasons.- Specified by:
equals
in interfaceCollection<Integer>
- Specified by:
equals
in interfaceList<Integer>
- Overrides:
equals
in classAbstractList<Integer>
- Parameters:
other
- the other object to compare with this list.- Returns:
true
if both object are equal.- Since:
- 1.1
-
clone
Returns a clone of this list.
-