Skip to content

Commit 88f82c0

Browse files
committed
Major performance improvements + bugfix
1 parent c318aeb commit 88f82c0

File tree

3 files changed

+131
-11
lines changed

3 files changed

+131
-11
lines changed

main.js

Lines changed: 123 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2009-2020 Sebastian Kraemer, basti.kr@gmail.com and others SPDX-License-Identifier: BSD-2-Clause
44
*/
55

6-
const {isEqual, uniqWith, get, sortBy, some, concat, groupBy, forOwn} = require("lodash");
6+
const {isEqual, uniqWith, get, sortBy, some, concat, groupBy, forOwn, uniq, isObject} = require("lodash");
77

88
const TOKEN_AND = 1;
99
const TOKEN_OR = 2;
@@ -410,6 +410,7 @@ let simplify = or => {
410410
};
411411

412412
function forceDisjunctive(or, not=false) {
413+
console.log("forceDisj");
413414
let res = []
414415
or.map(and=>{
415416
let orsFromAnd = []
@@ -432,23 +433,41 @@ function forceDisjunctive(or, not=false) {
432433
})
433434
}
434435
} else if(an.brack) {
436+
435437
let brackRes = forceDisjunctive(an.brack, not?!an.not:an.not)
436438
if(orsFromAnd.length===0) {
437439
orsFromAnd = brackRes
438440
} else {
439441
let newOrsFromAnd = []
440442
brackRes.map(b => {
441443
orsFromAnd.map(a=>{
442-
newOrsFromAnd.push([...a, ...b])
444+
b.map(c => {
445+
if(!some(a, c))
446+
newOrsFromAnd.push(uniq([...a, ...b]))
447+
})
448+
/*if(!isEqual(a, b)) {
449+
let addElement = uniq([...a, ...b]);
450+
if(!some(newOrsFromAnd, addElement))
451+
newOrsFromAnd.push(addElement)
452+
}*/
443453
})
454+
//newOrsFromAnd = cleanSameLiteral(newOrsFromAnd)
444455
})
456+
if(!isEqual(newOrsFromAnd, cleanSameLiteral(newOrsFromAnd))) {
457+
console.log("excess1!")
458+
}
459+
newOrsFromAnd = cleanSameLiteral(newOrsFromAnd)
445460
orsFromAnd = newOrsFromAnd
446461
}
447462
} else {
448463
throw new Error("Unexpected type");
449464
}
450465
})
451466
res = [...res, ...orsFromAnd]
467+
468+
if(!isEqual(res, cleanSameLiteral(res))) {
469+
console.log("excess2!")
470+
}
452471
})
453472
if(not) {
454473
res = [res.map(i=>{
@@ -457,6 +476,94 @@ function forceDisjunctive(or, not=false) {
457476
res = forceDisjunctive(res)
458477
}
459478
res = cleanTrivialLiterals(res)
479+
if(!isEqual(res, cleanSameLiteral(res))) {
480+
console.log("excess3!")
481+
}
482+
return res;
483+
}
484+
485+
function forceDisjunctive2(or, not=false) {
486+
//console.log("forceDisj2");
487+
let or2 = or;
488+
if(not) {
489+
or2 = [or2.map(i=>{
490+
return {brack: i.filter(j=>!isEqual(j,{})).map(j=>{
491+
if(isObject(j)) {
492+
j.not = !j.not
493+
} else {
494+
j = j.map(k=>{
495+
k.not = !k.not
496+
})
497+
}
498+
return [j]
499+
})}
500+
})]
501+
not = false
502+
}
503+
let res = []
504+
or2.map(and=>{
505+
let orsFromAnd = []
506+
and.filter(i=>!isEqual(i,{})).map(an => {
507+
if(an.sym) {
508+
let symObj = {...an, not: an.not};
509+
if(symObj.not && get(symObj, "sym.type")===TOKEN_TRUE) {
510+
symObj.not = false
511+
symObj.sym.type = TOKEN_FALSE
512+
} else if(symObj.not && get(symObj, "sym.type")===TOKEN_FALSE) {
513+
symObj.not = false
514+
symObj.sym.type = TOKEN_TRUE
515+
}
516+
if(orsFromAnd.length===0)
517+
orsFromAnd.push([symObj]);
518+
else {
519+
orsFromAnd.map(a=>{
520+
a.push(symObj)
521+
return a
522+
})
523+
}
524+
} else if(an.brack) {
525+
526+
let brackRes = forceDisjunctive2(an.brack, an.not)
527+
if(orsFromAnd.length===0) {
528+
orsFromAnd = brackRes
529+
} else {
530+
let newOrsFromAnd = []
531+
brackRes.map(b => {
532+
orsFromAnd.map(a=>{
533+
/*b.map(c => {
534+
if(!some(a, c))
535+
newOrsFromAnd.push(uniq([...a, ...b]))
536+
})*/
537+
let addElement = uniq([...a, ...b]);
538+
newOrsFromAnd.push(addElement)
539+
/*if(!isEqual(a, b)) {
540+
let addElement = uniq([...a, ...b]);
541+
if(!some(newOrsFromAnd, addElement))
542+
newOrsFromAnd.push(addElement)
543+
}*/
544+
})
545+
//newOrsFromAnd = cleanSameLiteral(newOrsFromAnd)
546+
})
547+
/*if(!isEqual(newOrsFromAnd, cleanSameLiteral(newOrsFromAnd))) {
548+
console.log("excess1!")
549+
}*/
550+
newOrsFromAnd = cleanSameLiteral(newOrsFromAnd)
551+
orsFromAnd = newOrsFromAnd
552+
}
553+
} else {
554+
throw new Error("Unexpected type");
555+
}
556+
})
557+
res = [...res, ...orsFromAnd]
558+
559+
/*if(!isEqual(res, cleanSameLiteral(res))) {
560+
console.log("excess2!")
561+
}*/
562+
})
563+
res = cleanTrivialLiterals(res)
564+
/*f(!isEqual(res, cleanSameLiteral(res))) {
565+
console.log("excess3!")
566+
}*/
460567
return res;
461568
}
462569

@@ -584,7 +691,7 @@ let simp = (expr) => {
584691
};
585692

586693
let disj = (expr) => {
587-
let res = forceDisjunctive(parse(tokenize(expr)));
694+
let res = forceDisjunctive2(parse(tokenize(expr)));
588695
res = cleanNotSatisfiableAndInDisj(res)
589696
res = cleanSameLiteral(res)
590697
res = sortAnds(res)
@@ -593,7 +700,7 @@ let disj = (expr) => {
593700
}
594701

595702
let impl = (expr) => {
596-
let res = forceDisjunctive(parse(tokenize(expr)));
703+
let res = forceDisjunctive2(parse(tokenize(expr)));
597704
res = cleanNotSatisfiableAndInDisj(res)
598705
res = cleanSameLiteral(res)
599706
res = sortAnds(res)
@@ -635,7 +742,7 @@ let cleanDuplicates = (ors) => {
635742
}
636743

637744
let lcrCode = (expr) => {
638-
let res = forceDisjunctive(parse(tokenize(expr)));
745+
let res = forceDisjunctive2(parse(tokenize(expr)));
639746
res = cleanNotSatisfiableAndInDisj(res)
640747
res = cleanSameLiteral(res)
641748
res = sortAnds(res)
@@ -680,7 +787,7 @@ let lcr = (rule, otherRules) => {
680787
})
681788
}
682789

683-
res = forceDisjunctive(res);
790+
res = forceDisjunctive2(res);
684791
res = cleanNotSatisfiableAndInDisj(res)
685792
res = cleanSameLiteral(res)
686793
res = sortAnds(res)
@@ -719,9 +826,9 @@ let replaceToVal = (ors, truthy = [], falsy = [], makeOthers = null) => {
719826

720827

721828
let eval = (expr, truthy = [], falsy= [], makeOthers = null) => {
722-
let res = forceDisjunctive(parse(tokenize(expr)));
829+
let res = forceDisjunctive2(parse(tokenize(expr)));
723830
res = replaceToVal(res, truthy, falsy, makeOthers);
724-
res = forceDisjunctive(res);
831+
res = forceDisjunctive2(res);
725832
res = cleanNotSatisfiableAndInDisj(res)
726833
res = cleanSameLiteral(res)
727834
res = sortAnds(res)
@@ -731,8 +838,14 @@ let eval = (expr, truthy = [], falsy= [], makeOthers = null) => {
731838
return res;
732839
}
733840

734-
console.log(impl("1+1"))
735-
console.log(eval("-x+-y", [], [], TOKEN_FALSE));
841+
console.log(impl("a+(a|b)"))
842+
//let lcrAnswer = lcr("1", ["A", "A+B"]);
843+
//console.log(lcrAnswer)
844+
//console.log(impl("-(A+(B+(C+-D+-E)))"))
845+
//console.log(impl("(A+B)|(A+C)"))
846+
//console.log(impl("-(421+513+(531|365)+-K26+(5XXL+-581L|2XXL)|(K34+(M256|M176)|ME05)+(460|494))|K26)"))
847+
//onsole.log(impl("(421+513+(531|365)+-K26+((B01+(M256|M176|M177)|ME05)+(5XXL+-512L+-516L+-521L+-526L+-581L|2XXL)|(K34+B01+(M256|M176|M177)|ME05)+(460|494))|K26)"))
848+
//onsole.log(eval("-x+-y", [], [], TOKEN_FALSE));
736849

737850

738851

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "boolean-simp",
3-
"version": "2.0.5",
3+
"version": "2.1.0",
44
"description": "Simplify boolean expressions",
55
"author": "Markus Gutbrod <gutbrod.markus@googlemail.com>",
66
"license": "MIT",

test/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ let expr = [
7070
{expr: "(A+(B+(C+-(D|-E))))", disj: "A+B+C+-D+E"},
7171
{expr: "B+A+C", disj: "A+B+C"},
7272
{expr: "A|A+B|A+B+C|B+C", impl: "A|(B+C)"},
73+
{expr: "A+(B+(C|D|E)|F)", impl: "(A+B+C)|(A+B+D)|(A+B+E)|(A+F)"},
74+
{expr: "-a+-(a+b)", impl: "-a"},
75+
{expr: "-a+(-a|-b)", impl: "-a"},
76+
{expr: "a+(a|b)", impl: "a"},
77+
{expr: "a|(a+b)", impl: "a"},
78+
{expr: "-(A+(B+(C+-D+-E)))", impl: "-A|-B|-C|D|E"},
79+
{expr: "-(J+K+(L|M)+-E+((F+(A|B|C)|D)+(N+-O+-P+-Q+-R+-S|T)|(I+F+(A|B|C)|D)+(G|H))|E)", impl: "(-E+-J)|(-E+-K)|(-E+-L+-M)|(-D+-E+-I+-N+-T)|(-D+-E+-I+O+-T)|(-D+-E+-I+P+-T)|(-D+-E+-I+Q+-T)|(-D+-E+-I+R+-T)|(-D+-E+-I+S+-T)|(-D+-E+-F)|(-A+-B+-C+-D+-E)|(-E+-G+-H+-N+-T)|(-E+-G+-H+O+-T)|(-E+-G+-H+P+-T)|(-E+-G+-H+Q+-T)|(-E+-G+-H+R+-T)|(-E+-G+-H+S+-T)"},
7380

7481
//{expr: "M654+-(O41+M256)", res: "M654+(-O41|-M256)", disj: "M654-O41|M654-M256"}
7582
];

0 commit comments

Comments
 (0)