int []数组和BitSet的交集可以更快吗?

时间:2013-05-08 06:41:41

标签: java arrays intersection bitset

在Java中,我的BitSet大于数组(100000 > 2000)。数组包含范围[1; 100000]的正整数。我想交叉给定的数组和bitset。显然,交集的大小将小于数组的大小,因此我希望将其存储为数组。我的代码如下:

BitSet b = new BitSet(100000);
int[] a = new int[2000];
// fill in b with some bits and a with positive integers in range from [1; 100000]

// intersect array a and bitset b and keep result in separate array
int[] intersection = new int[a.length];
int c = 0, i = 0, j = b.nextSetBit(0);
while (i < a.length && j >= 0) {
    if (a[i] < j) i++;
    else if (a[i] > j) j = b.nextSetBit(j+1);
    else {
        intersection[c] = j;
        i++; j = b.nextSetBit(j+1); c++;
    }
}

// usually intersection is less than a array in size so cut it
int[] zip = new int[c];
System.arraycopy(intersection, 0, zip, 0, c);

时间代码是否可能比上面提到的更快?

编辑数组a已排序,例如a = { 2, 115, 116, 2034, 26748 }

1 个答案:

答案 0 :(得分:2)

这肯定更简单,我认为可能更快,因为它只访问索引在b中的a元素。它不扫描整个b寻找下一个设置位。

  public static int[] intersect(BitSet b, int[] a){
    int[] rawResult = new int[a.length];
    int c = 0;
    for(int i : a){
      if(b.get(i)){
        rawResult[c] = i;
        c++;
      }
    }
    int[] result = new int[c];
    System.arraycopy(rawResult, 0, result, 0, c);
    return result;
  }