Long2Int.java
2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package is2.data;
/**
* @author Bernd Bohnet, 01.09.2009
*
* Maps for the Hash Kernel the long values to the int values.
*/
final public class Long2Int implements Long2IntInterface {
public Long2Int() {
size = 115911564;
}
public Long2Int(int s) {
size = s;
}
public static void main(String args[]) {
long l = 123456;
long l2 = 1010119;
System.out.println("l \t" + l + "\t" + printBits(l));
long x = 100000000;
System.out.println("1m\t" + l2 + "\t" + printBits(x) + "\t" + x);
System.out.println("l2\t" + l2 + "\t" + printBits(l));
System.out.println("l2*l\t" + l2 + "\t" + printBits(l * l2) + " \t " + l * l2);
System.out.println("l2*l*l2\t" + l2 + "\t" + printBits(l * l2 * l2) + " \t " + l * l2 * l2);
System.out.println("l2*l*l2\t" + l2 + "\t" + printBits(l * l2 * l2 * l2) + " \t " + l * l2 * l2 * l2);
System.out.println(
"l2*l*l2\t" + l2 + "\t" + printBits((l * l2) % 0xfffff) + " \t " + l * l2 * l2 * l2 + "\t " + 0xfffff);
System.out.println("l2*l*l2\t" + l2 + "\t" + printBits((l * l2) & 0xfffffff) + " \t " + l * l2 * l2 * l2);
}
/** Integer counter for long2int */
final private int size; // 0x03ffffff //0x07ffffff
/*
* (non-Javadoc)
*
* @see is2.sp09k9992.Long2IntIterface#size()
*/
@Override
public int size() {
return size;
}
/*
* (non-Javadoc)
*
* @see is2.sp09k9992.Long2IntIterface#start() has no meaning for this
* implementation
*/
final public void start() {
}
/*
* (non-Javadoc)
*
* @see is2.sp09k9992.Long2IntIterface#l2i(long)
*/
@Override
final public int l2i(long l) {
if (l < 0)
return -1;
// this works well LAS 88.138
// int r= (int)(( l ^ (l&0xffffffff00000000L) >>> 29 ));//0x811c9dc5 ^
// // 29
// return Math.abs(r % size);
// this works a bit better and good with 0x03ffffff
//
/*
* long r= l;//26 l = (l>>12)&0xfffffffffffff000L; r ^= l;//38 l =
* (l>>11)&0xffffffffffffc000L; r ^= l;//49 l = (l>>9)&
* 0xffffffffffff0000L; //53 r ^= l;//58 l = (l>>7)&0xfffffffffffc0000L;
* //62 r ^=l;//65 int x = (int)r; x = x % size; // return x >= 0 ? x :
* -x ;// Math.abs(r % size);
*
*/
// 26 0x03ffffff
// together with 0x07ffffff 27 88.372
long r = l;// 27
l = (l >> 13) & 0xffffffffffffe000L;
r ^= l; // 40
l = (l >> 11) & 0xffffffffffff0000L;
r ^= l; // 51
l = (l >> 9) & 0xfffffffffffc0000L; // 53
r ^= l; // 60
l = (l >> 7) & 0xfffffffffff00000L; // 62
r ^= l; // 67
int x = ((int) r) % size;
return x >= 0 ? x : -x;
}
static public StringBuffer printBits(long out) {
StringBuffer s = new StringBuffer();
for (int k = 0; k < 65; k++) {
s.append((out & 1) == 1 ? "1" : "0");
out >>= 1;
}
s.reverse();
return s;
}
}