java.lang.Object
de.grogra.xl.util.DoubleList
- All Implemented Interfaces:
DoubleConsumer
,VoidToDoubleGenerator
,Serializable
,Cloneable
public class DoubleList
extends Object
implements Cloneable, Serializable, DoubleConsumer, VoidToDoubleGenerator
A
DoubleList
represents a list of double
values.
It provides list- and stack-oriented methods for insertion, addition,
and removal, of values. The methods are not thread-safe.- Author:
- Ole Kniemeyer
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstructs a newDoubleList
.DoubleList
(double[] elements) Constructs a newDoubleList
whose elements are a copy ofelements
.DoubleList
(int capacity) Constructs a newDoubleList
with a given initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(double o) Addso
as last element to this list.void
add
(int index, double o) Insertso
at positionindex
to this list.addAll
(double[] v, int begin, int length) Appendslength
components ofv
to this list, starting at indexbegin
.addAll
(DoubleList v) Appends all elements ofv
to this list.void
addIfNotContained
(double o) Addso
as last element to this list if is not yet contained as reported bycontains(double)
.static void
arraycopy
(double[] src, int srcIndex, double[] dest, int destIndex, int length) int
binarySearch
(double value) Searches this list for the specified value using the binary search algorithm.static int
binarySearch
(double[] array, double value) static int
binarySearch
(double[] array, double value, int fromIndex, int toIndex) void
clear()
Removes all of the elements from this list.static void
clear
(double[] array, int index, int length) clone()
void
consume
(double value) Receives a value of typedouble
.boolean
contains
(double o) Returnstrue
iff this list contains the given elemento
.void
ensureCapacity
(int capacity) Ensures a capacity of the internal array of at leastcapacity
.boolean
void
evaluateDouble
(DoubleConsumer cons) This generator method yields all values tocons
.double
get
(int index) Returns the list element atindex
.int
hashCode()
int
indexOf
(double o) Returns the index ofo
in this list.final boolean
isEmpty()
Returns if this list is empty, i.e., if its size is zero.int
lastIndexOf
(double o) Returns the last index ofo
in this list.double
peek
(int index) Returns the list element atindex
as seen from the top, i.e., at absolute positionsize - index
.double
pop()
Removes and returns the object at the top of this list.final DoubleList
push
(double o) Pusheso
on top of this list, i.e., as last element.final DoubleList
push
(double o1, double o2) Pusheso1
ando2
on top of this list, i.e., as last elements.final DoubleList
push
(double o1, double o2, double o3) Pusheso1 ... o3
on top of this list, i.e., as last elements.boolean
remove
(double o) Removes the elemento
.double
removeAt
(int index) Removes the element at positionindex
.double
set
(int index, double o) Sets the element at positionindex
too
.void
setSize
(int size) Sets the size of this list to the given value.final int
size()
Returns the size of this list.double[]
toArray()
Returns an array containing the elements of this list.double[]
toArray
(double[] array) Returns an array containing the elements of this list.toString()
void
Trims the capacity of this list to be its current size.void
values
(DoubleConsumer cons) This method is an alias forevaluateDouble(de.grogra.xl.lang.DoubleConsumer)
.void
writeTo
(DoubleBuffer out)
-
Field Details
-
elements
public transient double[] elementsThe array holding the elements. Only the components from 0 tosize - 1
are valid. Direct operation on this array has to be designed carefully to avoid inconsistencies. -
size
public int sizeThe size of this list. Direct operation on this field has to be designed carefully to avoid inconsistencies.
-
-
Constructor Details
-
DoubleList
public DoubleList(int capacity) Constructs a newDoubleList
with a given initial capacity.- Parameters:
capacity
- the initial capacity
-
DoubleList
public DoubleList()Constructs a newDoubleList
. -
DoubleList
public DoubleList(double[] elements) Constructs a newDoubleList
whose elements are a copy ofelements
.- Parameters:
elements
- the initial elements of the list
-
-
Method Details
-
clone
-
ensureCapacity
public void ensureCapacity(int capacity) Ensures a capacity of the internal array of at leastcapacity
.- Parameters:
capacity
- the desired minimum capacity
-
trimToSize
public void trimToSize()Trims the capacity of this list to be its current size. -
push
Pusheso
on top of this list, i.e., as last element.- Parameters:
o
- the value to push- Returns:
- this list
-
push
Pusheso1
ando2
on top of this list, i.e., as last elements. The effect is the same as the invocationlist.push(o1).push(o2)
.- Parameters:
o1
- the first value to pusho2
- the second value to push- Returns:
- this list
-
push
Pusheso1 ... o3
on top of this list, i.e., as last elements. The effect is the same as the invocationlist.push(o1).push(o2).push(o3)
.- Parameters:
o1
- the first value to pusho2
- the second value to pusho3
- the third value to push- Returns:
- this list
-
add
public boolean add(double o) Addso
as last element to this list.- Parameters:
o
- the value to add- Returns:
true
-
add
public void add(int index, double o) Insertso
at positionindex
to this list. Ifindex
is not less thansize
, the list is enlarged and filled with0
-values before.- Parameters:
index
- the insert positiono
- the value to insert
-
addIfNotContained
public void addIfNotContained(double o) Addso
as last element to this list if is not yet contained as reported bycontains(double)
.- Parameters:
o
- the value to add
-
addAll
Appends all elements ofv
to this list.- Parameters:
v
- the list of elements to add- Returns:
- this list
-
addAll
Appendslength
components ofv
to this list, starting at indexbegin
.- Parameters:
v
- the array of elements to addbegin
- the array index to begin withlength
- the number of elements to add- Returns:
- this list
-
removeAt
public double removeAt(int index) Removes the element at positionindex
.- Parameters:
index
- the position of the element to be removed- Returns:
- the value of the removed element
-
remove
public boolean remove(double o) Removes the elemento
. The last occurence ofo
in this list is removed.- Parameters:
o
- the element to be removed- Returns:
true
iffo
was found and removed from the list
-
set
public double set(int index, double o) Sets the element at positionindex
too
. Ifindex
is not less thansize
, the list is enlarged and filled with0
-values before.- Parameters:
index
- the positiono
- the new value- Returns:
- the old value at
index
-
get
public double get(int index) Returns the list element atindex
. Ifindex
is not less thansize
,0
is returned.- Parameters:
index
- the position- Returns:
- the value at
index
-
peek
public double peek(int index) Returns the list element atindex
as seen from the top, i.e., at absolute positionsize - index
. Thus, the topmost element has index 1.- Parameters:
index
- the position as seen from the top- Returns:
- the value at that position
-
contains
public boolean contains(double o) Returnstrue
iff this list contains the given elemento
.- Parameters:
o
- a value- Returns:
true
iffo
is contained
-
indexOf
public int indexOf(double o) Returns the index ofo
in this list.- Parameters:
o
- a value- Returns:
- the index of
o
, or -1 ofo
is not contained
-
lastIndexOf
public int lastIndexOf(double o) Returns the last index ofo
in this list.- Parameters:
o
- a value- Returns:
- the index of
o
, or -1 ofo
is not contained
-
binarySearch
public int binarySearch(double value) Searches this list for the specified value using the binary search algorithm. This list has to be sorted in ascending order.- Parameters:
value
- the value to be searched for- Returns:
- index of the searched value, if it is contained in this list;
otherwise,
(-(insertion point) - 1)
. The insertion point is defined as the point at which the value would be inserted into the list: the index of the first element greater than the value, orsize()
, if all elements in the list are less than the specified value.
-
binarySearch
public static int binarySearch(double[] array, double value) -
binarySearch
public static int binarySearch(double[] array, double value, int fromIndex, int toIndex) -
writeTo
-
clear
public void clear()Removes all of the elements from this list. The list will be empty after this call returns. -
arraycopy
public static void arraycopy(double[] src, int srcIndex, double[] dest, int destIndex, int length) -
clear
public static void clear(double[] array, int index, int length) -
pop
public double pop()Removes and returns the object at the top of this list.- Returns:
- the removed object from the top of this list
-
setSize
public void setSize(int size) Sets the size of this list to the given value. If the new size is greater than the old size, the new elements are initialized with0
-values.- Parameters:
size
- the new size
-
size
public final int size()Returns the size of this list.- Returns:
- the size
-
isEmpty
public final boolean isEmpty()Returns if this list is empty, i.e., if its size is zero.- Returns:
true
iff this list is empty
-
toArray
public double[] toArray()Returns an array containing the elements of this list.- Returns:
- an array copy of this list
-
toArray
public double[] toArray(double[] array) Returns an array containing the elements of this list. The type of the returned array is that of the specifiedarray
. If this list fits in the specifiedarray
, it is returned therein. Otherwise, a new array is allocated whose length is the size of this list's size, the values of this list are copied into the new array, and this array is returned.If there is room for an additional element in the
array
, a0
-value is written behind the last copied element.- Parameters:
array
- an array to use- Returns:
- an array copy of this list
-
toString
-
equals
-
hashCode
public int hashCode() -
consume
public void consume(double value) Description copied from interface:DoubleConsumer
Receives a value of typedouble
.- Specified by:
consume
in interfaceDoubleConsumer
-
evaluateDouble
This generator method yields all values tocons
.- Specified by:
evaluateDouble
in interfaceVoidToDoubleGenerator
- Parameters:
cons
- the consumer which receives the values
-
values
This method is an alias forevaluateDouble(de.grogra.xl.lang.DoubleConsumer)
.
-