UNPKG

yjs-orderedtree

Version:

An ordered tree class for yjs. Lets you use a Y.Map like an ordered tree with insert, delete, and move operations. The children are ordered and the position of a child amongst its sibling can be manipulated

3 lines (2 loc) 34.6 kB
import*as t from"yjs";var e=function(t,e,r,n){return new(r||(r=Promise))((function(o,i){function a(t){try{s(n.next(t))}catch(t){i(t)}}function h(t){try{s(n.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}s((n=n.apply(t,e||[])).next())}))},r=function(t,e){var r,n,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=h(0),a.throw=h(1),a.return=h(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function h(h){return function(s){return function(h){if(r)throw new TypeError("Generator is already executing.");for(;a&&(a=0,h[0]&&(i=0)),i;)try{if(r=1,n&&(o=2&h[0]?n.return:h[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,h[1])).done)return o;switch(n=0,o&&(h=[2&h[0],o.value]),h[0]){case 0:case 1:o=h;break;case 4:return i.label++,{value:h[1],done:!1};case 5:i.label++,n=h[1],h=[0];continue;case 7:h=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==h[0]&&2!==h[0])){i=0;continue}if(3===h[0]&&(!o||h[1]>o[0]&&h[1]<o[3])){i.label=h[1];break}if(6===h[0]&&i.label<o[1]){i.label=o[1],o=h;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(h);break}o[2]&&i.ops.pop(),i.trys.pop();continue}h=e.call(t,i)}catch(t){h=[6,t],n=0}finally{r=o=0}if(5&h[0])throw h[1];return{value:h[0]?h[1]:void 0,done:!0}}([h,s])}}},n=function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,o,i=r.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(n=i.next()).done;)a.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return a},o=function(t,e,r){if(r||2===arguments.length)for(var n,o=0,i=e.length;o<i;o++)!n&&o in e||(n||(n=Array.prototype.slice.call(e,0,o)),n[o]=e[o]);return t.concat(n||Array.prototype.slice.call(e))},i=function(t){var e="function"==typeof Symbol&&Symbol.iterator,r=e&&t[e],n=0;if(r)return r.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")};!function(){function t(e){void 0===e&&(e=t.minComparator);var r=this;this.compare=e,this.heapArray=[],this._limit=0,this.offer=this.add,this.element=this.peek,this.poll=this.pop,this._invertedCompare=function(t,e){return r.compare(t,e).then((function(t){return-1*t}))}}t.getChildrenIndexOf=function(t){return[2*t+1,2*t+2]},t.getParentIndexOf=function(t){if(t<=0)return-1;var e=t%2?1:2;return Math.floor((t-e)/2)},t.getSiblingIndexOf=function(t){return t<=0?-1:t+(t%2?1:-1)},t.minComparator=function(t,n){return e(this,void 0,void 0,(function(){return r(this,(function(e){return t>n?[2,1]:t<n?[2,-1]:[2,0]}))}))},t.maxComparator=function(t,n){return e(this,void 0,void 0,(function(){return r(this,(function(e){return n>t?[2,1]:n<t?[2,-1]:[2,0]}))}))},t.minComparatorNumber=function(t,n){return e(this,void 0,void 0,(function(){return r(this,(function(e){return[2,t-n]}))}))},t.maxComparatorNumber=function(t,n){return e(this,void 0,void 0,(function(){return r(this,(function(e){return[2,n-t]}))}))},t.defaultIsEqual=function(t,n){return e(this,void 0,void 0,(function(){return r(this,(function(e){return[2,t===n]}))}))},t.print=function(e){function r(e){var r=t.getParentIndexOf(e);return Math.floor(Math.log2(r+1))}function n(t,e){for(var r="";e>0;--e)r+=t;return r}for(var o=0,i=[],a=r(e.length-1)+2,h=0;o<e.length;){var s=r(o)+1;0===o&&(s=0);var p=String(e.get(o));p.length>h&&(h=p.length),i[s]=i[s]||[],i[s].push(p),o+=1}return i.map((function(t,e){var r=Math.pow(2,a-e)-1;return n(" ",Math.floor(r/2)*h)+t.map((function(t){var e=(h-t.length)/2;return n(" ",Math.ceil(e))+t+n(" ",Math.floor(e))})).join(n(" ",r*h))})).join("\n")},t.heapify=function(n,o){return e(this,void 0,void 0,(function(){var e;return r(this,(function(r){switch(r.label){case 0:return(e=new t(o)).heapArray=n,[4,e.init()];case 1:return r.sent(),[2,e]}}))}))},t.heappop=function(e,r){var n=new t(r);return n.heapArray=e,n.pop()},t.heappush=function(n,o,i){return e(this,void 0,void 0,(function(){var e;return r(this,(function(r){switch(r.label){case 0:return(e=new t(i)).heapArray=n,[4,e.push(o)];case 1:return r.sent(),[2]}}))}))},t.heappushpop=function(e,r,n){var o=new t(n);return o.heapArray=e,o.pushpop(r)},t.heapreplace=function(e,r,n){var o=new t(n);return o.heapArray=e,o.replace(r)},t.heaptop=function(e,r,n){void 0===r&&(r=1);var o=new t(n);return o.heapArray=e,o.top(r)},t.heapbottom=function(e,r,n){void 0===r&&(r=1);var o=new t(n);return o.heapArray=e,o.bottom(r)},t.nlargest=function(i,a,h){return e(this,void 0,void 0,(function(){var e;return r(this,(function(r){switch(r.label){case 0:return(e=new t(h)).heapArray=o([],n(a),!1),[4,e.init()];case 1:return r.sent(),[2,e.top(i)]}}))}))},t.nsmallest=function(i,a,h){return e(this,void 0,void 0,(function(){var e;return r(this,(function(r){switch(r.label){case 0:return(e=new t(h)).heapArray=o([],n(a),!1),[4,e.init()];case 1:return r.sent(),[2,e.bottom(i)]}}))}))},t.prototype.add=function(t){return e(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,this._sortNodeUp(this.heapArray.push(t)-1)];case 1:return e.sent(),this._applyLimit(),[2,!0]}}))}))},t.prototype.addAll=function(t){return e(this,void 0,void 0,(function(){var e,i,a;return r(this,(function(r){switch(r.label){case 0:e=this.length,(a=this.heapArray).push.apply(a,o([],n(t),!1)),i=this.length,r.label=1;case 1:return e<i?[4,this._sortNodeUp(e)]:[3,4];case 2:r.sent(),r.label=3;case 3:return++e,[3,1];case 4:return this._applyLimit(),[2,!0]}}))}))},t.prototype.bottom=function(){return e(this,arguments,void 0,(function(t){return void 0===t&&(t=1),r(this,(function(e){return 0===this.heapArray.length||t<=0?[2,[]]:1===this.heapArray.length?[2,[this.heapArray[0]]]:t>=this.heapArray.length?[2,o([],n(this.heapArray),!1)]:[2,this._bottomN_push(~~t)]}))}))},t.prototype.check=function(){return e(this,void 0,void 0,(function(){var t,e,n,o,a,h,s,p,u;return r(this,(function(r){switch(r.label){case 0:t=0,r.label=1;case 1:if(!(t<this.heapArray.length))return[3,10];e=this.heapArray[t],n=this.getChildrenOf(t),r.label=2;case 2:r.trys.push([2,7,8,9]),p=void 0,o=i(n),a=o.next(),r.label=3;case 3:return a.done?[3,6]:(h=a.value,[4,this.compare(e,h)]);case 4:if(r.sent()>0)return[2,e];r.label=5;case 5:return a=o.next(),[3,3];case 6:return[3,9];case 7:return s=r.sent(),p={error:s},[3,9];case 8:try{a&&!a.done&&(u=o.return)&&u.call(o)}finally{if(p)throw p.error}return[7];case 9:return++t,[3,1];case 10:return[2]}}))}))},t.prototype.clear=function(){this.heapArray=[]},t.prototype.clone=function(){var e=new t(this.comparator());return e.heapArray=this.toArray(),e._limit=this._limit,e},t.prototype.comparator=function(){return this.compare},t.prototype.contains=function(n){return e(this,arguments,void 0,(function(e,n){var o,a,h,s,p,u;return void 0===n&&(n=t.defaultIsEqual),r(this,(function(t){switch(t.label){case 0:t.trys.push([0,5,6,7]),o=i(this.heapArray),a=o.next(),t.label=1;case 1:return a.done?[3,4]:(h=a.value,[4,n(h,e)]);case 2:if(t.sent())return[2,!0];t.label=3;case 3:return a=o.next(),[3,1];case 4:return[3,7];case 5:return s=t.sent(),p={error:s},[3,7];case 6:try{a&&!a.done&&(u=o.return)&&u.call(o)}finally{if(p)throw p.error}return[7];case 7:return[2,!1]}}))}))},t.prototype.init=function(t){return e(this,void 0,void 0,(function(){var e;return r(this,(function(r){switch(r.label){case 0:t&&(this.heapArray=o([],n(t),!1)),e=Math.floor(this.heapArray.length),r.label=1;case 1:return e>=0?[4,this._sortNodeDown(e)]:[3,4];case 2:r.sent(),r.label=3;case 3:return--e,[3,1];case 4:return this._applyLimit(),[2]}}))}))},t.prototype.isEmpty=function(){return 0===this.length},t.prototype.leafs=function(){if(0===this.heapArray.length)return[];var e=t.getParentIndexOf(this.heapArray.length-1);return this.heapArray.slice(e+1)},Object.defineProperty(t.prototype,"length",{get:function(){return this.heapArray.length},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"limit",{get:function(){return this._limit},set:function(t){this._limit=~~t,this._applyLimit()},enumerable:!1,configurable:!0}),t.prototype.peek=function(){return this.heapArray[0]},t.prototype.pop=function(){return e(this,void 0,void 0,(function(){var t;return r(this,(function(e){return t=this.heapArray.pop(),this.length>0&&void 0!==t?[2,this.replace(t)]:[2,t]}))}))},t.prototype.push=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return e(this,void 0,void 0,(function(){return r(this,(function(e){return t.length<1?[2,!1]:1===t.length?[2,this.add(t[0])]:[2,this.addAll(t)]}))}))},t.prototype.pushpop=function(t){return e(this,void 0,void 0,(function(){var e;return r(this,(function(r){switch(r.label){case 0:return[4,this.compare(this.heapArray[0],t)];case 1:return r.sent()<0?(e=n([this.heapArray[0],t],2),t=e[0],this.heapArray[0]=e[1],[4,this._sortNodeDown(0)]):[3,3];case 2:r.sent(),r.label=3;case 3:return[2,t]}}))}))},t.prototype.remove=function(i){return e(this,arguments,void 0,(function(e,i){var a,h,s,p=this;return void 0===i&&(i=t.defaultIsEqual),r(this,(function(r){switch(r.label){case 0:return this.heapArray.length?void 0!==e?[3,2]:[4,this.pop()]:[2,!1];case 1:return r.sent(),[2,!0];case 2:a=[0],r.label=3;case 3:return a.length?(h=a.shift(),[4,i(this.heapArray[h],e)]):[3,13];case 4:return r.sent()?0!==h?[3,6]:[4,this.pop()]:[3,11];case 5:return r.sent(),[3,10];case 6:return h!==this.heapArray.length-1?[3,7]:(this.heapArray.pop(),[3,10]);case 7:return this.heapArray.splice(h,1,this.heapArray.pop()),[4,this._sortNodeUp(h)];case 8:return r.sent(),[4,this._sortNodeDown(h)];case 9:r.sent(),r.label=10;case 10:return[2,!0];case 11:s=t.getChildrenIndexOf(h).filter((function(t){return t<p.heapArray.length})),a.push.apply(a,o([],n(s),!1)),r.label=12;case 12:return[3,3];case 13:return[2,!1]}}))}))},t.prototype.replace=function(t){return e(this,void 0,void 0,(function(){var e;return r(this,(function(r){switch(r.label){case 0:return e=this.heapArray[0],this.heapArray[0]=t,[4,this._sortNodeDown(0)];case 1:return r.sent(),[2,e]}}))}))},t.prototype.size=function(){return this.length},t.prototype.top=function(){return e(this,arguments,void 0,(function(t){return void 0===t&&(t=1),r(this,(function(e){return 0===this.heapArray.length||t<=0?[2,[]]:1===this.heapArray.length||1===t?[2,[this.heapArray[0]]]:t>=this.heapArray.length?[2,o([],n(this.heapArray),!1)]:[2,this._topN_push(~~t)]}))}))},t.prototype.toArray=function(){return o([],n(this.heapArray),!1)},t.prototype.toString=function(){return this.heapArray.toString()},t.prototype.get=function(t){return this.heapArray[t]},t.prototype.getChildrenOf=function(e){var r=this;return t.getChildrenIndexOf(e).map((function(t){return r.heapArray[t]})).filter((function(t){return void 0!==t}))},t.prototype.getParentOf=function(e){var r=t.getParentIndexOf(e);return this.heapArray[r]},t.prototype[Symbol.iterator]=function(){return r(this,(function(t){switch(t.label){case 0:return this.length?[4,this.pop()]:[3,2];case 1:return t.sent(),[3,0];case 2:return[2]}}))},t.prototype.iterator=function(){return this},t.prototype._applyLimit=function(){if(this._limit&&this._limit<this.heapArray.length)for(var t=this.heapArray.length-this._limit;t;)this.heapArray.pop(),--t},t.prototype._bottomN_push=function(n){return e(this,void 0,void 0,(function(){var e,o,i,a,h,s;return r(this,(function(r){switch(r.label){case 0:return(e=new t(this.compare)).limit=n,e.heapArray=this.heapArray.slice(-n),[4,e.init()];case 1:for(r.sent(),o=this.heapArray.length-1-n,i=t.getParentIndexOf(o),a=[],s=o;s>i;--s)a.push(s);h=this.heapArray,r.label=2;case 2:return a.length?(s=a.shift(),[4,this.compare(h[s],e.peek())]):[3,6];case 3:return r.sent()>0?[4,e.replace(h[s])]:[3,5];case 4:r.sent(),s%2&&a.push(t.getParentIndexOf(s)),r.label=5;case 5:return[3,2];case 6:return[2,e.toArray()]}}))}))},t.prototype._moveNode=function(t,e){var r;r=n([this.heapArray[e],this.heapArray[t]],2),this.heapArray[t]=r[0],this.heapArray[e]=r[1]},t.prototype._sortNodeDown=function(t){return e(this,void 0,void 0,(function(){var e,n,o,i,a,h;return r(this,(function(r){switch(r.label){case 0:e=this.heapArray.length,r.label=1;case 1:return o=(n=2*t+1)+1,i=t,(a=n<e)?[4,this.compare(this.heapArray[n],this.heapArray[i])]:[3,3];case 2:a=r.sent()<0,r.label=3;case 3:return a&&(i=n),(h=o<e)?[4,this.compare(this.heapArray[o],this.heapArray[i])]:[3,5];case 4:h=r.sent()<0,r.label=5;case 5:if(h&&(i=o),i===t)return[3,7];this._moveNode(t,i),t=i,r.label=6;case 6:return[3,1];case 7:return[2]}}))}))},t.prototype._sortNodeUp=function(n){return e(this,void 0,void 0,(function(){var e;return r(this,(function(r){switch(r.label){case 0:return n>0?(e=t.getParentIndexOf(n),[4,this.compare(this.heapArray[n],this.heapArray[e])]):[3,2];case 1:return r.sent()<0?(this._moveNode(n,e),n=e,[3,0]):[3,2];case 2:return[2]}}))}))},t.prototype._topN_push=function(i){return e(this,void 0,void 0,(function(){var e,a,h,s;return r(this,(function(r){switch(r.label){case 0:(e=new t(this._invertedCompare)).limit=i,a=[0],h=this.heapArray,r.label=1;case 1:return a.length?(s=a.shift())<h.length?e.length<i?[4,e.push(h[s])]:[3,3]:[3,6]:[3,7];case 2:return r.sent(),a.push.apply(a,o([],n(t.getChildrenIndexOf(s)),!1)),[3,6];case 3:return[4,this.compare(h[s],e.peek())];case 4:return r.sent()<0?[4,e.replace(h[s])]:[3,6];case 5:r.sent(),a.push.apply(a,o([],n(t.getChildrenIndexOf(s)),!1)),r.label=6;case 6:return[3,1];case 7:return[2,e.toArray()]}}))}))},t.prototype._topN_fill=function(i){return e(this,void 0,void 0,(function(){var e,a,h,s,p;return r(this,(function(r){switch(r.label){case 0:return e=this.heapArray,(a=new t(this._invertedCompare)).limit=i,a.heapArray=e.slice(0,i),[4,a.init()];case 1:for(r.sent(),h=t.getParentIndexOf(i-1)+1,s=[],p=h;p<i;++p)s.push.apply(s,o([],n(t.getChildrenIndexOf(p).filter((function(t){return t<e.length}))),!1));(i-1)%2&&s.push(i),r.label=2;case 2:return s.length?(p=s.shift())<e.length?[4,this.compare(e[p],a.peek())]:[3,5]:[3,6];case 3:return r.sent()<0?[4,a.replace(e[p])]:[3,5];case 4:r.sent(),s.push.apply(s,o([],n(t.getChildrenIndexOf(p)),!1)),r.label=5;case 5:return[3,2];case 6:return[2,a.toArray()]}}))}))},t.prototype._topN_heap=function(t){return e(this,void 0,void 0,(function(){var e,n,o,i,a;return r(this,(function(r){switch(r.label){case 0:e=this.clone(),n=[],o=0,r.label=1;case 1:return o<t?(a=(i=n).push,[4,e.pop()]):[3,4];case 2:a.apply(i,[r.sent()]),r.label=3;case 3:return++o,[3,1];case 4:return[2,n]}}))}))},t.prototype._topIdxOf=function(t){return e(this,void 0,void 0,(function(){var e,n,o;return r(this,(function(r){switch(r.label){case 0:if(!t.length)return[2,-1];n=t[e=0],o=1,r.label=1;case 1:return o<t.length?[4,this.compare(t[o],n)]:[3,4];case 2:r.sent()<0&&(e=o,n=t[o]),r.label=3;case 3:return++o,[3,1];case 4:return[2,e]}}))}))},t.prototype._topOf=function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return e(this,void 0,void 0,(function(){var e;return r(this,(function(r){switch(r.label){case 0:return[4,(e=new t(this.compare)).init(n)];case 1:return r.sent(),[2,e.peek()]}}))}))}}();var a=function(t,e){var r,n,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=h(0),a.throw=h(1),a.return=h(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function h(h){return function(s){return function(h){if(r)throw new TypeError("Generator is already executing.");for(;a&&(a=0,h[0]&&(i=0)),i;)try{if(r=1,n&&(o=2&h[0]?n.return:h[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,h[1])).done)return o;switch(n=0,o&&(h=[2&h[0],o.value]),h[0]){case 0:case 1:o=h;break;case 4:return i.label++,{value:h[1],done:!1};case 5:i.label++,n=h[1],h=[0];continue;case 7:h=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==h[0]&&2!==h[0])){i=0;continue}if(3===h[0]&&(!o||h[1]>o[0]&&h[1]<o[3])){i.label=h[1];break}if(6===h[0]&&i.label<o[1]){i.label=o[1],o=h;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(h);break}o[2]&&i.ops.pop(),i.trys.pop();continue}h=e.call(t,i)}catch(t){h=[6,t],n=0}finally{r=o=0}if(5&h[0])throw h[1];return{value:h[0]?h[1]:void 0,done:!0}}([h,s])}}},h=function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,o,i=r.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(n=i.next()).done;)a.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return a},s=function(t,e,r){if(r||2===arguments.length)for(var n,o=0,i=e.length;o<i;o++)!n&&o in e||(n||(n=Array.prototype.slice.call(e,0,o)),n[o]=e[o]);return t.concat(n||Array.prototype.slice.call(e))},p=function(){function t(e){void 0===e&&(e=t.minComparator);var r=this;this.compare=e,this.heapArray=[],this._limit=0,this.offer=this.add,this.element=this.peek,this.poll=this.pop,this.removeAll=this.clear,this._invertedCompare=function(t,e){return-1*r.compare(t,e)}}return t.getChildrenIndexOf=function(t){return[2*t+1,2*t+2]},t.getParentIndexOf=function(t){if(t<=0)return-1;var e=t%2?1:2;return Math.floor((t-e)/2)},t.getSiblingIndexOf=function(t){return t<=0?-1:t+(t%2?1:-1)},t.minComparator=function(t,e){return t>e?1:t<e?-1:0},t.maxComparator=function(t,e){return e>t?1:e<t?-1:0},t.minComparatorNumber=function(t,e){return t-e},t.maxComparatorNumber=function(t,e){return e-t},t.defaultIsEqual=function(t,e){return t===e},t.print=function(e){function r(e){var r=t.getParentIndexOf(e);return Math.floor(Math.log2(r+1))}function n(t,e){for(var r="";e>0;--e)r+=t;return r}for(var o=0,i=[],a=r(e.length-1)+2,h=0;o<e.length;){var s=r(o)+1;0===o&&(s=0);var p=String(e.get(o));p.length>h&&(h=p.length),i[s]=i[s]||[],i[s].push(p),o+=1}return i.map((function(t,e){var r=Math.pow(2,a-e)-1;return n(" ",Math.floor(r/2)*h)+t.map((function(t){var e=(h-t.length)/2;return n(" ",Math.ceil(e))+t+n(" ",Math.floor(e))})).join(n(" ",r*h))})).join("\n")},t.heapify=function(e,r){var n=new t(r);return n.heapArray=e,n.init(),n},t.heappop=function(e,r){var n=new t(r);return n.heapArray=e,n.pop()},t.heappush=function(e,r,n){var o=new t(n);o.heapArray=e,o.push(r)},t.heappushpop=function(e,r,n){var o=new t(n);return o.heapArray=e,o.pushpop(r)},t.heapreplace=function(e,r,n){var o=new t(n);return o.heapArray=e,o.replace(r)},t.heaptop=function(e,r,n){void 0===r&&(r=1);var o=new t(n);return o.heapArray=e,o.top(r)},t.heapbottom=function(e,r,n){void 0===r&&(r=1);var o=new t(n);return o.heapArray=e,o.bottom(r)},t.nlargest=function(e,r,n){var o=new t(n);return o.heapArray=s([],h(r),!1),o.init(),o.top(e)},t.nsmallest=function(e,r,n){var o=new t(n);return o.heapArray=s([],h(r),!1),o.init(),o.bottom(e)},t.prototype.add=function(t){return this._sortNodeUp(this.heapArray.push(t)-1),this._applyLimit(),!0},t.prototype.addAll=function(t){var e,r=this.length;(e=this.heapArray).push.apply(e,s([],h(t),!1));for(var n=this.length;r<n;++r)this._sortNodeUp(r);return this._applyLimit(),!0},t.prototype.bottom=function(t){return void 0===t&&(t=1),0===this.heapArray.length||t<=0?[]:1===this.heapArray.length?[this.heapArray[0]]:t>=this.heapArray.length?s([],h(this.heapArray),!1):this._bottomN_push(~~t)},t.prototype.check=function(){var t=this;return this.heapArray.find((function(e,r){return!!t.getChildrenOf(r).find((function(r){return t.compare(e,r)>0}))}))},t.prototype.clear=function(){this.heapArray=[]},t.prototype.clone=function(){var e=new t(this.comparator());return e.heapArray=this.toArray(),e._limit=this._limit,e},t.prototype.comparator=function(){return this.compare},t.prototype.contains=function(e,r){return void 0===r&&(r=t.defaultIsEqual),-1!==this.indexOf(e,r)},t.prototype.init=function(t){t&&(this.heapArray=s([],h(t),!1));for(var e=Math.floor(this.heapArray.length);e>=0;--e)this._sortNodeDown(e);this._applyLimit()},t.prototype.isEmpty=function(){return 0===this.length},t.prototype.indexOf=function(e,r){if(void 0===r&&(r=t.defaultIsEqual),0===this.heapArray.length)return-1;for(var n=[],o=0;o<this.heapArray.length;){var i=this.heapArray[o];if(r(i,e))return o;this.compare(i,e)<=0&&n.push.apply(n,s([],h(t.getChildrenIndexOf(o)),!1)),o=n.shift()||this.heapArray.length}return-1},t.prototype.indexOfEvery=function(e,r){if(void 0===r&&(r=t.defaultIsEqual),0===this.heapArray.length)return[];for(var n=[],o=[],i=0;i<this.heapArray.length;){var a=this.heapArray[i];r(a,e)?(o.push(i),n.push.apply(n,s([],h(t.getChildrenIndexOf(i)),!1))):this.compare(a,e)<=0&&n.push.apply(n,s([],h(t.getChildrenIndexOf(i)),!1)),i=n.shift()||this.heapArray.length}return o},t.prototype.leafs=function(){if(0===this.heapArray.length)return[];var e=t.getParentIndexOf(this.heapArray.length-1);return this.heapArray.slice(e+1)},Object.defineProperty(t.prototype,"length",{get:function(){return this.heapArray.length},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"limit",{get:function(){return this._limit},set:function(t){t<0||isNaN(t)?this._limit=0:this._limit=~~t,this._applyLimit()},enumerable:!1,configurable:!0}),t.prototype.setLimit=function(t){return this.limit=t,t<0||isNaN(t)?NaN:this._limit},t.prototype.peek=function(){return this.heapArray[0]},t.prototype.pop=function(){var t=this.heapArray.pop();return this.length>0&&void 0!==t?this.replace(t):t},t.prototype.push=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return!(t.length<1)&&(1===t.length?this.add(t[0]):this.addAll(t))},t.prototype.pushpop=function(t){var e;return this.compare(this.heapArray[0],t)<0&&(t=(e=h([this.heapArray[0],t],2))[0],this.heapArray[0]=e[1],this._sortNodeDown(0)),t},t.prototype.remove=function(e,r){var n=this;if(void 0===r&&(r=t.defaultIsEqual),!this.heapArray.length)return!1;if(void 0===e)return this.pop(),!0;for(var o=[0];o.length;){var i=o.shift();if(r(this.heapArray[i],e))return 0===i?this.pop():i===this.heapArray.length-1?this.heapArray.pop():(this.heapArray.splice(i,1,this.heapArray.pop()),this._sortNodeUp(i),this._sortNodeDown(i)),!0;if(this.compare(this.heapArray[i],e)<=0){var a=t.getChildrenIndexOf(i).filter((function(t){return t<n.heapArray.length}));o.push.apply(o,s([],h(a),!1))}}return!1},t.prototype.replace=function(t){var e=this.heapArray[0];return this.heapArray[0]=t,this._sortNodeDown(0),e},t.prototype.size=function(){return this.length},t.prototype.top=function(t){return void 0===t&&(t=1),0===this.heapArray.length||t<=0?[]:1===this.heapArray.length||1===t?[this.heapArray[0]]:t>=this.heapArray.length?s([],h(this.heapArray),!1):this._topN_push(~~t)},t.prototype.toArray=function(){return s([],h(this.heapArray),!1)},t.prototype.toString=function(){return this.heapArray.toString()},t.prototype.get=function(t){return this.heapArray[t]},t.prototype.getChildrenOf=function(e){var r=this;return t.getChildrenIndexOf(e).map((function(t){return r.heapArray[t]})).filter((function(t){return void 0!==t}))},t.prototype.getParentOf=function(e){var r=t.getParentIndexOf(e);return this.heapArray[r]},t.prototype[Symbol.iterator]=function(){return a(this,(function(t){switch(t.label){case 0:return this.length?[4,this.pop()]:[3,2];case 1:return t.sent(),[3,0];case 2:return[2]}}))},t.prototype.iterator=function(){return this.toArray()},t.prototype._applyLimit=function(){if(this._limit>0&&this._limit<this.heapArray.length)for(var t=this.heapArray.length-this._limit;t;)this.heapArray.pop(),--t},t.prototype._bottomN_push=function(e){var r=new t(this.compare);r.limit=e,r.heapArray=this.heapArray.slice(-e),r.init();for(var n=this.heapArray.length-1-e,o=t.getParentIndexOf(n),i=[],a=n;a>o;--a)i.push(a);for(var h=this.heapArray;i.length;){a=i.shift();this.compare(h[a],r.peek())>0&&(r.replace(h[a]),a%2&&i.push(t.getParentIndexOf(a)))}return r.toArray()},t.prototype._moveNode=function(t,e){var r;r=h([this.heapArray[e],this.heapArray[t]],2),this.heapArray[t]=r[0],this.heapArray[e]=r[1]},t.prototype._sortNodeDown=function(t){for(var e=this.heapArray.length;;){var r=2*t+1,n=r+1,o=t;if(r<e&&this.compare(this.heapArray[r],this.heapArray[o])<0&&(o=r),n<e&&this.compare(this.heapArray[n],this.heapArray[o])<0&&(o=n),o===t)break;this._moveNode(t,o),t=o}},t.prototype._sortNodeUp=function(e){for(;e>0;){var r=t.getParentIndexOf(e);if(!(this.compare(this.heapArray[e],this.heapArray[r])<0))break;this._moveNode(e,r),e=r}},t.prototype._topN_push=function(e){var r=new t(this._invertedCompare);r.limit=e;for(var n=[0],o=this.heapArray;n.length;){var i=n.shift();i<o.length&&(r.length<e?(r.push(o[i]),n.push.apply(n,s([],h(t.getChildrenIndexOf(i)),!1))):this.compare(o[i],r.peek())<0&&(r.replace(o[i]),n.push.apply(n,s([],h(t.getChildrenIndexOf(i)),!1))))}return r.toArray()},t.prototype._topN_fill=function(e){var r=this.heapArray,n=new t(this._invertedCompare);n.limit=e,n.heapArray=r.slice(0,e),n.init();for(var o=[],i=t.getParentIndexOf(e-1)+1;i<e;++i)o.push.apply(o,s([],h(t.getChildrenIndexOf(i).filter((function(t){return t<r.length}))),!1));for((e-1)%2&&o.push(e);o.length;){(i=o.shift())<r.length&&this.compare(r[i],n.peek())<0&&(n.replace(r[i]),o.push.apply(o,s([],h(t.getChildrenIndexOf(i)),!1)))}return n.toArray()},t.prototype._topN_heap=function(t){for(var e=this.clone(),r=[],n=0;n<t;++n)r.push(e.pop());return r},t.prototype._topIdxOf=function(t){if(!t.length)return-1;for(var e=0,r=t[e],n=1;n<t.length;++n){this.compare(t[n],r)<0&&(e=n,r=t[n])}return e},t.prototype._topOf=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];var n=new t(this.compare);return n.init(e),n.peek()},t}();const u=(t,e,r=0)=>{try{for(;r<t.length;r++)t[r](...e)}finally{r<t.length&&u(t,e,r+1)}};function c(t){let e=null,r=-1;for(const[n,o]of t.edges.entries())(o>r||o===r&&n>e)&&(e=n,r=o);return e}function l(t,e){const r="\0".charCodeAt(0),n="ÿ".charCodeAt(0);let o=!1,i="",a=0;for(;;){const h=a<t.length?t.charCodeAt(a):r,s=!o&&a<e.length?e.charCodeAt(a):n+1,p=h+s>>>1;if(i+=String.fromCharCode(p),p<=h){h<s&&(o=!0),a+=1;continue}let u=Math.floor(4096*Math.random());for(;u>0;){const t=n-r+1,e=u%t;u=(u-e)/t,i+=String.fromCharCode(r+e)}return i}}function f(t){return t.has("root")&&t.get("root").has("_parentHistory")}class d{constructor(e){if(this._ymap=null,void 0===e)throw new Error("[ytree] expected a yMap argument");if(null===e.doc)throw new Error("[ytree] expected yMap to be bounded to a Y.Doc instance");if(f(e))this._ymap=e;else{if(0!==e.size)throw new Error("[ytree] expected yMap to either be initialized for ytree or empty");this._ymap=e;this._ymap.set("root",new t.Map).set("_parentHistory",new t.Map)}this._ydoc=this._ymap.doc,this.computedMap=new Map,this._callbacks=new Array,this._ymap.observeDeep(((t,e)=>{t.forEach((t=>{(0==t.path.length||2==t.path.length&&"_parentHistory"===t.path[1])&&(0==t.path.length&&(t.changes.keys.forEach(((t,e)=>{if("update"===t.action)throw new Error("[ytree] The node should not be updated")})),this.recomputeParentsAndChildren(),u(this._callbacks,[])),2==t.path.length&&"_parentHistory"===t.path[1]&&(this.recomputeParentsAndChildren(),u(this._callbacks,[])))}))})),this.recomputeParentsAndChildren()}generateNodeKey(){const t=10**8;let e=Math.floor(Math.random()*t);for(;this._ymap.has(this._ydoc.clientID+""+e);)e=Math.floor(Math.random()*t);return this._ydoc.clientID+""+e}observe(t){this._callbacks.push(t)}unobserve(t){const e=this._callbacks.length;this._callbacks=this._callbacks.filter((e=>t!==e)),this._callbacks.length===e&&console.error("[ytree] tried to remove callback that does not exist")}getYMap(){return this._ymap}setNodeValueFromKey(t,e){if(!this.computedMap.has(t))throw new Error("[ytree] node with key: "+t+" does not exist");if(!e)throw new Error("[ytree] value is required");this._ymap.get(t).set("value",e)}getNodeValueFromKey(t){if(!this.computedMap.has(t))throw new Error("[ytree] node with key: "+t+" does not exist");return this._ymap.get(t).get("value")}getNodeChildrenFromKey(t){if(!this.computedMap.has(t))throw new Error("[ytree] node with key: "+t+" does not exist");return this.computedMap.get(t).children.map((t=>t.id))}getNodeParentFromKey(t){if(!this.computedMap.has(t))throw new Error("[ytree] node with key: "+t+" does not exist");return this.computedMap.get(t).parent.id}createNode(e,r,n){if(this.recomputeParentsAndChildren(),!this.computedMap.has(e))throw new Error("[ytree] Parent with key: "+e+" does not exist");if(this.computedMap.has(r))throw new Error("[ytree] Node with key: "+r+" already exists");const o=l(this._getHighestOrderIndex(this.getNodeChildrenFromKey(e),e),"");this._ydoc.transact((()=>{const i=new t.Map;i.set("value",n);const a=new t.Map;a.set(e,{counter:0,order:o}),i.set("_parentHistory",a),this._ymap.set(r,i)}))}deleteNodeAndDescendants(t){this.recomputeParentsAndChildren();const e=new Array;this.getAllDescendants(t,e),e.join(", "),this._ydoc.transact((()=>{for(const t of e)this._ymap.delete(t)}))}moveChildToParent(t,e){if(!this.computedMap.has(e))throw new Error("[ytree] Parent with key: "+e+" does not exist");if(!this.computedMap.has(t))throw new Error("[ytree] Child with key: "+t+" does not exist");const r=l(this._getHighestOrderIndex(this.getNodeChildrenFromKey(e),e),"");if(this.isNodeUnderOtherNode(this.computedMap.get(e),this.computedMap.get(t)))throw new Error("[ytree] Cannot reparent a node to itself or its descendants.");const n=t=>{for(;t;){const e=t.parent;if(!e)break;c(this.computedMap.get(t.id))!==e.id&&o.push({child:t,parent:e}),t=e}},o=[],i=this.computedMap.get(t),a=this.computedMap.get(e);n(i.parent),n(a),o.push({child:i,parent:a}),this._ydoc.transact((()=>{for(const{child:n,parent:i}of o){let o=-1;for(const{counter:t,order:e}of this._ymap.get(n.id).get("_parentHistory").values())o=Math.max(o,t);if(i.id===e&&n.id===t){this._ymap.get(n.id).get("_parentHistory").set(i.id,{counter:o+1,order:r});continue}const a=this._ymap.get(n.id).get("_parentHistory").get(i.id).order;this._ymap.get(n.id).get("_parentHistory").set(i.id,{counter:o+1,order:a})}}))}recomputeParentsAndChildren(){this.computedMap.clear();const t=this.computedMap;for(const e of this._ymap.keys())t.set(e,{id:e,parent:null,children:[],edges:new Map});for(const e of t.values()){const r=e.edges;for(const[n,{counter:o,order:i}]of this._ymap.get(e.id).get("_parentHistory").entries())t.has(n)&&r.set(n,o)}t.forEach(((e,r)=>{"root"!==r&&(e.parent=t.get(c(e)),e.parent.children.push(e))}));const e=new Set,r=["root"];for(;r.length>0;){const t=r.pop();e.add(t);const n=this.computedMap.get(t)?.children||[];for(const t of n)r.push(t.id)}const n=new Set;for(let[r,o]of t.entries())e.has(r)||n.add(o),o.children=[];const o=new Set;for(let e of t.values())if(!this.isNodeUnderOtherNode(e,t.get("root")))for(;e&&!o.has(e);)o.add(e),e=e.parent;if(o.size>0){const e=new Map,r=new p(((t,e)=>{const r=e.counter-t.counter;return 0!==r?r:t.parent.id<e.parent.id?-1:t.parent.id>e.parent.id?1:t.child.id<e.child.id?-1:t.child.id>e.child.id?1:0}));for(const n of o.values())for(const[i,a]of n.edges.entries()){const h=t.get(i);if(o.has(h)){let t=e.get(h);t||(t=[],e.set(h,t)),t.push({child:n,parent:h,counter:a})}else r.push({child:n,parent:h,counter:a})}for(let t;t=r.pop();){const n=t.child;if(!o.has(n))continue;n.parent=t.parent,o.delete(n);const i=e.get(n);if(i)for(const t of i)r.push(t)}}for(const e of t.values())e.parent&&e.parent.children.push(e);for(const e of t.values())e.children.sort(((t,e)=>t.id<e.id?-1:t.id>e.id?1:0))}getAllDescendants(t,e){const r=[t];for(;r.length>0;){const t=r.pop();e.push(t);const n=this.computedMap.get(t)?.children||[];for(const t of n)r.push(t.id)}}isNodeUnderOtherNode(t,e){if(t===e)return!0;let r=t,n=t.parent;for(;n&&n!==e;){if(r===n)return!1;if(n=n.parent,!n||n===e)break;r=r.parent,n=n.parent}return n===e}setNodeOrderToStart(t){if("root"===t)throw new Error("[ytree] cannot set order of root node");const e=this.computedMap.get(t).parent,r=this.getNodeChildrenFromKey(e.id),n=l("",this._getLowestOrderIndex(r,e.id)),o=this._ymap.get(t).get("_parentHistory"),i=o.get(e.id).counter;o.set(e.id,{counter:i,order:n})}setNodeOrderToEnd(t){if("root"===t)throw new Error("[ytree] cannot set order of root node");const e=this.computedMap.get(t).parent,r=this.getNodeChildrenFromKey(e.id),n=l(this._getHighestOrderIndex(r,e.id),""),o=this._ymap.get(t).get("_parentHistory"),i=o.get(e.id).counter;o.set(e.id,{counter:i,order:n})}setNodeAfter(t,e){if("root"===t)throw new Error("[ytree] cannot set order of root node");const r=this.computedMap.get(t).parent;if(this.computedMap.get(e).parent!==r)throw new Error("[ytree] expected nodes with keys nodeKey and target to have same parent");const n=this.getNodeChildrenFromKey(r.id),o=l(this._ymap.get(e).get("_parentHistory").get(r.id).order,this.getNextOrderIndex(e,n,r.id)),i=this._ymap.get(t).get("_parentHistory"),a=i.get(r.id).counter;i.set(r.id,{counter:a,order:o})}setNodeBefore(t,e){if("root"===t)throw new Error("[ytree] cannot set order of root node");const r=this.computedMap.get(t).parent;if(this.computedMap.get(e).parent!==r)throw new Error("[ytree] expected nodes with keys nodeKey and target to have same parent");const n=this.getNodeChildrenFromKey(r.id),o=l(this.getPreviousOrderIndex(e,n,r.id),this._ymap.get(e).get("_parentHistory").get(r.id).order),i=this._ymap.get(t).get("_parentHistory"),a=i.get(r.id).counter;i.set(r.id,{counter:a,order:o})}sortChildrenByOrder(t,e){return t.sort(((t,r)=>{const n=this._ymap.get(t).get("_parentHistory").get(e).order,o=this._ymap.get(r).get("_parentHistory").get(e).order;return n<o?-1:n>o?1:t<r?-1:t>r?1:0}))}getNextOrderIndex(t,e,r){if(0===e.length)return"";const n=this._ymap.get(t).get("_parentHistory").get(r).order;return e.reduce(((t,e)=>{const o=this._ymap.get(e).get("_parentHistory").get(r).order;return""===t?o>n?o:t:o<t&&o>n?o:t}),"")}getPreviousOrderIndex(t,e,r){if(0===e.length)return"";const n=this._ymap.get(t).get("_parentHistory").get(r).order;return e.reduce(((t,e)=>{const o=this._ymap.get(e).get("_parentHistory").get(r).order;return""===t?o<n?o:t:o>t&&o<n?o:t}),"")}_getHighestOrderIndex(t,e){if(0===t.length)return"";const r=this._ymap.get(t[0]).get("_parentHistory").get(e).order;return t.reduce(((t,r)=>{const n=this._ymap.get(r).get("_parentHistory").get(e).order;return n>t?n:t}),r)}_getLowestOrderIndex(t,e){if(0===t.length)return"";return t.reduce(((t,r)=>{const n=this._ymap.get(r).get("_parentHistory").get(e).order;return n<t?n:t}),this._ymap.get(t[0]).get("_parentHistory").get(e).order)}}export{d as YTree,f as checkForYTree}; //# sourceMappingURL=index.node.js.map