You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.2 KiB
61 lines
1.2 KiB
package class05;
|
|
|
|
import java.util.HashSet;
|
|
|
|
public class Code02_BitMap2 {
|
|
|
|
// 这个类的实现是正确的
|
|
public static class BitMap {
|
|
|
|
private long[] bits;
|
|
|
|
public BitMap(int max) {
|
|
bits = new long[(max + 64) >> 6];
|
|
}
|
|
|
|
public void add(int num) {
|
|
bits[num >> 6] |= (1L << (num & 63));
|
|
}
|
|
|
|
public void delete(int num) {
|
|
bits[num >> 6] &= ~(1L << (num & 63));
|
|
}
|
|
|
|
public boolean contains(int num) {
|
|
return (bits[num >> 6] & (1L << (num & 63))) != 0;
|
|
}
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
System.out.println("测试开始!");
|
|
int max = 10000;
|
|
BitMap bitMap = new BitMap(max);
|
|
HashSet<Integer> set = new HashSet<>();
|
|
int testTime = 10000000;
|
|
for (int i = 0; i < testTime; i++) {
|
|
int num = (int) (Math.random() * (max + 1));
|
|
double decide = Math.random();
|
|
if (decide < 0.333) {
|
|
bitMap.add(num);
|
|
set.add(num);
|
|
} else if (decide < 0.666) {
|
|
bitMap.delete(num);
|
|
set.remove(num);
|
|
} else {
|
|
if (bitMap.contains(num) != set.contains(num)) {
|
|
System.out.println("Oops!");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
for (int num = 0; num <= max; num++) {
|
|
if (bitMap.contains(num) != set.contains(num)) {
|
|
System.out.println("Oops!");
|
|
}
|
|
}
|
|
System.out.println("测试结束!");
|
|
}
|
|
|
|
}
|