1 22 23 package com.sosnoski.util.hashset; 24 25 41 42 public class DoubleHashSet extends PrimitiveSetBase 43 { 44 45 protected double[] m_keyTable; 46 47 53 54 public DoubleHashSet(int count, double fill) { 55 super(count, fill, double.class); 56 } 57 58 64 65 public DoubleHashSet(int count) { 66 this(count, DEFAULT_FILL); 67 } 68 69 72 73 public DoubleHashSet() { 74 this(0, DEFAULT_FILL); 75 } 76 77 82 83 public DoubleHashSet(DoubleHashSet base) { 84 super(base); 85 } 86 87 94 95 protected Object getKeyArray() { 96 return m_keyTable; 97 } 98 99 106 107 protected void setKeyArray(Object array) { 108 m_keyTable = (double[])array; 109 } 110 111 120 121 protected final boolean reinsert(int slot) { 122 m_flagTable[slot] = false; 123 return assignSlot(m_keyTable[slot]) != slot; 124 } 125 126 136 137 protected void restructure(boolean[] flags, Object karray) { 138 double[] keys = (double[])karray; 139 for (int i = 0; i < flags.length; i++) { 140 if (flags[i]) { 141 assignSlot(keys[i]); 142 } 143 } 144 } 145 146 152 153 protected final int computeSlot(double key) { 154 long bits = Double.doubleToRawLongBits(key); 155 int reduced = ((int)(bits >> 48)) + ((int)(bits >> 24)) + (int)bits; 156 return (reduced * KEY_MULTIPLIER & Integer.MAX_VALUE) % 157 m_flagTable.length; 158 } 159 160 170 171 protected int assignSlot(double key) { 172 int offset = freeSlot(computeSlot(key)); 173 m_flagTable[offset] = true; 174 m_keyTable[offset] = key; 175 return offset; 176 } 177 178 185 186 public boolean add(double key) { 187 ensureCapacity(m_entryCount+1); 188 int offset = -internalFind(key) - 1; 189 if (offset >= 0) { 190 m_entryCount++; 191 m_flagTable[offset] = true; 192 m_keyTable[offset] = key; 193 return true; 194 } else { 195 return false; 196 } 197 } 198 199 206 207 protected final int internalFind(double key) { 208 int slot = computeSlot(key); 209 while (m_flagTable[slot]) { 210 if (key == m_keyTable[slot]) { 211 return slot; 212 } 213 slot = stepSlot(slot); 214 } 215 return -slot - 1; 216 } 217 218 225 226 public boolean contains(double key) { 227 return internalFind(key) >= 0; 228 } 229 230 237 238 public boolean remove(double key) { 239 int slot = internalFind(key); 240 if (slot >= 0) { 241 m_flagTable[slot] = false; 242 m_entryCount--; 243 while (m_flagTable[(slot = stepSlot(slot))]) { 244 reinsert(slot); 245 } 246 return true; 247 } else { 248 return false; 249 } 250 } 251 252 257 258 public Object clone() { 259 return new DoubleHashSet(this); 260 } 261 } 262 | Popular Tags |