-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBitMap.java
More file actions
84 lines (69 loc) · 1.94 KB
/
BitMap.java
File metadata and controls
84 lines (69 loc) · 1.94 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
/*
* This file provides the implementation of the class BitMap.
* BitMap: a form of hash, using one bit to present an integer nubmer;
*
* FileName: BitMap.java
* description: use BitMap to implement hash.
* Author: wzb (zhongbo.wzb@alibaba-inc.com)
* Date: 2014-04-19 16:13
* Version: 0.1
*
* */
public class BitMap {
private long bits;
private int[] map;
public static final long SHIFT = 5;
public static final long MASK = ((1 << SHIFT) -1);
public BitMap(long nbits) {
bits = nbits + 1;
int size = (int) (((bits >>> SHIFT) + 1) & 0x0ffffffffL);
map = new int[size];
for (int i = 0; i < size; i++) {
map[i] = 0;
}
}
public long size() {
return bits;
}
public void set(long index) {
int offset = (int) ((index >>> SHIFT) & 0x0ffffffffL);
map[offset] |= (1 << (index & MASK));
}
public void clear(long index) {
int offset = (int) ((index >>> SHIFT) & 0x0ffffffffL);
map[offset] &= ~(1 << (index & MASK));
}
public boolean isSet(long index) {
int offset = (int) ((index >>> SHIFT) & 0x0ffffffffL);
int off = (int) (index & MASK);
return ((map[offset] >>> off) & 0x01) == 0x1;
}
/*
public static void main(String[] args) {
System.out.println(MASK);
long size = 1L << 32;
System.out.println(size);
BitMap bitMap = new BitMap(size);
System.out.println("bitMap size: " + bitMap.size());
for (long i = 0; i <= bitMap.size(); ++i) {
if (bitMap.isSet(i)) {
System.out.println("Error:@ " + i);
}
}
System.err.println("xxxx");
for (long i = 0; i <= bitMap.size(); i++) {
bitMap.set(i);
if (!bitMap.isSet(i)) {
System.out.println("not set:@ " + i);
}
bitMap.clear(i);
}
System.err.println("xxxx");
for (long i = 0; i <= bitMap.size(); ++i) {
if (bitMap.isSet(i)) {
System.out.println("Error:@ " + i);
}
}
}
*/
}