From 7739d75780227ea75517475950488bfabc8ead72 Mon Sep 17 00:00:00 2001 From: ray Date: Sat, 11 Mar 2006 02:40:49 +0000 Subject: [PATCH] Shrink our capacity by half if, after removing an element, it is bigger than the default capacity and we're using less than 1/8th of it. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1799 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/ArrayIntSet.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/java/com/samskivert/util/ArrayIntSet.java b/src/java/com/samskivert/util/ArrayIntSet.java index 15565293..e7764cab 100644 --- a/src/java/com/samskivert/util/ArrayIntSet.java +++ b/src/java/com/samskivert/util/ArrayIntSet.java @@ -57,7 +57,7 @@ public class ArrayIntSet extends AbstractSet */ public ArrayIntSet () { - this(16); + this(DEFAULT_CAPACITY); } // documentation inherited from interface @@ -256,8 +256,20 @@ public class ArrayIntSet extends AbstractSet { int index = binarySearch(value); if (index >= 0) { - System.arraycopy(_values, index+1, _values, index, --_size-index); - _values[_size] = 0; + _size--; + if ((_values.length > DEFAULT_CAPACITY) && + (_size < _values.length/8)) { + // if we're using less than 1/8 of our capacity, shrink by half + int[] newVals = new int[_values.length/2]; + System.arraycopy(_values, 0, newVals, 0, index); + System.arraycopy(_values, index+1, newVals, index, _size-index); + _values = newVals; + + } else { + // shift entries past the removed one downwards + System.arraycopy(_values, index+1, _values, index, _size-index); + _values[_size] = 0; + } return true; } return false; @@ -436,6 +448,9 @@ public class ArrayIntSet extends AbstractSet /** The number of elements in this set. */ protected int _size; + /** The default initial capacity of this set. */ + protected static final int DEFAULT_CAPACITY = 16; + /** Change this if the fields or inheritance hierarchy ever changes * (which is extremely unlikely). We override this because I'm tired * of serialized crap not working depending on whether I compiled with