这就是我目前的问题,我有一些布隆过滤器,我想把它们构建成一个矩阵,比如:
[0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1]
[1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0]
...
[1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0]
每列都将从BitSet派生,除了循环所有行并比较每个索引之外,有没有更有效的方法来查找位设置为1的所有列?
有什么数据结构可以帮助我做到这一点吗?
发布于 2013-05-12 13:59:24
假设您正在查找哪些列包含1,现在每列包含多少个,循环遍历它们似乎是最好的想法。
如果您使用一些短路逻辑实现您的循环,那么您将获得更好的平均运行时间。
类似于:
for (int column = 0; column < width; column++) {
for (int row = 0; row < height; row++) {
if (array[column][row] == 1) {
list.append(column);
break; // move on to the next column because we don't care what's
// left in this column as we already found our '1'
使用这段代码,您将获得n
(n
是bits
的总数)的最坏情况(如果每个bit
都是0
)的运行时间,这是非常好的。
但是,除非您非常不幸,否则由于short-circuit逻辑,您将获得更好的运行时间。
上面的方法适用于位数组,但是,BitSets
本身也有一些有用的功能。
BitSet
包含以下函数:length()
返回最高的索引,设置位为+1(如果没有设置位,则返回0
)和nextSetBit(index)
,返回index -> end
中下一个设置位的索引。
所以你的代码可能很容易是这样的:
int index = 0;
while (index < BitSet.length()) {
index = BitSet.nextSetBit(index);
list.append(index);
index++;
}
https://stackoverflow.com/questions/16507342
复制