com.samskivert.util
Class ArrayIntSet

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<Integer>
          extended by com.samskivert.util.ArrayIntSet
All Implemented Interfaces:
Interable, IntSet, Serializable, Cloneable, Iterable<Integer>, Collection<Integer>, Set<Integer>

public class ArrayIntSet
extends AbstractSet<Integer>
implements IntSet, Cloneable, Serializable

Provides an IntSet implementation using a sorted array of integers to maintain the contents of the set.

See Also:
Serialized Form

Field Summary
protected  int _size
          The number of elements in this set.
protected  int[] _values
          An array containing the values in this set.
protected static int DEFAULT_CAPACITY
          The default initial capacity of this set.
 
Constructor Summary
ArrayIntSet()
          Constructs an empty set with the default initial capacity.
ArrayIntSet(int initialCapacity)
          Construct an ArrayIntSet of the specified initial capacity.
ArrayIntSet(int[] values)
          Construct an ArrayIntSet with the specified starting values.
 
Method Summary
 boolean add(int value)
          Adds the specified element to this set if it is not already present (optional operation).
 boolean add(int[] values)
          Add all of the values in the supplied array to the set.
 boolean add(Integer o)
           
 boolean addAll(Collection<? extends Integer> c)
           
protected  int binarySearch(int key)
          Performs a binary search on our values array, looking for the specified value.
 void clear()
           
 Object clone()
           
 boolean contains(int value)
          Returns true if this set contains the specified element.
 boolean contains(Object o)
           
 boolean containsAll(Collection<?> c)
           
 boolean equals(Object o)
           
 int get(int index)
          Returns the element at the specified index.
 int hashCode()
           
 Interator interator()
          Return an Interator over the ints in this object.
 boolean isEmpty()
           
 Iterator<Integer> iterator()
           
 boolean remove(int value)
          Removes the specified element from this set if it is present (optional operation).
 boolean remove(int[] values)
          Removes all values in the supplied array from the set.
 boolean remove(Object o)
           
 boolean retainAll(Collection<?> c)
           
 int size()
           
 Object[] toArray()
           
 Integer[] toArray(Integer[] a)
           
 int[] toIntArray()
          Returns an array containing all of the elements in this set.
 int[] toIntArray(int[] target, int offset)
          Serializes this int set into an array at the specified offset.
 short[] toShortArray()
          Creates an array of shorts from the contents of this set.
 String toString()
           
 
Methods inherited from class java.util.AbstractSet
removeAll
 
Methods inherited from class java.util.AbstractCollection
toArray
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
removeAll, toArray
 

Field Detail

_values

protected int[] _values
An array containing the values in this set.


_size

protected int _size
The number of elements in this set.


DEFAULT_CAPACITY

protected static final int DEFAULT_CAPACITY
The default initial capacity of this set.

See Also:
Constant Field Values
Constructor Detail

ArrayIntSet

public ArrayIntSet(int[] values)
Construct an ArrayIntSet with the specified starting values.


ArrayIntSet

public ArrayIntSet(int initialCapacity)
Construct an ArrayIntSet of the specified initial capacity.


ArrayIntSet

public ArrayIntSet()
Constructs an empty set with the default initial capacity.

Method Detail

size

public int size()
Specified by:
size in interface Collection<Integer>
Specified by:
size in interface Set<Integer>
Specified by:
size in class AbstractCollection<Integer>

get

public int get(int index)
Returns the element at the specified index. Note that the elements in the set are unordered and could change order after insertion or removal. This method is useful only for accessing elements of a static set (and has the desirable property of allowing access to the values in this set without having to create integer objects).


isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<Integer>
Specified by:
isEmpty in interface Set<Integer>
Overrides:
isEmpty in class AbstractCollection<Integer>

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<Integer>
Specified by:
contains in interface Set<Integer>
Overrides:
contains in class AbstractCollection<Integer>

contains

public boolean contains(int value)
Description copied from interface: IntSet
Returns true if this set contains the specified element.

Specified by:
contains in interface IntSet
Parameters:
value - element whose presence in this set is to be tested.
Returns:
true if this set contains the specified element.

interator

public Interator interator()
Description copied from interface: Interable
Return an Interator over the ints in this object.

Specified by:
interator in interface Interable
Specified by:
interator in interface IntSet

iterator

public Iterator<Integer> iterator()
Specified by:
iterator in interface Iterable<Integer>
Specified by:
iterator in interface Collection<Integer>
Specified by:
iterator in interface Set<Integer>
Specified by:
iterator in class AbstractCollection<Integer>

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<Integer>
Specified by:
toArray in interface Set<Integer>
Overrides:
toArray in class AbstractCollection<Integer>

toArray

public Integer[] toArray(Integer[] a)

toIntArray

public int[] toIntArray()
Description copied from interface: IntSet
Returns an array containing all of the elements in this set. Obeys the general contract of the Collection.toArray method.

Specified by:
toIntArray in interface IntSet
Returns:
an array containing all of the elements in this set.

toIntArray

public int[] toIntArray(int[] target,
                        int offset)
Serializes this int set into an array at the specified offset. The array must be large enough to hold all the integers in our set at the offset specified.

Returns:
the array passed in.

toShortArray

public short[] toShortArray()
Creates an array of shorts from the contents of this set. Any values outside the range of a short will be truncated by way of a cast.


add

public boolean add(Integer o)
Specified by:
add in interface Collection<Integer>
Specified by:
add in interface Set<Integer>
Overrides:
add in class AbstractCollection<Integer>

add

public boolean add(int value)
Description copied from interface: IntSet
Adds the specified element to this set if it is not already present (optional operation). If this set already contains the specified element, the call leaves this set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.

Specified by:
add in interface IntSet
Parameters:
value - element to be added to this set.
Returns:
true if this set did not already contain the specified element.

add

public boolean add(int[] values)
Add all of the values in the supplied array to the set.

Parameters:
values - elements to be added to this set.
Returns:
true if this set did not already contain all of the specified elements.

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<Integer>
Specified by:
remove in interface Set<Integer>
Overrides:
remove in class AbstractCollection<Integer>

remove

public boolean remove(int value)
Description copied from interface: IntSet
Removes the specified element from this set if it is present (optional operation). Returns true if the set contained the specified element (or equivalently, if the set changed as a result of the call). (The set will not contain the specified element once the call returns.)

Specified by:
remove in interface IntSet
Parameters:
value - element to be removed from this set, if present.
Returns:
true if the set contained the specified element.

remove

public boolean remove(int[] values)
Removes all values in the supplied array from the set. Any values that are in the array but not in the set are simply ignored.

Parameters:
values - elements to be removed from the set.
Returns:
true if this set contained any of the specified elements (which will have been removed).

containsAll

public boolean containsAll(Collection<?> c)
Specified by:
containsAll in interface Collection<Integer>
Specified by:
containsAll in interface Set<Integer>
Overrides:
containsAll in class AbstractCollection<Integer>

addAll

public boolean addAll(Collection<? extends Integer> c)
Specified by:
addAll in interface Collection<Integer>
Specified by:
addAll in interface Set<Integer>
Overrides:
addAll in class AbstractCollection<Integer>

retainAll

public boolean retainAll(Collection<?> c)
Specified by:
retainAll in interface Collection<Integer>
Specified by:
retainAll in interface Set<Integer>
Overrides:
retainAll in class AbstractCollection<Integer>

clear

public void clear()
Specified by:
clear in interface Collection<Integer>
Specified by:
clear in interface Set<Integer>
Overrides:
clear in class AbstractCollection<Integer>

equals

public boolean equals(Object o)
Specified by:
equals in interface Collection<Integer>
Specified by:
equals in interface Set<Integer>
Overrides:
equals in class AbstractSet<Integer>

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection<Integer>
Specified by:
hashCode in interface Set<Integer>
Overrides:
hashCode in class AbstractSet<Integer>

clone

public Object clone()
Overrides:
clone in class Object

toString

public String toString()
Overrides:
toString in class AbstractCollection<Integer>

binarySearch

protected int binarySearch(int key)
Performs a binary search on our values array, looking for the specified value. Swiped from java.util.Arrays because those wankers didn't provide a means by which to perform a binary search on a subset of an array.



Copyright © 2000-2008 Michael Bayne. All Rights Reserved.