【问题标题】:Efficient way of creating a bit matrix and scan set columns创建位矩阵和扫描集列的有效方法
【发布时间】:2013-05-12 12:28:31
【问题描述】:

这是我目前的问题,我有许多布隆过滤器,我想将它们构造成一个矩阵,比如:

[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 的列?

是否有任何数据结构可以帮助我解决这个问题?

【问题讨论】:

    标签: java matrix bloom-filter


    【解决方案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'
    

    使用此代码,您将得到一个最坏情况(如果每个bit0)运行时间为nn 是@987654326 的总数@) 挺好的。

    但除非你非常倒霉,否则由于 短路 逻辑,你会获得更好的运行时间。

    以上内容适用于位数组,但是,BitSets 有一些自己的有用功能。

    BitSet 包含函数:length() 返回设置位 + 1 的最高索引(如果没有设置位,则返回 0)和 nextSetBit(index) 返回下一个设置位的索引来自index -&gt; end(含)。

    所以你的代码很容易是这样的:

    int index = 0;
    
    while (index < BitSet.length()) {
        index = BitSet.nextSetBit(index);
        list.append(index);
        index++;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多