1 19 package gnu.trove; 20 21 import java.io.Serializable; 22 23 31 32 abstract public class TIntHash extends TPrimitiveHash 33 implements Serializable, TIntHashingStrategy { 34 35 36 protected transient int[] _set; 37 38 39 protected TIntHashingStrategy _hashingStrategy; 40 41 45 public TIntHash() { 46 super(); 47 this._hashingStrategy = this; 48 } 49 50 57 public TIntHash(int initialCapacity) { 58 super(initialCapacity); 59 this._hashingStrategy = this; 60 } 61 62 70 public TIntHash(int initialCapacity, float loadFactor) { 71 super(initialCapacity, loadFactor); 72 this._hashingStrategy = this; 73 } 74 75 80 public TIntHash(TIntHashingStrategy strategy) { 81 super(); 82 this._hashingStrategy = strategy; 83 } 84 85 93 public TIntHash(int initialCapacity, TIntHashingStrategy strategy) { 94 super(initialCapacity); 95 this._hashingStrategy = strategy; 96 } 97 98 107 public TIntHash(int initialCapacity, float loadFactor, TIntHashingStrategy strategy) { 108 super(initialCapacity, loadFactor); 109 this._hashingStrategy = strategy; 110 } 111 112 115 public Object clone() { 116 TIntHash h = (TIntHash)super.clone(); 117 h._set = (int[])this._set.clone(); 118 return h; 119 } 120 121 128 protected int setUp(int initialCapacity) { 129 int capacity; 130 131 capacity = super.setUp(initialCapacity); 132 _set = new int[capacity]; 133 return capacity; 134 } 135 136 142 public boolean contains(int val) { 143 return index(val) >= 0; 144 } 145 146 153 public boolean forEach(TIntProcedure procedure) { 154 byte[] states = _states; 155 int[] set = _set; 156 for (int i = set.length; i-- > 0;) { 157 if (states[i] == FULL && ! procedure.execute(set[i])) { 158 return false; 159 } 160 } 161 return true; 162 } 163 164 169 protected void removeAt(int index) { 170 super.removeAt(index); 171 _set[index] = (int)0; 172 } 173 174 180 protected int index(int val) { 181 int hash, probe, index, length; 182 int[] set; 183 byte[] states; 184 185 states = _states; 186 set = _set; 187 length = states.length; 188 hash = _hashingStrategy.computeHashCode(val) & 0x7fffffff; 189 index = hash % length; 190 191 if (states[index] != FREE && 192 (states[index] == REMOVED || set[index] != val)) { 193 probe = 1 + (hash % (length - 2)); 195 196 do { 197 index -= probe; 198 if (index < 0) { 199 index += length; 200 } 201 } while (states[index] != FREE && 202 (states[index] == REMOVED || set[index] != val)); 203 } 204 205 return states[index] == FREE ? -1 : index; 206 } 207 208 216 protected int insertionIndex(int val) { 217 int hash, probe, index, length; 218 int[] set; 219 byte[] states; 220 221 states = _states; 222 set = _set; 223 length = states.length; 224 hash = _hashingStrategy.computeHashCode(val) & 0x7fffffff; 225 index = hash % length; 226 227 if (states[index] == FREE) { 228 return index; } else if (states[index] == FULL && set[index] == val) { 230 return -index -1; } else { probe = 1 + (hash % (length - 2)); 234 235 247 if (states[index] != REMOVED) { 248 do { 251 index -= probe; 252 if (index < 0) { 253 index += length; 254 } 255 } while (states[index] == FULL && set[index] != val); 256 } 257 258 if (states[index] == REMOVED) { 262 int firstRemoved = index; 263 while (states[index] != FREE && 264 (states[index] == REMOVED || set[index] != val)) { 265 index -= probe; 266 if (index < 0) { 267 index += length; 268 } 269 } 270 return states[index] == FULL ? -index -1 : firstRemoved; 271 } 272 return states[index] == FULL ? -index -1 : index; 274 } 275 } 276 277 284 public final int computeHashCode(int val) { 285 return HashFunctions.hash(val); 286 } 287 } | Popular Tags |