com.badlogic.gdx.utils
Class ObjectSet<T>

java.lang.Object
  extended by com.badlogic.gdx.utils.ObjectSet<T>
All Implemented Interfaces:
Iterable<T>

public class ObjectSet<T>
extends Object
implements Iterable<T>

An unordered set where the keys are objects. This implementation uses cuckoo hashing using 3 hashes, random walking, and a small stash for problematic keys. Null keys are not allowed. No allocation is done except when growing the table size.

This set performs very fast contains and remove (typically O(1), worst case O(log(n))). Add may be a bit slower, depending on hash collisions. Load factors greater than 0.91 greatly increase the chances the set will have to rehash to the next higher POT size.

Author:
Nathan Sweet

Nested Class Summary
static class ObjectSet.SetIterator<K>
           
 
Field Summary
 int size
           
 
Constructor Summary
ObjectSet()
          Creates a new set with an initial capacity of 32 and a load factor of 0.8.
ObjectSet(int initialCapacity)
          Creates a new set with a load factor of 0.8.
ObjectSet(int initialCapacity, float loadFactor)
          Creates a new set with the specified initial capacity and load factor.
 
Method Summary
 boolean add(T key)
          Returns true if the key was not already in the set.
 void addAll(Array<T> array)
           
 void addAll(ObjectSet<T> set)
           
 void clear()
           
 void clear(int maximumCapacity)
          Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger.
 boolean contains(T key)
           
 void ensureCapacity(int additionalCapacity)
          Increases the size of the backing array to acommodate the specified number of additional items.
 ObjectSet.SetIterator<T> iterator()
          Returns an iterator for the keys in the set.
 boolean remove(T key)
          Returns true if the key was removed.
 void shrink(int maximumCapacity)
          Reduces the size of the backing arrays to be the specified capacity or less.
 String toString()
           
 String toString(String separator)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

size

public int size
Constructor Detail

ObjectSet

public ObjectSet()
Creates a new set with an initial capacity of 32 and a load factor of 0.8. This set will hold 25 items before growing the backing table.


ObjectSet

public ObjectSet(int initialCapacity)
Creates a new set with a load factor of 0.8. This set will hold initialCapacity * 0.8 items before growing the backing table.


ObjectSet

public ObjectSet(int initialCapacity,
                 float loadFactor)
Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity * loadFactor items before growing the backing table.

Method Detail

add

public boolean add(T key)
Returns true if the key was not already in the set.


addAll

public void addAll(Array<T> array)

addAll

public void addAll(ObjectSet<T> set)

remove

public boolean remove(T key)
Returns true if the key was removed.


shrink

public void shrink(int maximumCapacity)
Reduces the size of the backing arrays to be the specified capacity or less. If the capacity is already less, nothing is done. If the map contains more items than the specified capacity, the next highest power of two capacity is used instead.


clear

public void clear(int maximumCapacity)
Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger.


clear

public void clear()

contains

public boolean contains(T key)

ensureCapacity

public void ensureCapacity(int additionalCapacity)
Increases the size of the backing array to acommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.


toString

public String toString()
Overrides:
toString in class Object

toString

public String toString(String separator)

iterator

public ObjectSet.SetIterator<T> iterator()
Returns an iterator for the keys in the set. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use the ObjectSet.SetIterator constructor for nested or multithreaded iteration.

Specified by:
iterator in interface Iterable<T>


Copyright © 2013. All Rights Reserved.