From deb50bc136916b999db28545ab82c7fb994f7ca9 Mon Sep 17 00:00:00 2001 From: "ray.j.greenwell" Date: Fri, 11 Dec 2009 23:39:53 +0000 Subject: [PATCH] - 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 --- src/java/com/samskivert/util/ArrayIntSet.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/java/com/samskivert/util/ArrayIntSet.java b/src/java/com/samskivert/util/ArrayIntSet.java index 70824860..47965549 100644 --- a/src/java/com/samskivert/util/ArrayIntSet.java +++ b/src/java/com/samskivert/util/ArrayIntSet.java @@ -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; }; }