Class OrderedSet<T>
- java.lang.Object
-
- com.badlogic.gdx.utils.ObjectSet<T>
-
- com.badlogic.gdx.utils.OrderedSet<T>
-
- All Implemented Interfaces:
java.lang.Iterable<T>
public class OrderedSet<T> extends ObjectSet<T>
AObjectSetthat also stores keys in anArrayusing the insertion order. Null keys are not allowed. No allocation is done except when growing the table size.Iterationis ordered and faster than an unordered set. Keys can also be accessed and the order changed usingorderedItems(). There is some additional overhead for put and remove. When used for faster iteration versus ObjectSet and the order does not actually matter, copying during remove can be greatly reduced by settingArray.orderedto false fororderedItems().This class performs fast contains (typically O(1), worst case O(n) but that is rare in practice). Remove is somewhat slower due to
orderedItems(). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT size.Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with OrderedSet and OrderedMap.
This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classOrderedSet.OrderedSetIterator<K>-
Nested classes/interfaces inherited from class com.badlogic.gdx.utils.ObjectSet
ObjectSet.ObjectSetIterator<K>
-
-
Constructor Summary
Constructors Constructor Description OrderedSet()OrderedSet(int initialCapacity)OrderedSet(int initialCapacity, float loadFactor)OrderedSet(OrderedSet<? extends T> set)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(T key)Returns true if the key was added to the set or false if it was already in the set.booleanadd(T key, int index)Sets the key at the specfied index.voidaddAll(OrderedSet<T> set)booleanalter(T before, T after)Changes the itembeforetoafterwithout changing its position in the order.booleanalterIndex(int index, T after)Changes the item at the givenindexin the order toafter, without changing the ordering of other items.voidclear()Clears the set, leaving the backing arrays at the current capacity.voidclear(int maximumCapacity)Clears the set and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.OrderedSet.OrderedSetIterator<T>iterator()Returns an iterator for the keys in the set.Array<T>orderedItems()booleanremove(T key)Returns true if the key was removed.TremoveIndex(int index)java.lang.StringtoString()java.lang.StringtoString(java.lang.String separator)static <T> OrderedSet<T>with(T... array)-
Methods inherited from class com.badlogic.gdx.utils.ObjectSet
addAll, addAll, addAll, addAll, addAll, contains, ensureCapacity, equals, first, get, hashCode, isEmpty, notEmpty, place, shrink
-
-
-
-
Constructor Detail
-
OrderedSet
public OrderedSet()
-
OrderedSet
public OrderedSet(int initialCapacity, float loadFactor)
-
OrderedSet
public OrderedSet(int initialCapacity)
-
OrderedSet
public OrderedSet(OrderedSet<? extends T> set)
-
-
Method Detail
-
add
public boolean add(T key)
Description copied from class:ObjectSetReturns true if the key was added to the set or false if it was already in the set. If this set already contains the key, the call leaves the set unchanged and returns false.
-
add
public boolean add(T key, int index)
Sets the key at the specfied index. Returns true if the key was added to the set or false if it was already in the set. If this set already contains the key, the existing key's index is changed if needed and false is returned.
-
addAll
public void addAll(OrderedSet<T> set)
-
remove
public boolean remove(T key)
Description copied from class:ObjectSetReturns true if the key was removed.
-
removeIndex
public T removeIndex(int index)
-
alter
public boolean alter(T before, T after)
Changes the itembeforetoafterwithout changing its position in the order. Returns true ifafterhas been added to the OrderedSet andbeforehas been removed; returns false ifafteris already present orbeforeis not present. If you are iterating over an OrderedSet and have an index, you should preferalterIndex(int, Object), which doesn't need to search for an index like this does and so can be faster.- Parameters:
before- an item that must be present for this to succeedafter- an item that must not be in this set for this to succeed- Returns:
- true if
beforewas removed andafterwas added, false otherwise
-
alterIndex
public boolean alterIndex(int index, T after)Changes the item at the givenindexin the order toafter, without changing the ordering of other items. Ifafteris already present, this returns false; it will also return false ifindexis invalid for the size of this set. Otherwise, it returns true. Unlikealter(Object, Object), this operates in constant time.- Parameters:
index- the index in the order of the item to change; must be non-negative and less thanObjectSet.sizeafter- the item that will replace the contents atindex; this item must not be present for this to succeed- Returns:
- true if
aftersuccessfully replaced the contents atindex, false otherwise
-
clear
public void clear(int maximumCapacity)
Description copied from class:ObjectSetClears the set and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger. The reduction is done by allocating new arrays, though for large arrays this can be faster than clearing the existing array.
-
clear
public void clear()
Description copied from class:ObjectSetClears the set, leaving the backing arrays at the current capacity. When the capacity is high and the population is low, iteration can be unnecessarily slow.ObjectSet.clear(int)can be used to reduce the capacity.
-
iterator
public OrderedSet.OrderedSetIterator<T> iterator()
Description copied from class:ObjectSetReturns an iterator for the keys in the set. Remove is supported.If
Collections.allocateIteratorsis false, the same iterator instance is returned each time this method is called. Use theObjectSet.ObjectSetIteratorconstructor for nested or multithreaded iteration.
-
toString
public java.lang.String toString(java.lang.String separator)
-
with
public static <T> OrderedSet<T> with(T... array)
-
-