Java位图操作相关问题

zhangdp_neu 2009-10-16 06:40:19
public final class BitVector {

private byte[] bits;
private int size;
private int count = -1;

/** Constructs a vector capable of holding <code>n</code> bits. */
public BitVector(int n) {
size = n;
bits = new byte[(size >> 3) + 1];
}

/** Sets the value of <code>bit</code> to one. */
public final void set(int bit) {
bits[bit >> 3] |= 1 << (bit & 7);
count = -1;
}

/** Sets the value of <code>bit</code> to zero. */
public final void clear(int bit) {
bits[bit >> 3] &= ~(1 << (bit & 7));
count = -1;
}

/** Returns <code>true</code> if <code>bit</code> is one and
<code>false</code> if it is zero. */
public final boolean get(int bit) {
return (bits[bit >> 3] & (1 << (bit & 7))) != 0;
}

/** Returns the number of bits in this vector. This is also one greater than
the number of the largest valid bit number. */
public final int size() {
return size;
}

/** Returns the total number of one bits in this vector. This is efficiently
computed and cached, so that, if the vector is not changed, no
recomputation is done for repeated calls. */
public final int count() {
// if the vector has been modified
if (count == -1) {
int c = 0;
int end = bits.length;
for (int i = 0; i < end; i++)
c += BYTE_COUNTS[bits[i] & 0xFF]; // sum bits per byte
count = c;
}
return count;
}

private static final byte[] BYTE_COUNTS = { // table of bits/byte
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
};
}

这个是lucene里的一个源码,但是 public final int count() 方法我没有看明白,怎么和下面的那个大数组结合使用。请指教。
...全文
194 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
swandragon 2009-10-16
  • 打赏
  • 举报
回复
看起来好复杂,不会
lz12366007 2009-10-16
  • 打赏
  • 举报
回复
记得以前看过 一个做 验证码的 用算法实现的程序!!
和这个差不多!!是很长的数组!!
应该是和像素相关的!!!
记得以前 柔化图像处理的时候!!
都是 把周围的像素 和数组相乘 然后取平均值!!!
设置像素!!就柔化了!!
不知道lucene是什么东东!!

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧