starry / dist /assets /solveStaffGroup.worker-41469644.js
k-l-lambda's picture
update: export from starry-refactor 2026-02-21 11:16
8c7d858
(function(){"use strict";var commonjsGlobal=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{};function getDefaultExportFromCjs(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var Sylvester={};Sylvester.Matrix=function(){},Sylvester.Matrix.create=function(e){var t=new Sylvester.Matrix;return t.setElements(e)},Sylvester.Matrix.I=function(e){for(var t=[],i=e,s;i--;)for(s=e,t[i]=[];s--;)t[i][s]=i===s?1:0;return Sylvester.Matrix.create(t)},Sylvester.Matrix.prototype={dup:function(){return Sylvester.Matrix.create(this.elements)},isSquare:function(){var e=this.elements.length===0?0:this.elements[0].length;return this.elements.length===e},toRightTriangular:function(){if(this.elements.length===0)return Sylvester.Matrix.create([]);var e=this.dup(),t,i=this.elements.length,s,r,o=this.elements[0].length,n;for(s=0;s<i;s++){if(e.elements[s][s]===0){for(r=s+1;r<i;r++)if(e.elements[r][s]!==0){for(t=[],n=0;n<o;n++)t.push(e.elements[s][n]+e.elements[r][n]);e.elements[s]=t;break}}if(e.elements[s][s]!==0)for(r=s+1;r<i;r++){var h=e.elements[r][s]/e.elements[s][s];for(t=[],n=0;n<o;n++)t.push(n<=s?0:e.elements[r][n]-e.elements[s][n]*h);e.elements[r]=t}}return e},determinant:function(){if(this.elements.length===0)return 1;if(!this.isSquare())return null;for(var e=this.toRightTriangular(),t=e.elements[0][0],i=e.elements.length,s=1;s<i;s++)t=t*e.elements[s][s];return t},isSingular:function(){return this.isSquare()&&this.determinant()===0},augment:function(e){if(this.elements.length===0)return this.dup();var t=e.elements||e;typeof t[0][0]>"u"&&(t=Sylvester.Matrix.create(t).elements);var i=this.dup(),s=i.elements[0].length,r=i.elements.length,o=t[0].length,n;if(r!==t.length)return null;for(;r--;)for(n=o;n--;)i.elements[r][s+n]=t[r][n];return i},inverse:function(){if(this.elements.length===0||!this.isSquare()||this.isSingular())return null;for(var e=this.elements.length,t=e,i,s=this.augment(Sylvester.Matrix.I(e)).toRightTriangular(),r=s.elements[0].length,o,n,h,a=[],l;t--;){for(n=[],a[t]=[],h=s.elements[t][t],o=0;o<r;o++)l=s.elements[t][o]/h,n.push(l),o>=e&&a[t].push(l);for(s.elements[t]=n,i=t;i--;){for(n=[],o=0;o<r;o++)n.push(s.elements[i][o]-s.elements[t][o]*s.elements[i][t]);s.elements[i]=n}}return Sylvester.Matrix.create(a)},setElements:function(e){var t,i,s=e.elements||e;if(s[0]&&typeof s[0][0]<"u"){for(t=s.length,this.elements=[];t--;)for(i=s[t].length,this.elements[t]=[];i--;)this.elements[t][i]=s[t][i];return this}var r=s.length;for(this.elements=[],t=0;t<r;t++)this.elements.push([s[t]]);return this}};var matrixInverse=function(e){const t=Sylvester.Matrix.create(e).inverse();return t!==null?t.elements:null},matrixInverse$1=getDefaultExportFromCjs(matrixInverse),sha1={exports:{}};/*
* [js-sha1]{@link https://github.com/emn178/js-sha1}
*
* @version 0.6.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2017
* @license MIT
*/(function(module){(function(){var root=typeof window=="object"?window:{},NODE_JS=!root.JS_SHA1_NO_NODE_JS&&typeof process=="object"&&process.versions&&process.versions.node;NODE_JS&&(root=commonjsGlobal);var COMMON_JS=!root.JS_SHA1_NO_COMMON_JS&&!0&&module.exports,HEX_CHARS="0123456789abcdef".split(""),EXTRA=[-2147483648,8388608,32768,128],SHIFT=[24,16,8,0],OUTPUT_TYPES=["hex","array","digest","arrayBuffer"],blocks=[],createOutputMethod=function(e){return function(t){return new Sha1(!0).update(t)[e]()}},createMethod=function(){var e=createOutputMethod("hex");NODE_JS&&(e=nodeWrap(e)),e.create=function(){return new Sha1},e.update=function(s){return e.create().update(s)};for(var t=0;t<OUTPUT_TYPES.length;++t){var i=OUTPUT_TYPES[t];e[i]=createOutputMethod(i)}return e},nodeWrap=function(method){var crypto=eval("require('crypto')"),Buffer=eval("require('buffer').Buffer"),nodeMethod=function(e){if(typeof e=="string")return crypto.createHash("sha1").update(e,"utf8").digest("hex");if(e.constructor===ArrayBuffer)e=new Uint8Array(e);else if(e.length===void 0)return method(e);return crypto.createHash("sha1").update(new Buffer(e)).digest("hex")};return nodeMethod};function Sha1(e){e?(blocks[0]=blocks[16]=blocks[1]=blocks[2]=blocks[3]=blocks[4]=blocks[5]=blocks[6]=blocks[7]=blocks[8]=blocks[9]=blocks[10]=blocks[11]=blocks[12]=blocks[13]=blocks[14]=blocks[15]=0,this.blocks=blocks):this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],this.h0=1732584193,this.h1=4023233417,this.h2=2562383102,this.h3=271733878,this.h4=3285377520,this.block=this.start=this.bytes=this.hBytes=0,this.finalized=this.hashed=!1,this.first=!0}Sha1.prototype.update=function(e){if(!this.finalized){var t=typeof e!="string";t&&e.constructor===root.ArrayBuffer&&(e=new Uint8Array(e));for(var i,s=0,r,o=e.length||0,n=this.blocks;s<o;){if(this.hashed&&(this.hashed=!1,n[0]=this.block,n[16]=n[1]=n[2]=n[3]=n[4]=n[5]=n[6]=n[7]=n[8]=n[9]=n[10]=n[11]=n[12]=n[13]=n[14]=n[15]=0),t)for(r=this.start;s<o&&r<64;++s)n[r>>2]|=e[s]<<SHIFT[r++&3];else for(r=this.start;s<o&&r<64;++s)i=e.charCodeAt(s),i<128?n[r>>2]|=i<<SHIFT[r++&3]:i<2048?(n[r>>2]|=(192|i>>6)<<SHIFT[r++&3],n[r>>2]|=(128|i&63)<<SHIFT[r++&3]):i<55296||i>=57344?(n[r>>2]|=(224|i>>12)<<SHIFT[r++&3],n[r>>2]|=(128|i>>6&63)<<SHIFT[r++&3],n[r>>2]|=(128|i&63)<<SHIFT[r++&3]):(i=65536+((i&1023)<<10|e.charCodeAt(++s)&1023),n[r>>2]|=(240|i>>18)<<SHIFT[r++&3],n[r>>2]|=(128|i>>12&63)<<SHIFT[r++&3],n[r>>2]|=(128|i>>6&63)<<SHIFT[r++&3],n[r>>2]|=(128|i&63)<<SHIFT[r++&3]);this.lastByteIndex=r,this.bytes+=r-this.start,r>=64?(this.block=n[16],this.start=r-64,this.hash(),this.hashed=!0):this.start=r}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296<<0,this.bytes=this.bytes%4294967296),this}},Sha1.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var e=this.blocks,t=this.lastByteIndex;e[16]=this.block,e[t>>2]|=EXTRA[t&3],this.block=e[16],t>=56&&(this.hashed||this.hash(),e[0]=this.block,e[16]=e[1]=e[2]=e[3]=e[4]=e[5]=e[6]=e[7]=e[8]=e[9]=e[10]=e[11]=e[12]=e[13]=e[14]=e[15]=0),e[14]=this.hBytes<<3|this.bytes>>>29,e[15]=this.bytes<<3,this.hash()}},Sha1.prototype.hash=function(){var e=this.h0,t=this.h1,i=this.h2,s=this.h3,r=this.h4,o,n,h,a=this.blocks;for(n=16;n<80;++n)h=a[n-3]^a[n-8]^a[n-14]^a[n-16],a[n]=h<<1|h>>>31;for(n=0;n<20;n+=5)o=t&i|~t&s,h=e<<5|e>>>27,r=h+o+r+1518500249+a[n]<<0,t=t<<30|t>>>2,o=e&t|~e&i,h=r<<5|r>>>27,s=h+o+s+1518500249+a[n+1]<<0,e=e<<30|e>>>2,o=r&e|~r&t,h=s<<5|s>>>27,i=h+o+i+1518500249+a[n+2]<<0,r=r<<30|r>>>2,o=s&r|~s&e,h=i<<5|i>>>27,t=h+o+t+1518500249+a[n+3]<<0,s=s<<30|s>>>2,o=i&s|~i&r,h=t<<5|t>>>27,e=h+o+e+1518500249+a[n+4]<<0,i=i<<30|i>>>2;for(;n<40;n+=5)o=t^i^s,h=e<<5|e>>>27,r=h+o+r+1859775393+a[n]<<0,t=t<<30|t>>>2,o=e^t^i,h=r<<5|r>>>27,s=h+o+s+1859775393+a[n+1]<<0,e=e<<30|e>>>2,o=r^e^t,h=s<<5|s>>>27,i=h+o+i+1859775393+a[n+2]<<0,r=r<<30|r>>>2,o=s^r^e,h=i<<5|i>>>27,t=h+o+t+1859775393+a[n+3]<<0,s=s<<30|s>>>2,o=i^s^r,h=t<<5|t>>>27,e=h+o+e+1859775393+a[n+4]<<0,i=i<<30|i>>>2;for(;n<60;n+=5)o=t&i|t&s|i&s,h=e<<5|e>>>27,r=h+o+r-1894007588+a[n]<<0,t=t<<30|t>>>2,o=e&t|e&i|t&i,h=r<<5|r>>>27,s=h+o+s-1894007588+a[n+1]<<0,e=e<<30|e>>>2,o=r&e|r&t|e&t,h=s<<5|s>>>27,i=h+o+i-1894007588+a[n+2]<<0,r=r<<30|r>>>2,o=s&r|s&e|r&e,h=i<<5|i>>>27,t=h+o+t-1894007588+a[n+3]<<0,s=s<<30|s>>>2,o=i&s|i&r|s&r,h=t<<5|t>>>27,e=h+o+e-1894007588+a[n+4]<<0,i=i<<30|i>>>2;for(;n<80;n+=5)o=t^i^s,h=e<<5|e>>>27,r=h+o+r-899497514+a[n]<<0,t=t<<30|t>>>2,o=e^t^i,h=r<<5|r>>>27,s=h+o+s-899497514+a[n+1]<<0,e=e<<30|e>>>2,o=r^e^t,h=s<<5|s>>>27,i=h+o+i-899497514+a[n+2]<<0,r=r<<30|r>>>2,o=s^r^e,h=i<<5|i>>>27,t=h+o+t-899497514+a[n+3]<<0,s=s<<30|s>>>2,o=i^s^r,h=t<<5|t>>>27,e=h+o+e-899497514+a[n+4]<<0,i=i<<30|i>>>2;this.h0=this.h0+e<<0,this.h1=this.h1+t<<0,this.h2=this.h2+i<<0,this.h3=this.h3+s<<0,this.h4=this.h4+r<<0},Sha1.prototype.hex=function(){this.finalize();var e=this.h0,t=this.h1,i=this.h2,s=this.h3,r=this.h4;return HEX_CHARS[e>>28&15]+HEX_CHARS[e>>24&15]+HEX_CHARS[e>>20&15]+HEX_CHARS[e>>16&15]+HEX_CHARS[e>>12&15]+HEX_CHARS[e>>8&15]+HEX_CHARS[e>>4&15]+HEX_CHARS[e&15]+HEX_CHARS[t>>28&15]+HEX_CHARS[t>>24&15]+HEX_CHARS[t>>20&15]+HEX_CHARS[t>>16&15]+HEX_CHARS[t>>12&15]+HEX_CHARS[t>>8&15]+HEX_CHARS[t>>4&15]+HEX_CHARS[t&15]+HEX_CHARS[i>>28&15]+HEX_CHARS[i>>24&15]+HEX_CHARS[i>>20&15]+HEX_CHARS[i>>16&15]+HEX_CHARS[i>>12&15]+HEX_CHARS[i>>8&15]+HEX_CHARS[i>>4&15]+HEX_CHARS[i&15]+HEX_CHARS[s>>28&15]+HEX_CHARS[s>>24&15]+HEX_CHARS[s>>20&15]+HEX_CHARS[s>>16&15]+HEX_CHARS[s>>12&15]+HEX_CHARS[s>>8&15]+HEX_CHARS[s>>4&15]+HEX_CHARS[s&15]+HEX_CHARS[r>>28&15]+HEX_CHARS[r>>24&15]+HEX_CHARS[r>>20&15]+HEX_CHARS[r>>16&15]+HEX_CHARS[r>>12&15]+HEX_CHARS[r>>8&15]+HEX_CHARS[r>>4&15]+HEX_CHARS[r&15]},Sha1.prototype.toString=Sha1.prototype.hex,Sha1.prototype.digest=function(){this.finalize();var e=this.h0,t=this.h1,i=this.h2,s=this.h3,r=this.h4;return[e>>24&255,e>>16&255,e>>8&255,e&255,t>>24&255,t>>16&255,t>>8&255,t&255,i>>24&255,i>>16&255,i>>8&255,i&255,s>>24&255,s>>16&255,s>>8&255,s&255,r>>24&255,r>>16&255,r>>8&255,r&255]},Sha1.prototype.array=Sha1.prototype.digest,Sha1.prototype.arrayBuffer=function(){this.finalize();var e=new ArrayBuffer(20),t=new DataView(e);return t.setUint32(0,this.h0),t.setUint32(4,this.h1),t.setUint32(8,this.h2),t.setUint32(12,this.h3),t.setUint32(16,this.h4),e};var exports=createMethod();COMMON_JS?module.exports=exports:root.sha1=exports})()})(sha1);var SemanticType=(e=>(e.ClefG="ClefG",e.ClefF="ClefF",e.ClefC="ClefC",e.NoteheadS0="NoteheadS0",e.NoteheadS1="NoteheadS1",e.NoteheadS2="NoteheadS2",e.NoteheadS1stemU="NoteheadS1stemU",e.NoteheadS1stemD="NoteheadS1stemD",e.NoteheadS2stemU="NoteheadS2stemU",e.NoteheadS2stemD="NoteheadS2stemD",e.vline_Stem="vline_Stem",e.Flag3="Flag3",e.BeamLeft="BeamLeft",e.BeamContinue="BeamContinue",e.BeamRight="BeamRight",e.TremoloLeft="TremoloLeft",e.TremoloRight="TremoloRight",e.TremoloMiddle="TremoloMiddle",e.Dot="Dot",e.Rest0="Rest0",e.Rest1="Rest1",e.Rest2="Rest2",e.Rest3="Rest3",e.Rest4="Rest4",e.Rest5="Rest5",e.Rest6="Rest6",e.Rest0W="Rest0W",e.RestM1="RestM1",e.AccNatural="AccNatural",e.AccSharp="AccSharp",e.AccDoublesharp="AccDoublesharp",e.AccFlat="AccFlat",e.AccFlatflat="AccFlatflat",e.vline_VoltaLeft="vline_VoltaLeft",e.vline_VoltaRight="vline_VoltaRight",e.VoltaLeft="VoltaLeft",e.VoltaRight="VoltaRight",e.VoltaAlternativeBegin="VoltaAlternativeBegin",e.BarMeasure="BarMeasure",e.vline_BarMeasure="vline_BarMeasure",e.vline_BarTerminal="vline_BarTerminal",e.vline_BarSegment="vline_BarSegment",e.SlurBegin="SlurBegin",e.SlurEnd="SlurEnd",e.TimesigC44="TimesigC44",e.TimesigC22="TimesigC22",e.TimesigZero="TimesigZero",e.TimesigOne="TimesigOne",e.TimesigTwo="TimesigTwo",e.TimesigThree="TimesigThree",e.TimesigFour="TimesigFour",e.TimesigFive="TimesigFive",e.TimesigSix="TimesigSix",e.TimesigSeven="TimesigSeven",e.TimesigEight="TimesigEight",e.TimesigNine="TimesigNine",e.OctaveShift8va="OctaveShift8va",e.OctaveShift8vb="OctaveShift8vb",e.OctaveShift8="OctaveShift8",e.OctaveShift0="OctaveShift0",e.Zero="Zero",e.One="One",e.Two="Two",e.Three="Three",e.Four="Four",e.Five="Five",e.Six="Six",e.Seven="Seven",e.Eight="Eight",e.Nine="Nine",e.f="f",e.p="p",e.m="m",e.n="n",e.r="r",e.s="s",e.z="z",e.CrescendoBegin="CrescendoBegin",e.CrescendoEnd="CrescendoEnd",e.DecrescendoBegin="DecrescendoBegin",e.DecrescendoEnd="DecrescendoEnd",e.ScriptFermata="ScriptFermata",e.ScriptShortFermata="ScriptShortFermata",e.ScriptSforzato="ScriptSforzato",e.ScriptStaccato="ScriptStaccato",e.ScriptStaccatissimo="ScriptStaccatissimo",e.ScriptTurn="ScriptTurn",e.ScriptTrill="ScriptTrill",e.ScriptSegno="ScriptSegno",e.ScriptCoda="ScriptCoda",e.ScriptArpeggio="ScriptArpeggio",e.ScriptPrall="ScriptPrall",e.ScriptMordent="ScriptMordent",e.ScriptMarcato="ScriptMarcato",e.ScriptTenuto="ScriptTenuto",e.ScriptPortato="ScriptPortato",e.PedalStar="PedalStar",e.PedalPed="PedalPed",e.KeyAcc="KeyAcc",e.TempoNotehead="TempoNotehead",e.GraceNotehead="GraceNotehead",e.SignLined="SignLined",e.SignInterval="SignInterval",e.rect_Text="rect_Text",e.rect_Lyric="rect_Lyric",e))(SemanticType||{});const st=SemanticType;st.NoteheadS0,st.NoteheadS1,st.NoteheadS2,st.Zero,st.One,st.Two,st.Three,st.Four,st.Five,st.Six,st.Seven,st.Eight,st.Nine,st.ScriptStaccatissimo,st.TimesigZero,st.TimesigOne,st.TimesigTwo,st.TimesigThree,st.TimesigFour,st.TimesigFive,st.TimesigSix,st.TimesigSeven,st.TimesigEight,st.TimesigNine,st.Rest0,st.Rest1,st.Rest2,st.Rest3,st.Rest4,st.Rest5,st.Rest6,st.Rest0W,st.RestM1,st.SignInterval,st.SignLined,st.BeamLeft,st.BeamContinue,st.BeamRight,st.ClefG,st.ClefF,st.ClefC,st.NoteheadS0,st.NoteheadS1,st.NoteheadS2,st.Dot,st.Rest0,st.Rest1,st.Rest2,st.Rest3,st.Rest4,st.Rest5,st.Rest6,st.RestM1,st.AccNatural,st.AccSharp,st.AccDoublesharp,st.AccFlat,st.AccFlatflat,st.TimesigC44,st.TimesigC22,st.TimesigZero,st.TimesigOne,st.TimesigTwo,st.TimesigThree,st.TimesigFour,st.TimesigFive,st.TimesigSix,st.TimesigSeven,st.TimesigEight,st.TimesigNine,st.One,st.Two,st.Three,st.Four,st.Five,st.OctaveShift8,st.OctaveShift0,st.f,st.p,st.m,st.n,st.r,st.s,st.z,st.ScriptFermata,st.ScriptShortFermata,st.ScriptSforzato,st.ScriptStaccato,st.ScriptStaccatissimo,st.ScriptTurn,st.ScriptTrill,st.ScriptSegno,st.ScriptCoda,st.ScriptArpeggio,st.ScriptPrall,st.ScriptMordent,st.ScriptMarcato,st.ScriptTenuto,st.ScriptPortato,st.PedalStar,st.PedalPed;const roundNumber=(e,t,i=-1/0)=>Math.max(Math.round(e/t)*t,i),gcd=(e,t)=>Number.isInteger(e)&&Number.isInteger(t)?t===0?e:gcd(t,e%t):(console.error("non-integer gcd:",e,t),1),frac=(e,t)=>({numerator:e,denominator:t}),reducedFraction=(e,t)=>{e=Math.round(e),t=Math.round(t);const i=e!==0?gcd(e,t):t;return frac(e/i,t/i)},fractionMul=(e,t)=>t?e*t.numerator/t.denominator:e;class DummyLogger{debug(...t){}group(...t){}groupCollapsed(...t){}groupEnd(){}info(...t){}warn(...t){}assert(...t){}}const EOM=-1,GREAT_NUMBER=1920,DURATION_MULTIPLIER=128*3*5*7*11*13,floatToFrac=e=>{const t=Math.round(e*GREAT_NUMBER);return reducedFraction(t,GREAT_NUMBER)},floatToTimeWarp=e=>e===1?null:floatToFrac(e);class Action{type;e1;e2;constructor(t){Object.assign(this,t)}static P(t){return new Action({type:0,e1:t})}static V(t,i,s=1){return new Action({type:1,e1:s>0?t:i,e2:s>0?i:t})}static H(t,i){return new Action({type:2,e1:t,e2:i})}get id(){switch(this.type){case 0:return this.e1.toString();case 1:return`${this.e1}|${this.e2}`;case 2:return`${this.e1}-${this.e2>=0?this.e2:"."}`}}get events(){return[this.e1,this.e2].filter(Number.isFinite)}}class StageMatrix{matrix;static fromNode(t,i){const s=Array(t.stages.length).fill(null).map(()=>Array(t.stages.length).fill(null).map(()=>new Set));t.actions.filter(l=>l.type===2).forEach(l=>{const u=t.stages.findIndex(d=>d.events.includes(l.e1)),v=t.stages.findIndex(d=>d.events.includes(l.e2));console.assert(u>=0&&v>=0,"invalid stages for H action:",t.id,t.stages,l),s[u][v].add(l.e1)}),s[0][t.stages.length-1].add(0);const r=t.stagedEvents,o=i.matrixH[i.matrixH.length-1].filter((l,u)=>!r.has(u)),n=Math.max(0,Math.max(...o)-.01),h=t.actions.filter(l=>l.type===2),a=Object.keys(i.eventMap).map(Number).filter(l=>!h.find(u=>u.e2===l));return t.stages.forEach(l=>{l.events.forEach(u=>{u>0&&!h.find(d=>d.e1===u)&&i.matrixH[i.matrixH.length-1][u]>=n&&(a.some(d=>i.matrixH[d][u]>0)||s[l.index][t.stages.length-1].add(u))})}),new StageMatrix({matrix:s})}constructor(t){Object.assign(this,t)}pathOf(t,i,s,r=0){if(this.matrix[t][i].size){const o=[...this.matrix[t][i]][r];if(i===s)return[o];for(let n=i+1;n<=s;++n){const h=this.pathOf(i,n,s);if(h)return[o,...h]}}return null}findDoublePath(t,i){const s=[];for(let r=i;r>=t+1;--r)for(let o=0;o<this.matrix[t][r].size;++o){const n=this.pathOf(t,r,i,o);if(n&&(s.push(n),s.length===2))return[s[0],s[1]]}return null}reducePath(t){this.matrix.forEach(i=>i.forEach(s=>t.forEach(r=>s.delete(r))))}toEquations(t){const i=[];for(let s=1;s<this.matrix.length;s++)for(let r=0;r<this.matrix.length-s;r++){const o=r+s;for(;;){const n=this.findDoublePath(r,o);if(n){const[h,a]=n,l=Array(t).fill(0);h.forEach(u=>l[u]=1),a.forEach(u=>l[u]=-1),i.push(l),this.reducePath(h.length>a.length?h:a)}else break}}return i}}class PathNode{logger;parent;action;possibility;children;stages;constraints;constructor(t){Object.assign(this,t),console.assert(this.logger,"logger is null:",t)}get actions(){const t=this.parent?this.parent.actions:[];return this.action?[...t,this.action]:t}get id(){return this.actions.map(i=>i.id).sort().join(" ")}get stagedEvents(){const t=new Set;return this.stages&&this.stages.forEach(i=>i.events.forEach(s=>s>=0&&t.add(s))),t}like(t){return t.split(" ").sort().join(" ")===this.id}constructStages(t){this.stages=[{events:[EOM]}];for(const i of this.actions)switch(i.type){case 0:this.stages.unshift({events:[i.e1]});break;case 1:{const s=this.stages.find(o=>o.events.includes(i.e1)),r=this.stages.find(o=>o.events.includes(i.e2));console.assert(s||r,"invalid V action:",this.stages,i),s&&r?(s.events.push(...r.events),r.events=null,this.stages=this.stages.filter(o=>o.events)):s?r||s.events.push(i.e2):r.events.unshift(i.e1)}break;case 2:{const s=this.stages.find(n=>n.events.includes(i.e1)),r=this.stages.find(n=>n.events.includes(i.e2));console.assert(s||r,"invalid H action:",this.stages,i);const o=n=>{console.assert(t.eventMap[n],"invalid event id:",i.id,n,t.eventMap);const h=t.eventMap[n].x,a=this.stages.find(l=>l.events.some(u=>u>0&&t.eventMap[u].x<=h)&&l.events.some(u=>u>0&&t.eventMap[u].x>=h));if(a)a.events.push(n);else{const l={events:[n]},u=this.stages.findIndex(v=>v.events[0]===EOM||t.eventMap[v.events[0]].x>=h);this.stages.splice(u,0,l)}};s||o(i.e1),r||o(i.e2)}break}this.stages.forEach((i,s)=>i.index=s)}constructConstraints(t){const i=Object.keys(t.eventMap).length,r=StageMatrix.fromNode(this,t).toEquations(i),o=Array(i).fill(null).map((n,h)=>t.eventMap[h].duration);this.constraints=r.map(n=>n.map((h,a)=>h*o[a]))}inbalancesConstraints(t){console.assert(this.constraints,"constraints not constructed.");const i=Object.keys(t.eventMap).length,s=Array(i).fill(!0),r=Array(i).fill(!1),o=[];for(const n of this.constraints){const h=n.reduce((a,l)=>a+l,0);if(h!==0){const a=h<0?n.map(l=>-l):n;if(a[0]>0)continue;o.push(a),a.forEach((l,u)=>{r[u]=r[u]||l<0,l&&(s[u]=l<0||r[u])})}}return this.constraints.forEach(n=>{n.reduce((a,l)=>a+l,0)===0&&!n[0]&&n.some((a,l)=>a&&!s[l])&&(n.forEach((a,l)=>a&&(s[l]=!1)),o.push(n))}),{ones:s,inbalances:o}}solveEquations({ones:t,inbalances:i}){if(!i.length)return t.map(()=>1);const s=t.map((c,g)=>({fixed:c,i:g})).filter(({fixed:c})=>!c).map(({i:c})=>c).filter(c=>i.some(g=>g[c]!==0));if(!s.length)return t.map(()=>1);const r=s.map(c=>Math.abs(i.find(g=>g[c]!==0)[c])),o=new Map;let n=!1;const h=i.map(c=>{const g=c.filter((m,S)=>s.includes(S)),p=-c.reduce((m,S,F)=>m+(s.includes(F)?0:S),0);return{line:g,bias:p}}).filter(({line:c,bias:g})=>{if(c.every(m=>m===0))return!1;const p=c.join(",");return o.has(p)?(n=o.get(p)!==g,!1):(o.set(p,g),!0)});if(n)return null;const a=h.slice(0,s.length),l=h.slice(s.length);if(a.length<s.length){const c=[];for(let g=0;g<s.length-1;++g){const p=g+1,m={line:s.map((S,F)=>F===g?1:F===p?-1:0),bias:0,prior:(r[g]+r[p])/DURATION_MULTIPLIER};a.some(S=>S.line[g]&&S.line[p])&&(m.prior-=10),a.some(S=>S.line.filter(Number).length===1&&(S.line[g]||S.line[p]))&&(m.prior+=1),c.push(m)}c.sort((g,p)=>g.prior-p.prior),a.push(...c.slice(0,s.length-a.length))}const u=a.map(({line:c})=>c),v=a.map(({bias:c})=>c),d=matrixInverse$1(u);if(!d)return this.logger.warn("null invert:",u),null;const x=d.map(c=>c.reduce((g,p,m)=>g+p*v[m],0));if(l.length&&l.some(c=>Math.abs(c.line.reduce((g,p,m)=>g+p*x[m],0))>.001))return null;const f=t.map(()=>1);return s.forEach((c,g)=>f[c]=x[g]),f}optimallySolve(t){const{ones:i,inbalances:s}=this.inbalancesConstraints(t),o=i.map((h,a)=>h?-1:roundNumber(t.eventMap[a].shrinkness,.01)).reduce((h,a,l)=>(a>=0&&(h[a]=h[a]||[],h[a].push(l)),h),{}),n=Object.entries(o).sort((h,a)=>Number(a[0])-Number(h[0])).map(h=>h[1]);for(let h=1;h<n.length;++h){const a=[].concat(...n.slice(0,h)),l=i.map((v,d)=>!a.includes(d)),u=this.solveEquations({ones:l,inbalances:s});if(u&&u.every((v,d)=>v<=1&&v>t.eventMap[d].lowWarp))return u}return this.solveEquations({ones:i,inbalances:s})}isConflicted(t){const{ones:i,inbalances:s}=this.inbalancesConstraints(t);for(const o of s)if(o.reduce((h,a,l)=>h+a*(i[l]||a<=0?1:t.eventMap[l].lowWarp),0)>=0)return o.forEach((h,a)=>{h&&(t.eventTendencies[a]+=h>0?1:-1)}),!0;if(!s.length)return!1;const r=this.solveEquations({ones:i,inbalances:s});return r?!r.every((o,n)=>o>t.eventMap[n].lowWarp&&o<=1):!0}getSolution(t){const i=f=>t.eventMap[f.e2]?t.eventMap[f.e2].x+Math.abs(t.eventMap[f.e2].x-t.eventMap[f.e1].x)*.06:t.eventMap[f.e1].x+1e4,s=this.actions.filter(f=>f.type===2).sort((f,c)=>i(f)-i(c)),r=s.reduce((f,c)=>({...f,[c.e1]:c.e2}),{}),o=new Set([...Object.keys(r)].map(Number));s.forEach(f=>o.delete(f.e2)),this.stages[0].events.forEach(f=>f>0&&o.add(f));let n=[...o].map(f=>{const c=[f];let g=f;for(;r[g]&&(g=r[g],!(g<0||c.includes(g)));)c.push(g);return c});const h=Object.values(t.eventMap).filter(f=>f.id>0).map(f=>({id:f.id,tick:null,endTick:null,tickGroup:null,timeWarp:null})),a=h.filter(f=>n.some(c=>c.includes(f.id))||s.some(c=>[c.e1,c.e2].includes(f.id))).reduce((f,c)=>({...f,[c.id]:c}),{});this.stages.forEach((f,c)=>f.events.forEach(g=>a[g]&&(a[g].tickGroup=c))),this.stages[0].tick=0,this.stages[0].events.forEach(f=>a[f]&&(a[f].tick=0));const l=this.optimallySolve(t);h.forEach(f=>f.timeWarp=floatToTimeWarp(l[f.id]));const u=this.stages.slice(0,this.stages.length-1),v=()=>{if(u.every(c=>Number.isFinite(c.tick)))return!1;let f=!1;return s.forEach(c=>{const g=this.stages.find(m=>m.events.includes(c.e1)),p=this.stages.find(m=>m.events.includes(c.e2));Number.isFinite(g.tick)&&!Number.isFinite(p.tick)&&(p.tick=g.tick+fractionMul(t.eventMap[c.e1].duration,a[c.e1].timeWarp),p.events.forEach(m=>a[m]&&(a[m].tick=p.tick)),f=!0)}),[...s].reverse().forEach(c=>{const g=this.stages.find(m=>m.events.includes(c.e1)),p=this.stages.find(m=>m.events.includes(c.e2));!Number.isFinite(g.tick)&&Number.isFinite(p.tick)&&(g.tick=p.tick-fractionMul(t.eventMap[c.e1].duration,a[c.e1].timeWarp),g.events.forEach(m=>a[m]&&(a[m].tick=g.tick)),f=!0)}),f};for(;v(););console.assert(u.every(f=>Number.isFinite(f.tick)),"stage ticks not all solved:",this.stages,this.id),h.filter(f=>Number.isFinite(f.tick)).forEach(f=>f.endTick=f.tick+fractionMul(t.eventMap[f.id].duration,f.timeWarp));const d=t.eventMap[0].duration;n.forEach(f=>{const c=f.findIndex(g=>a[g].endTick>d);c>=0&&f.splice(c,f.length-c).forEach(p=>{a[p].tick=null,a[p].endTick=null})}),n=n.filter(f=>f.length);const x=Math.max(0,...h.map(f=>f.endTick).filter(Number.isFinite));return this.logger.debug(String.fromCodePoint(127822),this.id,l),{voices:n,events:h,duration:x,actions:this.actions.map(f=>f.id).join(" ")}}deduce(t,i){this.stages||this.constructStages(t);const s=t.actionAccessing.get(this.id)||{times:0};if(++s.times,t.actionAccessing.set(this.id,s),this.constructConstraints(t),this.isConflicted(t))return s.closed=!0,this.logger.info(this.action.id,"❌"),null;if(this.logger.group(this.action&&this.action.id),i.credits>0){if(--i.credits,this.children||this.expand(t),this.children=this.children.filter(r=>!t.actionAccessing.get(r.id)||!t.actionAccessing.get(r.id).closed),this.children.length){const r=o=>o.possibility/((t.actionAccessing.get(o.id)||{times:0}).times+1);this.children.sort((o,n)=>r(n)-r(o));for(const o of this.children){const n=o.deduce(t,i);if(n)return this.logger.groupEnd(),n;if(i.credits<=0)break}}}else this.logger.debug("quota exhausted.");return this.logger.groupEnd(),s.closed=!0,this.getSolution(t)}expand(t){this.constructStages(t);const{eventMap:i,matrixV:s,matrixH:r}=t,o=this.stagedEvents,n=[],h=a=>{if(!this.actions.some(l=>l.id===a.action.id)&&!n.some(l=>l.action.id===a.action.id)){const l=this.stages.find(v=>v.events.includes(a.action.e1)),u=this.stages.find(v=>v.events.includes(a.action.e2));if(l===u||l&&u&&l.index>=u.index)return;if(l&&u){if(a.action.type===1){if(u.index-l.index>1||this.actions.some(v=>l.events.includes(v.e1)&&u.events.includes(v.e2)))return}else if(a.action.type===2&&l.index>u.index)return}if(a.action.type===2&&this.actions.some(v=>v.type===2&&(v.e1===a.action.e1||v.e2===a.action.e2||v.e1===a.action.e2&&v.e2===a.action.e1))||a.action.type===1&&(l&&(a.possibility=Math.min(a.possibility,...l.events.map(v=>s[a.action.e2][v])),a.possibility<=0)||u&&(a.possibility=Math.min(a.possibility,...u.events.map(v=>s[v][a.action.e1])),a.possibility<=0)))return;n.push(a)}};for(const a of o)a<0||(s[a].forEach((l,u)=>{l>0&&a!==u&&h({action:Action.V(u,a),possibility:l})}),s.forEach((l,u)=>{const v=l[a];v>0&&h({action:Action.V(a,u),possibility:v})}),r[a].forEach((l,u)=>{l>0&&h({action:Action.H(u,a),possibility:l})}),r.forEach((l,u)=>{u=u>=Object.keys(i).length?-1:u;const v=l[a];v>0&&h({action:Action.H(a,u),possibility:v})}));if(!n.some(a=>[2,0].includes(a.action.type)||!o.has(a.action.e1)||!o.has(a.action.e2))){this.children=[];return}this.children=n.map(a=>new PathNode({logger:this.logger,parent:this,...a}))}}class Solver{quota;logger;events;matrixH;matrixV;xSpan;eventMap;actionAccessing;pathRoot;constructor(t,{quota:i=1e3,logger:s=new DummyLogger}={}){this.quota=i,this.logger=s;const r={id:0,x:0,confidence:1,shrinkness:t.measureShrinkness,duration:t.expectedDuration,lowWarp:0};this.events=[r,...t.events.map(o=>({id:o.id,x:o.x,confidence:o.confidence,shrinkness:o.shrinkness,staff:o.staff,duration:o.duration,lowWarp:.5}))],this.eventMap=this.events.reduce((o,n)=>({...o,[n.id]:n}),{}),this.matrixH=t.matrixH,this.matrixV=t.matrixV,this.xSpan=t.endX-Math.min(t.endX-1,...t.events.map(o=>o.x)),this.actionAccessing=new Map}solve(){this.pathRoot=new PathNode({logger:this.logger,action:null}),this.pathRoot.children=this.events.slice(1).map(r=>new PathNode({logger:this.logger,parent:this.pathRoot,action:Action.P(r.id),possibility:this.matrixV[r.id].reduce((o,n)=>o+n,0)}));let t=null;this.logger.groupCollapsed("solve");const i=Array(this.events.length).fill(0),s={credits:this.quota,times:0};for(;s.credits>0;){++s.times;const r={eventMap:this.eventMap,matrixH:this.matrixH,matrixV:this.matrixV,actionAccessing:this.actionAccessing,eventTendencies:i},o=this.pathRoot.deduce(r,s);if(o.credits=this.quota-s.credits,o.times=s.times,this.evaluateSolution(o),this.logger.debug("loss:",o.loss),t=!t||o.loss<t.loss?o:t,!t.loss||this.actionAccessing.get("").closed)break}return this.logger.groupEnd(),this.logger.debug("solution",t&&t.loss,t),this.logger.debug("cost:",this.quota-s.credits),this.logger.debug("eventTendencies:",i.map(r=>r/s.times)),t}evaluateSolution(t){t.loss=0;const i=t.events.reduce((d,x)=>({...d,[x.id]:{...x,...this.eventMap[x.id]}}),{}),s=t.events.filter(d=>Number.isFinite(d.tick)).map(d=>i[d.id]),r=s.reduce((d,x)=>(d[x.staff]=d[x.staff]||[],d[x.staff].push(x),d),{});Object.values(r).forEach(d=>{d.sort((f,c)=>f.x-c.x).slice(0,d.length-1).forEach((f,c)=>{d[c+1].tick<f.tick&&(t.loss+=1e3)})});const o=new Map;t.events.forEach(d=>{if((!Number.isFinite(d.tick)||t.voices.every(x=>!x.includes(d.id)))&&(t.loss+=100*i[d.id].confidence),d.timeWarp){const{numerator:x,denominator:f}=d.timeWarp,c=i[d.id].shrinkness;o.set(x,Math.max(o.get(x)||0,1-c)),o.set(f,Math.max(o.get(f)||0,1-c))}});const n=reducedFraction(t.duration,this.eventMap[0].duration);o.set(n.numerator,Math.max(o.get(n.numerator)||0,1-this.eventMap[0].shrinkness)),o.set(n.denominator,Math.max(o.get(n.denominator)||0,1-this.eventMap[0].shrinkness));for(const[d,x]of o.entries())d>1&&(t.loss+=Math.log(d)*x);let h=0,a=0;t.voices.forEach(d=>{console.assert(i[d[0]],"invalid voice:",d,Object.keys(i));const x=Math.abs(i[d[0]].tick),f=i[d[d.length-1]].endTick;h+=Math.max(0,x+t.duration-f);let c=null;d.forEach(g=>{const p=i[g];p.staff!==c&&(c!==null&&++a,c=p.staff)})}),t.loss+=h*10/DURATION_MULTIPLIER,t.loss+=5**a-1;const l=[...s].sort((d,x)=>d.x-x.x),u=l.slice(1).map((d,x)=>{const f=l[x],c=d.x-f.x,g=d.tick-f.tick;return g?(Math.atan2(g/t.duration,c/this.xSpan)*4/Math.PI-1)**2:c/this.xSpan}),v=Math.max(...u,0);t.loss+=v**2,console.assert(t.loss>=0,"Invalid solution loss!!!",t.loss,o,h,a),t.loss<0&&(t.loss=1/0)}}const solveStaffGroup=(e,t)=>e.events.length?new Solver(e,t).solve():{events:[],voices:[],duration:0};self.onmessage=e=>{const{id:t,args:i}=e.data;try{const s=solveStaffGroup(i[0],i[1]);self.postMessage({id:t,result:s})}catch(s){self.postMessage({id:t,error:s.message})}}})();