简体   繁体   中英

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

I have an array of bit vectors in form of BitSet in Java. My goal is to check if these bit vectors have intersections (all have 1s in at least one bit). My current solution is using and operation offered by BitSet in Java.

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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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