提交 519c40d1 编写于 作者: V Vlad Ilyushchenko

tidying up hash function

上级 67b4cfb8
......@@ -36,68 +36,6 @@ public class Hash {
return (value >>> n) | (value << (32 - n));
}
public static int hashXX(MemoryBuffer data, int seed) {
int i32;
int p = 0;
int len = data.length();
if (len >= 16) {
int limit = len - 16;
int v1 = seed + PRIME32_1 + PRIME32_2;
int v2 = seed + PRIME32_2;
int v3 = seed;
int v4 = seed - PRIME32_1;
do {
v1 += data.getInt(p) * PRIME32_2;
v1 = rotl(v1, 13);
v1 *= PRIME32_1;
p += 4;
v2 += data.getInt(p) * PRIME32_2;
v2 = rotl(v2, 13);
v2 *= PRIME32_1;
p += 4;
v3 += data.getInt(p) * PRIME32_2;
v3 = rotl(v3, 13);
v3 *= PRIME32_1;
p += 4;
int i = data.getInt(p);
v4 += i * PRIME32_2;
v4 = rotl(v4, 13);
v4 *= PRIME32_1;
p += 4;
}
while (p <= limit);
i32 = rotl(v1, 1) + rotl(v2, 7) + rotl(v3, 12) + rotl(v4, 18);
} else {
i32 = seed + PRIME32_5;
}
i32 += len;
while (p + 4 <= len) {
i32 += data.getInt(p) * PRIME32_3;
i32 = rotl(i32, 17) * PRIME32_4;
p += 4;
}
while (p < len) {
i32 += data.getByte(p) * PRIME32_5;
i32 = rotl(i32, 11) * PRIME32_1;
p++;
}
i32 ^= i32 >> 15;
i32 *= PRIME32_2;
i32 ^= i32 >> 13;
i32 *= PRIME32_3;
i32 ^= i32 >> 16;
return i32;
}
public static int hashXX(long address, int len, int seed) {
int i32;
long p = address;
......
/*
* Copyright (c) 2014. Vlad Ilyushchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.nfsdb.journal.utils;
public interface MemoryBuffer {
int length();
int getInt(int p);
byte getByte(int p);
}
\ No newline at end of file
......@@ -63,6 +63,14 @@ public class Rnd {
return new String(chars);
}
public void nextChars(long address, int len) {
long limit = address + len - 2;
while (address < limit) {
Unsafe.getUnsafe().putChar(address, (char) (nextPositiveInt() % 25 + 66));
address += 2;
}
}
public byte[] nextBytes(int len) {
byte bytes[] = new byte[len];
for (int i = 0; i < len; i++) {
......
/*
* Copyright (c) 2014-2015. Vlad Ilyushchenko
* Copyright (c) 2014. Vlad Ilyushchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -18,7 +18,6 @@ package com.nfsdb.journal.lang.experimental;
import com.nfsdb.journal.collections.IntHashSet;
import com.nfsdb.journal.utils.Hash;
import com.nfsdb.journal.utils.MemoryBuffer;
import com.nfsdb.journal.utils.Rnd;
import com.nfsdb.journal.utils.Unsafe;
import org.junit.Assert;
......@@ -28,49 +27,20 @@ public class HashTest {
@Test
public void testStringHash() throws Exception {
MemoryBufferImpl buf = new MemoryBufferImpl(30);
Rnd rnd = new Rnd();
IntHashSet hashes = new IntHashSet(100000);
final int LEN = 30;
long address = Unsafe.getUnsafe().allocateMemory(LEN);
for (int i = 0; i < 100000; i++) {
buf.put(rnd.nextString(15));
hashes.add(Hash.hashXX(buf, rnd.nextInt()));
rnd.nextChars(address, LEN);
hashes.add(Hash.hashXX(address, LEN, rnd.nextInt()));
}
Assert.assertTrue("Hash function distribution dropped", hashes.size() > 99990);
}
private static class MemoryBufferImpl implements MemoryBuffer {
private final long address;
private final int len;
public MemoryBufferImpl(int len) {
this.len = len;
this.address = Unsafe.getUnsafe().allocateMemory(len);
}
@Override
public int length() {
return len;
}
@Override
public int getInt(int p) {
return Unsafe.getUnsafe().getInt(address + p);
}
System.out.println(hashes.size());
@Override
public byte getByte(int p) {
return Unsafe.getUnsafe().getByte(address + p);
}
Assert.assertTrue("Hash function distribution dropped", hashes.size() > 99990);
public void put(CharSequence value) {
for (int i = 0; i < value.length(); i++) {
Unsafe.getUnsafe().putChar(address + i * 2, value.charAt(i));
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册