-
Notifications
You must be signed in to change notification settings - Fork 10
/
sorted-set.min.js
2 lines (2 loc) · 8.14 KB
/
sorted-set.min.js
1
2
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self)["sorted-set"]=e()}(this,(function(){"use strict";class t{constructor(t,e){this.priv=t,this.index=e,this.data=this.priv.data}hasNext(){return this.index<this.data.length}hasPrevious(){return this.index>0}value(){return this.index<this.data.length?this.data[this.index]:null}setValue(t){if(!this.priv.options.allowSetValue)throw"Must set options.allowSetValue";if(!this.hasNext())throw"Cannot set value at end of set";return this.data[this.index]=t}next(){return this.index>=this.data.length?null:new t(this.priv,this.index+1)}previous(){return this.index<=0?null:new t(this.priv,this.index-1)}}const e=(t,e,r)=>{let n=0,i=t.length;for(;n<i;){const l=n+i>>>1;r(t[l],e)<0?n=l+1:i=l}return n};const r=(t,e)=>{for(;null!==e[t];){const r=e;(e=e[t])._iteratorParentNode=r}return e},n=(t,e)=>{let n,i;if(null!==e[t])n=e,(e=e[t])._iteratorParentNode=n,i="left"===t?"right":"left",e=r(i,e);else{for(;null!==(n=e._iteratorParentNode)&&n[t]===e;)e=n;e=n}return e};class i{constructor(t,e){this.tree=t,this.node=e}next(){if(null===this.node)return null;{const t=n("right",this.node);return new i(this.tree,t)}}previous(){if(null===this.node){if(null===this.tree.root)return null;{this.tree.root._iteratorParentNode=null;const t=r("right",this.tree.root);return new i(this.tree,t)}}{const t=n("left",this.node);return null===t?null:new i(this.tree,t)}}hasNext(){return null!==this.node}hasPrevious(){return null!==this.previous()}value(){return null===this.node?null:this.node.value}setValue(t){if(!this.tree.options.allowSetValue)throw"Must set options.allowSetValue";if(!this.hasNext())throw"Cannot set value at end of set";return this.node.value=t}}i.find=function(t,e,r){const n=t.root;null!=n&&(n._iteratorParentNode=null);let l=n,s=null;for(;null!==l;){const t=r(e,l.value);if(0===t)break;if(t<0){if(null===l.left)break;s=l,l.left._iteratorParentNode=l,l=l.left}else{if(null===l.right){l=s;break}l.right._iteratorParentNode=l,l=l.right}}return new i(t,l)},i.left=t=>{if(null===t.root)return new i(t,null);{t.root._iteratorParentNode=null;const e=r("left",t.root);return new i(t,e)}},i.right=t=>new i(t,null);const l=(t,e)=>{null!==t&&(l(t.left,e),e(t.value),l(t.right,e))};class s{toArray(){const t=[];return l(this.root,(function(e){return t.push(e)})),t}clear(){return this.root=null}forEachImpl(t,e,r){let n=0;l(this.root,(function(i){t.call(r,i,n,e),n+=1}))}contains(t){const e=this.comparator;let r=this.root;for(;null!==r;){const n=e(t,r.value);if(0===n)break;r=n<0?r.left:r.right}return null!==r&&0===e(r.value,t)}findIterator(t){return i.find(this,t,this.comparator)}beginIterator(){return i.left(this)}endIterator(){return i.right(this)}}class o{constructor(t){this.value=t,this.left=null,this.right=null}}const a=(t,e,r)=>{if(null===t)throw"Value not in set";const n=r(e,t.value);if(n<0)t.left=a(t.left,e,r);else if(n>0)t.right=a(t.right,e,r);else if(null===t.left&&null===t.right)t=null;else if(null===t.right)t=t.left;else if(null===t.left)t=t.right;else{const e=((t,e)=>{for(;null!==t[e];)t=t[e];return t})(t.right,"left");t.value=e.value,t.right=a(t.right,e.value,r)}return t};class h{constructor(t){this.value=t,this.left=null,this.right=null,this.isRed=!0}}const u=t=>{const e=t.right;return t.right=e.left,e.left=t,e.isRed=t.isRed,t.isRed=!0,e},f=t=>{const e=t.left;return t.left=e.right,e.right=t,e.isRed=t.isRed,t.isRed=!0,e},c=t=>{t.isRed=!t.isRed,t.left.isRed=!t.left.isRed,t.right.isRed=!t.right.isRed},d=t=>(c(t),null!==t.right&&null!==t.right.left&&t.right.left.isRed&&(t.right=f(t.right),t=u(t),c(t)),t),p=(t,e,r,n)=>{if(null===t)return new h(e);const i=r(e,t.value);return 0===i?t.value=n(t.value,e):i<0?t.left=p(t.left,e,r,n):t.right=p(t.right,e,r,n),null===t.right||!t.right.isRed||null!==t.left&&t.left.isRed||(t=u(t)),null!==t.left&&t.left.isRed&&null!==t.left.left&&t.left.left.isRed&&(t=f(t)),null!==t.left&&t.left.isRed&&null!==t.right&&t.right.isRed&&c(t),t},g=t=>(null!==t.right&&t.right.isRed&&(t=u(t)),null!==t.left&&t.left.isRed&&null!==t.left.left&&t.left.left.isRed&&(t=f(t)),null!==t.left&&t.left.isRed&&null!==t.right&&t.right.isRed&&c(t),t),v=t=>null===t.left?null:(t.left.isRed||null!==t.left.left&&t.left.left.isRed||(t=d(t)),t.left=v(t.left),g(t)),m=(t,e,r)=>{if(null===t)throw"Value not in set";if(r(e,t.value)<0){if(null===t.left)throw"Value not in set";t.left.isRed||null!==t.left.left&&t.left.left.isRed||(t=d(t)),t.left=m(t.left,e,r)}else{if(null!==t.left&&t.left.isRed&&(t=f(t)),null===t.right){if(0===r(e,t.value))return null;throw"Value not in set"}t.right.isRed||null!==t.right.left&&t.right.left.isRed||(t=(t=>(c(t),null!==t.left&&null!==t.left.left&&t.left.left.isRed&&(t=f(t),c(t)),t))(t)),0===r(e,t.value)?(t.value=(t=>{for(;null!==t.left;)t=t.left;return t})(t.right).value,t.right=v(t.right)):t.right=m(t.right,e,r)}return null!==t&&(t=g(t)),t};class R extends s{constructor(t){super(),this.options=t,this.comparator=this.options.comparator,this.onInsertConflict=this.options.onInsertConflict,this.root=null}insert(t){this.root=p(this.root,t,this.comparator,this.onInsertConflict),this.root.isRed=!1}remove(t){this.root=m(this.root,t,this.comparator),null!==this.root&&(this.root.isRed=!1)}}const w={OnInsertConflictThrow:(t,e)=>{throw new Error("Value already in set")},OnInsertConflictReplace:(t,e)=>e,OnInsertConflictIgnore:(t,e)=>t};class I extends class{constructor(t){if(null==(null!=t?t.strategy:void 0))throw"Must pass options.strategy, a strategy";if(null==(null!=t?t.comparator:void 0))throw"Must pass options.comparator, a comparator";if(null==(null!=t?t.onInsertConflict:void 0))throw"Must pass options.onInsertConflict, a function";this.priv=new t.strategy(t),this.length=0}insert(t){return this.priv.insert(t),this.length+=1,this}remove(t){return this.priv.remove(t),this.length-=1,this}clear(){return this.priv.clear(),this.length=0,this}contains(t){return this.priv.contains(t)}toArray(){return this.priv.toArray()}forEach(t,e){return this.priv.forEachImpl(t,this,e),this}map(t,e){const r=[];return this.forEach((function(n,i,l){return r.push(t.call(e,n,i,l))})),r}filter(t,e){const r=[];return this.forEach((function(n,i,l){if(t.call(e,n,i,l))return r.push(n)})),r}every(t,e){let r=!0;return this.forEach((function(n,i,l){r&&!t.call(e,n,i,l)&&(r=!1)})),r}some(t,e){let r=!1;return this.forEach((function(n,i,l){!r&&t.call(e,n,i,l)&&(r=!0)})),r}findIterator(t){return this.priv.findIterator(t)}beginIterator(){return this.priv.beginIterator()}endIterator(){return this.priv.endIterator()}}{constructor(t){t||(t={}),t.strategy||(t.strategy=R),t.comparator||(t.comparator=function(t,e){return(t||0)-(e||0)}),t.onInsertConflict||(t.onInsertConflict=w.OnInsertConflictThrow),super(t)}}return I.ArrayStrategy=class{constructor(t){this.options=t,this.onInsertConflict=this.options.onInsertConflict,this.comparator=this.options.comparator,this.data=[]}toArray(){return this.data}insert(t){const r=e(this.data,t,this.comparator);return void 0!==this.data[r]&&0===this.comparator(this.data[r],t)?this.data.splice(r,1,this.onInsertConflict(this.data[r],t)):this.data.splice(r,0,t)}remove(t){const r=e(this.data,t,this.comparator);if(0!==this.comparator(this.data[r],t))throw"Value not in set";return this.data.splice(r,1)}clear(){return this.data.length=0}contains(t){const r=e(this.data,t,this.comparator);return this.index!==this.data.length&&0===this.comparator(this.data[r],t)}forEachImpl(t,e,r){const n=this.data,i=n.length;for(let l=0;l<i;l++)t.call(r,n[l],l,e)}findIterator(r){const n=e(this.data,r,this.comparator);return new t(this,n)}beginIterator(){return new t(this,0)}endIterator(){return new t(this,this.data.length)}},I.BinaryTreeStrategy=class extends s{constructor(t){super(),this.options=t,this.comparator=this.options.comparator,this.onInsertConflict=this.options.onInsertConflict,this.root=null}insert(t){const e=this.comparator;if(null!==this.root){let r=this.root,n=null;for(;;){const i=e(t,r.value);if(0===i)return void(r.value=this.onInsertConflict(r.value,t));if(n=i<0?"left":"right",null===r[n])break;r=r[n]}return r[n]=new o(t)}return this.root=new o(t)}remove(t){return this.root=a(this.root,t,this.comparator)}},I.RedBlackTreeStrategy=R,Object.assign(I,w),I}));
//# sourceMappingURL=sorted-set.min.js.map