ObjectSet that also stores keys in an Array 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
using orderedItems(). 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 setting Array#ordered
to false for OrderedSet#orderedItems().
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.
| Modifier and Type | Class and Description |
|---|---|
| public static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final Array | |
| pack-priv transient OrderedSet. | |
| pack-priv transient OrderedSet. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | add(T key)
Overrides com. Returns true if the key was added to the set or false if it was already in the set. |
| public boolean | |
| public void | |
| public boolean | Returns: true ifbefore was removed and after was added, false otherwisean item that must be present for this to succeed before, T an item that must not be in this set for this to succeed after)Changes the item |
| public boolean | Returns: true ifafter successfully replaced the contents at index, false otherwisethe index in the order of the item to change; must be non-negative and less than index, T sizethe item that will replace the contents at after)index; this item must not be present for this to succeedChanges the item at the given |
| public void | clear(int maximumCapacity)
Overrides com. Clears the set and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger. |
| public void | clear()
Overrides com. Clears the set, leaving the backing arrays at the current capacity. |
| public void | ensureCapacity(int additionalCapacity)
Overrides com. Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. |
| public boolean | equals(Object obj)
Overrides com. Indicates whether some other object is "equal to" this one. |
| public T | |
| public int | hashCode()
Overrides com. Returns a hash code value for this object. |
| public OrderedSet. | iterator()
Overrides com. Implements java. Returns an iterator for the keys in the set. |
| public Array | |
| public boolean | remove(T key)
Overrides com. Returns true if the key was removed. |
| public T | |
| public String | toString()
Overrides com. Returns a string representation of the object. |
| public String | |
| public static <T> OrderedSet |
| items | back to summary |
|---|---|
| pack-priv final Array<T> items | |
| iterator1 | back to summary |
|---|---|
| pack-priv transient OrderedSet. | |
| iterator2 | back to summary |
|---|---|
| pack-priv transient OrderedSet. | |
| OrderedSet | back to summary |
|---|---|
| public OrderedSet() | |
| OrderedSet | back to summary |
|---|---|
| public OrderedSet(int initialCapacity, float loadFactor) | |
| OrderedSet | back to summary |
|---|---|
| public OrderedSet(int initialCapacity) | |
| OrderedSet | back to summary |
|---|---|
| public OrderedSet(OrderedSet<? extends T> set) | |
| add | back to summary |
|---|---|
| public boolean add(T key) Overrides com. Doc from com. 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 | back to summary |
|---|---|
| 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 | back to summary |
|---|---|
| public void addAll(OrderedSet<T> set) | |
| alter | back to summary |
|---|---|
| public boolean alter(T before, T after) Changes the item
| |
| alterIndex | back to summary |
|---|---|
| public boolean alterIndex(int index, T after) Changes the item at the given
| |
| clear | back to summary |
|---|---|
| public void clear(int maximumCapacity) Overrides com. Doc from com. 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 | back to summary |
|---|---|
| public void clear() Overrides com. Doc from com. 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. | |
| ensureCapacity | back to summary |
|---|---|
| public void ensureCapacity(int additionalCapacity) Overrides com. Doc from com. Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. Useful before adding many items to avoid multiple backing array resizes. | |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides com. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| first | back to summary |
|---|---|
| public T first() Overrides com. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides com. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| iterator | back to summary |
|---|---|
| public OrderedSet. Overrides com. Implements java. Doc from com. Returns an iterator for the keys in the set. Remove is supported.
If | |
| orderedItems | back to summary |
|---|---|
| public Array | |
| remove | back to summary |
|---|---|
| public boolean remove(T key) Overrides com. Doc from com. Returns true if the key was removed. | |
| removeIndex | back to summary |
|---|---|
| public T removeIndex(int index) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides com. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
| toString | back to summary |
|---|---|
| public String toString(String separator) Overrides com. | |
| with | back to summary |
|---|---|
| public static <T> OrderedSet | |
| Modifier and Type | Field and Description |
|---|---|
| private Array |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public K | next()
Overrides com. Implements java. Returns the next element in the iteration. |
| public void | remove()
Overrides com. Overrides default java. Removes from the underlying collection the last element returned by this iterator (optional operation). |
| public void | |
| public Array | toArray(Array<K> array)
Overrides com. Adds the remaining values to the array. |
| public Array | toArray()
Overrides com. Returns a new array containing the remaining values. |
| items | back to summary |
|---|---|
| private Array<K> items | |
| OrderedSetIterator | back to summary |
|---|---|
| public OrderedSetIterator(OrderedSet<K> set) | |
| next | back to summary |
|---|---|
| public K next() Overrides com. Implements java. Doc from java. Returns the next element in the iteration. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides com. Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the | |
| reset | back to summary |
|---|---|
| public void reset() Overrides com. | |
| toArray | back to summary |
|---|---|
| public Array Overrides com. Doc from com. Adds the remaining values to the array. | |
| toArray | back to summary |
|---|---|
| public Array Overrides com. Doc from com. Returns a new array containing the remaining values. | |