@Test public void testMap() { UniformMapper<Integer, Integer> mapper = new SHA256UniformMapper<>(new Random(42)); int cnt = 0; for (int i = 0; i < 100_000; i++) { double pnt = mapper.map(i, i); if (pnt < 0.2) cnt++; } double err = 1.0 * Math.abs(cnt - 20_000) / 20_000; assertTrue(err < 0.02); }
@Override public double map(K key, V val) { int h = Math.abs(key.hashCode()); String str = String.valueOf(key.hashCode()); byte[] hash = getDigest().digest(str.getBytes(StandardCharsets.UTF_8)); byte hashByte = hash[h % hash.length]; byte resByte = 0; for (int i = 0; i < 8; i++) resByte = (byte)(resByte << 1 | ((hashByte >> shuffleStgy.get(i)) & 0x1)); return 1.0 * (resByte & 0xFF) / 256; }
SHA256UniformMapper implements UniformMapper<K,V> { @Override public double map(K key, V val) { int h = Math.abs(key.hashCode()); String str = String.valueOf(key.hashCode()); byte[] hash = getDigest().digest(str.getBytes(StandardCharsets.UTF_8)); byte hashByte = hash[h % hash.length]; byte resByte = 0; for (int i = 0; i < 8; i++) resByte = (byte)(resByte << 1 | ((hashByte >> shuffleStgy.get(i)) & 0x1)); return 1.0 * (resByte & 0xFF) / 256; } }
SHA256UniformMapper implements UniformMapper<K,V> { @Override public double map(K key, V val) { int h = Math.abs(key.hashCode()); String str = String.valueOf(key.hashCode()); byte[] hash = getDigest().digest(str.getBytes(StandardCharsets.UTF_8)); byte hashByte = hash[h % hash.length]; byte resByte = 0; for (int i = 0; i < 8; i++) resByte = (byte)(resByte << 1 | ((hashByte >> shuffleStgy.get(i)) & 0x1)); return 1.0 * (resByte & 0xFF) / 256; } SHA256UniformMapper();  SHA256UniformMapper(Random random); }
SHA256UniformMapper implements UniformMapper<K,V> { @Override public double map(K key, V val) { int h = Math.abs(key.hashCode()); String str = String.valueOf(key.hashCode()); byte[] hash = getDigest().digest(str.getBytes(StandardCharsets.UTF_8)); byte hashByte = hash[h % hash.length]; byte resByte = 0; for (int i = 0; i < 8; i++) resByte = (byte)(resByte << 1 | ((hashByte >> shuffleStgy.get(i)) & 0x1)); return 1.0 * (resByte & 0xFF) / 256; } SHA256UniformMapper();  SHA256UniformMapper(Random random); @Override double map(K key, V val); }
SHA256UniformMapper implements UniformMapper<K,V> { @Override public double map(K key, V val) { int h = Math.abs(key.hashCode()); String str = String.valueOf(key.hashCode()); byte[] hash = getDigest().digest(str.getBytes(StandardCharsets.UTF_8)); byte hashByte = hash[h % hash.length]; byte resByte = 0; for (int i = 0; i < 8; i++) resByte = (byte)(resByte << 1 | ((hashByte >> shuffleStgy.get(i)) & 0x1)); return 1.0 * (resByte & 0xFF) / 256; } SHA256UniformMapper();  SHA256UniformMapper(Random random); @Override double map(K key, V val);  }