data-structure-typed 2.2.3 → 2.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -1
- package/README.md +70 -11
- package/dist/cjs/index.cjs +85 -75
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +85 -75
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +85 -75
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +85 -75
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +46 -26
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/data-structure-typed.js +85 -75
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +4 -5
- package/src/data-structures/binary-tree/bst.ts +111 -82
- package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +5 -8
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +2 -2
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
`),n.isShowNull&&(i+=`N for null
|
|
3
3
|
`),n.isShowRedBlackNIL&&(i+=`S for Sentinel Node(NIL)
|
|
4
4
|
`),(o=>{let[d]=this._displayAux(o,n),a="";for(let u of d)a+=u+`
|
|
5
|
-
`;i+=a})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType,o=!1,d=c=>!!c,a=c=>!!c,u=c=>o?this.isRealNodeOrNull(c):this.isRealNode(c),f=c=>this.isRealNodeOrNull(c)){if(i=this.ensureNode(i),!i)return[];let c=[];if(s==="RECURSIVE"){let _=p=>{if(!u(p))return;let g=()=>{d(p)&&(p==null?void 0:p.left)!==void 0&&_(p==null?void 0:p.left)},T=()=>{a(p)&&(p==null?void 0:p.right)!==void 0&&_(p==null?void 0:p.right)};switch(t){case"IN":if(g(),f(p)&&(c.push(e(p)),n))return;T();break;case"PRE":if(f(p)&&(c.push(e(p)),n))return;g(),T();break;case"POST":if(g(),T(),f(p)&&(c.push(e(p)),n))return;break}};_(i)}else{let _=[{opt:0,node:i}],p=m=>{var b;d(m.node)&&_.push({opt:0,node:(b=m.node)==null?void 0:b.left})},g=m=>{var b;a(m.node)&&_.push({opt:0,node:(b=m.node)==null?void 0:b.right})},T=m=>{u(m.node)&&_.push({opt:1,node:m.node})};for(;_.length>0;){let m=_.pop();if(m!==void 0&&u(m.node))if(m.opt===1){if(f(m.node)&&m.node!==void 0&&(c.push(e(m.node)),n))return c}else switch(t){case"IN":g(m),T(m),p(m);break;case"PRE":g(m),p(m),T(m);break;case"POST":T(m),g(m),p(m);break}}}return c}*_getIterator(e=this._root){if(e)if(this.iterationType==="ITERATIVE"){let t=[],n=e;for(;n||t.length>0;){for(;this.isRealNode(n);)t.push(n),n=n.left;n=t.pop(),this.isRealNode(n)&&(this._isMapMode?yield[n.key,this._store.get(n.key)]:yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),this._isMapMode?yield[e.key,this._store.get(e.key)]:yield[e.key,e.value],e.right&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t){if(e===void 0)return[void 0,void 0];if(e===null)return[null,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[n,i]=e;if(n===void 0)return[void 0,void 0];if(n===null)return[null,void 0];let s=t!=null?t:i;return[this.createNode(n,s),s]}return[this.createNode(e,t),t]}_clone(e){this.bfs(t=>{t===null?e.add(null):this._isMapMode?e.add([t.key,this._store.get(t.key)]):e.add([t.key,t.value])},this._root,this.iterationType,!0),this._isMapMode&&(e._store=this._store)}_displayAux(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:s}=t,o=[["\u2500"],1,0,0];if(e===null&&!n)return o;if(e===void 0&&!i)return o;if(this.isNIL(e)&&!s)return o;if(e!=null){let a=e.key,u=this.isNIL(e)?"S":String(a),f=u.length;return d(u,f,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let a=e===void 0?"U":"N",u=a.length;return d(a,u,[[""],1,0,0],[[""],1,0,0])}function d(a,u,f,c){let[_,p,g,T]=f,[m,b,V,y]=c,K=" ".repeat(Math.max(0,T+1))+"_".repeat(Math.max(0,p-T-1))+a+"_".repeat(Math.max(0,y))+" ".repeat(Math.max(0,b-y)),E=(g>0?" ".repeat(T)+"/"+" ".repeat(p-T-1):" ".repeat(p))+" ".repeat(u)+(V>0?" ".repeat(y)+"\\"+" ".repeat(b-y-1):" ".repeat(b)),R=[K,E];for(let N=0;N<Math.max(g,V);N++){let F=N<g?_[N]:" ".repeat(p),Ye=N<V?m[N]:" ".repeat(b);R.push(F+" ".repeat(u)+Ye)}return[R,p+u+b,Math.max(g,V)+2,p+Math.floor(u/2)]}}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i}=t,s=this.createNode(n,i);return s&&(t.key=e.key,this._isMapMode||(t.value=e.value),e.key=s.key,this._isMapMode||(e.value=s.value)),t}}_replaceNode(e,t){return e.parent&&(e.parent.left===e?e.parent.left=t:e.parent.right===e&&(e.parent.right=t)),t.left=e.left,t.right=e.right,t.parent=e.parent,this._root===e&&this._setRoot(t),t}_setRoot(e){e&&(e.parent=void 0),this._root=e}_ensurePredicate(e){if(e==null)return t=>!1;if(this._isPredicate(e))return e;if(this.isRealNode(e))return t=>t===e;if(this.isEntry(e)){let[t]=e;return n=>n?n.key===t:!1}return t=>t?t.key===e:!1}_isPredicate(e){return typeof e=="function"}_extractKey(e){if(e===null)return null;if(e!==void 0&&e!==this._NIL)return this.isNode(e)?e.key:this.isEntry(e)?e[0]:e}_setValue(e,t){return e==null||t===void 0?!1:this._store.set(e,t)}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};var ie=class{constructor(r,e){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},H=class extends ne{constructor(e=[],t){super([],t);l(this,"_root");l(this,"_isReverse",!1);l(this,"_comparator",(e,t)=>{if(O(e)&&O(t))return e>t?1:e<t?-1:0;if(this._specifyComparable){let n=this._specifyComparable(e),i=this._specifyComparable(t);return n>i?1:n<i?-1:0}if(typeof e=="object"||typeof t=="object")throw TypeError("When comparing object types, a custom specifyComparable must be defined in the constructor's options.");return 0});l(this,"_specifyComparable");if(t){let{specifyComparable:n,isReverse:i}=t;typeof n=="function"&&(this._specifyComparable=n),i!==void 0&&(this._isReverse=i)}e&&this.addMany(e)}get root(){return this._root}get isReverse(){return this._isReverse}get comparator(){return this._comparator}get specifyComparable(){return this._specifyComparable}createNode(e,t){return new ie(e,this._isMapMode?void 0:t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof ie}isValidKey(e){return O(e,this._specifyComparable!==void 0)}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType){return super.dfs(e,t,n,i,s)}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.bfs(e,t,n,!1)}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.listLevels(e,t,n,!1)}getNode(e,t=this._root,n=this.iterationType){var i;return(i=this.getNodes(e,!0,t,n)[0])!=null?i:void 0}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,s=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let o,d=this.isRange(e);d?o=f=>f?e.isInRange(f.key,this._comparator):!1:o=this._ensurePredicate(e);let a=f=>{if(!f||!this.isRealNode(f.left))return!1;if(d){let c=e,_=this.isReverse?c.high:c.low,p=this.isReverse?c.includeHigh:c.includeLow;return p&&this._compare(f.key,_)>=0||!p&&this._compare(f.key,_)>0}if(!d&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(f.key,c)>0}return!0},u=f=>{if(!f||!this.isRealNode(f.right))return!1;if(d){let c=e,_=this.isReverse?c.low:c.high,p=this.isReverse?c.includeLow:c.includeHigh;return p&&this._compare(f.key,_)<=0||!p&&this._compare(f.key,_)<0}if(!d&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(f.key,c)<0}return!0};return super._dfs(n,"IN",t,i,s,!1,a,u,()=>!0,f=>!!f&&o(f))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let s=e instanceof S?e:new S(e[0],e[1]);return this.search(s,!1,t,n,i)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(this._root===void 0)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;let s=this._root;for(;s!==void 0;){if(this._compare(s.key,n.key)===0)return this._replaceNode(s,n),this._isMapMode&&this._setValue(s.key,i),!0;if(this._compare(s.key,n.key)>0){if(s.left===void 0)return s.left=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.left!==null&&(s=s.left)}else{if(s.right===void 0)return s.right=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.right!==null&&(s=s.right)}}return!1}addMany(e,t,n=!0,i=this.iterationType){let s=[],o=t==null?void 0:t[Symbol.iterator]();if(!n){for(let _ of e){let p=o==null?void 0:o.next().value;this.isRaw(_)&&(_=this._toEntryFn(_)),s.push(this.add(_,p))}return s}let d=[],a=0;for(let _ of e)d.push({key:_,value:o==null?void 0:o.next().value,orgIndex:a++});let u=d.sort(({key:_},{key:p})=>{let g,T;return this.isRaw(_)?g=this._toEntryFn(_)[0]:this.isEntry(_)?g=_[0]:this.isRealNode(_)?g=_.key:g=_,this.isRaw(p)?T=this._toEntryFn(p)[0]:this.isEntry(p)?T=p[0]:this.isRealNode(p)?T=p.key:T=p,g!=null&&T!=null?this._compare(g,T):0}),f=_=>{if(_.length===0)return;let p=Math.floor((_.length-1)/2),{key:g,value:T,orgIndex:m}=_[p];if(this.isRaw(g)){let b=this._toEntryFn(g);s[m]=this.add(b)}else s[m]=this.add(g,T);f(_.slice(0,p)),f(_.slice(p+1))};return i==="RECURSIVE"?f(u):(()=>{let p=[[0,u.length-1]];for(;p.length>0;){let g=p.pop();if(!g)continue;let[T,m]=g;if(T>m)continue;let b=T+Math.floor((m-T)/2),{key:V,value:y,orgIndex:K}=u[b];if(this.isRaw(V)){let E=this._toEntryFn(V);s[K]=this.add(E)}else s[K]=this.add(V,y);p.push([b+1,m]),p.push([T,b-1])}})(),s}lowerBound(e,t=this.iterationType){return this._bound(e,!0,t)}upperBound(e,t=this.iterationType){return this._bound(e,!1,t)}lesserOrGreaterTraverse(e=this._DEFAULT_NODE_CALLBACK,t=-1,n=this._root,i=this.iterationType){let s=this.ensureNode(n),o=[];if(!this._root||!s)return o;let d=s.key;if(i==="RECURSIVE"){let a=u=>{let f=this._compare(u.key,d);Math.sign(f)==t&&o.push(e(u)),this.isRealNode(u.left)&&a(u.left),this.isRealNode(u.right)&&a(u.right)};return a(this._root),o}else{let a=new M([this._root]);for(;a.length>0;){let u=a.shift();if(this.isRealNode(u)){let f=this._compare(u.key,d);Math.sign(f)==t&&o.push(e(u)),this.isRealNode(u.left)&&a.push(u.left),this.isRealNode(u.right)&&a.push(u.right)}}return o}}perfectlyBalance(e=this.iterationType){let t=this.dfs(o=>o,"IN",!1,this._root,e),n=t.length;if(this._clearNodes(),n===0)return!1;let i=(o,d,a)=>{if(o>d)return;let u=o+(d-o>>1),f=t[u],c=i(o,u-1,f),_=i(u+1,d,f);return f.left=c,f.right=_,f.parent=a,f},s=i(0,n-1,void 0);return this._setRoot(s),this._size=n,!0}isAVLBalanced(e=this.iterationType){if(!this._root)return!0;let t=!0;if(e==="RECURSIVE"){let n=i=>{if(!i)return 0;let s=n(i.left),o=n(i.right);return Math.abs(s-o)>1&&(t=!1),Math.max(s,o)+1};n(this._root)}else{let n=[],i=this._root,s,o=new Map;for(;n.length>0||i;)if(i)n.push(i),i.left!==null&&(i=i.left);else if(i=n[n.length-1],!i.right||s===i.right){if(i=n.pop(),i){let d=i.left?o.get(i.left):-1,a=i.right?o.get(i.right):-1;if(Math.abs(d-a)>1)return!1;o.set(i,1+Math.max(d,a)),s=i,i=void 0}}else i=i.right}return t}map(e,t,n){let i=this._createLike([],t),s=0;for(let[o,d]of this)i.add(e.call(n,d,o,s++,this));return i}deleteWhere(e){let t=[],n=this._root,i=0;for(;t.length>0||n!==void 0;){for(;n!=null;)t.push(n),n=n.left;let s=t.pop();if(!s)break;let o=s.key,d=s.value;if(e(o,d,i++,this))return this._deleteByKey(o);n=s.right}return!1}_bound(e,t,n){if(e==null)return;if(this._isPredicate(e))return this._boundByPredicate(e,n);let i;if(this.isNode(e))i=e.key;else if(this.isEntry(e)){let s=e[0];if(s==null)return;i=s}else i=e;if(i!==void 0)return this._boundByKey(i,t,n)}_boundByKey(e,t,n){var i,s;if(n==="RECURSIVE"){let o=d=>{if(!this.isRealNode(d))return;let a=this.comparator(d.key,e);if(t?a>=0:a>0){let f=o(d.left);return f!=null?f:d}else return o(d.right)};return o(this.root)}else{let o=this.root,d;for(;this.isRealNode(o);){let a=this.comparator(o.key,e);(t?a>=0:a>0)?(d=o,o=(i=o.left)!=null?i:void 0):o=(s=o.right)!=null?s:void 0}return d}}_boundByPredicate(e,t){if(t==="RECURSIVE"){let n,i=s=>{n||!this.isRealNode(s)||(this.isRealNode(s.left)&&i(s.left),!n&&e(s)&&(n=s),!n&&this.isRealNode(s.right)&&i(s.right))};return i(this.root),n}else{let n=[],i=this.root;for(;n.length>0||this.isRealNode(i);)if(this.isRealNode(i))n.push(i),i=i.left;else{let s=n.pop();if(!this.isRealNode(s))break;if(e(s))return s;i=s.right}return}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_snapshotOptions(){return{...super._snapshotOptions(),specifyComparable:this.specifyComparable,isReverse:this.isReverse}}_keyValueNodeOrEntryToNodeAndValue(e,t){let[n,i]=super._keyValueNodeOrEntryToNodeAndValue(e,t);return n===null?[void 0,void 0]:[n,t!=null?t:i]}_setRoot(e){e&&(e.parent=void 0),this._root=e}_compare(e,t){return this._isReverse?-this._comparator(e,t):this._comparator(e,t)}_deleteByKey(e){var s;let t=this._root;for(;t;){let o=this._compare(t.key,e);if(o===0)break;t=o>0?t.left:t.right}if(!t)return!1;let n=(o,d)=>{let a=o==null?void 0:o.parent;a?a.left===o?a.left=d:a.right=d:this._setRoot(d),d&&(d.parent=a)},i=o=>{if(o){for(;o.left!==void 0&&o.left!==null;)o=o.left;return o}};if(t.left===void 0)n(t,t.right);else if(t.right===void 0)n(t,t.left);else{let o=i(t.right);o.parent!==t&&(n(o,o.right),o.right=t.right,o.right&&(o.right.parent=o)),n(t,o),o.left=t.left,o.left&&(o.left.parent=o)}return this._size=Math.max(0,((s=this._size)!=null?s:0)-1),!0}};var Fe=class{constructor({frequency:r=0,max:e}){l(this,"_freq");l(this,"_max");l(this,"_freqMap");l(this,"_msb");l(this,"_negativeCount");this._freq=r,this._max=e,this._freqMap={0:0},this._msb=Ke(e),this._negativeCount=r<0?e:0}get freqMap(){return this._freqMap}get msb(){return this._msb}get negativeCount(){return this._negativeCount}get freq(){return this._freq}get max(){return this._max}readSingle(r){return this._checkIndex(r),this._readSingle(r)}update(r,e){this._checkIndex(r);let t=this._readSingle(r);this._update(r,e),this._updateNegativeCount(t,t+e)}writeSingle(r,e){this._checkIndex(r),this._writeSingle(r,e)}read(r){if(!Number.isInteger(r))throw new Error("Invalid count");return this._read(Math.max(Math.min(r,this.max),0))}lowerBound(r){if(this.negativeCount>0)throw new Error("Sequence is not non-descending");return this._binarySearch(r,(e,t)=>e<t)}upperBound(r){if(this.negativeCount>0)throw new Error("Must not be descending");return this._binarySearch(r,(e,t)=>e<=t)}getPrefixSum(r){this._checkIndex(r),r++;let e=0;for(;r>0;)e+=this._getFrequency(r),r-=r&-r;return e}_getFrequency(r){return r in this.freqMap?this.freqMap[r]:this.freq*(r&-r)}_updateFrequency(r,e){this.freqMap[r]=this._getFrequency(r)+e}_checkIndex(r){if(!Number.isInteger(r))throw new Error("Invalid index: Index must be an integer.");if(r<0||r>=this.max)throw new Error("Index out of range: Index must be within the range [0, this.max).")}_readSingle(r){r=r+1;let e=this._getFrequency(r),t=r-(r&-r);for(r--;r!==t;)e-=this._getFrequency(r),r-=r&-r;return e}_updateNegativeCount(r,e){r<0&&e>=0?this._negativeCount--:r>=0&&e<0&&this._negativeCount++}_update(r,e){for(r=r+1;r<=this.max;)this._updateFrequency(r,e),r+=r&-r}_writeSingle(r,e){let t=this._readSingle(r);this._update(r,e-t),this._updateNegativeCount(t,e)}_read(r){let e=r,t=0;for(;e;)t+=this._getFrequency(e),e-=e&-e;return t}_binarySearch(r,e){let t=0,n=this.msb<<1,i=r;for(;n>t+1;){let s=t+n>>1,o=this._getFrequency(s);s<=this.max&&e(o,i)?(i-=o,t=s):n=s}return t}};var $=class{constructor(r,e,t,n){l(this,"_start",0);l(this,"_end",0);l(this,"_value");l(this,"_sum",0);l(this,"_left");l(this,"_right");this._start=r,this._end=e,this._sum=t,this._value=n||void 0}get start(){return this._start}set start(r){this._start=r}get end(){return this._end}set end(r){this._end=r}get value(){return this._value}set value(r){this._value=r}get sum(){return this._sum}set sum(r){this._sum=r}get left(){return this._left}set left(r){this._left=r}get right(){return this._right}set right(r){this._right=r}},Ae=class{constructor(r,e,t){l(this,"_values",[]);l(this,"_start",0);l(this,"_end");l(this,"_root");e=e||0,t=t||r.length-1,this._values=r,this._start=e,this._end=t,r.length>0?this._root=this.build(e,t):(this._root=void 0,this._values=[])}get values(){return this._values}get start(){return this._start}get end(){return this._end}get root(){return this._root}build(r,e){if(r>e)return new $(r,e,0);if(r===e)return new $(r,e,this._values[r]);let t=r+Math.floor((e-r)/2),n=this.build(r,t),i=this.build(t+1,e),s=new $(r,e,n.sum+i.sum);return s.left=n,s.right=i,s}updateNode(r,e,t){let n=this.root||void 0;if(!n)return;let i=(s,o,d,a)=>{if(s.start===s.end&&s.start===o){s.sum=d,a!==void 0&&(s.value=a);return}let u=s.start+Math.floor((s.end-s.start)/2);o<=u?s.left&&i(s.left,o,d,a):s.right&&i(s.right,o,d,a),s.left&&s.right&&(s.sum=s.left.sum+s.right.sum)};i(n,r,e,t)}querySumByRange(r,e){let t=this.root||void 0;if(!t)return 0;if(r<0||e>=this.values.length||r>e)return NaN;let n=(i,s,o)=>{if(s<=i.start&&o>=i.end)return i.sum;let d=i.start+Math.floor((i.end-i.start)/2);if(o<=d)return i.left?n(i.left,s,o):NaN;if(s>d)return i.right?n(i.right,s,o):NaN;{let a=0,u=0;return i.left&&(a=n(i.left,s,d)),i.right&&(u=n(i.right,d+1,o)),a+u}};return n(t,r,e)}};var re=class{constructor(r,e){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},j=class extends H{constructor(r=[],e){super([],e),r&&super.addMany(r)}createNode(r,e){return new re(r,this._isMapMode?void 0:e)}isNode(r){return r instanceof re}add(r,e){if(r===null)return!1;let t=super.add(r,e);return t&&this._balancePath(r),t}delete(r){let e=super.delete(r);for(let{needBalanced:t}of e)t&&this._balancePath(t);return e}perfectlyBalance(r=this.iterationType){let e=this.dfs(s=>s,"IN",!1,this._root,r),t=e.length;if(t===0)return!1;this._clearNodes();let n=(s,o,d)=>{if(s>o)return;let a=s+(o-s>>1),u=e[a];u.left=n(s,a-1,u),u.right=n(a+1,o,u),u.parent=d;let f=u.left?u.left.height:-1,c=u.right?u.right.height:-1;return u.height=Math.max(f,c)+1,u},i=n(0,t-1,void 0);return this._setRoot(i),this._size=t,!0}map(r,e,t){let n=this._createLike([],e),i=0;for(let[s,o]of this)n.add(r.call(t,o,s,i++,this));return n}_createInstance(r){let e=this.constructor;return new e([],{...this._snapshotOptions(),...r!=null?r:{}})}_createLike(r=[],e){let t=this.constructor;return new t(r,{...this._snapshotOptions(),...e!=null?e:{}})}_swapProperties(r,e){let t=this.ensureNode(r),n=this.ensureNode(e);if(t&&n){let{key:i,value:s,height:o}=n,d=this.createNode(i,s);return d&&(d.height=o,n.key=t.key,this._isMapMode||(n.value=t.value),n.height=t.height,t.key=d.key,this._isMapMode||(t.value=d.value),t.height=d.height),n}}_balanceFactor(r){let e=r.left?r.left.height:-1;return(r.right?r.right.height:-1)-e}_updateHeight(r){let e=r.left?r.left.height:-1,t=r.right?r.right.height:-1;r.height=1+Math.max(e,t)}_balanceLL(r){let e=r.parent,t=r.left;t!==null&&(r.parent=t),t&&t.right&&(t.right.parent=r),t&&(t.parent=e),r===this.root?t&&this._setRoot(t):(e==null?void 0:e.left)===r?e.left=t:e&&(e.right=t),t&&(r.left=t.right,t.right=r),this._updateHeight(r),t&&this._updateHeight(t)}_balanceLR(r){let e=r.parent,t=r.left,n;t&&(n=t.right),r&&n!==null&&(r.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&t!==null&&(n.left.parent=t),n.right&&(n.right.parent=r),n.parent=e),r===this.root?n&&this._setRoot(n):e&&(e.left===r?e.left=n:e.right=n),n&&(r.left=n.right,t&&(t.right=n.left),n.left=t,n.right=r),this._updateHeight(r),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balanceRR(r){let e=r.parent,t=r.right;t!==null&&(r.parent=t),t&&(t.left&&(t.left.parent=r),t.parent=e),r===this.root?t&&this._setRoot(t):e&&(e.left===r?e.left=t:e.right=t),t&&(r.right=t.left,t.left=r),this._updateHeight(r),t&&this._updateHeight(t)}_balanceRL(r){let e=r.parent,t=r.right,n;t&&(n=t.left),n!==null&&(r.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&(n.left.parent=r),n.right&&t!==null&&(n.right.parent=t),n.parent=e),r===this.root?n&&this._setRoot(n):e&&(e.left===r?e.left=n:e.right=n),n&&(r.right=n.left),t&&n&&(t.left=n.right),n&&(n.left=r),n&&(n.right=t),this._updateHeight(r),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balancePath(r){r=this.ensureNode(r);let e=this.getPathToRoot(r,t=>t,!1);for(let t=0;t<e.length;t++){let n=e[t];if(n)switch(this._updateHeight(n),this._balanceFactor(n)){case-2:n&&n.left&&(this._balanceFactor(n.left)<=0?this._balanceLL(n):this._balanceLR(n));break;case 2:n&&n.right&&(this._balanceFactor(n.right)>=0?this._balanceRR(n):this._balanceRL(n))}}}_replaceNode(r,e){return e.height=r.height,super._replaceNode(r,e)}};var se=class{constructor(r,e,t="BLACK"){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e,this.color=t}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},z=class extends H{constructor(e=[],t){super([],t);l(this,"_root");this._root=this.NIL,e&&this.addMany(e)}get root(){return this._root}createNode(e,t,n="BLACK"){return new se(e,this._isMapMode?void 0:t,n)}isNode(e){return e instanceof se}clear(){super.clear(),this._root=this.NIL}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(!this.isRealNode(n))return!1;let s=this._insert(n);if(s==="CREATED"){if(this.isRealNode(this._root))this._root.color="BLACK";else return!1;return this._isMapMode&&this._setValue(n.key,i),this._size++,!0}return s==="UPDATED"?(this._isMapMode&&this._setValue(n.key,i),!0):!1}delete(e){if(e===null)return[];let t=[],n;if(this._isPredicate(e)?n=this.getNode(e):n=this.isRealNode(e)?e:this.getNode(e),!n)return t;let i=n.color,s;if(!this.isRealNode(n.left))n.right!==null&&(s=n.right,this._transplant(n,n.right));else if(!this.isRealNode(n.right))s=n.left,this._transplant(n,n.left);else{let o=this.getLeftMost(d=>d,n.right);o&&(i=o.color,o.right!==null&&(s=o.right),o.parent===n?this.isRealNode(s)&&(s.parent=o):(o.right!==null&&(this._transplant(o,o.right),o.right=n.right),this.isRealNode(o.right)&&(o.right.parent=o)),this._transplant(n,o),o.left=n.left,this.isRealNode(o.left)&&(o.left.parent=o),o.color=n.color)}return this._isMapMode&&this._store.delete(n.key),this._size--,i==="BLACK"&&this._deleteFixup(s),t.push({deleted:n,needBalanced:void 0}),t}map(e,t,n){let i=this._createLike([],t),s=0;for(let[o,d]of this)i.add(e.call(n,d,o,s++,this));return i}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_setRoot(e){e&&(e.parent=void 0),this._root=e}_replaceNode(e,t){return t.color=e.color,super._replaceNode(e,t)}_insert(e){var i,s,o;let t=(i=this.root)!=null?i:this.NIL,n;for(;t!==this.NIL;){n=t;let d=this._compare(e.key,t.key);if(d<0)t=(s=t.left)!=null?s:this.NIL;else if(d>0)t=(o=t.right)!=null?o:this.NIL;else return this._replaceNode(t,e),"UPDATED"}return e.parent=n,n?this._compare(e.key,n.key)<0?n.left=e:n.right=e:this._setRoot(e),e.left=this.NIL,e.right=this.NIL,e.color="RED",this._insertFixup(e),"CREATED"}_transplant(e,t){e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t&&(t.parent=e.parent)}_insertFixup(e){var t,n,i,s,o;for(;((t=e==null?void 0:e.parent)==null?void 0:t.color)==="RED";)if(e.parent===((n=e.parent.parent)==null?void 0:n.left)){let d=e.parent.parent.right;(d==null?void 0:d.color)==="RED"?(e.parent.color="BLACK",d.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.right&&(e=e.parent,this._leftRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._rightRotate(e.parent.parent)))}else{let d=(o=(s=(i=e==null?void 0:e.parent)==null?void 0:i.parent)==null?void 0:s.left)!=null?o:void 0;(d==null?void 0:d.color)==="RED"?(e.parent.color="BLACK",d.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.left&&(e=e.parent,this._rightRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._leftRotate(e.parent.parent)))}this.isRealNode(this._root)&&(this._root.color="BLACK")}_deleteFixup(e){var t,n,i,s;if(!e||e===this.root||e.color==="BLACK"){e&&(e.color="BLACK");return}for(;e&&e!==this.root&&e.color==="BLACK";){let o=e.parent;if(!o)break;if(e===o.left){let d=o.right;(d==null?void 0:d.color)==="RED"&&(d.color="BLACK",o.color="RED",this._leftRotate(o),d=o.right),((n=(t=d==null?void 0:d.left)==null?void 0:t.color)!=null?n:"BLACK")==="BLACK"?(d&&(d.color="RED"),e=o):(d!=null&&d.left&&(d.left.color="BLACK"),d&&(d.color=o.color),o.color="BLACK",this._rightRotate(o),e=this.root)}else{let d=o.left;(d==null?void 0:d.color)==="RED"&&(d.color="BLACK",o&&(o.color="RED"),this._rightRotate(o),o&&(d=o.left)),((s=(i=d==null?void 0:d.right)==null?void 0:i.color)!=null?s:"BLACK")==="BLACK"?(d&&(d.color="RED"),e=o):(d!=null&&d.right&&(d.right.color="BLACK"),d&&(d.color=o.color),o&&(o.color="BLACK"),this._leftRotate(o),e=this.root)}}e&&(e.color="BLACK")}_leftRotate(e){if(!e||!e.right)return;let t=e.right;e.right=t.left,t.left&&t.left!==this.NIL&&(t.left.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.left=e,e.parent=t}_rightRotate(e){if(!e||!e.left)return;let t=e.left;e.left=t.right,t.right&&t.right!==this.NIL&&(t.right.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.right=e,e.parent=t}};var oe=class{constructor(r,e=[]){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},De=class extends j{constructor(r=[],e){super([],{...e,isMapMode:!0}),r&&this.addMany(r)}createNode(r,e=[]){return new oe(r,this._isMapMode?[]:e)}isNode(r){return r instanceof oe}add(r,e){if(this.isRealNode(r))return super.add(r);let t=(n,i)=>{if(n==null)return!1;let s=()=>{let d=this.get(n);if(d!==void 0&&i!==void 0){for(let a of i)d.push(a);return!0}return!1},o=()=>{let d=this.getNode(n);if(this.isRealNode(d)){let a=this.get(d);if(a===void 0)return super.add(n,i),!0;if(i!==void 0){for(let u of i)a.push(u);return!0}else return!1}else return super.add(n,i)};return this._isMapMode?o()||s():s()||o()};if(this.isEntry(r)){let[n,i]=r;return t(n,e!==void 0?[e]:i)}return t(r,e!==void 0?[e]:void 0)}deleteValue(r,e){let t=this.get(r);if(Array.isArray(t)){let n=t.indexOf(e);return n===-1?!1:(t.splice(n,1),t.length===0&&this.delete(r),!0)}return!1}perfectlyBalance(r=this.iterationType){let e=this.dfs(s=>s,"IN",!1,this._root,r),t=e.length;if(t===0)return!1;this._clearNodes();let n=(s,o,d)=>{if(s>o)return;let a=s+(o-s>>1),u=e[a];u.left=n(s,a-1,u),u.right=n(a+1,o,u),u.parent=d;let f=u.left?u.left.height:-1,c=u.right?u.right.height:-1;return u.height=Math.max(f,c)+1,u},i=n(0,t-1,void 0);return this._setRoot(i),this._size=t,!0}map(r,e,t){let n=this._createLike([],e),i=0;for(let[s,o]of this)n.add(r.call(t,o,s,i++,this));return n}_createInstance(r){var t,n;let e=this.constructor;return new e([],{...(n=(t=this._snapshotOptions)==null?void 0:t.call(this))!=null?n:{},...r!=null?r:{}})}_createLike(r=[],e){var n,i;let t=this.constructor;return new t(r,{...(i=(n=this._snapshotOptions)==null?void 0:n.call(this))!=null?i:{},...e!=null?e:{}})}};var de=class{constructor(r,e=[],t="BLACK"){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e,this.color=t}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},Pe=class extends z{constructor(r=[],e){super([],{...e}),r&&this.addMany(r)}createNode(r,e=[]){return new de(r,this._isMapMode?[]:e)}isNode(r){return r instanceof de}add(r,e){if(this.isRealNode(r))return super.add(r);let t=(n,i)=>{if(n==null)return!1;let s=()=>{let d=this.get(n);if(d!==void 0&&i!==void 0){for(let a of i)d.push(a);return!0}return!1},o=()=>{let d=this.getNode(n);if(this.isRealNode(d)){let a=this.get(d);if(a===void 0)return super.add(n,i),!0;if(i!==void 0){for(let u of i)a.push(u);return!0}else return!1}else return super.add(n,i)};return this._isMapMode?o()||s():s()||o()};if(this.isEntry(r)){let[n,i]=r;return t(n,e!==void 0?[e]:i)}return t(r,e!==void 0?[e]:void 0)}deleteValue(r,e){let t=this.get(r);if(Array.isArray(t)){let n=t.indexOf(e);return n===-1?!1:(t.splice(n,1),t.length===0&&this.delete(r),!0)}return!1}map(r,e,t){let n=this._createLike([],e),i=0;for(let[s,o]of this)n.add(r.call(t,o,s,i++,this));return n}_createInstance(r){var t,n;let e=this.constructor;return new e([],{...(n=(t=this._snapshotOptions)==null?void 0:t.call(this))!=null?n:{},...r!=null?r:{}})}_createLike(r=[],e){var n,i;let t=this.constructor;return new t(r,{...(i=(n=this._snapshotOptions)==null?void 0:n.call(this))!=null?i:{},...e!=null?e:{}})}};var ae=class{constructor(r,e,t=1,n="BLACK"){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e,this.color=n,this.count=t}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},He=class extends z{constructor(e=[],t){super([],t);l(this,"_count",0);e&&this.addMany(e)}get count(){return this._count}getComputedCount(){let e=0;return this.dfs(t=>e+=t?t.count:0),e}createNode(e,t,n="BLACK",i){return new ae(e,this._isMapMode?void 0:t,i,n)}isNode(e){return e instanceof ae}add(e,t,n=1){let[i,s]=this._keyValueNodeOrEntryToNodeAndValue(e,t,n),o=(i==null?void 0:i.count)||0;return super.add(i,s)?(this._count+=o,!0):!1}delete(e,t=!1){if(e===null)return[];let n=[],i;if(this._isPredicate(e)?i=this.getNode(e):i=this.isRealNode(e)?e:this.getNode(e),!i)return n;let s=i.color,o;if(this.isRealNode(i.left))if(this.isRealNode(i.right)){let d=this.getLeftMost(a=>a,i.right);if(d){if(s=d.color,d.right!==null&&(o=d.right),d.parent===i)this.isRealNode(o)&&(o.parent=d);else{if(t||i.count<=1)d.right!==null&&(this._transplant(d,d.right),this._count-=i.count);else return i.count--,this._count--,n.push({deleted:i,needBalanced:void 0}),n;d.right=i.right,this.isRealNode(d.right)&&(d.right.parent=d)}if(t||i.count<=1)this._transplant(i,d),this._count-=i.count;else return i.count--,this._count--,n.push({deleted:i,needBalanced:void 0}),n;d.left=i.left,this.isRealNode(d.left)&&(d.left.parent=d),d.color=i.color}}else if(o=i.left,t||i.count<=1)this._transplant(i,i.left),this._count-=i.count;else return i.count--,this._count--,n.push({deleted:i,needBalanced:void 0}),n;else if(i.right!==null&&(o=i.right),t||i.count<=1)i.right!==null&&(this._transplant(i,i.right),this._count-=i.count);else return i.count--,this._count--,n.push({deleted:i,needBalanced:void 0}),n;return this._size--,s==="BLACK"&&this._deleteFixup(o),n.push({deleted:i,needBalanced:void 0}),n}clear(){super.clear(),this._count=0}perfectlyBalance(e=this.iterationType){let t=this.dfs(d=>d,"IN",!1,this._root,e),n=t.length;if(n<1)return!1;let i=0;for(let d of t)i+=d?d.count:0;this._clearNodes();let s=(d,a,u)=>{if(d>a)return;let f=d+(a-d>>1),c=t[f],_=s(d,f-1,c),p=s(f+1,a,c);return c.left=_,c.right=p,c.parent=u,c},o=s(0,n-1,void 0);return this._setRoot(o),this._size=n,this._count=i,!0}map(e,t,n){let i=this._createLike([],t),s=0;for(let[o,d]of this)i.add(e.call(n,d,o,s++,this));return i}clone(){let e=this._createInstance();return this._clone(e),e._count=this._count,e}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t,n=1){if(e==null)return[void 0,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[i,s]=e;if(i==null)return[void 0,void 0];let o=t!=null?t:s;return[this.createNode(i,o,"BLACK",n),o]}return[this.createNode(e,t,"BLACK",n),t]}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i,count:s,color:o}=t,d=this.createNode(n,i,o,s);return d&&(d.color=o,t.key=e.key,this._isMapMode||(t.value=e.value),t.count=e.count,t.color=e.color,e.key=d.key,this._isMapMode||(e.value=d.value),e.count=d.count,e.color=d.color),t}}_replaceNode(e,t){return t.count=e.count+t.count,super._replaceNode(e,t)}};var le=class{constructor(r,e,t=1){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e,this.count=t}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},je=class extends j{constructor(e=[],t){super([],t);l(this,"_count",0);e&&this.addMany(e)}get count(){return this._count}getComputedCount(){let e=0;return this.dfs(t=>e+=t.count),e}createNode(e,t,n){return new le(e,this._isMapMode?void 0:t,n)}isNode(e){return e instanceof le}add(e,t,n=1){let[i,s]=this._keyValueNodeOrEntryToNodeAndValue(e,t,n);if(i===void 0)return!1;let o=(i==null?void 0:i.count)||0;return super.add(i,s)&&(this._count+=o),!0}delete(e,t=!1){var a;let n=[];if(!this.root)return n;let i=(a=this.getNode(e))!=null?a:void 0;if(!i)return n;let s=i!=null&&i.parent?i.parent:void 0,o,d=i;if(i.count>1&&!t)i.count--,this._count--;else{if(i.left){let u=i.left?this.getRightMost(f=>f,i.left):void 0;if(u){let f=u.parent;d=this._swapProperties(i,u),f&&(f.right===u?f.right=u.left:f.left=u.left,o=f)}}else if(!s)i.right!==void 0&&i.right!==null&&this._setRoot(i.right);else{let{familyPosition:u}=i;u==="LEFT"||u==="ROOT_LEFT"?s.left=i.right:(u==="RIGHT"||u==="ROOT_RIGHT")&&(s.right=i.right),o=s}this._size=this._size-1,d&&(this._count-=d.count)}return n.push({deleted:d,needBalanced:o}),o&&this._balancePath(o),n}clear(){super.clear(),this._count=0}perfectlyBalance(e=this.iterationType){let t=this.dfs(d=>d,"IN",!1,this._root,e),n=t.length;if(n===0)return!1;let i=0;for(let d of t)i+=d?d.count:0;this._clearNodes();let s=(d,a,u)=>{if(d>a)return;let f=d+(a-d>>1),c=t[f];c.left=s(d,f-1,c),c.right=s(f+1,a,c),c.parent=u;let _=c.left?c.left.height:-1,p=c.right?c.right.height:-1;return c.height=Math.max(_,p)+1,c},o=s(0,n-1,void 0);return this._setRoot(o),this._size=n,this._count=i,!0}clone(){let e=this._createInstance();return this._isMapMode?this.bfs(t=>e.add(t.key,void 0,t.count)):this.bfs(t=>e.add(t.key,t.value,t.count)),this._isMapMode&&(e._store=this._store),e}map(e,t,n){let i=this._createLike([],t),s=0;for(let[o,d]of this)i.add(e.call(n,d,o,s++,this));return i}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t,n=1){if(e==null)return[void 0,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[i,s]=e;if(i==null)return[void 0,void 0];let o=t!=null?t:s;return[this.createNode(i,o,n),o]}return[this.createNode(e,t,n),t]}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i,count:s,height:o}=t,d=this.createNode(n,i,s);return d&&(d.height=o,t.key=e.key,this._isMapMode||(t.value=e.value),t.count=e.count,t.height=e.height,e.key=d.key,this._isMapMode||(e.value=d.value),e.count=d.count,e.height=d.height),t}}_replaceNode(e,t){return t.count=e.count+t.count,super._replaceNode(e,t)}};var U=class extends x{constructor(r=[],e){super(r,e)}};var ze=class extends U{constructor(r=[],e){super(r,e)}};var Ue=class extends U{constructor(r=[],e){super(r,{comparator:(t,n)=>{if(typeof t=="object"||typeof n=="object")throw TypeError("When comparing object types, a custom comparator must be defined in the constructor's options parameter.");return t<n?1:t>n?-1:0},...e})}};var qe=class h{constructor(r,e){l(this,"_rows",0);l(this,"_cols",0);l(this,"_data");var t,n,i;if(e){let{rows:s,cols:o,addFn:d,subtractFn:a,multiplyFn:u}=e;typeof s=="number"&&s>0?this._rows=s:this._rows=r.length,typeof o=="number"&&o>0?this._cols=o:this._cols=((t=r[0])==null?void 0:t.length)||0,d&&(this._addFn=d),a&&(this._subtractFn=a),u&&(this._multiplyFn=u)}else this._rows=r.length,this._cols=(i=(n=r[0])==null?void 0:n.length)!=null?i:0;if(r.length>0)this._data=r;else{this._data=[];for(let s=0;s<this.rows;s++)this._data[s]=new Array(this.cols).fill(0)}}get rows(){return this._rows}get cols(){return this._cols}get data(){return this._data}get addFn(){return this._addFn}get subtractFn(){return this._subtractFn}get multiplyFn(){return this._multiplyFn}get(r,e){if(this.isValidIndex(r,e))return this.data[r][e]}set(r,e,t){return this.isValidIndex(r,e)?(this.data[r][e]=t,!0):!1}isMatchForCalculate(r){return this.rows===r.rows&&this.cols===r.cols}add(r){if(!this.isMatchForCalculate(r))throw new Error("Matrix dimensions must match for addition.");let e=[];for(let t=0;t<this.rows;t++){e[t]=[];for(let n=0;n<this.cols;n++){let i=this.get(t,n),s=r.get(t,n);if(i!==void 0&&s!==void 0){let o=this._addFn(i,s);o&&(e[t][n]=o)}}}return new h(e,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}subtract(r){if(!this.isMatchForCalculate(r))throw new Error("Matrix dimensions must match for subtraction.");let e=[];for(let t=0;t<this.rows;t++){e[t]=[];for(let n=0;n<this.cols;n++){let i=this.get(t,n),s=r.get(t,n);if(i!==void 0&&s!==void 0){let o=this._subtractFn(i,s);o&&(e[t][n]=o)}}}return new h(e,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}multiply(r){if(this.cols!==r.rows)throw new Error("Matrix dimensions must be compatible for multiplication (A.cols = B.rows).");let e=[];for(let t=0;t<this.rows;t++){e[t]=[];for(let n=0;n<r.cols;n++){let i;for(let s=0;s<this.cols;s++){let o=this.get(t,s),d=r.get(s,n);if(o!==void 0&&d!==void 0){let a=this.multiplyFn(o,d);a!==void 0&&(i=this.addFn(i,a))}}i!==void 0&&(e[t][n]=i)}}return new h(e,{rows:this.rows,cols:r.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}transpose(){if(this.data.some(e=>e.length!==this.rows))throw new Error("Matrix must be rectangular for transposition.");let r=[];for(let e=0;e<this.cols;e++){r[e]=[];for(let t=0;t<this.rows;t++){let n=this.get(t,e);n!==void 0&&(r[e][t]=n)}}return new h(r,{rows:this.cols,cols:this.rows,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}inverse(){var n;if(this.rows!==this.cols)throw new Error("Matrix must be square for inversion.");let r=[];for(let i=0;i<this.rows;i++){r[i]=this.data[i].slice();for(let s=0;s<this.cols;s++)r[i][this.cols+s]=i===s?1:0}let e=new h(r,{rows:this.rows,cols:this.cols*2,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn});for(let i=0;i<this.rows;i++){let s=i;for(;s<this.rows&&e.get(s,i)===0;)s++;if(s===this.rows)throw new Error("Matrix is singular, and its inverse does not exist.");e._swapRows(i,s);let o=(n=e.get(i,i))!=null?n:1;if(o===0)throw new Error("Matrix is singular, and its inverse does not exist (division by zero).");e._scaleRow(i,1/o);for(let d=0;d<this.rows;d++)if(d!==i){let a=e.get(d,i);a===void 0&&(a=0),e._addScaledRow(d,i,-a)}}let t=[];for(let i=0;i<this.rows;i++)t[i]=e.data[i].slice(this.cols);return new h(t,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}dot(r){if(this.cols!==r.rows)throw new Error("Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.");let e=[];for(let t=0;t<this.rows;t++){e[t]=[];for(let n=0;n<r.cols;n++){let i;for(let s=0;s<this.cols;s++){let o=this.get(t,s),d=r.get(s,n);if(o!==void 0&&d!==void 0){let a=this.multiplyFn(o,d);a!==void 0&&(i=this.addFn(i,a))}}i!==void 0&&(e[t][n]=i)}}return new h(e,{rows:this.rows,cols:r.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}isValidIndex(r,e){return r>=0&&r<this.rows&&e>=0&&e<this.cols}clone(){return new h(this.data,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}_addFn(r,e){return r===void 0?e:r+e}_subtractFn(r,e){return r-e}_multiplyFn(r,e){return r*e}_swapRows(r,e){let t=this.data[r];this.data[r]=this.data[e],this.data[e]=t}_scaleRow(r,e){for(let t=0;t<this.cols;t++){let n=this.multiplyFn(this.data[r][t],e);n===void 0&&(n=0),this.data[r][t]=n}}_addScaledRow(r,e,t){for(let n=0;n<this.cols;n++){let i=this.multiplyFn(this.data[e][n],t);i===void 0&&(i=0);let s=i,o=this.addFn(this.data[r][n],s);o===void 0&&(o=0),this.data[r][n]=o}}};var me=class h{constructor(r,e){l(this,"direction");l(this,"turn");this.direction=r,this.turn=()=>new h(e[r],e)}},Ge=class{constructor({matrix:r,turning:e,onMove:t,init:{cur:n,charDir:i,VISITED:s}}){l(this,"onMove");l(this,"_matrix");l(this,"_cur");l(this,"_character");l(this,"_VISITED");this._matrix=r,this._cur=n,this._character=new me(i,e),this.onMove=t,this.onMove&&this.onMove(this._cur),this._VISITED=s,this._matrix[this._cur[0]][this._cur[1]]=this._VISITED}start(){for(;this.check(this._character.direction)||this.check(this._character.turn().direction);){let{direction:r}=this._character;this.check(r)?this.move(r):this.check(this._character.turn().direction)&&(this._character=this._character.turn())}}check(r){let e,t,n=this._matrix,[i,s]=this._cur;switch(r){case"up":if(t=n[i-1],!t)return!1;e=t[s];break;case"right":e=n[i][s+1];break;case"down":if(t=n[i+1],!t)return!1;e=t[s];break;case"left":e=n[i][s-1];break}return e!==void 0&&e!==this._VISITED}move(r){switch(r){case"up":this._cur[0]--;break;case"right":this._cur[1]++;break;case"down":this._cur[0]++;break;case"left":this._cur[1]--;break}let[e,t]=this._cur;this._matrix[e][t]=this._VISITED,this.onMove&&this.onMove(this._cur)}};var J=class{constructor(r){l(this,"_key");l(this,"_children");l(this,"_isEnd");this._key=r,this._isEnd=!1,this._children=new Map}get key(){return this._key}set key(r){this._key=r}get children(){return this._children}set children(r){this._children=r}get isEnd(){return this._isEnd}set isEnd(r){this._isEnd=r}},We=class extends L{constructor(e=[],t){super(t);l(this,"_size",0);l(this,"_caseSensitive",!0);l(this,"_root",new J(""));if(t){let{caseSensitive:n}=t;n!==void 0&&(this._caseSensitive=n)}e&&this.addMany(e)}get size(){return this._size}get caseSensitive(){return this._caseSensitive}get root(){return this._root}get _total(){return this._size}add(e){e=this._caseProcess(e);let t=this.root,n=!1;for(let i of e){let s=t.children.get(i);s||(s=new J(i),t.children.set(i,s)),t=s}return t.isEnd||(n=!0,t.isEnd=!0,this._size++),n}addMany(e){let t=[];for(let n of e)this.toElementFn?t.push(this.add(this.toElementFn(n))):t.push(this.add(n));return t}has(e){e=this._caseProcess(e);let t=this.root;for(let n of e){let i=t.children.get(n);if(!i)return!1;t=i}return t.isEnd}isEmpty(){return this._size===0}clear(){this._size=0,this._root=new J("")}delete(e){e=this._caseProcess(e);let t=!1,n=(i,s)=>{let o=e[s],d=i.children.get(o);return d?s===e.length-1?d.isEnd?(d.children.size>0?d.isEnd=!1:i.children.delete(o),t=!0,!0):!1:n(d,s+1)&&!i.isEnd&&d.children.size===0?(i.children.delete(o),!0):!1:!1};return n(this.root,0),t&&this._size--,t}getHeight(){let e=this.root,t=0;if(e){let n=(i,s)=>{s>t&&(t=s);let{children:o}=i;if(o)for(let d of o.entries())n(d[1],s+1)};n(e,0)}return t}hasPurePrefix(e){e=this._caseProcess(e);let t=this.root;for(let n of e){let i=t.children.get(n);if(!i)return!1;t=i}return!t.isEnd}hasPrefix(e){e=this._caseProcess(e);let t=this.root;for(let n of e){let i=t.children.get(n);if(!i)return!1;t=i}return!0}hasCommonPrefix(e){e=this._caseProcess(e);let t="",n=i=>{if(t+=i.key,t!==e&&!i.isEnd)if(i&&i.children&&i.children.size===1)n(Array.from(i.children.values())[0]);else return};return n(this.root),t===e}getLongestCommonPrefix(){let e="",t=n=>{if(e+=n.key,!n.isEnd)if(n&&n.children&&n.children.size===1)t(Array.from(n.children.values())[0]);else return};return t(this.root),e}getWords(e="",t=Number.MAX_SAFE_INTEGER,n=!1){e=this._caseProcess(e);let i=[],s=0;function o(a,u){for(let f of a.children.keys()){let c=a.children.get(f);c!==void 0&&o(c,u.concat(f))}if(a.isEnd){if(s>t-1)return;i.push(u),s++}}let d=this.root;if(e)for(let a of e){let u=d.children.get(a);if(u)d=u;else return[]}return(n||d!==this.root)&&o(d,e),i}clone(){let e=this._createInstance();for(let t of this)e.add(t);return e}filter(e,t){let n=this._createInstance(),i=0;for(let s of this)e.call(t,s,i,this)&&n.add(s),i++;return n}map(e,t,n){let i=this._createLike([],t),s=0;for(let o of this){let d=n===void 0?e(o,s++,this):e.call(n,o,s++,this);if(typeof d!="string")throw new TypeError(`Trie.map callback must return string; got ${typeof d}`);i.add(d)}return i}mapSame(e,t){let n=this._createInstance(),i=0;for(let s of this){let o=t===void 0?e(s,i++,this):e.call(t,s,i++,this);n.add(o)}return n}_createInstance(e){let t=this.constructor;return new t([],{toElementFn:this.toElementFn,caseSensitive:this.caseSensitive,...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}_spawnLike(e){return this._createLike([],e)}*_getIterator(){function*e(t,n){t.isEnd&&(yield n);for(let[i,s]of t.children)yield*e(s,n+i)}yield*e(this.root,"")}_caseProcess(e){return this._caseSensitive||(e=e.toLowerCase()),e}};var Qe=class h{constructor(r,e,t){l(this,"_key");l(this,"_value");l(this,"_children");this._key=r,this._value=e||void 0,t&&(this._children=t)}get key(){return this._key}set key(r){this._key=r}get value(){return this._value}set value(r){this._value=r}get children(){return this._children}set children(r){this._children=r}addChildren(r){this._children||(this._children=[]),r instanceof h?this._children.push(r):this._children=this._children.concat(r)}getHeight(){let r=0;if(this){let e=(t,n)=>{n>r&&(r=n);let{_children:i}=t;if(i)for(let s=0,o=i.length;s<o;s++)e(i[s],n+1)};e(this,0)}return r}};return st(ht);})();
|
|
5
|
+
`;i+=a})(e)),i}print(e,t=this._root){console.log(this.toVisual(t,e))}_dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType,o=!1,d=c=>!!c,a=c=>!!c,u=c=>o?this.isRealNodeOrNull(c):this.isRealNode(c),f=c=>this.isRealNodeOrNull(c)){if(i=this.ensureNode(i),!i)return[];let c=[];if(s==="RECURSIVE"){let _=p=>{if(!u(p))return;let g=()=>{d(p)&&(p==null?void 0:p.left)!==void 0&&_(p==null?void 0:p.left)},T=()=>{a(p)&&(p==null?void 0:p.right)!==void 0&&_(p==null?void 0:p.right)};switch(t){case"IN":if(g(),f(p)&&(c.push(e(p)),n))return;T();break;case"PRE":if(f(p)&&(c.push(e(p)),n))return;g(),T();break;case"POST":if(g(),T(),f(p)&&(c.push(e(p)),n))return;break}};_(i)}else{let _=[{opt:0,node:i}],p=m=>{var b;d(m.node)&&_.push({opt:0,node:(b=m.node)==null?void 0:b.left})},g=m=>{var b;a(m.node)&&_.push({opt:0,node:(b=m.node)==null?void 0:b.right})},T=m=>{u(m.node)&&_.push({opt:1,node:m.node})};for(;_.length>0;){let m=_.pop();if(m!==void 0&&u(m.node))if(m.opt===1){if(f(m.node)&&m.node!==void 0&&(c.push(e(m.node)),n))return c}else switch(t){case"IN":g(m),T(m),p(m);break;case"PRE":g(m),p(m),T(m);break;case"POST":T(m),g(m),p(m);break}}}return c}*_getIterator(e=this._root){if(e)if(this.iterationType==="ITERATIVE"){let t=[],n=e;for(;n||t.length>0;){for(;this.isRealNode(n);)t.push(n),n=n.left;n=t.pop(),this.isRealNode(n)&&(this._isMapMode?yield[n.key,this._store.get(n.key)]:yield[n.key,n.value],n=n.right)}}else e.left&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.left)),this._isMapMode?yield[e.key,this._store.get(e.key)]:yield[e.key,e.value],e.right&&this.isRealNode(e)&&(yield*this[Symbol.iterator](e.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t){if(e===void 0)return[void 0,void 0];if(e===null)return[null,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[n,i]=e;if(n===void 0)return[void 0,void 0];if(n===null)return[null,void 0];let s=t!=null?t:i;return[this.createNode(n,s),s]}return[this.createNode(e,t),t]}_clone(e){this.bfs(t=>{t===null?e.add(null):this._isMapMode?e.add([t.key,this._store.get(t.key)]):e.add([t.key,t.value])},this._root,this.iterationType,!0),this._isMapMode&&(e._store=this._store)}_displayAux(e,t){let{isShowNull:n,isShowUndefined:i,isShowRedBlackNIL:s}=t,o=[["\u2500"],1,0,0];if(e===null&&!n)return o;if(e===void 0&&!i)return o;if(this.isNIL(e)&&!s)return o;if(e!=null){let a=e.key,u=this.isNIL(e)?"S":String(a),f=u.length;return d(u,f,this._displayAux(e.left,t),this._displayAux(e.right,t))}else{let a=e===void 0?"U":"N",u=a.length;return d(a,u,[[""],1,0,0],[[""],1,0,0])}function d(a,u,f,c){let[_,p,g,T]=f,[m,b,V,y]=c,K=" ".repeat(Math.max(0,T+1))+"_".repeat(Math.max(0,p-T-1))+a+"_".repeat(Math.max(0,y))+" ".repeat(Math.max(0,b-y)),E=(g>0?" ".repeat(T)+"/"+" ".repeat(p-T-1):" ".repeat(p))+" ".repeat(u)+(V>0?" ".repeat(y)+"\\"+" ".repeat(b-y-1):" ".repeat(b)),R=[K,E];for(let N=0;N<Math.max(g,V);N++){let F=N<g?_[N]:" ".repeat(p),Ye=N<V?m[N]:" ".repeat(b);R.push(F+" ".repeat(u)+Ye)}return[R,p+u+b,Math.max(g,V)+2,p+Math.floor(u/2)]}}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i}=t,s=this.createNode(n,i);return s&&(t.key=e.key,this._isMapMode||(t.value=e.value),e.key=s.key,this._isMapMode||(e.value=s.value)),t}}_replaceNode(e,t){return e.parent&&(e.parent.left===e?e.parent.left=t:e.parent.right===e&&(e.parent.right=t)),t.left=e.left,t.right=e.right,t.parent=e.parent,this._root===e&&this._setRoot(t),t}_setRoot(e){e&&(e.parent=void 0),this._root=e}_ensurePredicate(e){if(e==null)return t=>!1;if(this._isPredicate(e))return e;if(this.isRealNode(e))return t=>t===e;if(this.isEntry(e)){let[t]=e;return n=>n?n.key===t:!1}return t=>t?t.key===e:!1}_isPredicate(e){return typeof e=="function"}_extractKey(e){if(e===null)return null;if(e!==void 0&&e!==this._NIL)return this.isNode(e)?e.key:this.isEntry(e)?e[0]:e}_setValue(e,t){return e==null||t===void 0?!1:this._store.set(e,t)}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};var ie=class{constructor(r,e){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},H=class extends ne{constructor(e=[],t){super([],t);l(this,"_root");l(this,"_comparator");t?"comparator"in t&&t.comparator!==void 0?this._comparator=t.comparator:this._comparator=this._createDefaultComparator():this._comparator=this._createDefaultComparator(),e&&this.addMany(e)}get root(){return this._root}_createDefaultComparator(){return(e,t)=>{debugger;if(O(e)&&O(t))return e>t?1:e<t?-1:0;if(typeof e=="object"||typeof t=="object")throw TypeError("When comparing object type keys, a custom comparator must be provided in the constructor's options!");return 0}}get comparator(){return this._comparator}createNode(e,t){return new ie(e,this._isMapMode?void 0:t)}ensureNode(e,t=this.iterationType){var n;return(n=super.ensureNode(e,t))!=null?n:void 0}isNode(e){return e instanceof ie}isValidKey(e){return O(e)}dfs(e=this._DEFAULT_NODE_CALLBACK,t="IN",n=!1,i=this._root,s=this.iterationType){return super.dfs(e,t,n,i,s)}bfs(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.bfs(e,t,n,!1)}listLevels(e=this._DEFAULT_NODE_CALLBACK,t=this._root,n=this.iterationType){return super.listLevels(e,t,n,!1)}getNode(e,t=this._root,n=this.iterationType){var i;return(i=this.getNodes(e,!0,t,n)[0])!=null?i:void 0}search(e,t=!1,n=this._DEFAULT_NODE_CALLBACK,i=this._root,s=this.iterationType){if(e===void 0)return[];if(e===null)return[];if(i=this.ensureNode(i),!i)return[];let o,d=this.isRange(e);d?o=f=>f?e.isInRange(f.key,this._comparator):!1:o=this._ensurePredicate(e);let a=f=>{if(!f||!this.isRealNode(f.left))return!1;if(d){let c=e,_=c.low,p=c.includeLow;return p&&this._compare(f.key,_)>=0||!p&&this._compare(f.key,_)>0}if(!d&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(f.key,c)>0}return!0},u=f=>{if(!f||!this.isRealNode(f.right))return!1;if(d){let c=e,_=c.high,p=c.includeHigh;return p&&this._compare(f.key,_)<=0||!p&&this._compare(f.key,_)<0}if(!d&&!this._isPredicate(e)){let c=this._extractKey(e);return c!=null&&this._compare(f.key,c)<0}return!0};return super._dfs(n,"IN",t,i,s,!1,a,u,()=>!0,f=>!!f&&o(f))}rangeSearch(e,t=this._DEFAULT_NODE_CALLBACK,n=this._root,i=this.iterationType){let s=e instanceof S?e:new S(e[0],e[1]);return this.search(s,!1,t,n,i)}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(n===void 0)return!1;if(this._root===void 0)return this._setRoot(n),this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;let s=this._root;for(;s!==void 0;){if(this._compare(s.key,n.key)===0)return this._replaceNode(s,n),this._isMapMode&&this._setValue(s.key,i),!0;if(this._compare(s.key,n.key)>0){if(s.left===void 0)return s.left=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.left!==null&&(s=s.left)}else{if(s.right===void 0)return s.right=n,this._isMapMode&&this._setValue(n==null?void 0:n.key,i),this._size++,!0;s.right!==null&&(s=s.right)}}return!1}addMany(e,t,n=!0,i=this.iterationType){let s=[],o=t==null?void 0:t[Symbol.iterator]();if(!n){for(let _ of e){let p=o==null?void 0:o.next().value;this.isRaw(_)&&(_=this._toEntryFn(_)),s.push(this.add(_,p))}return s}let d=[],a=0;for(let _ of e)d.push({key:_,value:o==null?void 0:o.next().value,orgIndex:a++});let u=d.sort(({key:_},{key:p})=>{let g,T;return this.isRaw(_)?g=this._toEntryFn(_)[0]:this.isEntry(_)?g=_[0]:this.isRealNode(_)?g=_.key:g=_,this.isRaw(p)?T=this._toEntryFn(p)[0]:this.isEntry(p)?T=p[0]:this.isRealNode(p)?T=p.key:T=p,g!=null&&T!=null?this._compare(g,T):0}),f=_=>{if(_.length===0)return;let p=Math.floor((_.length-1)/2),{key:g,value:T,orgIndex:m}=_[p];if(this.isRaw(g)){let b=this._toEntryFn(g);s[m]=this.add(b)}else s[m]=this.add(g,T);f(_.slice(0,p)),f(_.slice(p+1))};return i==="RECURSIVE"?f(u):(()=>{let p=[[0,u.length-1]];for(;p.length>0;){let g=p.pop();if(!g)continue;let[T,m]=g;if(T>m)continue;let b=T+Math.floor((m-T)/2),{key:V,value:y,orgIndex:K}=u[b];if(this.isRaw(V)){let E=this._toEntryFn(V);s[K]=this.add(E)}else s[K]=this.add(V,y);p.push([b+1,m]),p.push([T,b-1])}})(),s}lowerBound(e,t=this.iterationType){return this._bound(e,!0,t)}upperBound(e,t=this.iterationType){return this._bound(e,!1,t)}lesserOrGreaterTraverse(e=this._DEFAULT_NODE_CALLBACK,t=-1,n=this._root,i=this.iterationType){let s=this.ensureNode(n),o=[];if(!this._root||!s)return o;let d=s.key;if(i==="RECURSIVE"){let a=u=>{let f=this._compare(u.key,d);Math.sign(f)==t&&o.push(e(u)),this.isRealNode(u.left)&&a(u.left),this.isRealNode(u.right)&&a(u.right)};return a(this._root),o}else{let a=new M([this._root]);for(;a.length>0;){let u=a.shift();if(this.isRealNode(u)){let f=this._compare(u.key,d);Math.sign(f)==t&&o.push(e(u)),this.isRealNode(u.left)&&a.push(u.left),this.isRealNode(u.right)&&a.push(u.right)}}return o}}perfectlyBalance(e=this.iterationType){let t=this.dfs(o=>o,"IN",!1,this._root,e),n=t.length;if(this._clearNodes(),n===0)return!1;let i=(o,d,a)=>{if(o>d)return;let u=o+(d-o>>1),f=t[u],c=i(o,u-1,f),_=i(u+1,d,f);return f.left=c,f.right=_,f.parent=a,f},s=i(0,n-1,void 0);return this._setRoot(s),this._size=n,!0}isAVLBalanced(e=this.iterationType){if(!this._root)return!0;let t=!0;if(e==="RECURSIVE"){let n=i=>{if(!i)return 0;let s=n(i.left),o=n(i.right);return Math.abs(s-o)>1&&(t=!1),Math.max(s,o)+1};n(this._root)}else{let n=[],i=this._root,s,o=new Map;for(;n.length>0||i;)if(i)n.push(i),i.left!==null&&(i=i.left);else if(i=n[n.length-1],!i.right||s===i.right){if(i=n.pop(),i){let d=i.left?o.get(i.left):-1,a=i.right?o.get(i.right):-1;if(Math.abs(d-a)>1)return!1;o.set(i,1+Math.max(d,a)),s=i,i=void 0}}else i=i.right}return t}map(e,t,n){let i=this._createLike([],t),s=0;for(let[o,d]of this)i.add(e.call(n,d,o,s++,this));return i}deleteWhere(e,t=!1,n=this._root,i=this.iterationType){let s=this.search(e,t,d=>d,n,i),o=[];for(let d of s){let a=this.delete(d);o=o.concat(a)}return o}_bound(e,t,n){if(e==null)return;if(this._isPredicate(e))return this._boundByPredicate(e,n);let i;if(this.isNode(e))i=e.key;else if(this.isEntry(e)){let s=e[0];if(s==null)return;i=s}else i=e;if(i!==void 0)return this._boundByKey(i,t,n)}_boundByKey(e,t,n){var i,s;if(n==="RECURSIVE"){let o=d=>{if(!this.isRealNode(d))return;let a=this.comparator(d.key,e);if(t?a>=0:a>0){let f=o(d.left);return f!=null?f:d}else return o(d.right)};return o(this.root)}else{let o=this.root,d;for(;this.isRealNode(o);){let a=this.comparator(o.key,e);(t?a>=0:a>0)?(d=o,o=(i=o.left)!=null?i:void 0):o=(s=o.right)!=null?s:void 0}return d}}_boundByPredicate(e,t){if(t==="RECURSIVE"){let n,i=s=>{n||!this.isRealNode(s)||(this.isRealNode(s.left)&&i(s.left),!n&&e(s)&&(n=s),!n&&this.isRealNode(s.right)&&i(s.right))};return i(this.root),n}else{let n=[],i=this.root;for(;n.length>0||this.isRealNode(i);)if(this.isRealNode(i))n.push(i),i=i.left;else{let s=n.pop();if(!this.isRealNode(s))break;if(e(s))return s;i=s.right}return}}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_snapshotOptions(){return{...super._snapshotOptions(),comparator:this._comparator}}_keyValueNodeOrEntryToNodeAndValue(e,t){let[n,i]=super._keyValueNodeOrEntryToNodeAndValue(e,t);return n===null?[void 0,void 0]:[n,t!=null?t:i]}_setRoot(e){e&&(e.parent=void 0),this._root=e}_compare(e,t){return this._comparator(e,t)}_deleteByKey(e){var s;let t=this._root;for(;t;){let o=this._compare(t.key,e);if(o===0)break;t=o>0?t.left:t.right}if(!t)return!1;let n=(o,d)=>{let a=o==null?void 0:o.parent;a?a.left===o?a.left=d:a.right=d:this._setRoot(d),d&&(d.parent=a)},i=o=>{if(o){for(;o.left!==void 0&&o.left!==null;)o=o.left;return o}};if(t.left===void 0)n(t,t.right);else if(t.right===void 0)n(t,t.left);else{let o=i(t.right);o.parent!==t&&(n(o,o.right),o.right=t.right,o.right&&(o.right.parent=o)),n(t,o),o.left=t.left,o.left&&(o.left.parent=o)}return this._size=Math.max(0,((s=this._size)!=null?s:0)-1),!0}};var Fe=class{constructor({frequency:r=0,max:e}){l(this,"_freq");l(this,"_max");l(this,"_freqMap");l(this,"_msb");l(this,"_negativeCount");this._freq=r,this._max=e,this._freqMap={0:0},this._msb=Ke(e),this._negativeCount=r<0?e:0}get freqMap(){return this._freqMap}get msb(){return this._msb}get negativeCount(){return this._negativeCount}get freq(){return this._freq}get max(){return this._max}readSingle(r){return this._checkIndex(r),this._readSingle(r)}update(r,e){this._checkIndex(r);let t=this._readSingle(r);this._update(r,e),this._updateNegativeCount(t,t+e)}writeSingle(r,e){this._checkIndex(r),this._writeSingle(r,e)}read(r){if(!Number.isInteger(r))throw new Error("Invalid count");return this._read(Math.max(Math.min(r,this.max),0))}lowerBound(r){if(this.negativeCount>0)throw new Error("Sequence is not non-descending");return this._binarySearch(r,(e,t)=>e<t)}upperBound(r){if(this.negativeCount>0)throw new Error("Must not be descending");return this._binarySearch(r,(e,t)=>e<=t)}getPrefixSum(r){this._checkIndex(r),r++;let e=0;for(;r>0;)e+=this._getFrequency(r),r-=r&-r;return e}_getFrequency(r){return r in this.freqMap?this.freqMap[r]:this.freq*(r&-r)}_updateFrequency(r,e){this.freqMap[r]=this._getFrequency(r)+e}_checkIndex(r){if(!Number.isInteger(r))throw new Error("Invalid index: Index must be an integer.");if(r<0||r>=this.max)throw new Error("Index out of range: Index must be within the range [0, this.max).")}_readSingle(r){r=r+1;let e=this._getFrequency(r),t=r-(r&-r);for(r--;r!==t;)e-=this._getFrequency(r),r-=r&-r;return e}_updateNegativeCount(r,e){r<0&&e>=0?this._negativeCount--:r>=0&&e<0&&this._negativeCount++}_update(r,e){for(r=r+1;r<=this.max;)this._updateFrequency(r,e),r+=r&-r}_writeSingle(r,e){let t=this._readSingle(r);this._update(r,e-t),this._updateNegativeCount(t,e)}_read(r){let e=r,t=0;for(;e;)t+=this._getFrequency(e),e-=e&-e;return t}_binarySearch(r,e){let t=0,n=this.msb<<1,i=r;for(;n>t+1;){let s=t+n>>1,o=this._getFrequency(s);s<=this.max&&e(o,i)?(i-=o,t=s):n=s}return t}};var $=class{constructor(r,e,t,n){l(this,"_start",0);l(this,"_end",0);l(this,"_value");l(this,"_sum",0);l(this,"_left");l(this,"_right");this._start=r,this._end=e,this._sum=t,this._value=n||void 0}get start(){return this._start}set start(r){this._start=r}get end(){return this._end}set end(r){this._end=r}get value(){return this._value}set value(r){this._value=r}get sum(){return this._sum}set sum(r){this._sum=r}get left(){return this._left}set left(r){this._left=r}get right(){return this._right}set right(r){this._right=r}},Ae=class{constructor(r,e,t){l(this,"_values",[]);l(this,"_start",0);l(this,"_end");l(this,"_root");e=e||0,t=t||r.length-1,this._values=r,this._start=e,this._end=t,r.length>0?this._root=this.build(e,t):(this._root=void 0,this._values=[])}get values(){return this._values}get start(){return this._start}get end(){return this._end}get root(){return this._root}build(r,e){if(r>e)return new $(r,e,0);if(r===e)return new $(r,e,this._values[r]);let t=r+Math.floor((e-r)/2),n=this.build(r,t),i=this.build(t+1,e),s=new $(r,e,n.sum+i.sum);return s.left=n,s.right=i,s}updateNode(r,e,t){let n=this.root||void 0;if(!n)return;let i=(s,o,d,a)=>{if(s.start===s.end&&s.start===o){s.sum=d,a!==void 0&&(s.value=a);return}let u=s.start+Math.floor((s.end-s.start)/2);o<=u?s.left&&i(s.left,o,d,a):s.right&&i(s.right,o,d,a),s.left&&s.right&&(s.sum=s.left.sum+s.right.sum)};i(n,r,e,t)}querySumByRange(r,e){let t=this.root||void 0;if(!t)return 0;if(r<0||e>=this.values.length||r>e)return NaN;let n=(i,s,o)=>{if(s<=i.start&&o>=i.end)return i.sum;let d=i.start+Math.floor((i.end-i.start)/2);if(o<=d)return i.left?n(i.left,s,o):NaN;if(s>d)return i.right?n(i.right,s,o):NaN;{let a=0,u=0;return i.left&&(a=n(i.left,s,d)),i.right&&(u=n(i.right,d+1,o)),a+u}};return n(t,r,e)}};var re=class{constructor(r,e){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},j=class extends H{constructor(r=[],e){super([],e),r&&super.addMany(r)}createNode(r,e){return new re(r,this._isMapMode?void 0:e)}isNode(r){return r instanceof re}add(r,e){if(r===null)return!1;let t=super.add(r,e);return t&&this._balancePath(r),t}delete(r){let e=super.delete(r);for(let{needBalanced:t}of e)t&&this._balancePath(t);return e}perfectlyBalance(r=this.iterationType){let e=this.dfs(s=>s,"IN",!1,this._root,r),t=e.length;if(t===0)return!1;this._clearNodes();let n=(s,o,d)=>{if(s>o)return;let a=s+(o-s>>1),u=e[a];u.left=n(s,a-1,u),u.right=n(a+1,o,u),u.parent=d;let f=u.left?u.left.height:-1,c=u.right?u.right.height:-1;return u.height=Math.max(f,c)+1,u},i=n(0,t-1,void 0);return this._setRoot(i),this._size=t,!0}map(r,e,t){let n=this._createLike([],e),i=0;for(let[s,o]of this)n.add(r.call(t,o,s,i++,this));return n}_createInstance(r){let e=this.constructor;return new e([],{...this._snapshotOptions(),...r!=null?r:{}})}_createLike(r=[],e){let t=this.constructor;return new t(r,{...this._snapshotOptions(),...e!=null?e:{}})}_swapProperties(r,e){let t=this.ensureNode(r),n=this.ensureNode(e);if(t&&n){let{key:i,value:s,height:o}=n,d=this.createNode(i,s);return d&&(d.height=o,n.key=t.key,this._isMapMode||(n.value=t.value),n.height=t.height,t.key=d.key,this._isMapMode||(t.value=d.value),t.height=d.height),n}}_balanceFactor(r){let e=r.left?r.left.height:-1;return(r.right?r.right.height:-1)-e}_updateHeight(r){let e=r.left?r.left.height:-1,t=r.right?r.right.height:-1;r.height=1+Math.max(e,t)}_balanceLL(r){let e=r.parent,t=r.left;t!==null&&(r.parent=t),t&&t.right&&(t.right.parent=r),t&&(t.parent=e),r===this.root?t&&this._setRoot(t):(e==null?void 0:e.left)===r?e.left=t:e&&(e.right=t),t&&(r.left=t.right,t.right=r),this._updateHeight(r),t&&this._updateHeight(t)}_balanceLR(r){let e=r.parent,t=r.left,n;t&&(n=t.right),r&&n!==null&&(r.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&t!==null&&(n.left.parent=t),n.right&&(n.right.parent=r),n.parent=e),r===this.root?n&&this._setRoot(n):e&&(e.left===r?e.left=n:e.right=n),n&&(r.left=n.right,t&&(t.right=n.left),n.left=t,n.right=r),this._updateHeight(r),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balanceRR(r){let e=r.parent,t=r.right;t!==null&&(r.parent=t),t&&(t.left&&(t.left.parent=r),t.parent=e),r===this.root?t&&this._setRoot(t):e&&(e.left===r?e.left=t:e.right=t),t&&(r.right=t.left,t.left=r),this._updateHeight(r),t&&this._updateHeight(t)}_balanceRL(r){let e=r.parent,t=r.right,n;t&&(n=t.left),n!==null&&(r.parent=n),t&&n!==null&&(t.parent=n),n&&(n.left&&(n.left.parent=r),n.right&&t!==null&&(n.right.parent=t),n.parent=e),r===this.root?n&&this._setRoot(n):e&&(e.left===r?e.left=n:e.right=n),n&&(r.right=n.left),t&&n&&(t.left=n.right),n&&(n.left=r),n&&(n.right=t),this._updateHeight(r),t&&this._updateHeight(t),n&&this._updateHeight(n)}_balancePath(r){r=this.ensureNode(r);let e=this.getPathToRoot(r,t=>t,!1);for(let t=0;t<e.length;t++){let n=e[t];if(n)switch(this._updateHeight(n),this._balanceFactor(n)){case-2:n&&n.left&&(this._balanceFactor(n.left)<=0?this._balanceLL(n):this._balanceLR(n));break;case 2:n&&n.right&&(this._balanceFactor(n.right)>=0?this._balanceRR(n):this._balanceRL(n))}}}_replaceNode(r,e){return e.height=r.height,super._replaceNode(r,e)}};var se=class{constructor(r,e,t="BLACK"){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e,this.color=t}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},z=class extends H{constructor(e=[],t){super([],t);l(this,"_root");this._root=this.NIL,e&&this.addMany(e)}get root(){return this._root}createNode(e,t,n="BLACK"){return new se(e,this._isMapMode?void 0:t,n)}isNode(e){return e instanceof se}clear(){super.clear(),this._root=this.NIL}add(e,t){let[n,i]=this._keyValueNodeOrEntryToNodeAndValue(e,t);if(!this.isRealNode(n))return!1;let s=this._insert(n);if(s==="CREATED"){if(this.isRealNode(this._root))this._root.color="BLACK";else return!1;return this._isMapMode&&this._setValue(n.key,i),this._size++,!0}return s==="UPDATED"?(this._isMapMode&&this._setValue(n.key,i),!0):!1}delete(e){if(e===null)return[];let t=[],n;if(this._isPredicate(e)?n=this.getNode(e):n=this.isRealNode(e)?e:this.getNode(e),!n)return t;let i=n.color,s;if(!this.isRealNode(n.left))n.right!==null&&(s=n.right,this._transplant(n,n.right));else if(!this.isRealNode(n.right))s=n.left,this._transplant(n,n.left);else{let o=this.getLeftMost(d=>d,n.right);o&&(i=o.color,o.right!==null&&(s=o.right),o.parent===n?this.isRealNode(s)&&(s.parent=o):(o.right!==null&&(this._transplant(o,o.right),o.right=n.right),this.isRealNode(o.right)&&(o.right.parent=o)),this._transplant(n,o),o.left=n.left,this.isRealNode(o.left)&&(o.left.parent=o),o.color=n.color)}return this._isMapMode&&this._store.delete(n.key),this._size--,i==="BLACK"&&this._deleteFixup(s),t.push({deleted:n,needBalanced:void 0}),t}map(e,t,n){let i=this._createLike([],t),s=0;for(let[o,d]of this)i.add(e.call(n,d,o,s++,this));return i}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_setRoot(e){e&&(e.parent=void 0),this._root=e}_replaceNode(e,t){return t.color=e.color,super._replaceNode(e,t)}_insert(e){var i,s,o;let t=(i=this.root)!=null?i:this.NIL,n;for(;t!==this.NIL;){n=t;let d=this._compare(e.key,t.key);if(d<0)t=(s=t.left)!=null?s:this.NIL;else if(d>0)t=(o=t.right)!=null?o:this.NIL;else return this._replaceNode(t,e),"UPDATED"}return e.parent=n,n?this._compare(e.key,n.key)<0?n.left=e:n.right=e:this._setRoot(e),e.left=this.NIL,e.right=this.NIL,e.color="RED",this._insertFixup(e),"CREATED"}_transplant(e,t){e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t&&(t.parent=e.parent)}_insertFixup(e){var t,n,i,s,o;for(;((t=e==null?void 0:e.parent)==null?void 0:t.color)==="RED";)if(e.parent===((n=e.parent.parent)==null?void 0:n.left)){let d=e.parent.parent.right;(d==null?void 0:d.color)==="RED"?(e.parent.color="BLACK",d.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.right&&(e=e.parent,this._leftRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._rightRotate(e.parent.parent)))}else{let d=(o=(s=(i=e==null?void 0:e.parent)==null?void 0:i.parent)==null?void 0:s.left)!=null?o:void 0;(d==null?void 0:d.color)==="RED"?(e.parent.color="BLACK",d.color="BLACK",e.parent.parent.color="RED",e=e.parent.parent):(e===e.parent.left&&(e=e.parent,this._rightRotate(e)),e&&e.parent&&e.parent.parent&&(e.parent.color="BLACK",e.parent.parent.color="RED",this._leftRotate(e.parent.parent)))}this.isRealNode(this._root)&&(this._root.color="BLACK")}_deleteFixup(e){var t,n,i,s;if(!e||e===this.root||e.color==="BLACK"){e&&(e.color="BLACK");return}for(;e&&e!==this.root&&e.color==="BLACK";){let o=e.parent;if(!o)break;if(e===o.left){let d=o.right;(d==null?void 0:d.color)==="RED"&&(d.color="BLACK",o.color="RED",this._leftRotate(o),d=o.right),((n=(t=d==null?void 0:d.left)==null?void 0:t.color)!=null?n:"BLACK")==="BLACK"?(d&&(d.color="RED"),e=o):(d!=null&&d.left&&(d.left.color="BLACK"),d&&(d.color=o.color),o.color="BLACK",this._rightRotate(o),e=this.root)}else{let d=o.left;(d==null?void 0:d.color)==="RED"&&(d.color="BLACK",o&&(o.color="RED"),this._rightRotate(o),o&&(d=o.left)),((s=(i=d==null?void 0:d.right)==null?void 0:i.color)!=null?s:"BLACK")==="BLACK"?(d&&(d.color="RED"),e=o):(d!=null&&d.right&&(d.right.color="BLACK"),d&&(d.color=o.color),o&&(o.color="BLACK"),this._leftRotate(o),e=this.root)}}e&&(e.color="BLACK")}_leftRotate(e){if(!e||!e.right)return;let t=e.right;e.right=t.left,t.left&&t.left!==this.NIL&&(t.left.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.left=e,e.parent=t}_rightRotate(e){if(!e||!e.left)return;let t=e.left;e.left=t.right,t.right&&t.right!==this.NIL&&(t.right.parent=e),t.parent=e.parent,e.parent?e===e.parent.left?e.parent.left=t:e.parent.right=t:this._setRoot(t),t.right=e,e.parent=t}};var oe=class{constructor(r,e=[]){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},De=class extends j{constructor(r=[],e){super([],{...e,isMapMode:!0}),r&&this.addMany(r)}createNode(r,e=[]){return new oe(r,this._isMapMode?[]:e)}isNode(r){return r instanceof oe}add(r,e){if(this.isRealNode(r))return super.add(r);let t=(n,i)=>{if(n==null)return!1;let s=()=>{let d=this.get(n);if(d!==void 0&&i!==void 0){for(let a of i)d.push(a);return!0}return!1},o=()=>{let d=this.getNode(n);if(this.isRealNode(d)){let a=this.get(d);if(a===void 0)return super.add(n,i),!0;if(i!==void 0){for(let u of i)a.push(u);return!0}else return!1}else return super.add(n,i)};return this._isMapMode?o()||s():s()||o()};if(this.isEntry(r)){let[n,i]=r;return t(n,e!==void 0?[e]:i)}return t(r,e!==void 0?[e]:void 0)}deleteValue(r,e){let t=this.get(r);if(Array.isArray(t)){let n=t.indexOf(e);return n===-1?!1:(t.splice(n,1),t.length===0&&this.delete(r),!0)}return!1}perfectlyBalance(r=this.iterationType){let e=this.dfs(s=>s,"IN",!1,this._root,r),t=e.length;if(t===0)return!1;this._clearNodes();let n=(s,o,d)=>{if(s>o)return;let a=s+(o-s>>1),u=e[a];u.left=n(s,a-1,u),u.right=n(a+1,o,u),u.parent=d;let f=u.left?u.left.height:-1,c=u.right?u.right.height:-1;return u.height=Math.max(f,c)+1,u},i=n(0,t-1,void 0);return this._setRoot(i),this._size=t,!0}map(r,e,t){let n=this._createLike([],e),i=0;for(let[s,o]of this)n.add(r.call(t,o,s,i++,this));return n}_createInstance(r){var t,n;let e=this.constructor;return new e([],{...(n=(t=this._snapshotOptions)==null?void 0:t.call(this))!=null?n:{},...r!=null?r:{}})}_createLike(r=[],e){var n,i;let t=this.constructor;return new t(r,{...(i=(n=this._snapshotOptions)==null?void 0:n.call(this))!=null?i:{},...e!=null?e:{}})}};var de=class{constructor(r,e=[],t="BLACK"){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e,this.color=t}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},Pe=class extends z{constructor(r=[],e){super([],{...e}),r&&this.addMany(r)}createNode(r,e=[]){return new de(r,this._isMapMode?[]:e)}isNode(r){return r instanceof de}add(r,e){if(this.isRealNode(r))return super.add(r);let t=(n,i)=>{if(n==null)return!1;let s=()=>{let d=this.get(n);if(d!==void 0&&i!==void 0){for(let a of i)d.push(a);return!0}return!1},o=()=>{let d=this.getNode(n);if(this.isRealNode(d)){let a=this.get(d);if(a===void 0)return super.add(n,i),!0;if(i!==void 0){for(let u of i)a.push(u);return!0}else return!1}else return super.add(n,i)};return this._isMapMode?o()||s():s()||o()};if(this.isEntry(r)){let[n,i]=r;return t(n,e!==void 0?[e]:i)}return t(r,e!==void 0?[e]:void 0)}deleteValue(r,e){let t=this.get(r);if(Array.isArray(t)){let n=t.indexOf(e);return n===-1?!1:(t.splice(n,1),t.length===0&&this.delete(r),!0)}return!1}map(r,e,t){let n=this._createLike([],e),i=0;for(let[s,o]of this)n.add(r.call(t,o,s,i++,this));return n}_createInstance(r){var t,n;let e=this.constructor;return new e([],{...(n=(t=this._snapshotOptions)==null?void 0:t.call(this))!=null?n:{},...r!=null?r:{}})}_createLike(r=[],e){var n,i;let t=this.constructor;return new t(r,{...(i=(n=this._snapshotOptions)==null?void 0:n.call(this))!=null?i:{},...e!=null?e:{}})}};var ae=class{constructor(r,e,t=1,n="BLACK"){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e,this.color=n,this.count=t}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},He=class extends z{constructor(e=[],t){super([],t);l(this,"_count",0);e&&this.addMany(e)}get count(){return this._count}getComputedCount(){let e=0;return this.dfs(t=>e+=t?t.count:0),e}createNode(e,t,n="BLACK",i){return new ae(e,this._isMapMode?void 0:t,i,n)}isNode(e){return e instanceof ae}add(e,t,n=1){let[i,s]=this._keyValueNodeOrEntryToNodeAndValue(e,t,n),o=(i==null?void 0:i.count)||0;return super.add(i,s)?(this._count+=o,!0):!1}delete(e,t=!1){if(e===null)return[];let n=[],i;if(this._isPredicate(e)?i=this.getNode(e):i=this.isRealNode(e)?e:this.getNode(e),!i)return n;let s=i.color,o;if(this.isRealNode(i.left))if(this.isRealNode(i.right)){let d=this.getLeftMost(a=>a,i.right);if(d){if(s=d.color,d.right!==null&&(o=d.right),d.parent===i)this.isRealNode(o)&&(o.parent=d);else{if(t||i.count<=1)d.right!==null&&(this._transplant(d,d.right),this._count-=i.count);else return i.count--,this._count--,n.push({deleted:i,needBalanced:void 0}),n;d.right=i.right,this.isRealNode(d.right)&&(d.right.parent=d)}if(t||i.count<=1)this._transplant(i,d),this._count-=i.count;else return i.count--,this._count--,n.push({deleted:i,needBalanced:void 0}),n;d.left=i.left,this.isRealNode(d.left)&&(d.left.parent=d),d.color=i.color}}else if(o=i.left,t||i.count<=1)this._transplant(i,i.left),this._count-=i.count;else return i.count--,this._count--,n.push({deleted:i,needBalanced:void 0}),n;else if(i.right!==null&&(o=i.right),t||i.count<=1)i.right!==null&&(this._transplant(i,i.right),this._count-=i.count);else return i.count--,this._count--,n.push({deleted:i,needBalanced:void 0}),n;return this._size--,s==="BLACK"&&this._deleteFixup(o),n.push({deleted:i,needBalanced:void 0}),n}clear(){super.clear(),this._count=0}perfectlyBalance(e=this.iterationType){let t=this.dfs(d=>d,"IN",!1,this._root,e),n=t.length;if(n<1)return!1;let i=0;for(let d of t)i+=d?d.count:0;this._clearNodes();let s=(d,a,u)=>{if(d>a)return;let f=d+(a-d>>1),c=t[f],_=s(d,f-1,c),p=s(f+1,a,c);return c.left=_,c.right=p,c.parent=u,c},o=s(0,n-1,void 0);return this._setRoot(o),this._size=n,this._count=i,!0}map(e,t,n){let i=this._createLike([],t),s=0;for(let[o,d]of this)i.add(e.call(n,d,o,s++,this));return i}clone(){let e=this._createInstance();return this._clone(e),e._count=this._count,e}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t,n=1){if(e==null)return[void 0,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[i,s]=e;if(i==null)return[void 0,void 0];let o=t!=null?t:s;return[this.createNode(i,o,"BLACK",n),o]}return[this.createNode(e,t,"BLACK",n),t]}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i,count:s,color:o}=t,d=this.createNode(n,i,o,s);return d&&(d.color=o,t.key=e.key,this._isMapMode||(t.value=e.value),t.count=e.count,t.color=e.color,e.key=d.key,this._isMapMode||(e.value=d.value),e.count=d.count,e.color=d.color),t}}_replaceNode(e,t){return t.count=e.count+t.count,super._replaceNode(e,t)}};var le=class{constructor(r,e,t=1){l(this,"key");l(this,"value");l(this,"parent");l(this,"_left");l(this,"_right");l(this,"_height",0);l(this,"_color","BLACK");l(this,"_count",1);this.key=r,this.value=e,this.count=t}get left(){return this._left}set left(r){r&&(r.parent=this),this._left=r}get right(){return this._right}set right(r){r&&(r.parent=this),this._right=r}get height(){return this._height}set height(r){this._height=r}get color(){return this._color}set color(r){this._color=r}get count(){return this._count}set count(r){this._count=r}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}},je=class extends j{constructor(e=[],t){super([],t);l(this,"_count",0);e&&this.addMany(e)}get count(){return this._count}getComputedCount(){let e=0;return this.dfs(t=>e+=t.count),e}createNode(e,t,n){return new le(e,this._isMapMode?void 0:t,n)}isNode(e){return e instanceof le}add(e,t,n=1){let[i,s]=this._keyValueNodeOrEntryToNodeAndValue(e,t,n);if(i===void 0)return!1;let o=(i==null?void 0:i.count)||0;return super.add(i,s)&&(this._count+=o),!0}delete(e,t=!1){var a;let n=[];if(!this.root)return n;let i=(a=this.getNode(e))!=null?a:void 0;if(!i)return n;let s=i!=null&&i.parent?i.parent:void 0,o,d=i;if(i.count>1&&!t)i.count--,this._count--;else{if(i.left){let u=i.left?this.getRightMost(f=>f,i.left):void 0;if(u){let f=u.parent;d=this._swapProperties(i,u),f&&(f.right===u?f.right=u.left:f.left=u.left,o=f)}}else if(!s)i.right!==void 0&&i.right!==null&&this._setRoot(i.right);else{let{familyPosition:u}=i;u==="LEFT"||u==="ROOT_LEFT"?s.left=i.right:(u==="RIGHT"||u==="ROOT_RIGHT")&&(s.right=i.right),o=s}this._size=this._size-1,d&&(this._count-=d.count)}return n.push({deleted:d,needBalanced:o}),o&&this._balancePath(o),n}clear(){super.clear(),this._count=0}perfectlyBalance(e=this.iterationType){let t=this.dfs(d=>d,"IN",!1,this._root,e),n=t.length;if(n===0)return!1;let i=0;for(let d of t)i+=d?d.count:0;this._clearNodes();let s=(d,a,u)=>{if(d>a)return;let f=d+(a-d>>1),c=t[f];c.left=s(d,f-1,c),c.right=s(f+1,a,c),c.parent=u;let _=c.left?c.left.height:-1,p=c.right?c.right.height:-1;return c.height=Math.max(_,p)+1,c},o=s(0,n-1,void 0);return this._setRoot(o),this._size=n,this._count=i,!0}clone(){let e=this._createInstance();return this._isMapMode?this.bfs(t=>e.add(t.key,void 0,t.count)):this.bfs(t=>e.add(t.key,t.value,t.count)),this._isMapMode&&(e._store=this._store),e}map(e,t,n){let i=this._createLike([],t),s=0;for(let[o,d]of this)i.add(e.call(n,d,o,s++,this));return i}_createInstance(e){let t=this.constructor;return new t([],{...this._snapshotOptions(),...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,{...this._snapshotOptions(),...t!=null?t:{}})}_keyValueNodeOrEntryToNodeAndValue(e,t,n=1){if(e==null)return[void 0,void 0];if(this.isNode(e))return[e,t];if(this.isEntry(e)){let[i,s]=e;if(i==null)return[void 0,void 0];let o=t!=null?t:s;return[this.createNode(i,o,n),o]}return[this.createNode(e,t,n),t]}_swapProperties(e,t){if(e=this.ensureNode(e),t=this.ensureNode(t),e&&t){let{key:n,value:i,count:s,height:o}=t,d=this.createNode(n,i,s);return d&&(d.height=o,t.key=e.key,this._isMapMode||(t.value=e.value),t.count=e.count,t.height=e.height,e.key=d.key,this._isMapMode||(e.value=d.value),e.count=d.count,e.height=d.height),t}}_replaceNode(e,t){return t.count=e.count+t.count,super._replaceNode(e,t)}};var U=class extends x{constructor(r=[],e){super(r,e)}};var ze=class extends U{constructor(r=[],e){super(r,e)}};var Ue=class extends U{constructor(r=[],e){super(r,{comparator:(t,n)=>{if(typeof t=="object"||typeof n=="object")throw TypeError("When comparing object types, a custom comparator must be defined in the constructor's options parameter.");return t<n?1:t>n?-1:0},...e})}};var qe=class h{constructor(r,e){l(this,"_rows",0);l(this,"_cols",0);l(this,"_data");var t,n,i;if(e){let{rows:s,cols:o,addFn:d,subtractFn:a,multiplyFn:u}=e;typeof s=="number"&&s>0?this._rows=s:this._rows=r.length,typeof o=="number"&&o>0?this._cols=o:this._cols=((t=r[0])==null?void 0:t.length)||0,d&&(this._addFn=d),a&&(this._subtractFn=a),u&&(this._multiplyFn=u)}else this._rows=r.length,this._cols=(i=(n=r[0])==null?void 0:n.length)!=null?i:0;if(r.length>0)this._data=r;else{this._data=[];for(let s=0;s<this.rows;s++)this._data[s]=new Array(this.cols).fill(0)}}get rows(){return this._rows}get cols(){return this._cols}get data(){return this._data}get addFn(){return this._addFn}get subtractFn(){return this._subtractFn}get multiplyFn(){return this._multiplyFn}get(r,e){if(this.isValidIndex(r,e))return this.data[r][e]}set(r,e,t){return this.isValidIndex(r,e)?(this.data[r][e]=t,!0):!1}isMatchForCalculate(r){return this.rows===r.rows&&this.cols===r.cols}add(r){if(!this.isMatchForCalculate(r))throw new Error("Matrix dimensions must match for addition.");let e=[];for(let t=0;t<this.rows;t++){e[t]=[];for(let n=0;n<this.cols;n++){let i=this.get(t,n),s=r.get(t,n);if(i!==void 0&&s!==void 0){let o=this._addFn(i,s);o&&(e[t][n]=o)}}}return new h(e,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}subtract(r){if(!this.isMatchForCalculate(r))throw new Error("Matrix dimensions must match for subtraction.");let e=[];for(let t=0;t<this.rows;t++){e[t]=[];for(let n=0;n<this.cols;n++){let i=this.get(t,n),s=r.get(t,n);if(i!==void 0&&s!==void 0){let o=this._subtractFn(i,s);o&&(e[t][n]=o)}}}return new h(e,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}multiply(r){if(this.cols!==r.rows)throw new Error("Matrix dimensions must be compatible for multiplication (A.cols = B.rows).");let e=[];for(let t=0;t<this.rows;t++){e[t]=[];for(let n=0;n<r.cols;n++){let i;for(let s=0;s<this.cols;s++){let o=this.get(t,s),d=r.get(s,n);if(o!==void 0&&d!==void 0){let a=this.multiplyFn(o,d);a!==void 0&&(i=this.addFn(i,a))}}i!==void 0&&(e[t][n]=i)}}return new h(e,{rows:this.rows,cols:r.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}transpose(){if(this.data.some(e=>e.length!==this.rows))throw new Error("Matrix must be rectangular for transposition.");let r=[];for(let e=0;e<this.cols;e++){r[e]=[];for(let t=0;t<this.rows;t++){let n=this.get(t,e);n!==void 0&&(r[e][t]=n)}}return new h(r,{rows:this.cols,cols:this.rows,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}inverse(){var n;if(this.rows!==this.cols)throw new Error("Matrix must be square for inversion.");let r=[];for(let i=0;i<this.rows;i++){r[i]=this.data[i].slice();for(let s=0;s<this.cols;s++)r[i][this.cols+s]=i===s?1:0}let e=new h(r,{rows:this.rows,cols:this.cols*2,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn});for(let i=0;i<this.rows;i++){let s=i;for(;s<this.rows&&e.get(s,i)===0;)s++;if(s===this.rows)throw new Error("Matrix is singular, and its inverse does not exist.");e._swapRows(i,s);let o=(n=e.get(i,i))!=null?n:1;if(o===0)throw new Error("Matrix is singular, and its inverse does not exist (division by zero).");e._scaleRow(i,1/o);for(let d=0;d<this.rows;d++)if(d!==i){let a=e.get(d,i);a===void 0&&(a=0),e._addScaledRow(d,i,-a)}}let t=[];for(let i=0;i<this.rows;i++)t[i]=e.data[i].slice(this.cols);return new h(t,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}dot(r){if(this.cols!==r.rows)throw new Error("Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.");let e=[];for(let t=0;t<this.rows;t++){e[t]=[];for(let n=0;n<r.cols;n++){let i;for(let s=0;s<this.cols;s++){let o=this.get(t,s),d=r.get(s,n);if(o!==void 0&&d!==void 0){let a=this.multiplyFn(o,d);a!==void 0&&(i=this.addFn(i,a))}}i!==void 0&&(e[t][n]=i)}}return new h(e,{rows:this.rows,cols:r.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}isValidIndex(r,e){return r>=0&&r<this.rows&&e>=0&&e<this.cols}clone(){return new h(this.data,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}_addFn(r,e){return r===void 0?e:r+e}_subtractFn(r,e){return r-e}_multiplyFn(r,e){return r*e}_swapRows(r,e){let t=this.data[r];this.data[r]=this.data[e],this.data[e]=t}_scaleRow(r,e){for(let t=0;t<this.cols;t++){let n=this.multiplyFn(this.data[r][t],e);n===void 0&&(n=0),this.data[r][t]=n}}_addScaledRow(r,e,t){for(let n=0;n<this.cols;n++){let i=this.multiplyFn(this.data[e][n],t);i===void 0&&(i=0);let s=i,o=this.addFn(this.data[r][n],s);o===void 0&&(o=0),this.data[r][n]=o}}};var me=class h{constructor(r,e){l(this,"direction");l(this,"turn");this.direction=r,this.turn=()=>new h(e[r],e)}},Ge=class{constructor({matrix:r,turning:e,onMove:t,init:{cur:n,charDir:i,VISITED:s}}){l(this,"onMove");l(this,"_matrix");l(this,"_cur");l(this,"_character");l(this,"_VISITED");this._matrix=r,this._cur=n,this._character=new me(i,e),this.onMove=t,this.onMove&&this.onMove(this._cur),this._VISITED=s,this._matrix[this._cur[0]][this._cur[1]]=this._VISITED}start(){for(;this.check(this._character.direction)||this.check(this._character.turn().direction);){let{direction:r}=this._character;this.check(r)?this.move(r):this.check(this._character.turn().direction)&&(this._character=this._character.turn())}}check(r){let e,t,n=this._matrix,[i,s]=this._cur;switch(r){case"up":if(t=n[i-1],!t)return!1;e=t[s];break;case"right":e=n[i][s+1];break;case"down":if(t=n[i+1],!t)return!1;e=t[s];break;case"left":e=n[i][s-1];break}return e!==void 0&&e!==this._VISITED}move(r){switch(r){case"up":this._cur[0]--;break;case"right":this._cur[1]++;break;case"down":this._cur[0]++;break;case"left":this._cur[1]--;break}let[e,t]=this._cur;this._matrix[e][t]=this._VISITED,this.onMove&&this.onMove(this._cur)}};var J=class{constructor(r){l(this,"_key");l(this,"_children");l(this,"_isEnd");this._key=r,this._isEnd=!1,this._children=new Map}get key(){return this._key}set key(r){this._key=r}get children(){return this._children}set children(r){this._children=r}get isEnd(){return this._isEnd}set isEnd(r){this._isEnd=r}},We=class extends L{constructor(e=[],t){super(t);l(this,"_size",0);l(this,"_caseSensitive",!0);l(this,"_root",new J(""));if(t){let{caseSensitive:n}=t;n!==void 0&&(this._caseSensitive=n)}e&&this.addMany(e)}get size(){return this._size}get caseSensitive(){return this._caseSensitive}get root(){return this._root}get _total(){return this._size}add(e){e=this._caseProcess(e);let t=this.root,n=!1;for(let i of e){let s=t.children.get(i);s||(s=new J(i),t.children.set(i,s)),t=s}return t.isEnd||(n=!0,t.isEnd=!0,this._size++),n}addMany(e){let t=[];for(let n of e)this.toElementFn?t.push(this.add(this.toElementFn(n))):t.push(this.add(n));return t}has(e){e=this._caseProcess(e);let t=this.root;for(let n of e){let i=t.children.get(n);if(!i)return!1;t=i}return t.isEnd}isEmpty(){return this._size===0}clear(){this._size=0,this._root=new J("")}delete(e){e=this._caseProcess(e);let t=!1,n=(i,s)=>{let o=e[s],d=i.children.get(o);return d?s===e.length-1?d.isEnd?(d.children.size>0?d.isEnd=!1:i.children.delete(o),t=!0,!0):!1:n(d,s+1)&&!i.isEnd&&d.children.size===0?(i.children.delete(o),!0):!1:!1};return n(this.root,0),t&&this._size--,t}getHeight(){let e=this.root,t=0;if(e){let n=(i,s)=>{s>t&&(t=s);let{children:o}=i;if(o)for(let d of o.entries())n(d[1],s+1)};n(e,0)}return t}hasPurePrefix(e){e=this._caseProcess(e);let t=this.root;for(let n of e){let i=t.children.get(n);if(!i)return!1;t=i}return!t.isEnd}hasPrefix(e){e=this._caseProcess(e);let t=this.root;for(let n of e){let i=t.children.get(n);if(!i)return!1;t=i}return!0}hasCommonPrefix(e){e=this._caseProcess(e);let t="",n=i=>{if(t+=i.key,t!==e&&!i.isEnd)if(i&&i.children&&i.children.size===1)n(Array.from(i.children.values())[0]);else return};return n(this.root),t===e}getLongestCommonPrefix(){let e="",t=n=>{if(e+=n.key,!n.isEnd)if(n&&n.children&&n.children.size===1)t(Array.from(n.children.values())[0]);else return};return t(this.root),e}getWords(e="",t=Number.MAX_SAFE_INTEGER,n=!1){e=this._caseProcess(e);let i=[],s=0;function o(a,u){for(let f of a.children.keys()){let c=a.children.get(f);c!==void 0&&o(c,u.concat(f))}if(a.isEnd){if(s>t-1)return;i.push(u),s++}}let d=this.root;if(e)for(let a of e){let u=d.children.get(a);if(u)d=u;else return[]}return(n||d!==this.root)&&o(d,e),i}clone(){let e=this._createInstance();for(let t of this)e.add(t);return e}filter(e,t){let n=this._createInstance(),i=0;for(let s of this)e.call(t,s,i,this)&&n.add(s),i++;return n}map(e,t,n){let i=this._createLike([],t),s=0;for(let o of this){let d=n===void 0?e(o,s++,this):e.call(n,o,s++,this);if(typeof d!="string")throw new TypeError(`Trie.map callback must return string; got ${typeof d}`);i.add(d)}return i}mapSame(e,t){let n=this._createInstance(),i=0;for(let s of this){let o=t===void 0?e(s,i++,this):e.call(t,s,i++,this);n.add(o)}return n}_createInstance(e){let t=this.constructor;return new t([],{toElementFn:this.toElementFn,caseSensitive:this.caseSensitive,...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}_spawnLike(e){return this._createLike([],e)}*_getIterator(){function*e(t,n){t.isEnd&&(yield n);for(let[i,s]of t.children)yield*e(s,n+i)}yield*e(this.root,"")}_caseProcess(e){return this._caseSensitive||(e=e.toLowerCase()),e}};var Qe=class h{constructor(r,e,t){l(this,"_key");l(this,"_value");l(this,"_children");this._key=r,this._value=e||void 0,t&&(this._children=t)}get key(){return this._key}set key(r){this._key=r}get value(){return this._value}set value(r){this._value=r}get children(){return this._children}set children(r){this._children=r}addChildren(r){this._children||(this._children=[]),r instanceof h?this._children.push(r):this._children=this._children.concat(r)}getHeight(){let r=0;if(this){let e=(t,n)=>{n>r&&(r=n);let{_children:i}=t;if(i)for(let s=0,o=i.length;s<o;s++)e(i[s],n+1)};e(this,0)}return r}};return st(ht);})();
|
|
6
6
|
/**
|
|
7
7
|
* data-structure-typed
|
|
8
8
|
*
|