1 19 package gnu.trove; 20 21 32 33 abstract public class THash implements Cloneable { 34 35 protected transient int _size; 36 37 38 protected transient int _free; 39 40 41 protected static final float DEFAULT_LOAD_FACTOR = 0.5f; 42 43 48 protected static final int DEFAULT_INITIAL_CAPACITY = 10; 49 50 56 protected float _loadFactor; 57 58 62 protected int _maxSize; 63 64 65 68 protected int _autoCompactRemovesRemaining; 69 70 75 protected float _autoCompactionFactor; 76 77 78 79 83 public THash() { 84 this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); 85 } 86 87 94 public THash(int initialCapacity) { 95 this(initialCapacity, DEFAULT_LOAD_FACTOR); 96 } 97 98 107 public THash(int initialCapacity, float loadFactor) { 108 super(); 109 _loadFactor = loadFactor; 110 111 _autoCompactionFactor = loadFactor; 114 115 setUp((int)Math.ceil(initialCapacity / loadFactor)); 116 } 117 118 public Object clone() { 119 try { 120 return super.clone(); 121 } catch (CloneNotSupportedException cnse) { 122 return null; } 124 } 125 126 131 public boolean isEmpty() { 132 return 0 == _size; 133 } 134 135 140 public int size() { 141 return _size; 142 } 143 144 147 abstract protected int capacity(); 148 149 157 public void ensureCapacity(int desiredCapacity) { 158 if (desiredCapacity > (_maxSize - size())) { 159 rehash(PrimeFinder.nextPrime((int)Math.ceil(desiredCapacity + size() / 160 _loadFactor) + 1)); 161 computeMaxSize(capacity()); 162 } 163 } 164 165 182 public void compact() { 183 rehash(PrimeFinder.nextPrime((int)Math.ceil(size()/_loadFactor) + 1)); 185 computeMaxSize(capacity()); 186 187 if ( _autoCompactionFactor != 0 ) { 189 computeNextAutoCompactionAmount(size()); 190 } 191 } 192 193 194 203 public void setAutoCompactionFactor( float factor ) { 204 if ( factor < 0 ) { 205 throw new IllegalArgumentException ( "Factor must be >= 0: " + factor ); 206 } 207 208 _autoCompactionFactor = factor; 209 } 210 211 214 public float getAutoCompactionFactor() { 215 return _autoCompactionFactor; 216 } 217 218 219 228 public final void trimToSize() { 229 compact(); 230 } 231 232 238 protected void removeAt(int index) { 239 _size--; 240 241 if ( _autoCompactionFactor != 0 ) { 243 _autoCompactRemovesRemaining--; 244 245 if ( _autoCompactRemovesRemaining == 0 ) { 246 compact(); 249 } 250 } 251 } 252 253 256 public void clear() { 257 _size = 0; 258 _free = capacity(); 259 } 260 261 268 protected int setUp(int initialCapacity) { 269 int capacity; 270 271 capacity = PrimeFinder.nextPrime(initialCapacity); 272 computeMaxSize(capacity); 273 computeNextAutoCompactionAmount(initialCapacity); 274 275 return capacity; 276 } 277 278 283 protected abstract void rehash(int newCapacity); 284 285 291 private final void computeMaxSize(int capacity) { 292 _maxSize = Math.min(capacity - 1, 294 (int)Math.floor(capacity * _loadFactor)); 295 _free = capacity - _size; } 297 298 299 303 private void computeNextAutoCompactionAmount( int size ) { 304 if ( _autoCompactionFactor != 0 ) { 305 _autoCompactRemovesRemaining = Math.round( size * _autoCompactionFactor ); 306 } 307 } 308 309 310 314 protected final void postInsertHook(boolean usedFreeSlot) { 315 if (usedFreeSlot) { 316 _free--; 317 } 318 319 if (++_size > _maxSize || _free == 0) { 321 int newCapacity = _size > _maxSize ? PrimeFinder.nextPrime(capacity() << 1) : capacity(); 326 rehash(newCapacity); 327 computeMaxSize(capacity()); 328 } 329 } 330 331 protected int calculateGrownCapacity() { 332 return capacity() << 1; 333 } 334 } | Popular Tags |