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>
AObjectSet
that also stores keys in anArray
using the insertion order. Null keys are not allowed. No allocation is done except when growing the table size.Iteration
is 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.ordered
to 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 class
OrderedSet.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 boolean
add(T key)
Returns true if the key was added to the set or false if it was already in the set.boolean
add(T key, int index)
Sets the key at the specfied index.void
addAll(OrderedSet<T> set)
boolean
alter(T before, T after)
Changes the itembefore
toafter
without changing its position in the order.boolean
alterIndex(int index, T after)
Changes the item at the givenindex
in the order toafter
, without changing the ordering of other items.void
clear()
Clears the set, leaving the backing arrays at the current capacity.void
clear(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()
boolean
remove(T key)
Returns true if the key was removed.T
removeIndex(int index)
java.lang.String
toString()
java.lang.String
toString(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:ObjectSet
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 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:ObjectSet
Returns true if the key was removed.
-
removeIndex
public T removeIndex(int index)
-
alter
public boolean alter(T before, T after)
Changes the itembefore
toafter
without changing its position in the order. Returns true ifafter
has been added to the OrderedSet andbefore
has been removed; returns false ifafter
is already present orbefore
is 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
before
was removed andafter
was added, false otherwise
-
alterIndex
public boolean alterIndex(int index, T after)
Changes the item at the givenindex
in the order toafter
, without changing the ordering of other items. Ifafter
is already present, this returns false; it will also return false ifindex
is 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.size
after
- the item that will replace the contents atindex
; this item must not be present for this to succeed- Returns:
- true if
after
successfully replaced the contents atindex
, false otherwise
-
clear
public void clear(int maximumCapacity)
Description copied from class:ObjectSet
Clears 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:ObjectSet
Clears 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:ObjectSet
Returns an iterator for the keys in the set. Remove is supported.If
Collections.allocateIterators
is false, the same iterator instance is returned each time this method is called. Use theObjectSet.ObjectSetIterator
constructor for nested or multithreaded iteration.
-
toString
public java.lang.String toString(java.lang.String separator)
-
with
public static <T> OrderedSet<T> with(T... array)
-
-