Package com.opencloud.util
Class IntSet
- java.lang.Object
-
- com.opencloud.util.IntSet
-
- All Implemented Interfaces:
FastSerializable,Serializable,Cloneable,Iterable<Integer>
public class IntSet extends Object implements Cloneable, Serializable, FastSerializable, Iterable<Integer>
IntSet provides efficient management of a set of primitive integers. The underlying storage is an int[]. IntSet tries to avoid reallocating storage where possible.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description IntSet()Construct a new empty set.IntSet(int capacity)Construct a new empty set, with the given initial allocation sizeIntSet(int[] fromData)Construct a set from an integer array.IntSet(int[] fromData, boolean sorted)Construct a set from an integer array.IntSet(IntSet fromSet)Construct a copy of an IntSet.IntSet(DataInput inStream)FastSerializable constructor.IntSet(String str)Construct a set from a toString() representation
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(int val)Add a value to the set.IntSetaddAll(int[] vals)Add an array of values to the set.IntSetaddAll(IntSet other)Add the values from anotherIntSetto this set.StringBufferappendToStringBuffer(StringBuffer sb)Appends a textual representation of this IntSet to the given StringBuffer.voidclear()Sets this IntSet to represent the empty set.IntSetclone()Performs a deep copy of the set.static IntSetcomplement(IntSet superset, IntSet subset)Return a new set that is the complement of a subset within a superset.booleancontains(int val)Check if a value is contained in the set.booleancontainsAll(int[] vals)Check if the given values are contained in the set.booleancontainsAll(IntSet other)Check if the given values are contained in this set.static IntSetcopyOf(int[] from)Construct a new set that contains the given values.static IntSetcopyOf(Set<Integer> fromSet)Construct a new set that contains a copy of the values in the given set.static IntSetemptySet()Construct a new empty set.booleanequals(Object other)Set equality.voidfromArray(int[] fromData)Sets the integers in this IntSet to be those in the given array.voidfromArray(int[] fromData, boolean sorted)Sets the integers in this IntSet to be those in the given array.intget(int index)Get the value at a specified index.inthashCode()Computes a hash code value for this object.static IntSetintersection(IntSet setA, IntSet setB)Return a new set that is the intersection of two other sets.booleanisEmpty()Determines whether this IntSet represents an empty set.booleanisSubsetOf(IntSet superset)Determines whether this IntSet is a subset of the given IntSet.booleanisSupersetOf(IntSet subset)Determines whether this IntSet is a superset of the given IntSet.Iterator<Integer>iterator()intlowestMember()Gets the smallest valued int from this IntSet.static IntSetof(int... values)Construct a set with the given values.booleanremove(int val)Remove a value from the set.IntSetremoveAll(int[] vals)Remove an array of values to the set.IntSetremoveAll(IntSet other)Remove all values in the providedIntSetfrom this set.intsize()Gets the size of this IntSet.int[]toArray()Return an array containing only the set data (sorted).voidtoStream(DataOutput outStream)Serialize the state of this object to a stream.StringtoString()Returns a textual representation of this IntSet.static IntSetunion(IntSet setA, IntSet setB)Return a new set that is the union of two other sets.-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
IntSet
public IntSet()
Construct a new empty set.
-
IntSet
public IntSet(int capacity)
Construct a new empty set, with the given initial allocation size- Parameters:
capacity- the initial storage capacity to use; may be adjusted if too small.
-
IntSet
public IntSet(IntSet fromSet)
Construct a copy of an IntSet.- Parameters:
fromSet- the set to copy
-
IntSet
public IntSet(int[] fromData, boolean sorted)Construct a set from an integer array.- Parameters:
fromData- the initial contents of the set; not modifiedsorted- if true, fromArray is assumed to be already sorted
-
IntSet
public IntSet(int[] fromData)
Construct a set from an integer array.- Parameters:
fromData- an array holding the initial set contents; assumed to be unsorted.
-
IntSet
public IntSet(DataInput inStream) throws IOException
FastSerializable constructor.- Throws:
IOException
-
IntSet
public IntSet(String str) throws IllegalArgumentException
Construct a set from a toString() representation- Throws:
IllegalArgumentException
-
-
Method Detail
-
of
public static IntSet of(int... values)
Construct a set with the given values. This is a separate static method to avoid confusion with the single-int ctor variant.- Parameters:
values- values to include in the new set- Returns:
- a new IntSet
-
emptySet
public static IntSet emptySet()
Construct a new empty set. Equivalent to "new IntSet()" but reads a little more nicely.- Returns:
- a new empty IntSet
-
copyOf
public static IntSet copyOf(int[] from)
Construct a new set that contains the given values.- Parameters:
from- an array of unsorted values- Returns:
- a new IntSet
-
copyOf
public static IntSet copyOf(Set<Integer> fromSet)
Construct a new set that contains a copy of the values in the given set.- Parameters:
fromSet- a set of Integers- Returns:
- a new IntSet
-
toStream
public void toStream(DataOutput outStream) throws IOException
Description copied from interface:FastSerializableSerialize the state of this object to a stream.- Specified by:
toStreamin interfaceFastSerializable- Parameters:
outStream- the stream to serialize to.- Throws:
IOException- if an error occurs during serialization
-
hashCode
public int hashCode()
Computes a hash code value for this object.
-
isEmpty
public boolean isEmpty()
Determines whether this IntSet represents an empty set.- Returns:
- whether size of set is 0
-
add
public boolean add(int val)
Add a value to the set.- Parameters:
val- the value to add- Returns:
- true if the value was not already present
-
addAll
public IntSet addAll(int[] vals)
Add an array of values to the set.- Parameters:
vals- the values to add.- Returns:
- this
IntSet
-
addAll
public IntSet addAll(IntSet other)
Add the values from anotherIntSetto this set.- Parameters:
other- the set of values to add.- Returns:
- this
IntSet.
-
remove
public boolean remove(int val)
Remove a value from the set.- Parameters:
val- the value to remove- Returns:
- true if the value was present (and removed)
-
removeAll
public IntSet removeAll(int[] vals)
Remove an array of values to the set.- Parameters:
vals- the values to remove.- Returns:
- this
IntSet
-
removeAll
public IntSet removeAll(IntSet other)
Remove all values in the providedIntSetfrom this set.- Parameters:
other- the set of values to remove from this set.- Returns:
- this
IntSet.
-
contains
public boolean contains(int val)
Check if a value is contained in the set.- Parameters:
val- the value to check for- Returns:
- true if val is present in the set
-
containsAll
public boolean containsAll(int[] vals)
Check if the given values are contained in the set.- Parameters:
vals- the values to check for- Returns:
- true if all the values are present in the set, false otherwise
-
containsAll
public boolean containsAll(IntSet other)
Check if the given values are contained in this set.- Parameters:
other- the set of values to check for.- Returns:
- true if all the values are present in the set, false otherwise
-
fromArray
public void fromArray(int[] fromData)
Sets the integers in this IntSet to be those in the given array.- Parameters:
fromData- array of ints
-
fromArray
public void fromArray(int[] fromData, boolean sorted)Sets the integers in this IntSet to be those in the given array.- Parameters:
fromData- array of intssorted- whether the ints in array fromData are in sorted sequence
-
clear
public void clear()
Sets this IntSet to represent the empty set.
-
toArray
public int[] toArray()
Return an array containing only the set data (sorted).- Returns:
- a sorted array containing the set members
-
toString
public String toString()
Returns a textual representation of this IntSet. The textual representation is of the form [i_1, ..., i_n].
-
appendToStringBuffer
public StringBuffer appendToStringBuffer(StringBuffer sb)
Appends a textual representation of this IntSet to the given StringBuffer. The textual representation is of the form [i_1, ..., i_n].- Parameters:
sb- StringBuffer- Returns:
- the given StringBuffer
-
intersection
public static IntSet intersection(IntSet setA, IntSet setB)
Return a new set that is the intersection of two other sets. O(n+m).- Parameters:
setA- the first set to intersect; null means an empty setsetB- the second set to intersect; null means an empty set- Returns:
- a new set (always non-null) containing those members in both setA and setB
-
union
public static IntSet union(IntSet setA, IntSet setB)
Return a new set that is the union of two other sets. O(n+m).- Parameters:
setA- the first set to union; null means an empty setsetB- the second set to union; null means an empty set- Returns:
- a new set (always non-null) containing all members of setA and setB
-
complement
public static IntSet complement(IntSet superset, IntSet subset)
Return a new set that is the complement of a subset within a superset. O(n+m).- Parameters:
superset- the superset to perform the complement within; null means the empty setsubset- the subset to complement; null means the empty set- Returns:
- a new set (always non-null) containing all members of superset that are not in subset
-
isSupersetOf
public boolean isSupersetOf(IntSet subset)
Determines whether this IntSet is a superset of the given IntSet.- Parameters:
subset- the conjectured subset of this IntSet
-
isSubsetOf
public boolean isSubsetOf(IntSet superset)
Determines whether this IntSet is a subset of the given IntSet.- Parameters:
superset- the conjectured superset of this IntSet
-
size
public int size()
Gets the size of this IntSet.- Returns:
- int size
-
lowestMember
public int lowestMember()
Gets the smallest valued int from this IntSet.- Returns:
- lowest int
- Throws:
NoSuchElementException- if this IntSet is empty
-
get
public int get(int index) throws ArrayIndexOutOfBoundsExceptionGet the value at a specified index. As the set is sorted, an index of 0 returns the smallest value in the set, an index ofreturns the highest value, and any index in between returns the corresponding value.size()- 1- Parameters:
index- the index.- Returns:
- the value at the index.
- Throws:
ArrayIndexOutOfBoundsException- ifindexis less than zero or greater than or equal to the size of the set.
-
-