Package com.badlogic.gdx.utils
Class SnapshotArray<T>
- java.lang.Object
-
- com.badlogic.gdx.utils.Array<T>
-
- com.badlogic.gdx.utils.SnapshotArray<T>
-
- All Implemented Interfaces:
java.lang.Iterable<T>
public class SnapshotArray<T> extends Array<T>
An array that allows modification during iteration. Guarantees that array entries provided bybegin()between indexes 0 andArray.sizeat the time begin was called will not be modified untilend()is called. If modification of the SnapshotArray occurs between begin/end, the backing array is copied prior to the modification, ensuring that the backing array that was returned bybegin()is unaffected. To avoid allocation, an attempt is made to reuse any extra array created as a result of this copy on subsequent copies.Note that SnapshotArray is not for thread safety, only for modification during iteration.
It is suggested iteration be done in this specific way:
SnapshotArray
- array = new SnapshotArray(); // ... Object[] items = array.begin(); for (int i = 0, n = array.size; i < n; i++) { Item item = (Item)items[i]; // ... } array.end();
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.badlogic.gdx.utils.Array
Array.ArrayIterable<T>, Array.ArrayIterator<T>
-
-
Constructor Summary
Constructors Constructor Description SnapshotArray()SnapshotArray(boolean ordered, int capacity)SnapshotArray(boolean ordered, int capacity, java.lang.Class arrayType)SnapshotArray(boolean ordered, T[] array, int startIndex, int count)SnapshotArray(int capacity)SnapshotArray(Array array)SnapshotArray(java.lang.Class arrayType)SnapshotArray(T[] array)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description T[]begin()Returns the backing array, which is guaranteed to not be modified beforeend().voidclear()voidend()Releases the guarantee that the array returned bybegin()won't be modified.voidinsert(int index, T value)voidinsertRange(int index, int count)Inserts the specified number of items at the specified index.Tpop()Removes and returns the last item.booleanremoveAll(Array<? extends T> array, boolean identity)Removes from this array all of elements contained in the specified array.TremoveIndex(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(T value, boolean identity)Removes the first instance of the specified value in the array.voidreverse()voidset(int index, T value)T[]setSize(int newSize)Sets the array size, leaving any values beyond the current size null.voidshuffle()voidsort()Sorts this array.voidsort(java.util.Comparator<? super T> comparator)Sorts the array.voidswap(int first, int second)voidtruncate(int newSize)Reduces the size of the array to the specified size.static <T> SnapshotArray<T>with(T... array)-
Methods inherited from class com.badlogic.gdx.utils.Array
add, add, add, add, addAll, addAll, addAll, addAll, contains, containsAll, containsAny, ensureCapacity, equals, equalsIdentity, first, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, notEmpty, of, of, peek, random, resize, select, selectRanked, selectRankedIndex, shrink, toArray, toArray, toString, toString
-
-
-
-
Constructor Detail
-
SnapshotArray
public SnapshotArray()
-
SnapshotArray
public SnapshotArray(Array array)
-
SnapshotArray
public SnapshotArray(boolean ordered, int capacity, java.lang.Class arrayType)
-
SnapshotArray
public SnapshotArray(boolean ordered, int capacity)
-
SnapshotArray
public SnapshotArray(boolean ordered, T[] array, int startIndex, int count)
-
SnapshotArray
public SnapshotArray(java.lang.Class arrayType)
-
SnapshotArray
public SnapshotArray(int capacity)
-
SnapshotArray
public SnapshotArray(T[] array)
-
-
Method Detail
-
begin
public T[] begin()
Returns the backing array, which is guaranteed to not be modified beforeend().
-
end
public void end()
Releases the guarantee that the array returned bybegin()won't be modified.
-
insertRange
public void insertRange(int index, int count)Description copied from class:ArrayInserts 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.- Overrides:
insertRangein classArray<T>
-
removeValue
public boolean removeValue(T value, boolean identity)
Description copied from class:ArrayRemoves the first instance of the specified value in the array.- Overrides:
removeValuein classArray<T>- Parameters:
value- May be null.identity- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- true if value was found and removed, false otherwise
-
removeIndex
public T removeIndex(int index)
Description copied from class:ArrayRemoves and returns the item at the specified index.- Overrides:
removeIndexin classArray<T>
-
removeRange
public void removeRange(int start, int end)Description copied from class:ArrayRemoves the items between the specified indices, inclusive.- Overrides:
removeRangein classArray<T>
-
removeAll
public boolean removeAll(Array<? extends T> array, boolean identity)
Description copied from class:ArrayRemoves from this array all of elements contained in the specified array.
-
sort
public void sort()
Description copied from class:ArraySorts this array. The array elements must implementComparable. This method is not thread safe (usesSort.instance()).
-
sort
public void sort(java.util.Comparator<? super T> comparator)
Description copied from class:ArraySorts the array. This method is not thread safe (usesSort.instance()).
-
truncate
public void truncate(int newSize)
Description copied from class:ArrayReduces the size of the array to the specified size. If the array is already smaller than the specified size, no action is taken.
-
setSize
public T[] setSize(int newSize)
Description copied from class:ArraySets the array size, leaving any values beyond the current size null.- Overrides:
setSizein classArray<T>- Returns:
Array.items
-
with
public static <T> SnapshotArray<T> with(T... array)
- See Also:
SnapshotArray(Object[])
-
-