简体   繁体   English

Java:检查多个位向量/位集是否相交的最快方法?

[英]Java: Fastest way to check if multiple bit vectors/bitsets intersect?

I have an array of bit vectors in form of BitSet in Java. 我在Java中有一组以BitSet形式的位向量。 My goal is to check if these bit vectors have intersections (all have 1s in at least one bit). 我的目标是检查这些位向量是否有交集(所有至少一位中都具有1)。 My current solution is using and operation offered by BitSet in Java. 我当前的解决方案是使用Java中的BitSet和提供的操作。

BitSet[] bitVecs = new BitSet[10]

//
// initialize all bit vectors and their lengths are 100 ...
//

BitSet check = new BitSet(100); //  
check.set(0, 100);
for (int i = 0; i < bitVecs.length; i++) {
    check.add(bitVecs[i]);
    if (check.isEmpty())
       return false // we know the bitVecs do not intersect
}

return true; // we know bitVecs intersects

BitSet has an intersects function and should be faster than and , but it only checks two bit vectors and no more. BitSet具有intersects函数,并且应比and快,但它仅检查两个位向量,而不再检查。 I appreciate if anyone knows how to make this faster. 如果有人知道如何使其更快,我将不胜感激。

I'm going to make some assumptions. 我要作一些假设。 It looks like you are going to be dealing with a list of Unit vectors in a 10d space. 看来您将要处理10d空间中的单位向量列表。 The problem is how vectors are defined. 问题是如何定义向量。 start at the origin and go 1 unit in any direction where the corresponding dimension has a 1 instead of 0. well all your vectors are going to intersect at the origin, and at infinite points if they are the same vector. 从原点开始,并在任意方向上以1个单位移动,其中相应维度的大小为1而不是0。那么所有向量都将在原点相交,如果它们是相同向量,则在无限点处相交。 the vector is just going to define the slope of the line. 向量仅用于定义线的斜率。 you will also need some other vector to define an offset. 您还需要其他向量来定义偏移量。 what you need to do is turn the vectors into lines and then you can tell if they intersect. 您需要做的是将向量变成线,然后可以判断它们是否相交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM