Skip to content

Commit 158b212

Browse files
Reworked again
1 parent f477d52 commit 158b212

File tree

5 files changed

+119
-66
lines changed

5 files changed

+119
-66
lines changed

src/Tests/Test_BasicHashingGenerator.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Tests/Test_hgCombined.java

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,30 @@
33
import org.junit.Assert;
44
import org.junit.Test;
55

6+
import YASL.Hashing.CIntegerHashing;
67
import YASL.Hashing.ChgCombined;
78
import YASL.Hashing.IHasher;
89

910
public class Test_hgCombined {
11+
1012
@Test
1113
public void returnHashesFirst() {
1214
IHasher<Integer> gen = new ChgCombined<Integer>( //
13-
x -> 1, x -> 2 //
15+
new CIntegerHashing<>(x -> 1), //
16+
new CIntegerHashing<>(x -> 2) //
1417
).generate(Integer.MAX_VALUE, 2);
1518

1619
Assert.assertArrayEquals( //
17-
new int[] { 1, 2 }, //
18-
gen.apply(1) //
20+
new int[] { 1, 2 }, gen.apply(1) //
1921
);
2022
}
2123

2224
@Test
2325
public void respectRange() {
2426
IHasher<Integer> gen = new ChgCombined<Integer>( //
25-
x -> 21, x -> -24//
27+
(x, r) -> x, // salt
28+
(x, r) -> r.put((byte) 21), //
29+
(x, r) -> r.put((byte) -24) //
2630
).generate(5, 2);
2731

2832
Assert.assertArrayEquals( //
@@ -34,28 +38,62 @@ public void respectRange() {
3438
@Test
3539
public void useOnlyWhatNeed() {
3640
IHasher<Integer> gen = new ChgCombined<Integer>( //
37-
x -> 1, //
38-
x -> {
41+
new CIntegerHashing<>(x -> 1), //
42+
(x, r) -> {
3943
throw new RuntimeException("Don't touch me!");
40-
}//
41-
).generate(5, 1);
44+
} //
45+
).generate(Integer.MAX_VALUE, 1);
4246

43-
Assert.assertArrayEquals( //
44-
new int[] { 1 }, //
45-
gen.apply(1) //
46-
);
47+
Assert.assertArrayEquals(new int[] { 1 }, gen.apply(2));
4748
}
4849

4950
@Test
5051
public void applySalting() {
5152
IHasher<Integer> gen = new ChgCombined<Integer>( //
5253
(x, r) -> x * r * 4, //
53-
x -> x, x -> x + 1 //
54+
new CIntegerHashing<>(x -> x), //
55+
new CIntegerHashing<>(x -> x + 1) //
5456
).generate(Integer.MAX_VALUE, 5);
5557

5658
Assert.assertArrayEquals( //
57-
new int[] { 1, 2, 4, 5, 8 }, //
58-
gen.apply(1) //
59+
new int[] { 1, 2, 4, 5, 8 }, gen.apply(1) //
60+
);
61+
}
62+
63+
@Test
64+
public void sizeOfRange() {
65+
ChgCombined<Integer> gen = new ChgCombined<Integer>(null);
66+
Assert.assertEquals(1, gen.bytesIn(3));
67+
68+
Assert.assertEquals(2, gen.bytesIn(256));
69+
Assert.assertEquals(2, gen.bytesIn(0xFFFF));
70+
71+
Assert.assertEquals(4, gen.bytesIn(0xFFFF00));
72+
Assert.assertEquals(4, gen.bytesIn(0x7FFFFF00));
73+
}
74+
75+
@Test
76+
public void use4bytes() {
77+
IHasher<Integer> gen = new ChgCombined<Integer>( //
78+
new CIntegerHashing<>(x -> x) //
79+
).generate(Integer.MAX_VALUE, 1);
80+
81+
Assert.assertArrayEquals( //
82+
new int[] { 1 }, gen.apply(1) //
83+
);
84+
}
85+
86+
@Test
87+
public void use2bytes() {
88+
IHasher<Integer> gen = new ChgCombined<Integer>( //
89+
new CIntegerHashing<>(x -> x), //
90+
new CIntegerHashing<>(x -> {
91+
throw new RuntimeException("Don't touch me.");
92+
}) //
93+
).generate(0xFFFF, 2);
94+
95+
Assert.assertArrayEquals( //
96+
new int[] { 1, 2 }, gen.apply(0x00010002) //
5997
);
6098
}
6199
}

src/YASL/Hashing/CIntegerHashing.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package YASL.Hashing;
2+
3+
import java.nio.ByteBuffer;
4+
5+
public class CIntegerHashing<T> implements IHashingAlgorithm<T> {
6+
public interface IIntegerHashing<T> {
7+
public int apply(T x);
8+
}
9+
10+
private final IIntegerHashing<T> _hashing;
11+
12+
public CIntegerHashing(IIntegerHashing<T> _hashing) {
13+
this._hashing = _hashing;
14+
}
15+
16+
@Override
17+
public void hash(T value, ByteBuffer res) {
18+
res.putInt(_hashing.apply(value));
19+
}
20+
21+
}

src/YASL/Hashing/ChgCombined.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package YASL.Hashing;
22

3+
import java.nio.ByteBuffer;
4+
35
public class ChgCombined<T> implements IHashingGenerator<T> {
46
public interface ISalter<T> {
57
public T apply(T value, int round);
@@ -27,27 +29,51 @@ public ChgCombined(IHashingAlgorithm<T>... hashes) {
2729
@Override
2830
public IHasher<T> generate(int range, int levels) {
2931
return x -> {
30-
final int[] res = new int[levels];
31-
int pos = populate(x, res, 0);
32+
final int bytesPerItem = bytesIn(range);
33+
final ByteBuffer res = populate(x, levels * bytesPerItem);
34+
final int[] resArr = new int[levels];
35+
for (int i = 0; i < levels; i++) {
36+
switch (bytesPerItem) {
37+
case 4:
38+
resArr[i] = Math.abs(res.getInt() % range);
39+
break;
3240

33-
int round = 0;
34-
while (pos < levels) {
35-
round++;
36-
pos = populate(_salting.apply(x, round), res, pos);
37-
}
41+
case 2:
42+
resArr[i] = Math.abs(res.getShort() % range);
43+
break;
3844

39-
for (int i = 0; i < res.length; i++)
40-
res[i] = Math.abs(res[i] % range);
41-
return res;
45+
case 1:
46+
resArr[i] = Math.abs(res.get() % range);
47+
break;
48+
}
49+
}
50+
return resArr;
4251
};
4352
}
4453

45-
private int populate(T x, int[] res, int pos) {
46-
final int N = Math.min(_hashes.length, res.length - pos);
47-
for (int i = 0; i < N; i++) {
48-
res[pos] = _hashes[i].hash(x);
49-
pos++;
54+
public int bytesIn(int range) {
55+
if (range <= 0xFF)
56+
return 1;
57+
if (range <= 0xFFFF)
58+
return 2;
59+
return 4;
60+
}
61+
62+
private void populate(T x, int sz, ByteBuffer res) {
63+
for (int i = 0; i < _hashes.length; i++) {
64+
if (sz <= res.position())
65+
break;
66+
_hashes[i].hash(x, res);
5067
}
51-
return pos;
68+
}
69+
70+
private ByteBuffer populate(T x, int sz) {
71+
final ByteBuffer res = ByteBuffer.allocate(32 + sz);
72+
populate(x, sz, res);
73+
for (int round = 1; res.position() < sz; round++)
74+
populate(_salting.apply(x, round), sz, res);
75+
76+
res.rewind();
77+
return res;
5278
}
5379
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package YASL.Hashing;
22

3+
import java.nio.ByteBuffer;
4+
35
public interface IHashingAlgorithm<T> {
4-
public int hash(T value);
6+
public void hash(T value, ByteBuffer res);
57
}

0 commit comments

Comments
 (0)