Skip to content

Instantly share code, notes, and snippets.

@tomasklapka
Created February 3, 2019 08:44
Show Gist options
  • Save tomasklapka/868fd920d1654d74363fab4814200372 to your computer and use it in GitHub Desktop.
Save tomasklapka/868fd920d1654d74363fab4814200372 to your computer and use it in GitHub Desktop.
BDD apply unary op non recursive
function _enum(obj) { return Object.freeze(obj); }
class bdds extends bdds_base {
static apply(b, root, r, op) {
const get = id => op.op(b, b.getnode(id)); // fn evaluates the operator
const parents = []; // path from root to the current node
let n = get(root); // current node
let nn = 0; // new node (id)
let hi = 0; // last high leaf
let lo = 0; // last low leaf
const s = _enum({ "LO": 1, "HI": 2, "OP": 3 }); // traversing states
let ts = s.LO; // current traversing state
do { // traversing the binary tree
if (ts === s.LO) { // search low
if(bdds.leaf(n.lo)) {
lo = n.lo; // remember last low leaf
ts = s.HI; // leaf, go search high
} else { // not a leaf
parents.push(n); // store parent
n = get(n.lo); // go low (and search low)
}
} else if (ts === s.HI) { // search high
if (bdds.leaf(n.hi)) {
hi = n.hi; // remember last high leaf
ts = s.OP; // leaf, do op
} else { // not a leaf
parents.push(n); // store parent
n = get(n.hi); // go high
ts = s.LO; // and search low
}
} else if (ts === s.OP) { // do op
nn = r.add(new node(n.var, hi, lo)); // adds new node (if not exists) with evaluated hi and lo
if (parents.length === 0) break; // we are back at the top -> break inf. loop
n = parents.pop(); // go up
if (nn === n.lo) { // if we operated on low
lo = nn; ts = s.HI; // set new last low and go high
} else { // else we operated on high already
hi = nn; ts = s.OP; // set new last high and go op
}
}
} while (true);
return nn; // return the last new node
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment