A List of int's; as full an implementation of the java.util.List
interface as possible, with an eye toward minimal creation of
objects
the mimicry of List is as follows:
| Method from org.apache.poi.util.IntList Detail: |
public boolean add(int value) {
if (_limit == _array.length)
{
growArray(_limit * 2);
}
_array[ _limit++ ] = value;
return true;
}
Appends the specified element to the end of this list |
public void add(int index,
int value) {
if (index > _limit)
{
throw new IndexOutOfBoundsException();
}
else if (index == _limit)
{
add(value);
}
else
{
// index < limit -- insert into the middle
if (_limit == _array.length)
{
growArray(_limit * 2);
}
System.arraycopy(_array, index, _array, index + 1,
_limit - index);
_array[ index ] = value;
_limit++;
}
}
add the specfied value at the specified index |
public boolean addAll(IntList c) {
if (c._limit != 0)
{
if ((_limit + c._limit) > _array.length)
{
growArray(_limit + c._limit);
}
System.arraycopy(c._array, 0, _array, _limit, c._limit);
_limit += c._limit;
}
return true;
}
Appends all of the elements in the specified collection to the
end of this list, in the order that they are returned by the
specified collection's iterator. The behavior of this
operation is unspecified if the specified collection is
modified while the operation is in progress. (Note that this
will occur if the specified collection is this list, and it's
nonempty.) |
public boolean addAll(int index,
IntList c) {
if (index > _limit)
{
throw new IndexOutOfBoundsException();
}
if (c._limit != 0)
{
if ((_limit + c._limit) > _array.length)
{
growArray(_limit + c._limit);
}
// make a hole
System.arraycopy(_array, index, _array, index + c._limit,
_limit - index);
// fill it in
System.arraycopy(c._array, 0, _array, index, c._limit);
_limit += c._limit;
}
return true;
}
Inserts all of the elements in the specified collection into
this list at the specified position. Shifts the element
currently at that position (if any) and any subsequent elements
to the right (increases their indices). The new elements will
appear in this list in the order that they are returned by the
specified collection's iterator. The behavior of this
operation is unspecified if the specified collection is
modified while the operation is in progress. (Note that this
will occur if the specified collection is this list, and it's
nonempty.) |
public void clear() {
_limit = 0;
}
Removes all of the elements from this list. This list will be
empty after this call returns (unless it throws an exception). |
public boolean contains(int o) {
boolean rval = false;
for (int j = 0; !rval && (j < _limit); j++)
{
if (_array[ j ] == o)
{
rval = true;
}
}
return rval;
}
Returns true if this list contains the specified element. More
formally, returns true if and only if this list contains at
least one element e such that o == e |
public boolean containsAll(IntList c) {
boolean rval = true;
if (this != c)
{
for (int j = 0; rval && (j < c._limit); j++)
{
if (!contains(c._array[ j ]))
{
rval = false;
}
}
}
return rval;
}
Returns true if this list contains all of the elements of the
specified collection. |
public boolean equals(Object o) {
boolean rval = this == o;
if (!rval && (o != null) && (o.getClass() == this.getClass()))
{
IntList other = ( IntList ) o;
if (other._limit == _limit)
{
// assume match
rval = true;
for (int j = 0; rval && (j < _limit); j++)
{
rval = _array[ j ] == other._array[ j ];
}
}
}
return rval;
}
Compares the specified object with this list for equality.
Returns true if and only if the specified object is also a
list, both lists have the same size, and all corresponding
pairs of elements in the two lists are equal. (Two elements e1
and e2 are equal if e1 == e2.) In other words, two lists are
defined to be equal if they contain the same elements in the
same order. This definition ensures that the equals method
works properly across different implementations of the List
interface. |
public int get(int index) {
if (index >= _limit)
{
throw new IndexOutOfBoundsException();
}
return _array[ index ];
}
Returns the element at the specified position in this list. |
public int hashCode() {
int hash = 0;
for (int j = 0; j < _limit; j++)
{
hash = (31 * hash) + _array[ j ];
}
return hash;
}
Returns the hash code value for this list. The hash code of a
list is defined to be the result of the following calculation:
hashCode = 1;
Iterator i = list.iterator();
while (i.hasNext()) {
Object obj = i.next();
hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
}
This ensures that list1.equals(list2) implies that
list1.hashCode()==list2.hashCode() for any two lists, list1 and
list2, as required by the general contract of Object.hashCode. |
public int indexOf(int o) {
int rval = 0;
for (; rval < _limit; rval++)
{
if (o == _array[ rval ])
{
break;
}
}
if (rval == _limit)
{
rval = -1; // didn't find it
}
return rval;
}
Returns the index in this list of the first occurrence of the
specified element, or -1 if this list does not contain this
element. More formally, returns the lowest index i such that
(o == get(i)), or -1 if there is no such index. |
public boolean isEmpty() {
return _limit == 0;
}
Returns true if this list contains no elements. |
public int lastIndexOf(int o) {
int rval = _limit - 1;
for (; rval >= 0; rval--)
{
if (o == _array[ rval ])
{
break;
}
}
return rval;
}
Returns the index in this list of the last occurrence of the
specified element, or -1 if this list does not contain this
element. More formally, returns the highest index i such that
(o == get(i)), or -1 if there is no such index. |
public int remove(int index) {
if (index >= _limit)
{
throw new IndexOutOfBoundsException();
}
int rval = _array[ index ];
System.arraycopy(_array, index + 1, _array, index, _limit - index);
_limit--;
return rval;
}
Removes the element at the specified position in this list.
Shifts any subsequent elements to the left (subtracts one from
their indices). Returns the element that was removed from the
list. |
public boolean removeAll(IntList c) {
boolean rval = false;
for (int j = 0; j < c._limit; j++)
{
if (removeValue(c._array[ j ]))
{
rval = true;
}
}
return rval;
}
Removes from this list all the elements that are contained in
the specified collection |
public boolean removeValue(int o) {
boolean rval = false;
for (int j = 0; !rval && (j < _limit); j++)
{
if (o == _array[ j ])
{
if (j+1 < _limit) {
System.arraycopy(_array, j + 1, _array, j, _limit - j);
}
_limit--;
rval = true;
}
}
return rval;
}
Removes the first occurrence in this list of the specified
element (optional operation). If this list does not contain
the element, it is unchanged. More formally, removes the
element with the lowest index i such that (o.equals(get(i)))
(if such an element exists). |
public boolean retainAll(IntList c) {
boolean rval = false;
for (int j = 0; j < _limit; )
{
if (!c.contains(_array[ j ]))
{
remove(j);
rval = true;
}
else
{
j++;
}
}
return rval;
}
Retains only the elements in this list that are contained in
the specified collection. In other words, removes from this
list all the elements that are not contained in the specified
collection. |
public int set(int index,
int element) {
if (index >= _limit)
{
throw new IndexOutOfBoundsException();
}
int rval = _array[ index ];
_array[ index ] = element;
return rval;
}
Replaces the element at the specified position in this list
with the specified element |
public int size() {
return _limit;
}
Returns the number of elements in this list. If this list
contains more than Integer.MAX_VALUE elements, returns
Integer.MAX_VALUE. |
public int[] toArray() {
int[] rval = new int[ _limit ];
System.arraycopy(_array, 0, rval, 0, _limit);
return rval;
}
Returns an array containing all of the elements in this list in
proper sequence. Obeys the general contract of the
Collection.toArray method. |
public int[] toArray(int[] a) {
int[] rval;
if (a.length == _limit)
{
System.arraycopy(_array, 0, a, 0, _limit);
rval = a;
}
else
{
rval = toArray();
}
return rval;
}
Returns an array containing all of the elements in this list in
proper sequence. Obeys the general contract of the
Collection.toArray(Object[]) method. |