UNPKG

@cosmicmind/algojs

Version:

A TypeScript library featuring algorithms and data structures.

2 lines (1 loc) 9.07 kB
"use strict";var e=Object.defineProperty,t=(t,r)=>e(t,"name",{value:r,configurable:!0});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("@cosmicmind/foundationjs"),s=t((e,t)=>e===t?0:e>t?1:-1,"stringCompare"),o=t((e,t)=>e===t?0:e>t?1:-1,"numericCompare"),n=t((e,t)=>e.key===t.key?0:e.key>t.key?1:-1,"stringKeyCompare"),i=t((e,t)=>e.key===t.key?0:e.key>t.key?1:-1,"numericKeyCompare"),a=void 0,u=t((e,t)=>{for(let r=1,s=e.length;r<s;++r){const s=e[r];let o=r-1;for(;0<=o&&0<t(e[o],s);)e[o+1]=e[o],--o;e[o+1]=s}},"insertionSort"),p=t((e,t,r,s,o)=>{const n=r-t+1,i=s-r;let a,u,p;const c=[],f=[];for(a=0;a<n;++a)c[a]=e[t+a];for(u=0;u<i;++u)f[u]=e[r+u+1];for(a=0,u=0,p=t;a<n&&u<i;)1>o(c[a],f[u])?(e[p]=c[a],++a):(e[p]=f[u],++u),++p;for(;a<n;)e[p]=c[a],++a,++p;for(;u<i;)e[p]=f[u],++u,++p},"merge"),c=t((e,t,r,s)=>{if(t<r){const o=Math.floor(t+(r-t)/2);c(e,t,o,s),c(e,o+1,r,s),p(e,t,o,r,s)}},"sort"),f=t((e,t)=>c(e,0,e.length-1,t),"mergeSort"),l=t((e,t)=>{let r=0;for(let s=0,o=e.length;s<o;++s){r=s;for(let i=s+1;i<o;++i)0<t(e[r],e[i])&&(r=i);const n=e[r];e[r]=e[s],e[s]=n}},"selectionSort"),d=t(e=>(r.assert(0<=e,"index must be 0 or greater"),Math.floor((e-1)/2)),"heapParent"),x=t(e=>(r.assert(0<=e,"index must be 0 or greater"),2*e+1),"heapLeft"),g=t(e=>(r.assert(0<=e,"index must be 0 or greater"),2*e+2),"heapRight"),h=t((e,t,s)=>{r.assert(0<=t,"first index must be 0 or greater"),r.assert(0<=s,"second index must be 0 or greater");const o=e[t];e[t]=e[s],e[s]=o},"heapSwapAt"),m=t((e,t,s=0)=>{r.assert(0<=t,"size must be 0 or greater"),r.assert(0<=s,"index must be 0 or greater");const o=x(s),n=g(s);let i=o<t&&e[o]>e[s]?o:s;n<t&&e[n]>e[i]&&(i=n),s!==i&&(h(e,s,i),m(e,t,i))},"heapMaxHeapify"),I=t(e=>{const t=e.length;for(let r=Math.floor(t/2)-1;0<=r;--r)m(e,t,r)},"buildMaxHeap"),C=t(e=>{I(e);for(let t=e.length-1;0<t;--t)h(e,0,t),m(e,t)},"heapSort"),v=t((e,t)=>e===t?0:e>t?1:-1,"ListCompareFn"),k=t(e=>({...e,next:a,previous:a}),"listNodeCreate"),y=t(()=>({first:a,last:a,count:0}),"listCreate");function F(e,t){r.guard(e.first)?(e.first.previous=t,t.next=e.first,t.previous=a):(e.last=t,t.previous=a,t.next=a),e.first=t,++e.count}function S(e){const t=e.first;if(r.guard(t)){const s=t.next;r.guard(s)?(e.first=s,s.previous=a):(e.first=a,e.last=a),t.previous=a,t.next=a,--e.count}return t}function b(e,t){r.guard(e.last)?(e.last.next=t,t.previous=e.last,t.next=a):(e.first=t,t.previous=a,t.next=a),e.last=t,++e.count}function R(e){const t=e.last;if(r.guard(t)){const s=t.previous;r.guard(s)?(e.last=s,s.next=a):(e.first=a,e.last=a),t.previous=a,t.next=a,--e.count}return t}function z(e,t,s,o=v){if(r.guard(e.first)&&0===o(e.first,s))F(e,t);else{const o=s.previous;r.guard(o)&&(o.next=t,t.previous=o,t.next=s,s.previous=t,++e.count)}}function L(e,t,s=v){const o=t.previous;if(r.guard(e.first)&&r.guard(o)&&0===s(e.first,o))S(e);else if(r.guard(o)){const s=o.previous;r.guard(s)&&(t.previous=s,s.next=t,o.previous=a,o.next=a,--e.count)}return o}function P(e,t,s,o=v){if(r.guard(e.last)&&0===o(e.last,s))b(e,t);else{const o=s.next;r.guard(o)&&(o.previous=t,t.next=o,t.previous=s,s.next=t,++e.count)}}function A(e,t,s=v){const o=t.next;if(r.guard(e.last)&&r.guard(o)&&0===s(e.last,o))R(e);else if(r.guard(o)){const s=o.next;r.guard(s)&&(s.previous=t,t.next=s,o.previous=a,o.next=a,--e.count)}return o}function T(e,t,s=v){if(r.guard(e.first)&&0===s(e.first,t))S(e);else if(r.guard(e.last)&&0===s(e.last,t))R(e);else{const s=t.previous,o=t.next;r.guard(s)&&r.guard(o)&&(s.next=o,o.previous=s,t.previous=a,t.next=a,--e.count)}}function*D(e){let t=e.first;for(;r.guard(t);)yield t,t=t.next}function*H(e){let t=e.last;for(;r.guard(t);)yield t,t=t.previous}function*M(e){let t=e;for(;r.guard(t);)yield t,t=t.next}function*N(e){let t=e;for(;r.guard(t);)yield t,t=t.previous}function B(e){for(;r.guard(e.first);)S(e)}function Q(e,t,s=v){return r.guard(e.first)&&0===s(e.first,t)}function w(e,t,s=v){return r.guard(e.last)&&0===s(e.last,t)}function K(e,t){for(const r of D(e))if(r===t)return!0;return!1}function O(e,...t){const r=new Set;e:for(const s of D(e)){for(const e of t)if(!e(s))continue e;r.add(s)}return r}t(F,"listInsert"),t(S,"listRemoveFirst"),t(b,"listAppend"),t(R,"listRemoveLast"),t(z,"listInsertBefore"),t(L,"listRemoveBefore"),t(P,"listInsertAfter"),t(A,"listRemoveAfter"),t(T,"listRemove"),t(D,"listIterateFromFirst"),t(H,"listIterateFromLast"),t(M,"listIterateToNext"),t(N,"listIterateToPrevious"),t(B,"listClear"),t(Q,"listIsFirst"),t(w,"listIsLast"),t(K,"listHas"),t(O,"listQuery");const j=t((e,t)=>e===t?0:-1,"StackCompareFn"),q=t(e=>({...e??{},parent:a}),"stackNodeCreate"),E=t(()=>({top:a,count:0}),"stackCreate");function G(e){return e.top}function J(e,t){t.parent=e.top,e.top=t,++e.count}function U(e){const t=e.top;return r.guard(t)&&(e.top=t.parent,t.parent=a,--e.count),t}function*V(e){let t=e.top;for(;r.guard(t);)yield t,t=t.parent}function*W(e){let t=e;for(;r.guard(t);)yield t,t=t.parent}function*X(e){let t=e.parent;for(;r.guard(t);)yield t,t=t.parent}function Y(e){for(;r.guard(e.top);)U(e)}function Z(e){let t=e.parent,s=0;for(;r.guard(t);)++s,t=t.parent;return s}function $(e,t,s=j){return r.guard(e.top)&&0===s(e.top,t)}function _(e,t,r=j){for(const s of V(e))if(0===r(s,t))return!0;return!1}function ee(e,...t){const r=new Set;e:for(const s of V(e)){for(const e of t)if(!e(s))continue e;r.add(s)}return r}t(G,"stackPeek"),t(J,"stackPush"),t(U,"stackPop"),t(V,"stackIterator"),t(W,"stackIterateFrom"),t(X,"stackIterateToParent"),t(Y,"stackClear"),t(Z,"stackDepth"),t($,"stackIsTop"),t(_,"stackHas"),t(ee,"stackQuery");const te=t((e,t)=>e===t?0:e>t?1:-1,"TreeCompareFn"),re=t(e=>({...e??{},parent:a,next:a,previous:a,children:y(),size:1}),"treeCreate");function se(e,t){t.parent=e,F(e.children,t),he(e,t.size)}function oe(e,t,r,s=te){t.parent=e,z(e.children,t,r,s),he(e,t.size)}function ne(e,t,r,s=te){t.parent=e,P(e.children,t,r,s),he(e,t.size)}function ie(e,t){t.parent=e,b(e.children,t),he(e,t.size)}function ae(e,t=te){const s=e.parent;r.guard(s)&&(T(s.children,e,t),me(s,e.size),e.parent=a)}t(se,"treeInsertChild"),t(oe,"treeInsertChildBefore"),t(ne,"treeInsertChildAfter"),t(ie,"treeAppendChild"),t(ae,"treeRemove");const ue=t(e=>Z(e),"treeDepth");function pe(e){return a===e.parent}function ce(e){return 0===e.children.count}function fe(e,t,s=te){return r.guard(t.parent)&&0===s(t.parent,e)}function le(e,t,r=te){return Q(e.children,t,r)}function de(e,t,r=te){return w(e.children,t,r)}function xe(e,t,r=te){return Q(e.children,t,r)&&w(e.children,t,r)}function ge(e,t,s=te){let o=t.parent;for(;r.guard(o);){if(0===s(o,e))return!0;o=o.parent}return!1}function he(e,t){r.assert(0<t,"size must be greater than 0");for(const r of W(e))r.size+=t}function me(e,t){r.assert(0<t,"size must be greater than 0");for(const r of W(e))r.size-=t}function*Ie(e){yield e;for(const t of D(e.children))yield*Ie(t)}function*Ce(e){const t=y();for(b(t,{...e});0<t.count;){const e=S(t);yield e;for(const r of D(e.children))b(t,{...r})}}function ve(e,...t){const r=new Set;e:for(const s of Ie(e)){for(const e of t)if(!e(s))continue e;r.add(s)}return r}t(pe,"treeIsRoot"),t(ce,"treeIsLeaf"),t(fe,"treeIsChild"),t(le,"treeIsFirstChild"),t(de,"treeIsLastChild"),t(xe,"treeIsOnlyChild"),t(ge,"treeIsChildDeep"),t(he,"treeIncreaseSize"),t(me,"treeDecreaseSize"),t(Ie,"depthFirstIterator"),t(Ce,"breadthFirstIterator"),t(ve,"treeQuery"),exports.ListCompareFn=v,exports.SentinelNode=a,exports.StackCompareFn=j,exports.TreeCompareFn=te,exports.breadthFirstIterator=Ce,exports.buildMaxHeap=I,exports.depthFirstIterator=Ie,exports.heapLeft=x,exports.heapMaxHeapify=m,exports.heapParent=d,exports.heapRight=g,exports.heapSort=C,exports.heapSwapAt=h,exports.insertionSort=u,exports.listAppend=b,exports.listClear=B,exports.listCreate=y,exports.listHas=K,exports.listInsert=F,exports.listInsertAfter=P,exports.listInsertBefore=z,exports.listIsFirst=Q,exports.listIsLast=w,exports.listIterateFromFirst=D,exports.listIterateFromLast=H,exports.listIterateToNext=M,exports.listIterateToPrevious=N,exports.listNodeCreate=k,exports.listQuery=O,exports.listRemove=T,exports.listRemoveAfter=A,exports.listRemoveBefore=L,exports.listRemoveFirst=S,exports.listRemoveLast=R,exports.mergeSort=f,exports.numericCompare=o,exports.numericKeyCompare=i,exports.selectionSort=l,exports.stackClear=Y,exports.stackCreate=E,exports.stackDepth=Z,exports.stackHas=_,exports.stackIsTop=$,exports.stackIterateFrom=W,exports.stackIterateToParent=X,exports.stackIterator=V,exports.stackNodeCreate=q,exports.stackPeek=G,exports.stackPop=U,exports.stackPush=J,exports.stackQuery=ee,exports.stringCompare=s,exports.stringKeyCompare=n,exports.treeAppendChild=ie,exports.treeCreate=re,exports.treeDecreaseSize=me,exports.treeDepth=ue,exports.treeIncreaseSize=he,exports.treeInsertChild=se,exports.treeInsertChildAfter=ne,exports.treeInsertChildBefore=oe,exports.treeIsChild=fe,exports.treeIsChildDeep=ge,exports.treeIsFirstChild=le,exports.treeIsLastChild=de,exports.treeIsLeaf=ce,exports.treeIsOnlyChild=xe,exports.treeIsRoot=pe,exports.treeQuery=ve,exports.treeRemove=ae;