I have 2 lists of ints: A and B. How can I efficiently ensure that for every element in A there exists at least one element in B such that when they are bit-anded, answer is that element in A.
Eg. B = [14, 13 ]
So for elements in A: a1 = 12 is valid as 12&14 = 12 a2 = 3 is not valid as 3&14 = 2 and 3&13 = 1
A and B can have 10^10 entries.
I am reading B as a column from csv and generating A on fly. And checking for above condition using
df.applymap(lambda x: (x&a) == a ).any().any()
But as size of B increases this check is my bottleneck.
If speed is a concert you can try to use numba:
Prints:
Benchmark:
On my computer (AMD 5700x) this prints:
EDIT: For
np.uint64values from0toMAX(np.unit64):Prints:
EDIT 2: Sorting the first 1000 items from
Barray according bit-count:Prints:
EDIT3: If you want to check every element from A to array B:
Prints:
EDIT4: Parralel version that creates mask:
Prints: