Socket
Socket
Sign inDemoInstall

sorted-btree

Package Overview
Dependencies
0
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.6.1 to 1.6.2

4

b+tree.d.ts

@@ -312,4 +312,4 @@ import { ISortedMap, ISortedMapF } from './interfaces';

/** Performs a greedy clone, immediately duplicating any nodes that are
* not currently marked as shared, in order to avoid marking any nodes
* as shared.
* not currently marked as shared, in order to avoid marking any
* additional nodes as shared.
* @param force Clone all nodes, even shared ones.

@@ -316,0 +316,0 @@ */

@@ -1,1 +0,1 @@

"use strict";var __extends=this&&this.__extends||function(){var i=function(e,t){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])})(e,t)};return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}i(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}}();function defaultComparator(e,t){if(Number.isFinite(e)&&Number.isFinite(t))return e-t;var r=typeof e,i=typeof t;if(r!==i)return r<i?-1:1;if("object"===r){if(null===e)return null===t?0:-1;if(null===t)return 1;if((r=typeof(e=e.valueOf()))!==(i=typeof(t=t.valueOf())))return r<i?-1:1}return e<t?-1:t<e?1:e===t?0:Number.isNaN(e)?Number.isNaN(t)?0:-1:Number.isNaN(t)?1:Array.isArray(e)?0:Number.NaN}function simpleComparator(e,t){return t<e?1:e<t?-1:0}Object.defineProperty(exports,"__esModule",{value:!0}),exports.EmptyBTree=exports.simpleComparator=exports.defaultComparator=void 0,exports.defaultComparator=defaultComparator,exports.simpleComparator=simpleComparator;var BTree=function(){function x(e,t,r){this._root=EmptyLeaf,this._size=0,this._maxNodeSize=4<=r?Math.min(r,256):32,this._compare=t||defaultComparator,e&&this.setPairs(e)}return Object.defineProperty(x.prototype,"size",{get:function(){return this._size},enumerable:!1,configurable:!0}),Object.defineProperty(x.prototype,"length",{get:function(){return this._size},enumerable:!1,configurable:!0}),Object.defineProperty(x.prototype,"isEmpty",{get:function(){return 0===this._size},enumerable:!1,configurable:!0}),x.prototype.clear=function(){this._root=EmptyLeaf,this._size=0},x.prototype.forEach=function(r,e){var i=this;return void 0!==e&&(r=r.bind(e)),this.forEachPair(function(e,t){return r(t,e,i)})},x.prototype.forEachPair=function(e,t){var r=this.minKey(),i=this.maxKey();return this.forRange(r,i,!0,e,t)},x.prototype.get=function(e,t){return this._root.get(e,t,this)},x.prototype.set=function(e,t,r){this._root.isShared&&(this._root=this._root.clone());r=this._root.set(e,t,r,this);return!0===r||!1===r?r:(this._root=new BNodeInternal([this._root,r]),!0)},x.prototype.has=function(e){return 0!==this.forRange(e,e,!0,void 0)},x.prototype.delete=function(e){return 0!==this.editRange(e,e,!0,DeleteRange)},x.prototype.with=function(e,t,r){var i=this.clone();return i.set(e,t,r)||r?i:this},x.prototype.withPairs=function(e,t){var r=this.clone();return 0!==r.setPairs(e,t)||t?r:this},x.prototype.withKeys=function(e,t){for(var r=this.clone(),i=!1,n=0;n<e.length;n++)i=r.set(e[n],void 0,!1)||i;return t&&!i?this:r},x.prototype.without=function(e,t){return this.withoutRange(e,e,!0,t)},x.prototype.withoutKeys=function(e,t){var r=this.clone();return r.deleteKeys(e)||!t?r:this},x.prototype.withoutRange=function(e,t,r,i){var n=this.clone();return 0===n.deleteRange(e,t,r)&&i?this:n},x.prototype.filter=function(i,e){var n,t=this.greedyClone();return t.editAll(function(e,t,r){if(!i(e,t,r))return n=Delete}),!n&&e?this:t},x.prototype.mapValues=function(i){var n={},e=this.greedyClone();return e.editAll(function(e,t,r){return n.value=i(t,e,r),n}),e},x.prototype.reduce=function(e,t){for(var r,i=0,n=t,o=this.entries(this.minKey(),ReusedArray);!(r=o.next()).done;)n=e(n,r.value,i++,this);return n},x.prototype.entries=function(e,t){var r=this.findPath(e);if(void 0===r)return iterator();var i=r.nodequeue,n=r.nodeindex,o=r.leaf,s=void 0!==t?1:0,a=void 0===e?-1:o.indexOf(e,0,this._compare)-1;return iterator(function(){e:for(;;)switch(s){case 0:if(++a<o.keys.length)return{done:!1,value:[o.keys[a],o.values[a]]};s=2;continue;case 1:if(++a<o.keys.length)return t[0]=o.keys[a],t[1]=o.values[a],{done:!1,value:t};s=2;case 2:for(var e=-1;;){if(++e>=i.length){s=3;continue e}if(++n[e]<i[e].length)break}for(;0<e;e--)i[e-1]=i[e][n[e]].children,n[e-1]=0;o=i[0][n[0]],a=-1,s=void 0!==t?1:0;continue;case 3:return{done:!0,value:void 0}}})},x.prototype.entriesReversed=function(e,t,r){if(void 0===e&&(r=void 0)===(e=this.maxKey()))return iterator();var i=this.findPath(e)||this.findPath(this.maxKey()),n=i.nodequeue,o=i.nodeindex,s=i.leaf;check(!n[0]||s===n[0][o[0]],"wat!");var a=s.indexOf(e,0,this._compare);!r&&a<s.keys.length&&this._compare(s.keys[a],e)<=0&&a++;var h=void 0!==t?1:0;return iterator(function(){e:for(;;)switch(h){case 0:if(0<=--a)return{done:!1,value:[s.keys[a],s.values[a]]};h=2;continue;case 1:if(0<=--a)return t[0]=s.keys[a],t[1]=s.values[a],{done:!1,value:t};h=2;case 2:for(var e=-1;;){if(++e>=n.length){h=3;continue e}if(0<=--o[e])break}for(;0<e;e--)n[e-1]=n[e][o[e]].children,o[e-1]=n[e-1].length-1;s=n[0][o[0]],a=s.keys.length,h=void 0!==t?1:0;continue;case 3:return{done:!0,value:void 0}}})},x.prototype.findPath=function(e){var t,r,i=this._root;if(i.isLeaf)r=t=EmptyArray;else{t=[],r=[];for(var n=0;!i.isLeaf;n++){if(t[n]=i.children,r[n]=void 0===e?0:i.indexOf(e,0,this._compare),r[n]>=t[n].length)return;i=t[n][r[n]]}t.reverse(),r.reverse()}return{nodequeue:t,nodeindex:r,leaf:i}},x.prototype.diffAgainst=function(e,t,r,i){if(e._compare!==this._compare)throw new Error("Tree comparators are not the same.");if(this.isEmpty||e.isEmpty)return this.isEmpty&&e.isEmpty?void 0:this.isEmpty?void 0===r?void 0:x.stepToEnd(x.makeDiffCursor(e),r):void 0===t?void 0:x.stepToEnd(x.makeDiffCursor(this),t);for(var n=this._compare,o=x.makeDiffCursor(this),s=x.makeDiffCursor(e),a=!0,h=!0,u=x.compare(o,s,n);a&&h;){var l=x.compare(o,s,n),f=o.leaf,p=o.internalSpine,c=o.levelIndices,y=s.leaf,d=s.internalSpine,v=s.levelIndices;if(f||y){if(0!==u)if(0===l){if(f&&y&&i){var g=f.values[c[c.length-1]],m=y.values[v[v.length-1]];if(!Object.is(g,m))if((k=i(o.currentKey,g,m))&&k.break)return k.break}}else if(0<l){if(y&&r){m=y.values[v[v.length-1]];if((k=r(s.currentKey,m))&&k.break)return k.break}}else if(t&&f&&0!==u){var k,g=f.values[c[c.length-1]];if((k=t(o.currentKey,g))&&k.break)return k.break}}else if(!f&&!y&&0===l){f=p.length-1,y=d.length-1,f=p[f][c[f]];if(d[y][v[y]]===f){a=x.step(o,!(u=0)),h=x.step(s,!0);continue}}(u=l)<0?a=x.step(o):h=x.step(s)}return a&&t?x.finishCursorWalk(o,s,n,t):h&&r?x.finishCursorWalk(s,o,n,r):void 0},x.finishCursorWalk=function(e,t,r,i){r=x.compare(e,t,r);if(0===r){if(!x.step(e))return}else r<0&&check(!1,"cursor walk terminated early");return x.stepToEnd(e,i)},x.stepToEnd=function(e,t){for(var r=!0;r;){var i=e.leaf,n=e.levelIndices,o=e.currentKey;if(i){n=t(o,i.values[n[n.length-1]]);if(n&&n.break)return n.break}r=x.step(e)}},x.makeDiffCursor=function(e){var t=e._root;return{height:e.height,internalSpine:[[t]],levelIndices:[0],leaf:void 0,currentKey:t.maxKey()}},x.step=function(e,t){var r=e.internalSpine,i=e.levelIndices,n=e.leaf;if(!0===t||n){var o=i.length;if(!0===t||0===i[o-1]){var s=r.length;if(0===s)return!1;for(var a=s-1,h=a;0<=h;){if(0<i[h])return h<o-1&&(e.leaf=void 0,i.pop()),h<a&&(e.internalSpine=r.slice(0,h+1)),e.currentKey=r[h][--i[h]].maxKey(),!0;h--}return!1}var u=--i[o-1];return e.currentKey=n.keys[u],!0}s=r.length,n=s-1,n=r[n][i[n]];return n.isLeaf?(e.leaf=n,u=i[s]=n.values.length-1,e.currentKey=n.keys[u]):(u=n.children,n=(r[s]=u).length-1,i[s]=n,e.currentKey=u[n].maxKey()),!0},x.compare=function(e,t,r){var i=e.height,n=e.currentKey,o=e.levelIndices,s=t.height,e=t.currentKey,t=t.levelIndices,n=r(e,n);if(0!==n)return n;n=i<s?i:s;return o.length-(i-n)-(t.length-(s-n))},x.prototype.keys=function(e){var t=this.entries(e,ReusedArray);return iterator(function(){var e=t.next();return e.value&&(e.value=e.value[0]),e})},x.prototype.values=function(e){var t=this.entries(e,ReusedArray);return iterator(function(){var e=t.next();return e.value&&(e.value=e.value[1]),e})},Object.defineProperty(x.prototype,"maxNodeSize",{get:function(){return this._maxNodeSize},enumerable:!1,configurable:!0}),x.prototype.minKey=function(){return this._root.minKey()},x.prototype.maxKey=function(){return this._root.maxKey()},x.prototype.clone=function(){this._root.isShared=!0;var e=new x(void 0,this._compare,this._maxNodeSize);return e._root=this._root,e._size=this._size,e},x.prototype.greedyClone=function(e){var t=new x(void 0,this._compare,this._maxNodeSize);return t._root=this._root.greedyClone(e),t._size=this._size,t},x.prototype.toArray=function(e){void 0===e&&(e=2147483647);var t=this.minKey(),r=this.maxKey();return void 0!==t?this.getRange(t,r,!0,e):[]},x.prototype.keysArray=function(){var r=[];return this._root.forRange(this.minKey(),this.maxKey(),!0,!1,this,0,function(e,t){r.push(e)}),r},x.prototype.valuesArray=function(){var r=[];return this._root.forRange(this.minKey(),this.maxKey(),!0,!1,this,0,function(e,t){r.push(t)}),r},x.prototype.toString=function(){return this.toArray().toString()},x.prototype.setIfNotPresent=function(e,t){return this.set(e,t,!1)},x.prototype.nextHigherPair=function(e,t){return t=t||[],void 0===e?this._root.minPair(t):this._root.getPairOrNextHigher(e,this._compare,!1,t)},x.prototype.nextHigherKey=function(e){e=this.nextHigherPair(e,ReusedArray);return e&&e[0]},x.prototype.nextLowerPair=function(e,t){return t=t||[],void 0===e?this._root.maxPair(t):this._root.getPairOrNextLower(e,this._compare,!1,t)},x.prototype.nextLowerKey=function(e){e=this.nextLowerPair(e,ReusedArray);return e&&e[0]},x.prototype.getPairOrNextLower=function(e,t){return this._root.getPairOrNextLower(e,this._compare,!0,t||[])},x.prototype.getPairOrNextHigher=function(e,t){return this._root.getPairOrNextHigher(e,this._compare,!0,t||[])},x.prototype.changeIfPresent=function(e,r){return 0!==this.editRange(e,e,!0,function(e,t){return{value:r}})},x.prototype.getRange=function(e,t,r,i){void 0===i&&(i=67108863);var n=[];return this._root.forRange(e,t,r,!1,this,0,function(e,t){return n.push([e,t]),n.length>i?Break:void 0}),n},x.prototype.setPairs=function(e,t){for(var r=0,i=0;i<e.length;i++)this.set(e[i][0],e[i][1],t)&&r++;return r},x.prototype.forRange=function(e,t,r,i,n){i=this._root.forRange(e,t,r,!1,this,n||0,i);return"number"==typeof i?i:i.break},x.prototype.editRange=function(e,t,r,i,n){var o=this._root;o.isShared&&(this._root=o=o.clone());try{var s=o.forRange(e,t,r,!0,this,n||0,i);return"number"==typeof s?s:s.break}finally{for(var a=void 0;o.keys.length<=1&&!o.isLeaf;)a=a||o.isShared,this._root=o=0===o.keys.length?EmptyLeaf:o.children[0];a&&(o.isShared=!0)}},x.prototype.editAll=function(e,t){return this.editRange(this.minKey(),this.maxKey(),!0,e,t)},x.prototype.deleteRange=function(e,t,r){return this.editRange(e,t,r,DeleteRange)},x.prototype.deleteKeys=function(e){for(var t=0,r=0;t<e.length;t++)this.delete(e[t])&&r++;return r},Object.defineProperty(x.prototype,"height",{get:function(){for(var e=this._root,t=-1;e;)t++,e=e.isLeaf?void 0:e.children[0];return t},enumerable:!1,configurable:!0}),x.prototype.freeze=function(){this.clear=this.set=this.editRange=function(){throw new Error("Attempted to modify a frozen BTree")}},x.prototype.unfreeze=function(){delete this.clear,delete this.set,delete this.editRange},Object.defineProperty(x.prototype,"isFrozen",{get:function(){return this.hasOwnProperty("editRange")},enumerable:!1,configurable:!0}),x.prototype.checkValid=function(){var e=this._root.checkValid(0,this,0);check(e===this.size,"size mismatch: counted ",e,"but stored",this.size)},x}();function iterator(e){void 0===e&&(e=function(){return{done:!0,value:void 0}});e={next:e};return Symbol&&Symbol.iterator&&(e[Symbol.iterator]=function(){return this}),e}exports.default=BTree,Symbol&&Symbol.iterator&&(BTree.prototype[Symbol.iterator]=BTree.prototype.entries),BTree.prototype.where=BTree.prototype.filter,BTree.prototype.setRange=BTree.prototype.setPairs,BTree.prototype.add=BTree.prototype.set;var BNode=function(){function t(e,t){void 0===e&&(e=[]),this.keys=e,this.values=t||undefVals,this.isShared=void 0}return Object.defineProperty(t.prototype,"isLeaf",{get:function(){return void 0===this.children},enumerable:!1,configurable:!0}),t.prototype.maxKey=function(){return this.keys[this.keys.length-1]},t.prototype.indexOf=function(e,t,r){for(var i=this.keys,n=0,o=i.length,s=o>>1;n<o;){var a=r(i[s],e);if(a<0)n=s+1;else{if(!(0<a)){if(0===a)return s;if(e==e)return i.length;throw new Error("BTree: NaN was used as a key")}o=s}s=n+o>>1}return s^t},t.prototype.minKey=function(){return this.keys[0]},t.prototype.minPair=function(e){if(0!==this.keys.length)return e[0]=this.keys[0],e[1]=this.values[0],e},t.prototype.maxPair=function(e){if(0!==this.keys.length){var t=this.keys.length-1;return e[0]=this.keys[t],e[1]=this.values[t],e}},t.prototype.clone=function(){var e=this.values;return new t(this.keys.slice(0),e===undefVals?e:e.slice(0))},t.prototype.greedyClone=function(e){return this.isShared&&!e?this:this.clone()},t.prototype.get=function(e,t,r){r=this.indexOf(e,-1,r._compare);return r<0?t:this.values[r]},t.prototype.getPairOrNextLower=function(e,t,r,i){t=this.indexOf(e,-1,t),t=t<0?~t-1:r?t:t-1;if(0<=t)return i[0]=this.keys[t],i[1]=this.values[t],i},t.prototype.getPairOrNextHigher=function(e,t,r,i){t=this.indexOf(e,-1,t),r=t<0?~t:r?t:t+1,t=this.keys;if(r<t.length)return i[0]=t[r],i[1]=this.values[r],i},t.prototype.checkValid=function(e,t,r){var i=this.keys.length,n=this.values.length;return check(this.values===undefVals?i<=n:i===n,"keys/values length mismatch: depth",e,"with lengths",i,n,"and baseIndex",r),check(0==e||0<i,"empty leaf at depth",e,"and baseIndex",r),i},t.prototype.set=function(e,t,r,i){var n=this.indexOf(e,-1,i._compare);if(n<0){if(n=~n,i._size++,this.keys.length<i._maxNodeSize)return this.insertInLeaf(n,e,t,i);var o=this.splitOffRightSide(),s=this;return n>this.keys.length&&(n-=this.keys.length,s=o),s.insertInLeaf(n,e,t,i),o}return!1!==r&&(void 0!==t&&this.reifyValues(),this.keys[n]=e,this.values[n]=t),!1},t.prototype.reifyValues=function(){return this.values===undefVals?this.values=this.values.slice(0,this.keys.length):this.values},t.prototype.insertInLeaf=function(e,t,r,i){if(this.keys.splice(e,0,t),this.values===undefVals){for(;undefVals.length<i._maxNodeSize;)undefVals.push(void 0);if(void 0===r)return!0;this.values=undefVals.slice(0,this.keys.length-1)}return this.values.splice(e,0,r),!0},t.prototype.takeFromRight=function(e){var t=this.values;e.values===undefVals?t!==undefVals&&t.push(void 0):(t=this.reifyValues()).push(e.values.shift()),this.keys.push(e.keys.shift())},t.prototype.takeFromLeft=function(e){var t=this.values;e.values===undefVals?t!==undefVals&&t.unshift(void 0):(t=this.reifyValues()).unshift(e.values.pop()),this.keys.unshift(e.keys.pop())},t.prototype.splitOffRightSide=function(){var e=this.keys.length>>1;return new t(this.keys.splice(e),this.values===undefVals?undefVals:this.values.splice(e))},t.prototype.forRange=function(e,t,r,i,n,o,s){var a,h,u=n._compare;if(t===e){if(!r)return o;if(h=(a=this.indexOf(e,-1,u))+1,a<0)return o}else a=this.indexOf(e,0,u),(h=this.indexOf(t,-1,u))<0?h=~h:!0===r&&h++;var l=this.keys,f=this.values;if(void 0!==s)for(var p=a;p<h;p++){var c=l[p],y=s(c,f[p],o++);if(void 0!==y){if(!0===i){if(c!==l[p]||!0===this.isShared)throw new Error("BTree illegally changed or cloned in editRange");y.delete?(this.keys.splice(p,1),this.values!==undefVals&&this.values.splice(p,1),n._size--,p--,h--):y.hasOwnProperty("value")&&(f[p]=y.value)}if(void 0!==y.break)return y}}else o+=h-a;return o},t.prototype.mergeSibling=function(e,t){if(this.keys.push.apply(this.keys,e.keys),this.values===undefVals){if(e.values===undefVals)return;this.values=this.values.slice(0,this.keys.length)}this.values.push.apply(this.values,e.reifyValues())},t}(),BNodeInternal=function(n){function i(e,t){var r=this;if(!t){t=[];for(var i=0;i<e.length;i++)t[i]=e[i].maxKey()}return(r=n.call(this,t)||this).children=e,r}return __extends(i,n),i.prototype.clone=function(){for(var e=this.children.slice(0),t=0;t<e.length;t++)e[t].isShared=!0;return new i(e,this.keys.slice(0))},i.prototype.greedyClone=function(e){if(this.isShared&&!e)return this;for(var t=new i(this.children.slice(0),this.keys.slice(0)),r=0;r<t.children.length;r++)t.children[r]=t.children[r].greedyClone();return t},i.prototype.minKey=function(){return this.children[0].minKey()},i.prototype.minPair=function(e){return this.children[0].minPair(e)},i.prototype.maxPair=function(e){return this.children[this.children.length-1].maxPair(e)},i.prototype.get=function(e,t,r){var i=this.indexOf(e,0,r._compare),n=this.children;return i<n.length?n[i].get(e,t,r):void 0},i.prototype.getPairOrNextLower=function(e,t,r,i){var n=this.indexOf(e,0,t),o=this.children;if(n>=o.length)return this.maxPair(i);r=o[n].getPairOrNextLower(e,t,r,i);return void 0===r&&0<n?o[n-1].maxPair(i):r},i.prototype.getPairOrNextHigher=function(e,t,r,i){var n=this.indexOf(e,0,t),o=this.children,s=o.length;if(!(s<=n)){r=o[n].getPairOrNextHigher(e,t,r,i);return void 0===r&&n<s-1?o[n+1].minPair(i):r}},i.prototype.checkValid=function(e,t,r){var i=this.keys.length,n=this.children.length;check(i===n,"keys/children length mismatch: depth",e,"lengths",i,n,"baseIndex",r),check(1<i||0<e,"internal node has length",i,"at depth",e,"baseIndex",r);for(var o=0,s=this.children,a=this.keys,h=0,u=0;u<n;u++)o+=s[u].checkValid(e+1,t,r+o),check((h+=s[u].keys.length)<=o,"wtf",r),check(0===u||s[u-1].constructor===s[u].constructor,"type mismatch, baseIndex:",r),s[u].maxKey()!=a[u]&&check(!1,"keys[",u,"] =",a[u],"is wrong, should be ",s[u].maxKey(),"at depth",e,"baseIndex",r),0===u||t._compare(a[u-1],a[u])<0||check(!1,"sort violation at depth",e,"index",u,"keys",a[u-1],a[u]);i=0===h;return(i||h>t.maxNodeSize*n)&&check(!1,i?"too few":"too many","children (",h,o,") at depth",e,"maxNodeSize:",t.maxNodeSize,"children.length:",n,"baseIndex:",r),o},i.prototype.set=function(e,t,r,i){var n,o=this.children,s=i._maxNodeSize,a=i._compare,h=Math.min(this.indexOf(e,0,a),o.length-1),u=o[h];u.isShared&&(o[h]=u=u.clone()),u.keys.length>=s&&(0<h&&(n=o[h-1]).keys.length<s&&a(u.keys[0],e)<0?(n.isShared&&(o[h-1]=n=n.clone()),n.takeFromRight(u),this.keys[h-1]=n.maxKey()):void 0!==(n=o[h+1])&&n.keys.length<s&&a(u.maxKey(),e)<0&&(n.isShared&&(o[h+1]=n=n.clone()),n.takeFromLeft(u),this.keys[h]=o[h].maxKey()));i=u.set(e,t,r,i);if(!1===i)return!1;if(this.keys[h]=u.maxKey(),!0===i)return!0;if(this.keys.length<s)return this.insert(h+1,i),!0;u=this.splitOffRightSide(),s=this;return 0<a(i.maxKey(),this.maxKey())&&(s=u,h-=this.keys.length),s.insert(h+1,i),u},i.prototype.insert=function(e,t){this.children.splice(e,0,t),this.keys.splice(e,0,t.maxKey())},i.prototype.splitOffRightSide=function(){var e=this.children.length>>1;return new i(this.children.splice(e),this.keys.splice(e))},i.prototype.takeFromRight=function(e){this.keys.push(e.keys.shift()),this.children.push(e.children.shift())},i.prototype.takeFromLeft=function(e){this.keys.unshift(e.keys.pop()),this.children.unshift(e.children.pop())},i.prototype.forRange=function(e,t,r,i,n,o,s){var a=n._compare,h=this.keys,u=this.children,l=this.indexOf(e,0,a),f=l,p=Math.min(t===e?l:this.indexOf(t,0,a),h.length-1);if(i){if(f<=p)try{for(;f<=p;f++){u[f].isShared&&(u[f]=u[f].clone());var c=u[f].forRange(e,t,r,i,n,o,s);if(h[f]=u[f].maxKey(),"number"!=typeof c)return c;o=c}}finally{var y=n._maxNodeSize>>1;for(0<l&&l--,f=p;l<=f;f--)u[f].keys.length<=y&&(0!==u[f].keys.length?this.tryMerge(f,n._maxNodeSize):(h.splice(f,1),u.splice(f,1)));0!==u.length&&0===u[0].keys.length&&check(!1,"emptiness bug")}}else for(;f<=p;f++){if("number"!=typeof(c=u[f].forRange(e,t,r,i,n,o,s)))return c;o=c}return o},i.prototype.tryMerge=function(e,t){var r=this.children;return 0<=e&&e+1<r.length&&r[e].keys.length+r[e+1].keys.length<=t&&(r[e].isShared&&(r[e]=r[e].clone()),r[e].mergeSibling(r[e+1],t),r.splice(e+1,1),this.keys.splice(e+1,1),this.keys[e]=r[e].maxKey(),!0)},i.prototype.mergeSibling=function(e,t){var r=this.keys.length;this.keys.push.apply(this.keys,e.keys),this.children.push.apply(this.children,e.children),this.tryMerge(r-1,t)},i}(BNode),undefVals=[],Delete={delete:!0},DeleteRange=function(){return Delete},Break={break:!0},EmptyLeaf=function(){var e=new BNode;return e.isShared=!0,e}(),EmptyArray=[],ReusedArray=[];function check(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];if(!e)throw t.unshift("B+ tree"),new Error(t.join(" "))}exports.EmptyBTree=function(){var e=new BTree;return e.freeze(),e}();
"use strict";var __extends=this&&this.__extends||function(){var i=function(e,t){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])})(e,t)};return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}i(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}}();function defaultComparator(e,t){if(Number.isFinite(e)&&Number.isFinite(t))return e-t;var r=typeof e,i=typeof t;if(r!==i)return r<i?-1:1;if("object"===r){if(null===e)return null===t?0:-1;if(null===t)return 1;if((r=typeof(e=e.valueOf()))!==(i=typeof(t=t.valueOf())))return r<i?-1:1}return e<t?-1:t<e?1:e===t?0:Number.isNaN(e)?Number.isNaN(t)?0:-1:Number.isNaN(t)?1:Array.isArray(e)?0:Number.NaN}function simpleComparator(e,t){return t<e?1:e<t?-1:0}Object.defineProperty(exports,"__esModule",{value:!0}),exports.EmptyBTree=exports.simpleComparator=exports.defaultComparator=void 0,exports.defaultComparator=defaultComparator,exports.simpleComparator=simpleComparator;var BTree=function(){function x(e,t,r){this._root=EmptyLeaf,this._size=0,this._maxNodeSize=4<=r?Math.min(r,256):32,this._compare=t||defaultComparator,e&&this.setPairs(e)}return Object.defineProperty(x.prototype,"size",{get:function(){return this._size},enumerable:!1,configurable:!0}),Object.defineProperty(x.prototype,"length",{get:function(){return this._size},enumerable:!1,configurable:!0}),Object.defineProperty(x.prototype,"isEmpty",{get:function(){return 0===this._size},enumerable:!1,configurable:!0}),x.prototype.clear=function(){this._root=EmptyLeaf,this._size=0},x.prototype.forEach=function(r,e){var i=this;return void 0!==e&&(r=r.bind(e)),this.forEachPair(function(e,t){return r(t,e,i)})},x.prototype.forEachPair=function(e,t){var r=this.minKey(),i=this.maxKey();return this.forRange(r,i,!0,e,t)},x.prototype.get=function(e,t){return this._root.get(e,t,this)},x.prototype.set=function(e,t,r){this._root.isShared&&(this._root=this._root.clone());r=this._root.set(e,t,r,this);return!0===r||!1===r?r:(this._root=new BNodeInternal([this._root,r]),!0)},x.prototype.has=function(e){return 0!==this.forRange(e,e,!0,void 0)},x.prototype.delete=function(e){return 0!==this.editRange(e,e,!0,DeleteRange)},x.prototype.with=function(e,t,r){var i=this.clone();return i.set(e,t,r)||r?i:this},x.prototype.withPairs=function(e,t){var r=this.clone();return 0!==r.setPairs(e,t)||t?r:this},x.prototype.withKeys=function(e,t){for(var r=this.clone(),i=!1,n=0;n<e.length;n++)i=r.set(e[n],void 0,!1)||i;return t&&!i?this:r},x.prototype.without=function(e,t){return this.withoutRange(e,e,!0,t)},x.prototype.withoutKeys=function(e,t){var r=this.clone();return r.deleteKeys(e)||!t?r:this},x.prototype.withoutRange=function(e,t,r,i){var n=this.clone();return 0===n.deleteRange(e,t,r)&&i?this:n},x.prototype.filter=function(i,e){var n,t=this.greedyClone();return t.editAll(function(e,t,r){if(!i(e,t,r))return n=Delete}),!n&&e?this:t},x.prototype.mapValues=function(i){var n={},e=this.greedyClone();return e.editAll(function(e,t,r){return n.value=i(t,e,r),n}),e},x.prototype.reduce=function(e,t){for(var r,i=0,n=t,o=this.entries(this.minKey(),ReusedArray);!(r=o.next()).done;)n=e(n,r.value,i++,this);return n},x.prototype.entries=function(e,t){var r=this.findPath(e);if(void 0===r)return iterator();var i=r.nodequeue,n=r.nodeindex,o=r.leaf,s=void 0!==t?1:0,a=void 0===e?-1:o.indexOf(e,0,this._compare)-1;return iterator(function(){e:for(;;)switch(s){case 0:if(++a<o.keys.length)return{done:!1,value:[o.keys[a],o.values[a]]};s=2;continue;case 1:if(++a<o.keys.length)return t[0]=o.keys[a],t[1]=o.values[a],{done:!1,value:t};s=2;case 2:for(var e=-1;;){if(++e>=i.length){s=3;continue e}if(++n[e]<i[e].length)break}for(;0<e;e--)i[e-1]=i[e][n[e]].children,n[e-1]=0;o=i[0][n[0]],a=-1,s=void 0!==t?1:0;continue;case 3:return{done:!0,value:void 0}}})},x.prototype.entriesReversed=function(e,t,r){if(void 0===e&&(r=void 0)===(e=this.maxKey()))return iterator();var i=this.findPath(e)||this.findPath(this.maxKey()),n=i.nodequeue,o=i.nodeindex,s=i.leaf;check(!n[0]||s===n[0][o[0]],"wat!");var a=s.indexOf(e,0,this._compare);!r&&a<s.keys.length&&this._compare(s.keys[a],e)<=0&&a++;var h=void 0!==t?1:0;return iterator(function(){e:for(;;)switch(h){case 0:if(0<=--a)return{done:!1,value:[s.keys[a],s.values[a]]};h=2;continue;case 1:if(0<=--a)return t[0]=s.keys[a],t[1]=s.values[a],{done:!1,value:t};h=2;case 2:for(var e=-1;;){if(++e>=n.length){h=3;continue e}if(0<=--o[e])break}for(;0<e;e--)n[e-1]=n[e][o[e]].children,o[e-1]=n[e-1].length-1;s=n[0][o[0]],a=s.keys.length,h=void 0!==t?1:0;continue;case 3:return{done:!0,value:void 0}}})},x.prototype.findPath=function(e){var t,r,i=this._root;if(i.isLeaf)r=t=EmptyArray;else{t=[],r=[];for(var n=0;!i.isLeaf;n++){if(t[n]=i.children,r[n]=void 0===e?0:i.indexOf(e,0,this._compare),r[n]>=t[n].length)return;i=t[n][r[n]]}t.reverse(),r.reverse()}return{nodequeue:t,nodeindex:r,leaf:i}},x.prototype.diffAgainst=function(e,t,r,i){if(e._compare!==this._compare)throw new Error("Tree comparators are not the same.");if(this.isEmpty||e.isEmpty)return this.isEmpty&&e.isEmpty?void 0:this.isEmpty?void 0===r?void 0:x.stepToEnd(x.makeDiffCursor(e),r):void 0===t?void 0:x.stepToEnd(x.makeDiffCursor(this),t);for(var n=this._compare,o=x.makeDiffCursor(this),s=x.makeDiffCursor(e),a=!0,h=!0,u=x.compare(o,s,n);a&&h;){var l=x.compare(o,s,n),f=o.leaf,p=o.internalSpine,c=o.levelIndices,y=s.leaf,d=s.internalSpine,v=s.levelIndices;if(f||y){if(0!==u)if(0===l){if(f&&y&&i){var g=f.values[c[c.length-1]],m=y.values[v[v.length-1]];if(!Object.is(g,m))if((k=i(o.currentKey,g,m))&&k.break)return k.break}}else if(0<l){if(y&&r){m=y.values[v[v.length-1]];if((k=r(s.currentKey,m))&&k.break)return k.break}}else if(t&&f&&0!==u){var k,g=f.values[c[c.length-1]];if((k=t(o.currentKey,g))&&k.break)return k.break}}else if(!f&&!y&&0===l){f=p.length-1,y=d.length-1,f=p[f][c[f]];if(d[y][v[y]]===f){a=x.step(o,!(u=0)),h=x.step(s,!0);continue}}(u=l)<0?a=x.step(o):h=x.step(s)}return a&&t?x.finishCursorWalk(o,s,n,t):h&&r?x.finishCursorWalk(s,o,n,r):void 0},x.finishCursorWalk=function(e,t,r,i){r=x.compare(e,t,r);if(0===r){if(!x.step(e))return}else r<0&&check(!1,"cursor walk terminated early");return x.stepToEnd(e,i)},x.stepToEnd=function(e,t){for(var r=!0;r;){var i=e.leaf,n=e.levelIndices,o=e.currentKey;if(i){n=t(o,i.values[n[n.length-1]]);if(n&&n.break)return n.break}r=x.step(e)}},x.makeDiffCursor=function(e){var t=e._root;return{height:e.height,internalSpine:[[t]],levelIndices:[0],leaf:void 0,currentKey:t.maxKey()}},x.step=function(e,t){var r=e.internalSpine,i=e.levelIndices,n=e.leaf;if(!0===t||n){var o=i.length;if(!0===t||0===i[o-1]){var s=r.length;if(0===s)return!1;for(var a=s-1,h=a;0<=h;){if(0<i[h])return h<o-1&&(e.leaf=void 0,i.pop()),h<a&&(e.internalSpine=r.slice(0,h+1)),e.currentKey=r[h][--i[h]].maxKey(),!0;h--}return!1}var u=--i[o-1];return e.currentKey=n.keys[u],!0}s=r.length,n=s-1,n=r[n][i[n]];return n.isLeaf?(e.leaf=n,u=i[s]=n.values.length-1,e.currentKey=n.keys[u]):(u=n.children,n=(r[s]=u).length-1,i[s]=n,e.currentKey=u[n].maxKey()),!0},x.compare=function(e,t,r){var i=e.height,n=e.currentKey,o=e.levelIndices,s=t.height,e=t.currentKey,t=t.levelIndices,n=r(e,n);if(0!==n)return n;n=i<s?i:s;return o.length-(i-n)-(t.length-(s-n))},x.prototype.keys=function(e){var t=this.entries(e,ReusedArray);return iterator(function(){var e=t.next();return e.value&&(e.value=e.value[0]),e})},x.prototype.values=function(e){var t=this.entries(e,ReusedArray);return iterator(function(){var e=t.next();return e.value&&(e.value=e.value[1]),e})},Object.defineProperty(x.prototype,"maxNodeSize",{get:function(){return this._maxNodeSize},enumerable:!1,configurable:!0}),x.prototype.minKey=function(){return this._root.minKey()},x.prototype.maxKey=function(){return this._root.maxKey()},x.prototype.clone=function(){this._root.isShared=!0;var e=new x(void 0,this._compare,this._maxNodeSize);return e._root=this._root,e._size=this._size,e},x.prototype.greedyClone=function(e){var t=new x(void 0,this._compare,this._maxNodeSize);return t._root=this._root.greedyClone(e),t._size=this._size,t},x.prototype.toArray=function(e){void 0===e&&(e=2147483647);var t=this.minKey(),r=this.maxKey();return void 0!==t?this.getRange(t,r,!0,e):[]},x.prototype.keysArray=function(){var r=[];return this._root.forRange(this.minKey(),this.maxKey(),!0,!1,this,0,function(e,t){r.push(e)}),r},x.prototype.valuesArray=function(){var r=[];return this._root.forRange(this.minKey(),this.maxKey(),!0,!1,this,0,function(e,t){r.push(t)}),r},x.prototype.toString=function(){return this.toArray().toString()},x.prototype.setIfNotPresent=function(e,t){return this.set(e,t,!1)},x.prototype.nextHigherPair=function(e,t){return t=t||[],void 0===e?this._root.minPair(t):this._root.getPairOrNextHigher(e,this._compare,!1,t)},x.prototype.nextHigherKey=function(e){e=this.nextHigherPair(e,ReusedArray);return e&&e[0]},x.prototype.nextLowerPair=function(e,t){return t=t||[],void 0===e?this._root.maxPair(t):this._root.getPairOrNextLower(e,this._compare,!1,t)},x.prototype.nextLowerKey=function(e){e=this.nextLowerPair(e,ReusedArray);return e&&e[0]},x.prototype.getPairOrNextLower=function(e,t){return this._root.getPairOrNextLower(e,this._compare,!0,t||[])},x.prototype.getPairOrNextHigher=function(e,t){return this._root.getPairOrNextHigher(e,this._compare,!0,t||[])},x.prototype.changeIfPresent=function(e,r){return 0!==this.editRange(e,e,!0,function(e,t){return{value:r}})},x.prototype.getRange=function(e,t,r,i){void 0===i&&(i=67108863);var n=[];return this._root.forRange(e,t,r,!1,this,0,function(e,t){return n.push([e,t]),n.length>i?Break:void 0}),n},x.prototype.setPairs=function(e,t){for(var r=0,i=0;i<e.length;i++)this.set(e[i][0],e[i][1],t)&&r++;return r},x.prototype.forRange=function(e,t,r,i,n){i=this._root.forRange(e,t,r,!1,this,n||0,i);return"number"==typeof i?i:i.break},x.prototype.editRange=function(e,t,r,i,n){var o=this._root;o.isShared&&(this._root=o=o.clone());try{var s=o.forRange(e,t,r,!0,this,n||0,i);return"number"==typeof s?s:s.break}finally{for(var a=void 0;o.keys.length<=1&&!o.isLeaf;)a=a||o.isShared,this._root=o=0===o.keys.length?EmptyLeaf:o.children[0];a&&(o.isShared=!0)}},x.prototype.editAll=function(e,t){return this.editRange(this.minKey(),this.maxKey(),!0,e,t)},x.prototype.deleteRange=function(e,t,r){return this.editRange(e,t,r,DeleteRange)},x.prototype.deleteKeys=function(e){for(var t=0,r=0;t<e.length;t++)this.delete(e[t])&&r++;return r},Object.defineProperty(x.prototype,"height",{get:function(){for(var e=this._root,t=-1;e;)t++,e=e.isLeaf?void 0:e.children[0];return t},enumerable:!1,configurable:!0}),x.prototype.freeze=function(){this.clear=this.set=this.editRange=function(){throw new Error("Attempted to modify a frozen BTree")}},x.prototype.unfreeze=function(){delete this.clear,delete this.set,delete this.editRange},Object.defineProperty(x.prototype,"isFrozen",{get:function(){return this.hasOwnProperty("editRange")},enumerable:!1,configurable:!0}),x.prototype.checkValid=function(){var e=this._root.checkValid(0,this,0);check(e===this.size,"size mismatch: counted ",e,"but stored",this.size)},x}();function iterator(e){void 0===e&&(e=function(){return{done:!0,value:void 0}});e={next:e};return Symbol&&Symbol.iterator&&(e[Symbol.iterator]=function(){return this}),e}exports.default=BTree,Symbol&&Symbol.iterator&&(BTree.prototype[Symbol.iterator]=BTree.prototype.entries),BTree.prototype.where=BTree.prototype.filter,BTree.prototype.setRange=BTree.prototype.setPairs,BTree.prototype.add=BTree.prototype.set;var BNode=function(){function t(e,t){void 0===e&&(e=[]),this.keys=e,this.values=t||undefVals,this.isShared=void 0}return Object.defineProperty(t.prototype,"isLeaf",{get:function(){return void 0===this.children},enumerable:!1,configurable:!0}),t.prototype.maxKey=function(){return this.keys[this.keys.length-1]},t.prototype.indexOf=function(e,t,r){for(var i=this.keys,n=0,o=i.length,s=o>>1;n<o;){var a=r(i[s],e);if(a<0)n=s+1;else{if(!(0<a)){if(0===a)return s;if(e==e)return i.length;throw new Error("BTree: NaN was used as a key")}o=s}s=n+o>>1}return s^t},t.prototype.minKey=function(){return this.keys[0]},t.prototype.minPair=function(e){if(0!==this.keys.length)return e[0]=this.keys[0],e[1]=this.values[0],e},t.prototype.maxPair=function(e){if(0!==this.keys.length){var t=this.keys.length-1;return e[0]=this.keys[t],e[1]=this.values[t],e}},t.prototype.clone=function(){var e=this.values;return new t(this.keys.slice(0),e===undefVals?e:e.slice(0))},t.prototype.greedyClone=function(e){return this.isShared&&!e?this:this.clone()},t.prototype.get=function(e,t,r){r=this.indexOf(e,-1,r._compare);return r<0?t:this.values[r]},t.prototype.getPairOrNextLower=function(e,t,r,i){t=this.indexOf(e,-1,t),t=t<0?~t-1:r?t:t-1;if(0<=t)return i[0]=this.keys[t],i[1]=this.values[t],i},t.prototype.getPairOrNextHigher=function(e,t,r,i){t=this.indexOf(e,-1,t),r=t<0?~t:r?t:t+1,t=this.keys;if(r<t.length)return i[0]=t[r],i[1]=this.values[r],i},t.prototype.checkValid=function(e,t,r){var i=this.keys.length,n=this.values.length;return check(this.values===undefVals?i<=n:i===n,"keys/values length mismatch: depth",e,"with lengths",i,n,"and baseIndex",r),check(0==e||0<i,"empty leaf at depth",e,"and baseIndex",r),i},t.prototype.set=function(e,t,r,i){var n=this.indexOf(e,-1,i._compare);if(n<0){if(n=~n,i._size++,this.keys.length<i._maxNodeSize)return this.insertInLeaf(n,e,t,i);var o=this.splitOffRightSide(),s=this;return n>this.keys.length&&(n-=this.keys.length,s=o),s.insertInLeaf(n,e,t,i),o}return!1!==r&&(void 0!==t&&this.reifyValues(),this.keys[n]=e,this.values[n]=t),!1},t.prototype.reifyValues=function(){return this.values===undefVals?this.values=this.values.slice(0,this.keys.length):this.values},t.prototype.insertInLeaf=function(e,t,r,i){if(this.keys.splice(e,0,t),this.values===undefVals){for(;undefVals.length<i._maxNodeSize;)undefVals.push(void 0);if(void 0===r)return!0;this.values=undefVals.slice(0,this.keys.length-1)}return this.values.splice(e,0,r),!0},t.prototype.takeFromRight=function(e){var t=this.values;e.values===undefVals?t!==undefVals&&t.push(void 0):(t=this.reifyValues()).push(e.values.shift()),this.keys.push(e.keys.shift())},t.prototype.takeFromLeft=function(e){var t=this.values;e.values===undefVals?t!==undefVals&&t.unshift(void 0):(t=this.reifyValues()).unshift(e.values.pop()),this.keys.unshift(e.keys.pop())},t.prototype.splitOffRightSide=function(){var e=this.keys.length>>1;return new t(this.keys.splice(e),this.values===undefVals?undefVals:this.values.splice(e))},t.prototype.forRange=function(e,t,r,i,n,o,s){var a,h,u=n._compare;if(t===e){if(!r)return o;if(h=(a=this.indexOf(e,-1,u))+1,a<0)return o}else a=this.indexOf(e,0,u),(h=this.indexOf(t,-1,u))<0?h=~h:!0===r&&h++;var l=this.keys,f=this.values;if(void 0!==s)for(var p=a;p<h;p++){var c=l[p],y=s(c,f[p],o++);if(void 0!==y){if(!0===i){if(c!==l[p]||!0===this.isShared)throw new Error("BTree illegally changed or cloned in editRange");y.delete?(this.keys.splice(p,1),this.values!==undefVals&&this.values.splice(p,1),n._size--,p--,h--):y.hasOwnProperty("value")&&(f[p]=y.value)}if(void 0!==y.break)return y}}else o+=h-a;return o},t.prototype.mergeSibling=function(e,t){if(this.keys.push.apply(this.keys,e.keys),this.values===undefVals){if(e.values===undefVals)return;this.values=this.values.slice(0,this.keys.length)}this.values.push.apply(this.values,e.reifyValues())},t}(),BNodeInternal=function(n){function i(e,t){var r=this;if(!t){t=[];for(var i=0;i<e.length;i++)t[i]=e[i].maxKey()}return(r=n.call(this,t)||this).children=e,r}return __extends(i,n),i.prototype.clone=function(){for(var e=this.children.slice(0),t=0;t<e.length;t++)e[t].isShared=!0;return new i(e,this.keys.slice(0))},i.prototype.greedyClone=function(e){if(this.isShared&&!e)return this;for(var t=new i(this.children.slice(0),this.keys.slice(0)),r=0;r<t.children.length;r++)t.children[r]=t.children[r].greedyClone(e);return t},i.prototype.minKey=function(){return this.children[0].minKey()},i.prototype.minPair=function(e){return this.children[0].minPair(e)},i.prototype.maxPair=function(e){return this.children[this.children.length-1].maxPair(e)},i.prototype.get=function(e,t,r){var i=this.indexOf(e,0,r._compare),n=this.children;return i<n.length?n[i].get(e,t,r):void 0},i.prototype.getPairOrNextLower=function(e,t,r,i){var n=this.indexOf(e,0,t),o=this.children;if(n>=o.length)return this.maxPair(i);r=o[n].getPairOrNextLower(e,t,r,i);return void 0===r&&0<n?o[n-1].maxPair(i):r},i.prototype.getPairOrNextHigher=function(e,t,r,i){var n=this.indexOf(e,0,t),o=this.children,s=o.length;if(!(s<=n)){r=o[n].getPairOrNextHigher(e,t,r,i);return void 0===r&&n<s-1?o[n+1].minPair(i):r}},i.prototype.checkValid=function(e,t,r){var i=this.keys.length,n=this.children.length;check(i===n,"keys/children length mismatch: depth",e,"lengths",i,n,"baseIndex",r),check(1<i||0<e,"internal node has length",i,"at depth",e,"baseIndex",r);for(var o=0,s=this.children,a=this.keys,h=0,u=0;u<n;u++)o+=s[u].checkValid(e+1,t,r+o),check((h+=s[u].keys.length)<=o,"wtf",r),check(0===u||s[u-1].constructor===s[u].constructor,"type mismatch, baseIndex:",r),s[u].maxKey()!=a[u]&&check(!1,"keys[",u,"] =",a[u],"is wrong, should be ",s[u].maxKey(),"at depth",e,"baseIndex",r),0===u||t._compare(a[u-1],a[u])<0||check(!1,"sort violation at depth",e,"index",u,"keys",a[u-1],a[u]);i=0===h;return(i||h>t.maxNodeSize*n)&&check(!1,i?"too few":"too many","children (",h,o,") at depth",e,"maxNodeSize:",t.maxNodeSize,"children.length:",n,"baseIndex:",r),o},i.prototype.set=function(e,t,r,i){var n,o=this.children,s=i._maxNodeSize,a=i._compare,h=Math.min(this.indexOf(e,0,a),o.length-1),u=o[h];u.isShared&&(o[h]=u=u.clone()),u.keys.length>=s&&(0<h&&(n=o[h-1]).keys.length<s&&a(u.keys[0],e)<0?(n.isShared&&(o[h-1]=n=n.clone()),n.takeFromRight(u),this.keys[h-1]=n.maxKey()):void 0!==(n=o[h+1])&&n.keys.length<s&&a(u.maxKey(),e)<0&&(n.isShared&&(o[h+1]=n=n.clone()),n.takeFromLeft(u),this.keys[h]=o[h].maxKey()));i=u.set(e,t,r,i);if(!1===i)return!1;if(this.keys[h]=u.maxKey(),!0===i)return!0;if(this.keys.length<s)return this.insert(h+1,i),!0;u=this.splitOffRightSide(),s=this;return 0<a(i.maxKey(),this.maxKey())&&(s=u,h-=this.keys.length),s.insert(h+1,i),u},i.prototype.insert=function(e,t){this.children.splice(e,0,t),this.keys.splice(e,0,t.maxKey())},i.prototype.splitOffRightSide=function(){var e=this.children.length>>1;return new i(this.children.splice(e),this.keys.splice(e))},i.prototype.takeFromRight=function(e){this.keys.push(e.keys.shift()),this.children.push(e.children.shift())},i.prototype.takeFromLeft=function(e){this.keys.unshift(e.keys.pop()),this.children.unshift(e.children.pop())},i.prototype.forRange=function(e,t,r,i,n,o,s){var a=n._compare,h=this.keys,u=this.children,l=this.indexOf(e,0,a),f=l,p=Math.min(t===e?l:this.indexOf(t,0,a),h.length-1);if(i){if(f<=p)try{for(;f<=p;f++){u[f].isShared&&(u[f]=u[f].clone());var c=u[f].forRange(e,t,r,i,n,o,s);if(h[f]=u[f].maxKey(),"number"!=typeof c)return c;o=c}}finally{var y=n._maxNodeSize>>1;for(0<l&&l--,f=p;l<=f;f--)u[f].keys.length<=y&&(0!==u[f].keys.length?this.tryMerge(f,n._maxNodeSize):(h.splice(f,1),u.splice(f,1)));0!==u.length&&0===u[0].keys.length&&check(!1,"emptiness bug")}}else for(;f<=p;f++){if("number"!=typeof(c=u[f].forRange(e,t,r,i,n,o,s)))return c;o=c}return o},i.prototype.tryMerge=function(e,t){var r=this.children;return 0<=e&&e+1<r.length&&r[e].keys.length+r[e+1].keys.length<=t&&(r[e].isShared&&(r[e]=r[e].clone()),r[e].mergeSibling(r[e+1],t),r.splice(e+1,1),this.keys.splice(e+1,1),this.keys[e]=r[e].maxKey(),!0)},i.prototype.mergeSibling=function(e,t){var r=this.keys.length;this.keys.push.apply(this.keys,e.keys);var i=e.children;if(this.children.push.apply(this.children,i),e.isShared&&!this.isShared)for(var n=0;n<i.length;n++)i[n].isShared=!0;this.tryMerge(r-1,t)},i}(BNode),undefVals=[],Delete={delete:!0},DeleteRange=function(){return Delete},Break={break:!0},EmptyLeaf=function(){var e=new BNode;return e.isShared=!0,e}(),EmptyArray=[],ReusedArray=[];function check(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];if(!e)throw t.unshift("B+ tree"),new Error(t.join(" "))}exports.EmptyBTree=function(){var e=new BTree;return e.freeze(),e}();
{
"name": "sorted-btree",
"version": "1.6.1",
"version": "1.6.2",
"description": "A sorted list of key-value pairs in a fast, typed in-memory B+ tree with a powerful API.",

@@ -5,0 +5,0 @@ "main": "b+tree.js",

@@ -26,6 +26,6 @@ B+ tree

root node as "shared between instances". This makes the tree read-only
with copy-on-edit behavior; both copies of the tree remain mutable.
I call this category of data structure "dynamically persistent" because
AFAIK no one else has given it a name; it walks the line between mutating
and [persistent](https://en.wikipedia.org/wiki/Persistent_data_structure).
with copy-on-edit behavior; both copies of the tree remain mutable. I call
this category of data structure "dynamically persistent" or "mutably
persistent" because AFAIK no one else has given it a name; it walks the line
between mutating and [persistent](https://en.wikipedia.org/wiki/Persistent_data_structure).
- Includes persistent methods such as `with` and `without`, which return a

@@ -379,2 +379,7 @@ modified tree without changing the original (in O(log(size)) time).

### v1.6.2 ###
- Bug fixes: two rare situations were discovered in which shared nodes could fail to be marked as shared, and as a result, mutations could affect copies that should have been completely separate.
- Bug fix: greedyClone(true) did not clone shared nodes recursively.
### v1.6.0 ###

@@ -381,0 +386,0 @@

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc