Package gaiasky.util
Class DoubleArray
java.lang.Object
gaiasky.util.DoubleArray
A resizable, ordered or unordered double array. Avoids the boxing that occurs with ArrayList. If unordered, this class
avoids a memory copy when removing elements (the last element is moved to the removed element's position).
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates 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 TypeMethodDescriptionvoid
add
(double value) void
add
(double value1, double value2) void
add
(double value1, double value2, double value3) void
add
(double value1, double value2, double value3, double value4) void
addAll
(double... array) void
addAll
(double[] array, int offset, int length) void
addAll
(DoubleArray array) void
addAll
(DoubleArray array, int offset, int length) void
clear()
boolean
contains
(double value) double[]
ensureCapacity
(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items.boolean
Returns false if either array is unordered.boolean
Returns false if either array is unordered.double
first()
Returns the first item.double
get
(int index) int
hashCode()
void
incr
(double value) void
incr
(int index, double value) int
indexOf
(double value) void
insert
(int index, double value) void
insertRange
(int index, int count) Inserts the specified number of items at the specified index.boolean
isEmpty()
Returns true if the array is empty.int
lastIndexOf
(double value) void
mul
(double value) void
mul
(int index, double value) boolean
notEmpty()
Returns true if the array has one or more items.double
peek()
Returns the last item.double
pop()
Removes and returns the last item.double
random()
Returns a random item from the array, or zero if the array is empty.boolean
removeAll
(DoubleArray array) Removes from this array all of elements contained in the specified array.double
removeIndex
(int index) Removes and returns the item at the specified index.void
removeRange
(int start, int end) Removes the items between the specified indices, inclusive.boolean
removeValue
(double value) protected double[]
resize
(int newSize) void
reverse()
void
set
(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.void
shuffle()
void
sort()
void
swap
(int first, int second) double[]
toArray()
toString()
void
truncate
(int newSize) Reduces the size of the array to the specified size.static DoubleArray
with
(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
-
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 double 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 of 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
public int hashCode() -
equals
Returns false if either array is unordered. -
equals
Returns false if either array is unordered. -
toString
-
toString
-
with
- See Also:
-