public class KeyedSet<K extends Comparable<K>,T extends Keyed<K>> extends Object implements Iterable<T>
KeyedSet does not implement java.util.Set, as the interface
is not well-suited to an immutable set, but provides the common
set operations and a number of map-like operations.
Null values are not allowed.
The containsAll, containsAny, addAll,
removeAll and retainAll could be faster if
methods specialized to KeyedSet were added.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
KeyedSet.Empty<T extends Keyed<K>,K extends Comparable<K>>
Empty is a singleton object representing the empty set.
|
protected class |
KeyedSet.ReverseTreeIterable |
protected class |
KeyedSet.ReverseTreeIterator |
protected class |
KeyedSet.ReverseValueIterable |
protected class |
KeyedSet.ReverseValueIterator |
protected class |
KeyedSet.TreeIterable |
protected class |
KeyedSet.TreeState |
protected class |
KeyedSet.ValueIterable |
protected class |
KeyedSet.ValueIterator |
| Modifier and Type | Field and Description |
|---|---|
static KeyedSet |
EMPTY |
protected int |
height |
protected KeyedSet<K,T> |
left |
protected T |
member |
protected KeyedSet<K,T> |
right |
| Modifier | Constructor and Description |
|---|---|
|
KeyedSet(Iterable<T> iterable)
Construct set from iterable.
|
protected |
KeyedSet(T element,
KeyedSet<K,T> left,
KeyedSet<K,T> right)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
KeyedSet<K,T> |
add(T element)
Add an element to the set.
|
KeyedSet<K,T> |
add(T element,
boolean[] added)
Convenience method for java.util.Set, which returns true
if the element was not already present in the tree.
|
KeyedSet<K,T> |
addAll(Iterable<T> iterable)
Add all elements in iterable to set.
|
protected KeyedSet<K,T> |
addElement(T element,
boolean[] added)
addEntry returns this tree with added element
|
protected KeyedSet<K,T> |
balanceTree(KeyedSet<K,T> node) |
KeyedSet<K,T> |
clear()
Vacuous method for compatibility.
|
void |
collect(Collection<T> collection)
Add all elements to collection.
|
boolean |
contains(T value) |
boolean |
containsAll(Iterable<T> elements) |
boolean |
containsAny(Iterable<T> elements) |
boolean |
containsKey(K key)
Test if tree contains the specified key.
|
protected boolean |
containsKey(T value) |
KeyedSet<K,T> |
delete(K key)
Delete the element with matching key.
|
KeyedSet<K,T> |
delete(K key,
boolean[] deleted)
Delete the element with matching key.
|
KeyedSet<K,T> |
deleteFirst()
Delete the first element of the tree.
|
protected KeyedSet<K,T> |
deleteKey(K key,
boolean[] deleted) |
KeyedSet<K,T> |
deleteLast()
Delete the last element of the tree.
|
Iterable<T> |
elements()
Return an iterable that can be used in foreach to obtain elements
in ascending order.
|
boolean |
equals(Object o)
Compare sets.
|
KeyedSet<K,T> |
filter(Filter filter)
Filter the set, removing all elements for which the
test method returns false. |
T |
get(K key)
Get the element with matching key.
|
protected int |
getBalance() |
T |
getFirst()
Get the first element in the tree.
|
T |
getLast()
Get the last element in the tree.
|
int |
hashCode()
Get the hashCode for the set.
|
protected void |
init() |
boolean |
isEmpty() |
Iterator<T> |
iterator()
Return an iterator over the elements of the set in ascending order.
|
Iterable<K> |
keys()
Get an iterable that can be used in foreach to obtain keys
in ascending order.
|
protected KeyedSet<K,T> |
newTree(T element,
KeyedSet<K,T> left,
KeyedSet<K,T> right)
Function that wraps constructor.
|
void |
prettyPrint() |
protected void |
prettyPrint(String indent) |
KeyedSet<K,T> |
remove(T element)
Remove an element if the keys match and the values are equal.
|
KeyedSet<K,T> |
remove(T element,
boolean[] removed)
Remove an element if the keys match and the values are equal.
|
KeyedSet<K,T> |
removeAll(Iterable<T> iterable)
Remove all elements in iterable from tree.
|
protected KeyedSet<K,T> |
removeElement(T element,
boolean[] removed) |
protected KeyedSet<K,T> |
removeHigh(KeyedSet<K,T> node,
KeyedSet<K,T> high) |
KeyedSet<K,T> |
retainAll(Iterable<T> iterable)
Get all elements in both set and iterable.
|
Iterable<T> |
reverseElements()
Return an iterable that can be used in foreach to obtain elements
in descending order.
|
Iterator<T> |
reverseIterator()
Return an iterator over the elements of the set in descending order.
|
Iterable<K> |
reverseKeys()
Get an iterable that can be used in foreach to obtain keys
in descending order.
|
protected KeyedSet<K,T> |
setLeft(KeyedSet<K,T> left) |
protected KeyedSet<K,T> |
setRight(KeyedSet<K,T> right) |
int |
size()
Get the tree size.
|
String |
toString() |
boolean |
visitFwd(ConditionalVisitor visitor)
Visit elements in order.
|
void |
visitFwd(Visitor visitor)
Visit all elements in order.
|
boolean |
visitRev(ConditionalVisitor visitor)
Visit all elements in reverse order.
|
void |
visitRev(Visitor visitor)
Visit all elements in reverse order.
|
protected int height
public static final KeyedSet EMPTY
public KeyedSet(Iterable<T> iterable)
iterable - elements of new setprotected KeyedSet<K,T> newTree(T element, KeyedSet<K,T> left, KeyedSet<K,T> right)
element - value of nodeleft - left subtreeright - right subtreeprotected void init()
public boolean equals(Object o)
public int hashCode()
public int size()
isEmpty() rather than getSize() == 0.public boolean containsKey(K key)
key - public T getFirst()
public T getLast()
public KeyedSet<K,T> deleteFirst()
public KeyedSet<K,T> deleteLast()
public Iterable<K> keys()
public Iterable<K> reverseKeys()
protected int getBalance()
public boolean contains(T value)
protected boolean containsKey(T value)
public boolean isEmpty()
public KeyedSet<K,T> add(T element)
element - public KeyedSet<K,T> add(T element, boolean[] added)
element - to addadded - on return, added[0] true if element added
and set size increased by 1; otherwise false; added[1]
true if element with matching key was replaced (set size
unchange); otherwise false; if neither are true, the element
was already in the setprotected KeyedSet<K,T> addElement(T element, boolean[] added)
public T get(K key)
key - public KeyedSet<K,T> delete(K key)
key - public KeyedSet<K,T> delete(K key, boolean[] deleted)
key - deleted - deleted[0] true if the size of the tree reduced by 1;
otherwise falsepublic KeyedSet<K,T> remove(T element)
element - public KeyedSet<K,T> remove(T element, boolean[] removed)
element - removed - on return, removed[0] true if element removed and
set size reduced by 1; otherwise, falsepublic Iterator<T> iterator()
public Iterator<T> reverseIterator()
public Iterable<T> elements()
iterator()public Iterable<T> reverseElements()
reverseIterator()public KeyedSet<K,T> addAll(Iterable<T> iterable)
iterable - elements to addpublic KeyedSet<K,T> removeAll(Iterable<T> iterable)
iterable - elements to removepublic KeyedSet<K,T> retainAll(Iterable<T> iterable)
iterable - public void visitFwd(Visitor visitor)
visitor - visits each elementpublic boolean visitFwd(ConditionalVisitor visitor)
visitor - visits each element, returning false if next element
is to be visited; otherwise true (desired/last element found)public void visitRev(Visitor visitor)
visitor - visits each elementpublic boolean visitRev(ConditionalVisitor visitor)
visitor - visits each element, returning false if next element
is to be visited; otherwise true (desired element found)public KeyedSet<K,T> filter(Filter filter)
test method returns false.filter - public void collect(Collection<T> collection)
collection - targetpublic boolean containsAll(Iterable<T> elements)
elements - public boolean containsAny(Iterable<T> elements)
elements - public void prettyPrint()
protected void prettyPrint(String indent)
Copyright © 2015. All rights reserved.