Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boolean search on an array

I have multiple arrays with about 100 possible values, ie:

a[0] = (a, b, c, d)
a[1] = (a, e)
a[2] = (d, f, g)

I want to FASTLY return which arrays contains (a || b) && (d || e)

in this example, 0 and 1

I was thinking about bitwise operations... like representing "abcd" by "1111"; "ad" by "1001", and so on. Then I could solve the "OR" with just a bitwise OR, and then check if both are non-zero

can anyone think on a better solution? this one isn't very pratical since it doesn't seem to be very escalable

are there any DBMS that can do that quickly? I tried with mongodb, but it seems they didn't add the "$and" function yet (doc says it's on version 1.9.1, but I can only download 1.9.0, and it's not stable anyway)

I suppose that's a "boolean search", similar to what google does all the time... so I'm guessing there's a better way (maybe not so fast, but more escalable) than that

like image 783
Lem0n Avatar asked Jul 26 '11 04:07

Lem0n


People also ask

How do you boolean an array?

The boolean array can be used to store boolean datatype values only and the default value of the boolean array is false. An array of booleans are initialized to false and arrays of reference types are initialized to null. In some cases, we need to initialize all values of the boolean array with true or false.

Can you binary search an array?

Binary Search is a searching algorithm for finding an element's position in a sorted array. In this approach, the element is always searched in the middle of a portion of an array. Binary search can be implemented only on a sorted list of items. If the elements are not sorted already, we need to sort them first.

How do you search an array?

Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.

What are boolean arrays used for?

Boolean arrays can be used to select elements of other numpy arrays. If a is any numpy array and b is a boolean array of the same dimensions then a[b] selects all elements of a for which the corresponding value of b is True .


1 Answers

Yes, a bitwise solution works quite nicely for this. Yes, some databases include such a capability, usually named a bitmapped column (or bitmapped index, depending). The usual advice is to apply it to a column that has relatively low cardinality (i.e., a fairly small number of possible values, such as sex).

like image 66
Jerry Coffin Avatar answered Oct 17 '22 01:10

Jerry Coffin