Optimizing Logic Tree Valuation

I have many true / false results stored as bits in arrays long[]. I have a huge amount of these (millions and millions of lengths).

For example, let's say I have only five results, I would have:

+----- condition 5 is true
|
|+---- condition 4 is false
||
||+--- condition 3 is true
|||
|||+-- condition 2 is true
||||
||||+- condition 1 is false
10110

I also have several trees representing expressions like:

condition1 AND (condition2 OR (condition3 AND condition 4))

The trees are very simple but very long. They basically look like this (this is a simplification below, just to show what I have):

class Node {    
    int operator();
    List<Node> nodes;
    int conditionNumber();    
}

In principle, either Node is a leaf and then has a condition number (corresponding to one of the bits in long [] arrays), or Node is not a leaf and, therefore, refers to several trays.

They are simple, but they allow you to express complex Boolean expressions. It works great.

, . : , , . , , .

true, false long[].

, , :

boolean solve( Node node, long[] trueorfalse ) {
   ...
}

node Node, , , ( , solve ).

, ( , ), long[], , , ?

( (sub) long [], long[], ) .. , (AND OR NOT ..), if/else switch.

(), O (x) O (y), y , x.

, , - "times x": , 5 , 5- , .

, - , "times x" , , - - . , ( : , , ).

- ?

, -, ?

+5
3

, .

, ,

  • AND false
  • OR true

, . / , , , .

, ( ), tree- node ( ), "" .

, , , - . , , , :)

+3

, C. , z = (x op y), :

 z = result[op+x+(y<<1)];

, op 4 AND, OR, XOR .., . , :

z = (MAGIC_NUMBER >> (op+x+(y<<1))) & 1;

. , , node 2 . . , 2 . , , , .

. , , , , , 2 1,5, . YMMV.

EDIT: , - . (node) . , . , , , . node , . - (, 1 0 ).

+3

, - . , , . , .

((word&1) && ((word&2) || ((word&4) && (word&8))))

" " , , -/​​dll , .

, . 10-100 .

JDK. , Java- , , . :

while(iop < nop){
  switch(code[iop++]){
    case BIT1: // check the 1 bit and stack a boolean
      stack[nstack++] = ((word & 1) != 0);
      break;
    case BIT2: // check the 2 bit and stack a boolean
      stack[nstack++] = ((word & 2) != 0);
      break;
    case BIT4: // check the 4 bit and stack a boolean
      stack[nstack++] = ((word & 4) != 0);
      break;
    // etc. etc.
    case AND: // pop 2 booleans and push their AND
      nstack--;
      stack[nstack-1] = (stack[nstack-1] && stack[nstack]);
      break;
    case OR: // pop 2 booleans and push their OR
      nstack--;
      stack[nstack-1] = (stack[nstack-1] || stack[nstack]);
      break;
  }
}

, , . , .

In addition, you can simplify this by manipulating De Morgan's laws so that you can check multiple bits at a time.

+1
source

All Articles