!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).whynot={})}(this,(function(t){"use strict";function s(t,s,i,r){const n={op:s,func:i,data:r};return t.push(n),n}function i(t,s){return t}class r{constructor(){this.program=[]}test(t,i){return s(this.program,5,t,void 0===i?null:i)}jump(t){return s(this.program,3,null,t)}record(t,r){return s(this.program,4,void 0===r?i:r,t)}bad(t=1){return s(this.program,1,null,t)}accept(){return s(this.program,0,null,null)}fail(t){return s(this.program,2,t||null,null)}}class n{constructor(t,s,i){this.programLength=t,this.maxFromByPc=s,this.maxSurvivorFromByPc=i}static fromProgram(t){const s=t.length,i=[],r=[];return t.forEach(t=>{i.push(0),r.push(0)}),t.forEach((t,n)=>{switch(t.op){case 2:if(null===t.func)return;if(n+1>=s)throw new Error("Invalid program: program could run past end");i[n+1]+=1;break;case 1:case 4:if(n+1>=s)throw new Error("Invalid program: program could run past end");i[n+1]+=1;break;case 3:t.data.forEach(t=>{if(t<0||t>=s)throw new Error("Invalid program: program could run past end");i[t]+=1});break;case 5:if(n+1>=s)throw new Error("Invalid program: program could run past end");r[n+1]+=1;break;case 0:r[n]+=1}}),new n(s,i,r)}static createStub(t){const s=[],i=[];for(let r=0;r0}}const h=255;class l{constructor(t){this.t=0,this.i=0,this.h=new Uint16Array(t),this.l=new Uint8Array(t)}getBadness(t){return this.l[t]}add(t,s){this.l[t]=s>h?h:s;const i=function(t,s,i,r,n){let e=r,h=n;for(;e>>1;ih?h:s);if(this.l[t]!==i){const s=this.h.indexOf(t,this.i);if(s<0||s>=this.t)return void(this.l[t]=i);this.h.copyWithin(s,s+1,this.t),this.t-=1,this.add(t,i)}}getNextPc(){return this.i>=this.t?null:this.h[this.i++]}reset(){this.t=0,this.i=0,this.l.fill(0)}}class o{constructor(t){this.o=[];let s=t.length;t.forEach(t=>{this.o.push(t>0?s:-1),s+=t}),this.u=new Uint16Array(s)}clear(){this.u.fill(0,0,this.o.length)}add(t,s){const i=this.u[s],r=this.o[s];this.u[s]+=1,this.u[r+i]=t}has(t){return this.u[t]>0}forEach(t,s){const i=this.u[t],r=this.o[t];for(let t=r;t{const i=this.trace(s,r,n,e);var l,o,u;o=i,u=h,t=null===(l=t)?o:null===o?l:Array.isArray(o)?o.reduce((t,s)=>c(t,s,t===o),l):c(l,o,u),h=t===i}),t}trace(t,s,i,r){switch(this.p[t]){case 2:return this.v[t];case 1:return null}this.p[t]=1;let n=null;const e=s[t];if(null!==e)n=e;else if(!i.has(t))throw new Error("Trace without source at pc "+t);if(n=this.mergeTraces(n,t,i,s,i,r),null!==n){const s=r[t];null!==s&&(n=a(n,s))}return this.v[t]=n,this.p[t]=2,n}buildSurvivorTraces(t,s,i,r,n){for(let e=0,h=t.length;ec(t,this.k[s]),null);return null===s?[]:Array.isArray(s)?s:[s]}}class w{constructor(t){this.I=[],this.M=new l(t.programLength),this.N=new l(t.programLength),this.j=new d(t)}reset(){this.M.reset(),this.M.add(0,0),this.I.length=0,this.j.reset(!0)}getNextThreadPc(){return this.M.getNextPc()}step(t,s,i){const r=this.j.has(s);this.j.add(t,s);const n=this.M.getBadness(t)+i;r?this.M.reschedule(s,n):this.M.add(s,n)}stepToNextGeneration(t,s){const i=this.j.hasSurvivor(s);this.j.addSurvivor(t,s);const r=this.M.getBadness(t);i?this.N.reschedule(s,r):this.N.add(s,r)}accept(t){this.I.push(t),this.j.addSurvivor(t,t)}fail(t){}record(t,s){this.j.record(t,s)}nextGeneration(){this.j.buildSurvivorTraces(),this.j.reset(!1);const t=this.M;t.reset(),this.M=this.N,this.N=t}getAcceptingTraces(){return this.j.getTraces(this.I)}}class p{constructor(t){this.P=[],this.U=t,this.G=n.fromProgram(t),this.P.push(new w(this.G))}execute(t,s){const i=this.P.pop()||new w(this.G);i.reset();const r=t.length;let n,h=-1;do{let e=i.getNextThreadPc();if(null===e)break;for(++h,n=h>=r?null:t[h];null!==e;){const t=this.U[e];switch(t.op){case 0:null===n?i.accept(e):i.fail(e);break;case 2:{const r=t.func;if(null===r||r(s)){i.fail(e);break}i.step(e,e+1,0);break}case 1:i.step(e,e+1,t.data);break;case 5:if(null===n){i.fail(e);break}if(!(0,t.func)(n,t.data,s)){i.fail(e);break}i.stepToNextGeneration(e,e+1);break;case 3:{const s=t.data,r=s.length;if(0===r){i.fail(e);break}for(let t=0;t