- Behave better at large sizes.

- Don't wig-out trying to grow from a size larger
    than half of Integer.MAX_VALUE.
  - Don't try to add any values once size reaches Integer.MAX_VALUE.
- Fixed the Interator's remove() to behave properly should it be
  called improperly.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2679 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray.j.greenwell
2009-12-11 23:39:53 +00:00
parent 791121ff37
commit deb50bc136
+12 -6
View File
@@ -167,6 +167,9 @@ public class ArrayIntSet extends AbstractIntSet
if (index >= 0) {
return false;
}
if (_size == Integer.MAX_VALUE) {
throw new IllegalStateException("Cannot grow ArrayIntSet at maximum size.");
}
// convert the return value into the insertion point
index += 1;
@@ -176,7 +179,10 @@ public class ArrayIntSet extends AbstractIntSet
int valen = _values.length;
int[] source = _values;
if (valen == _size) {
_values = new int[Math.max(DEFAULT_CAPACITY, valen*2)];
int newLen = (valen >= Integer.MAX_VALUE/2)
? Integer.MAX_VALUE
: Math.max(DEFAULT_CAPACITY, valen*2);
_values = new int[newLen];
System.arraycopy(source, 0, _values, 0, index);
}
@@ -226,23 +232,23 @@ public class ArrayIntSet extends AbstractIntSet
public int nextInt () {
if (_pos >= _size) {
throw new NoSuchElementException();
} else {
return _values[_pos++];
}
_canRemove = true;
return _values[_pos++];
}
@Override public void remove () {
if (_pos == 0) {
if (!_canRemove) {
throw new IllegalStateException();
}
// does not correctly return IllegalStateException if
// remove() is called twice in a row...
System.arraycopy(_values, _pos, _values, _pos - 1, _size - _pos);
_pos--;
_size--; //_values[--_size] = 0;
_canRemove = false;
}
protected int _pos;
protected boolean _canRemove;
};
}