Class DoubleArray
java.lang.Object
gaiasky.util.DoubleArray
A wrapper around a double array to make it resizable and optionally ordered.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates an ordered array with a capacity of 16.DoubleArray(boolean ordered, double[] array, int startIndex, int count) Creates a new array containing the elements in the specified array.DoubleArray(boolean ordered, int capacity) DoubleArray(double[] array) Creates a new ordered array containing the elements in the specified array.DoubleArray(int capacity) Creates an ordered array with the specified capacity.DoubleArray(DoubleArray array) Creates a new array containing the elements in the specific array. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(double value) voidadd(double value1, double value2) voidadd(double value1, double value2, double value3) voidadd(double value1, double value2, double value3, double value4) voidaddAll(double... array) voidaddAll(double[] array, int offset, int length) voidaddAll(DoubleArray array) voidaddAll(DoubleArray array, int offset, int length) voidclear()booleancontains(double value) double[]ensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items.booleanReturns false if either array is unordered.booleanReturns false if either array is unordered.doublefirst()Returns the first item.doubleget(int index) inthashCode()voidincr(double value) voidincr(int index, double value) intindexOf(double value) voidinsert(int index, double value) voidinsertRange(int index, int count) Inserts the specified number of items at the specified index.booleanisEmpty()Returns true if the array is empty.intlastIndexOf(double value) voidmul(double value) voidmul(int index, double value) booleannotEmpty()Returns true if the array has one or more items.doublepeek()Returns the last item.doublepop()Removes and returns the last item.doublerandom()Returns a random item from the array, or zero if the array is empty.booleanremoveAll(DoubleArray array) Removes from this array all the elements contained in the specified array.voidremoveIndex(int index) Removes and returns the item at the specified index.voidremoveRange(int start, int end) Removes the items between the specified indices, inclusive.booleanremoveValue(double value) protected double[]resize(int newSize) voidreverse()voidset(int index, double value) double[]setSize(int newSize) Sets the array size, leaving any values beyond the current size undefined.double[]shrink()Reduces the size of the backing array to the size of the actual items.voidshuffle()voidsort()voidswap(int first, int second) double[]toArray()toString()voidtruncate(int newSize) Reduces the size of the array to the specified size.static DoubleArraywith(double... array)
-
Field Details
-
items
public double[] items -
size
public int size -
ordered
public boolean ordered
-
-
Constructor Details
-
DoubleArray
public DoubleArray()Creates an ordered array with a capacity of 16. -
DoubleArray
public DoubleArray(int capacity) Creates an ordered array with the specified capacity. -
DoubleArray
public DoubleArray(boolean ordered, int capacity) - Parameters:
ordered- If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.capacity- Any elements added beyond this will cause the backing array to be grown.
-
DoubleArray
Creates a new array containing the elements in the specific array. The new array will be ordered if the specific array is ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown. -
DoubleArray
public DoubleArray(double[] array) Creates a new ordered array containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown. -
DoubleArray
public DoubleArray(boolean ordered, double[] array, int startIndex, int count) Creates a new array containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.- Parameters:
ordered- If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
-
-
Method Details
-
with
- See Also:
-
add
public void add(double value) -
add
public void add(double value1, double value2) -
add
public void add(double value1, double value2, double value3) -
add
public void add(double value1, double value2, double value3, double value4) -
addAll
-
addAll
-
addAll
public void addAll(double... array) -
addAll
public void addAll(double[] array, int offset, int length) -
get
public double get(int index) -
set
public void set(int index, double value) -
incr
public void incr(int index, double value) -
incr
public void incr(double value) -
mul
public void mul(int index, double value) -
mul
public void mul(double value) -
insert
public void insert(int index, double value) -
insertRange
public void insertRange(int index, int count) Inserts the specified number of items at the specified index. The new items will have values equal to the values at those indices before the insertion. -
swap
public void swap(int first, int second) -
contains
public boolean contains(double value) -
indexOf
public int indexOf(double value) -
lastIndexOf
public int lastIndexOf(double value) -
removeValue
public boolean removeValue(double value) -
removeIndex
public void removeIndex(int index) Removes and returns the item at the specified index. -
removeRange
public void removeRange(int start, int end) Removes the items between the specified indices, inclusive. -
removeAll
Removes from this array all the elements contained in the specified array.- Returns:
- true if this array was modified.
-
pop
public double pop()Removes and returns the last item. -
peek
public double peek()Returns the last item. -
first
public double first()Returns the first item. -
notEmpty
public boolean notEmpty()Returns true if the array has one or more items. -
isEmpty
public boolean isEmpty()Returns true if the array is empty. -
clear
public void clear() -
shrink
public double[] shrink()Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items have been removed, or if it is known that more items will not be added.- Returns:
items
-
ensureCapacity
public double[] ensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.- Returns:
items
-
setSize
public double[] setSize(int newSize) Sets the array size, leaving any values beyond the current size undefined.- Returns:
items
-
resize
protected double[] resize(int newSize) -
sort
public void sort() -
reverse
public void reverse() -
shuffle
public void shuffle() -
truncate
public void truncate(int newSize) Reduces the size of the array to the specified size. If the array is already smaller than the specified size, no action is taken. -
random
public double random()Returns a random item from the array, or zero if the array is empty. -
toArray
public double[] toArray() -
hashCode
-
equals
-
equals
Returns false if either array is unordered. -
toString
-
toString
-