Package not found. Please check the package name and try again.

btree-core 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/LICENSE +23 -0
  2. package/b+tree.d.ts +429 -0
  3. package/b+tree.js +1545 -0
  4. package/b+tree.min.js +1 -0
  5. package/extended/bulkLoad.d.ts +14 -0
  6. package/extended/bulkLoad.js +113 -0
  7. package/extended/bulkLoad.min.js +1 -0
  8. package/extended/decompose.d.ts +1 -0
  9. package/extended/decompose.js +680 -0
  10. package/extended/decompose.min.js +1 -0
  11. package/extended/diffAgainst.d.ts +23 -0
  12. package/extended/diffAgainst.js +254 -0
  13. package/extended/diffAgainst.min.js +1 -0
  14. package/extended/forEachKeyInBoth.d.ts +19 -0
  15. package/extended/forEachKeyInBoth.js +73 -0
  16. package/extended/forEachKeyInBoth.min.js +1 -0
  17. package/extended/forEachKeyNotIn.d.ts +18 -0
  18. package/extended/forEachKeyNotIn.js +87 -0
  19. package/extended/forEachKeyNotIn.min.js +1 -0
  20. package/extended/index.d.ts +133 -0
  21. package/extended/index.js +200 -0
  22. package/extended/index.min.js +1 -0
  23. package/extended/intersect.d.ts +16 -0
  24. package/extended/intersect.js +44 -0
  25. package/extended/intersect.min.js +1 -0
  26. package/extended/parallelWalk.d.ts +1 -0
  27. package/extended/parallelWalk.js +188 -0
  28. package/extended/parallelWalk.min.js +1 -0
  29. package/extended/shared.d.ts +1 -0
  30. package/extended/shared.js +64 -0
  31. package/extended/shared.min.js +1 -0
  32. package/extended/subtract.d.ts +16 -0
  33. package/extended/subtract.js +35 -0
  34. package/extended/subtract.min.js +1 -0
  35. package/extended/union.d.ts +16 -0
  36. package/extended/union.js +36 -0
  37. package/extended/union.min.js +1 -0
  38. package/interfaces.d.ts +307 -0
  39. package/package.json +122 -0
  40. package/readme.md +420 -0
  41. package/sorted-array.d.ts +22 -0
  42. package/sorted-array.js +71 -0
package/b+tree.min.js ADDED
@@ -0,0 +1 @@
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}function fixMaxSize(e){return 4<=e?Math.min(0|e,256):32}Object.defineProperty(exports,"__esModule",{value:!0}),exports.EmptyBTree=exports.check=exports.areOverlapping=exports.sumChildSizes=exports.BNodeInternal=exports.BNode=exports.asSet=exports.fixMaxSize=exports.simpleComparator=exports.defaultComparator=void 0,exports.defaultComparator=defaultComparator,exports.simpleComparator=simpleComparator,exports.fixMaxSize=fixMaxSize;var BTree=function(){function r(e,t,r){this._root=EmptyLeaf,this._maxNodeSize=fixMaxSize(r),this._compare=t||defaultComparator,e&&this.setPairs(e)}return Object.defineProperty(r.prototype,"size",{get:function(){return this._root.size()},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"length",{get:function(){return this.size},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"isEmpty",{get:function(){return 0===this._root.size()},enumerable:!1,configurable:!0}),r.prototype.clear=function(){this._root=EmptyLeaf},r.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)})},r.prototype.forEachPair=function(e,t){var r=this.minKey(),i=this.maxKey();return this.forRange(r,i,!0,e,t)},r.prototype.get=function(e,t){return this._root.get(e,t,this)},r.prototype.set=function(e,t,r){this._root.isShared&&(this._root=this._root.clone());r=this._root.set(e,t,r,this);if(!0===r||!1===r)return r;r=[this._root,r];return this._root=new BNodeInternal(r,sumChildSizes(r)),!0},r.prototype.has=function(e){return 0!==this.forRange(e,e,!0,void 0)},r.prototype.delete=function(e){return 0!==this.editRange(e,e,!0,DeleteRange)},r.prototype.with=function(e,t,r){var i=this.clone();return i.set(e,t,r)||r?i:this},r.prototype.withPairs=function(e,t){var r=this.clone();return 0!==r.setPairs(e,t)||t?r:this},r.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},r.prototype.without=function(e,t){return this.withoutRange(e,e,!0,t)},r.prototype.withoutKeys=function(e,t){var r=this.clone();return r.deleteKeys(e)||!t?r:this},r.prototype.withoutRange=function(e,t,r,i){var n=this.clone();return 0===n.deleteRange(e,t,r)&&i?this:n},r.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},r.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},r.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},r.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,h=void 0===e?-1:o.indexOf(e,0,this._compare)-1;return iterator(function(){e:for(;;)switch(s){case 0:if(++h<o.keys.length)return{done:!1,value:[o.keys[h],o.values[h]]};s=2;continue;case 1:if(++h<o.keys.length)return t[0]=o.keys[h],t[1]=o.values[h],{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]],h=-1,s=void 0!==t?1:0;continue;case 3:return{done:!0,value:void 0}}})},r.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 h=s.indexOf(e,0,this._compare);!r&&h<s.keys.length&&this._compare(s.keys[h],e)<=0&&h++;var a=void 0!==t?1:0;return iterator(function(){e:for(;;)switch(a){case 0:if(0<=--h)return{done:!1,value:[s.keys[h],s.values[h]]};a=2;continue;case 1:if(0<=--h)return t[0]=s.keys[h],t[1]=s.values[h],{done:!1,value:t};a=2;case 2:for(var e=-1;;){if(++e>=n.length){a=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]],h=s.keys.length,a=void 0!==t?1:0;continue;case 3:return{done:!0,value:void 0}}})},r.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}},r.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})},r.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(r.prototype,"maxNodeSize",{get:function(){return this._maxNodeSize},enumerable:!1,configurable:!0}),r.prototype.minKey=function(){return this._root.minKey()},r.prototype.maxKey=function(){return this._root.maxKey()},r.prototype.clone=function(){this._root.isShared=!0;var e=new r(void 0,this._compare,this._maxNodeSize);return e._root=this._root,e},r.prototype.greedyClone=function(e){var t=new r(void 0,this._compare,this._maxNodeSize);return t._root=this._root.greedyClone(e),t},r.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):[]},r.prototype.keysArray=function(){var r=[];return this._root.forRange(this.minKey(),this.maxKey(),!0,!1,this,0,function(e,t){r.push(e)}),r},r.prototype.valuesArray=function(){var r=[];return this._root.forRange(this.minKey(),this.maxKey(),!0,!1,this,0,function(e,t){r.push(t)}),r},r.prototype.toString=function(){return this.toArray().toString()},r.prototype.setIfNotPresent=function(e,t){return this.set(e,t,!1)},r.prototype.nextHigherPair=function(e,t){return t=t||[],void 0===e?this._root.minPair(t):this._root.getPairOrNextHigher(e,this._compare,!1,t)},r.prototype.nextHigherKey=function(e){e=this.nextHigherPair(e,ReusedArray);return e&&e[0]},r.prototype.nextLowerPair=function(e,t){return t=t||[],void 0===e?this._root.maxPair(t):this._root.getPairOrNextLower(e,this._compare,!1,t)},r.prototype.nextLowerKey=function(e){e=this.nextLowerPair(e,ReusedArray);return e&&e[0]},r.prototype.getPairOrNextLower=function(e,t){return this._root.getPairOrNextLower(e,this._compare,!0,t||[])},r.prototype.getPairOrNextHigher=function(e,t){return this._root.getPairOrNextHigher(e,this._compare,!0,t||[])},r.prototype.changeIfPresent=function(e,r){return 0!==this.editRange(e,e,!0,function(e,t){return{value:r}})},r.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},r.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},r.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},r.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 h=void 0;o.keys.length<=1&&!o.isLeaf;)h=h||o.isShared,this._root=o=0===o.keys.length?EmptyLeaf:o.children[0];h&&(o.isShared=!0)}},r.prototype.editAll=function(e,t){return this.editRange(this.minKey(),this.maxKey(),!0,e,t)},r.prototype.deleteRange=function(e,t,r){return this.editRange(e,t,r,DeleteRange)},r.prototype.deleteKeys=function(e){for(var t=0,r=0;t<e.length;t++)this.delete(e[t])&&r++;return r},Object.defineProperty(r.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}),r.prototype.freeze=function(){this.clear=this.set=this.editRange=function(){throw new Error("Attempted to modify a frozen BTree")}},r.prototype.unfreeze=function(){delete this.clear,delete this.set,delete this.editRange},Object.defineProperty(r.prototype,"isFrozen",{get:function(){return this.hasOwnProperty("editRange")},enumerable:!1,configurable:!0}),r.prototype.checkValid=function(e){void 0===e&&(e=!1);e=this._root.checkValid(0,this,0,e)[0];check(e===this.size,"size mismatch: counted ",e,"but stored",this.size)},r}();function asSet(e){return e}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,exports.asSet=asSet,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.size=function(){return this.keys.length},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 h=r(i[s],e);if(h<0)n=s+1;else{if(!(0<h)){if(0===h)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,i){var n=this.keys.length,o=this.values.length;if(check(this.values===undefVals?n<=o:n===o,"keys/values length mismatch: depth",e,"with lengths",n,o,"and baseIndex",r),check(0==e||0<n,"empty leaf at depth",e,"and baseIndex",r),!0===i)for(var s=1;s<n;s++)check(t._compare(this.keys[s-1],this.keys[s])<0,"keys out of order at depth",e,"and baseIndex",r+s-1,": ",this.keys[s-1]," !< ",this.keys[s]);return[n,this.keys[0],this.keys[n-1]]},t.prototype.set=function(e,t,r,i){var n=this.indexOf(e,-1,i._compare);if(n<0){if(n=~n,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 h,a,n=n._compare;if(t===e){if(!r)return o;if(a=(h=this.indexOf(e,-1,n))+1,h<0)return o}else h=this.indexOf(e,0,n),(a=this.indexOf(t,-1,n))<0?a=~a:!0===r&&a++;var u=this.keys,l=this.values;if(void 0!==s)for(var p=h;p<a;p++){var f=u[p],c=s(f,l[p],o++);if(void 0!==c){if(!0===i){if(f!==u[p]||!0===this.isShared)throw new Error("BTree illegally changed or cloned in editRange");c.delete?(this.keys.splice(p,1),this.values!==undefVals&&this.values.splice(p,1),p--,a--):c.hasOwnProperty("value")&&(l[p]=c.value)}if(void 0!==c.break)return c}}else o+=a-h;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(o){function i(e,t,r){var i=this;if(!r){r=[];for(var n=0;n<e.length;n++)r[n]=e[n].maxKey()}return(i=o.call(this,r)||this).children=e,i._size=t,i}return __extends(i,o),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._size,this.keys.slice(0))},i.prototype.size=function(){return this._size},i.prototype.greedyClone=function(e){if(this.isShared&&!e)return this;for(var t=new i(this.children.slice(0),this._size,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,i){var n=this.keys.length,o=this.children.length;check(n===o,"keys/children length mismatch: depth",e,"lengths",n,o,"baseIndex",r),check(1<n||0<e,"internal node has length",n,"at depth",e,"baseIndex",r);for(var s=0,h=this.children,a=this.keys,u=0,l=void 0,p=void 0,f=0;f<o;f++){var c=h[f],y=c.checkValid(e+1,t,r+s,i),d=y[0],v=y[1],y=y[2];check(d===c.size(),"cached size mismatch at depth",e,"index",f,"baseIndex",r),check(1===d||t._compare(v,y)<0,"child node keys not sorted at depth",e,"index",f,"baseIndex",r),void 0!==l&&void 0!==p&&i&&(check(!areOverlapping(l,p,v,y,t._compare),"children keys not sorted at depth",e,"index",f,"baseIndex",r,": ",p," !< ",v),check(t._compare(p,v)<0,"children keys not sorted at depth",e,"index",f,"baseIndex",r,": ",p," !< ",v)),l=v,p=y,s+=d,check((u+=c.keys.length)<=s,"wtf",r),check(0===f||h[f-1].constructor===c.constructor,"type mismatch, baseIndex:",r),c.maxKey()!=a[f]&&check(!1,"keys[",f,"] =",a[f],"is wrong, should be ",c.maxKey(),"at depth",e,"baseIndex",r),0===f||t._compare(a[f-1],a[f])<0||check(!1,"sort violation at depth",e,"index",f,"keys",a[f-1],a[f])}check(this._size===s,"internal node cached size mismatch at depth",e,"baseIndex",r,"cached",this._size,"actual",s);n=0===u;return(n||u>t.maxNodeSize*o)&&check(!1,n?"too few":"too many","children (",u,s,") at depth",e,"maxNodeSize:",t.maxNodeSize,"children.length:",o,"baseIndex:",r),[s,this.minKey(),this.maxKey()]},i.prototype.set=function(e,t,r,i){var n,o=this.children,s=i._maxNodeSize,h=i._compare,a=Math.min(this.indexOf(e,0,h),o.length-1),u=o[a];u.isShared&&(o[a]=u=u.clone()),u.keys.length>=s&&(0<a&&(n=o[a-1]).keys.length<s&&h(u.keys[0],e)<0?(n.isShared&&(o[a-1]=n=n.clone()),n.takeFromRight(u),this.keys[a-1]=n.maxKey()):void 0!==(n=o[a+1])&&n.keys.length<s&&h(u.maxKey(),e)<0&&(n.isShared&&(o[a+1]=n=n.clone()),n.takeFromLeft(u),this.keys[a]=o[a].maxKey()));o=u.size(),i=u.set(e,t,r,i);if(this._size+=u.size()-o,!1===i)return!1;if(this.keys[a]=u.maxKey(),!0===i)return!0;if(this.keys.length<s)return this.insert(a+1,i),!0;u=this.splitOffRightSide(),s=this;return 0<h(i.maxKey(),this.maxKey())&&(s=u,a-=this.keys.length),s.insert(a+1,i),u},i.prototype.insert=function(e,t){this.children.splice(e,0,t),this.keys.splice(e,0,t.maxKey()),this._size+=t.size()},i.prototype.splitOffRightSide=function(){var e=this.children.length>>1,t=this.children.splice(e),r=this.keys.splice(e),e=this._size;return this._size=sumChildSizes(this.children),new i(t,e-this._size,r)},i.prototype.splitOffLeftSide=function(){var e=this.children.length>>1,t=this.children.splice(0,e),r=this.keys.splice(0,e),e=this._size;return this._size=sumChildSizes(this.children),new i(t,e-this._size,r)},i.prototype.takeFromRight=function(e){var t=e;this.keys.push(e.keys.shift());e=t.children.shift();this.children.push(e);e=e.size();t._size-=e,this._size+=e},i.prototype.takeFromLeft=function(e){var t=e,r=t.children.pop();this.keys.unshift(e.keys.pop()),this.children.unshift(r);r=r.size();t._size-=r,this._size+=r},i.prototype.forRange=function(e,t,r,i,n,o,s){var h=n._compare,a=this.keys,u=this.children,l=this.indexOf(e,0,h),p=l,f=Math.min(t===e?l:this.indexOf(t,0,h),a.length-1);if(i){if(p<=f)try{for(;p<=f;p++){var c=u[p];c.isShared&&(u[p]=c=c.clone());var y=c.size(),d=c.forRange(e,t,r,i,n,o,s);if(a[p]=c.maxKey(),this._size+=c.size()-y,"number"!=typeof d)return d;o=d}}finally{var v=n._maxNodeSize>>1;for(0<l&&l--,p=f;l<=p;p--)u[p].keys.length<=v&&(0!==u[p].keys.length?this.tryMerge(p,n._maxNodeSize):(a.splice(p,1),check(0===u.splice(p,1)[0].size(),"emptiness cleanup")));0!==u.length&&0===u[0].keys.length&&check(!1,"emptiness bug")}}else for(;p<=f;p++){var g=u[p].forRange(e,t,r,i,n,o,s);if("number"!=typeof g)return g;o=g}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),this._size+=e.size(),e.isShared&&!this.isShared)for(var n=0;n<i.length;n++)i[n].isShared=!0;this.tryMerge(r-1,t)},i}(exports.BNode=BNode);exports.BNodeInternal=BNodeInternal;var undefVals=[];function sumChildSizes(e){for(var t=0,r=0;r<e.length;r++)t+=e[r].size();return t}function areOverlapping(e,t,r,i,n){return n(e,i)<=0&&0<=n(t,r)}exports.sumChildSizes=sumChildSizes,exports.areOverlapping=areOverlapping;var 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.check=check,exports.EmptyBTree=function(){var e=new BTree;return e.freeze(),e}();
@@ -0,0 +1,14 @@
1
+ import BTree from '../b+tree';
2
+ /**
3
+ * Loads a B-Tree from a sorted list of entries in bulk. This is faster than inserting
4
+ * entries one at a time, and produces a more optimally balanced tree.
5
+ * Time and space complexity: O(n).
6
+ * @param keys Keys to load, sorted in strictly ascending order.
7
+ * @param values Values corresponding to each key.
8
+ * @param maxNodeSize The branching factor (maximum node size) for the resulting tree.
9
+ * @param compare Function to compare keys.
10
+ * @param loadFactor Desired load factor for created leaves. Must be between 0.5 and 1.0.
11
+ * @returns A new BTree containing the given entries.
12
+ * @throws Error if the entries are not sorted by key in strictly ascending order (duplicates disallowed) or if the load factor is out of the allowed range.
13
+ */
14
+ export declare function bulkLoad<K, V>(keys: K[], values: V[], maxNodeSize: number, compare: (a: K, b: K) => number, loadFactor?: number): BTree<K, V>;
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.bulkLoadRoot = exports.bulkLoad = void 0;
27
+ var b_tree_1 = __importStar(require("../b+tree"));
28
+ var shared_1 = require("./shared");
29
+ /**
30
+ * Loads a B-Tree from a sorted list of entries in bulk. This is faster than inserting
31
+ * entries one at a time, and produces a more optimally balanced tree.
32
+ * Time and space complexity: O(n).
33
+ * @param keys Keys to load, sorted in strictly ascending order.
34
+ * @param values Values corresponding to each key.
35
+ * @param maxNodeSize The branching factor (maximum node size) for the resulting tree.
36
+ * @param compare Function to compare keys.
37
+ * @param loadFactor Desired load factor for created leaves. Must be between 0.5 and 1.0.
38
+ * @returns A new BTree containing the given entries.
39
+ * @throws Error if the entries are not sorted by key in strictly ascending order (duplicates disallowed) or if the load factor is out of the allowed range.
40
+ */
41
+ function bulkLoad(keys, values, maxNodeSize, compare, loadFactor) {
42
+ if (loadFactor === void 0) { loadFactor = 0.8; }
43
+ var root = bulkLoadRoot(keys, values, maxNodeSize, compare, loadFactor);
44
+ var tree = new b_tree_1.default(undefined, compare, maxNodeSize);
45
+ var target = tree;
46
+ target._root = root;
47
+ return tree;
48
+ }
49
+ exports.bulkLoad = bulkLoad;
50
+ /**
51
+ * Bulk loads, returns the root node of the resulting tree.
52
+ * @internal
53
+ */
54
+ function bulkLoadRoot(keys, values, maxNodeSize, compare, loadFactor) {
55
+ if (loadFactor === void 0) { loadFactor = 0.8; }
56
+ if (loadFactor < 0.5 || loadFactor > 1.0)
57
+ throw new Error("bulkLoad: loadFactor must be between 0.5 and 1.0");
58
+ if (keys.length !== values.length)
59
+ throw new Error("bulkLoad: keys and values arrays must be the same length");
60
+ maxNodeSize = (0, b_tree_1.fixMaxSize)(maxNodeSize);
61
+ // Verify keys are sorted
62
+ var totalPairs = keys.length;
63
+ if (totalPairs > 1) {
64
+ var previousKey = keys[0];
65
+ for (var i = 1; i < totalPairs; i++) {
66
+ var key = keys[i];
67
+ if (compare(previousKey, key) >= 0)
68
+ throw new Error("bulkLoad: keys must be sorted in strictly ascending order");
69
+ previousKey = key;
70
+ }
71
+ }
72
+ // Get ALL the leaf nodes with which the tree will be populated
73
+ var currentNodes = [];
74
+ (0, shared_1.makeLeavesFrom)(keys, values, maxNodeSize, loadFactor, currentNodes.push.bind(currentNodes));
75
+ if (currentNodes.length === 0)
76
+ return new b_tree_1.BNode();
77
+ var targetNodeSize = Math.ceil(maxNodeSize * loadFactor);
78
+ var isExactlyHalf = targetNodeSize === maxNodeSize / 2;
79
+ var minSize = Math.floor(maxNodeSize / 2);
80
+ for (var nextLevel = void 0; currentNodes.length > 1; currentNodes = nextLevel) {
81
+ var nodeCount = currentNodes.length;
82
+ if (nodeCount <= maxNodeSize && (nodeCount !== maxNodeSize || !isExactlyHalf)) {
83
+ currentNodes = [new b_tree_1.BNodeInternal(currentNodes, (0, b_tree_1.sumChildSizes)(currentNodes))];
84
+ break;
85
+ }
86
+ var nextLevelCount = Math.ceil(nodeCount / targetNodeSize);
87
+ (0, b_tree_1.check)(nextLevelCount > 1);
88
+ nextLevel = new Array(nextLevelCount);
89
+ var remainingNodes = nodeCount;
90
+ var remainingParents = nextLevelCount;
91
+ var childIndex = 0;
92
+ for (var i = 0; i < nextLevelCount; i++) {
93
+ var chunkSize = Math.ceil(remainingNodes / remainingParents);
94
+ var children = new Array(chunkSize);
95
+ var size = 0;
96
+ for (var j = 0; j < chunkSize; j++) {
97
+ var child = currentNodes[childIndex++];
98
+ children[j] = child;
99
+ size += child.size();
100
+ }
101
+ remainingNodes -= chunkSize;
102
+ remainingParents--;
103
+ nextLevel[i] = new b_tree_1.BNodeInternal(children, size);
104
+ }
105
+ // If last node is underfilled, balance with left sibling
106
+ var secondLastNode = nextLevel[nextLevelCount - 2];
107
+ var lastNode = nextLevel[nextLevelCount - 1];
108
+ while (lastNode.children.length < minSize)
109
+ lastNode.takeFromLeft(secondLastNode);
110
+ }
111
+ return currentNodes[0];
112
+ }
113
+ exports.bulkLoadRoot = bulkLoadRoot;
@@ -0,0 +1 @@
1
+ "use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,r,t,o){void 0===o&&(o=t);var a=Object.getOwnPropertyDescriptor(r,t);a&&("get"in a?r.__esModule:!a.writable&&!a.configurable)||(a={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,o,a)}:function(e,r,t,o){void 0===o&&(o=t),e[o]=r[t]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,r){Object.defineProperty(e,"default",{enumerable:!0,value:r})}:function(e,r){e.default=r}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var r={};if(null!=e)for(var t in e)"default"!==t&&Object.prototype.hasOwnProperty.call(e,t)&&__createBinding(r,e,t);return __setModuleDefault(r,e),r};Object.defineProperty(exports,"__esModule",{value:!0}),exports.bulkLoadRoot=exports.bulkLoad=void 0;var b_tree_1=__importStar(require("../b+tree")),shared_1=require("./shared");function bulkLoad(e,r,t,o,a){void 0===a&&(a=.8);a=bulkLoadRoot(e,r,t,o,a),t=new b_tree_1.default(void 0,o,t);return t._root=a,t}function bulkLoadRoot(e,r,t,o,a){if(void 0===a&&(a=.8),a<.5||1<a)throw new Error("bulkLoad: loadFactor must be between 0.5 and 1.0");if(e.length!==r.length)throw new Error("bulkLoad: keys and values arrays must be the same length");t=(0,b_tree_1.fixMaxSize)(t);var n=e.length;if(1<n)for(var i=e[0],l=1;l<n;l++){var u=e[l];if(0<=o(i,u))throw new Error("bulkLoad: keys must be sorted in strictly ascending order");i=u}var d=[];if((0,shared_1.makeLeavesFrom)(e,r,t,a,d.push.bind(d)),0===d.length)return new b_tree_1.BNode;for(var _=Math.ceil(t*a),s=_===t/2,b=Math.floor(t/2),f=void 0;1<d.length;d=f){var c=d.length;if(c<=t&&(c!==t||!s)){d=[new b_tree_1.BNodeInternal(d,(0,b_tree_1.sumChildSizes)(d))];break}var h=Math.ceil(c/_);(0,b_tree_1.check)(1<h),f=new Array(h);for(var v=c,k=h,p=0,l=0;l<h;l++){for(var g=Math.ceil(v/k),w=new Array(g),L=0,m=0;m<g;m++){var y=d[p++];L+=(w[m]=y).size()}v-=g,k--,f[l]=new b_tree_1.BNodeInternal(w,L)}for(var M=f[h-2],O=f[h-1];O.children.length<b;)O.takeFromLeft(M)}return d[0]}exports.bulkLoad=bulkLoad,exports.bulkLoadRoot=bulkLoadRoot;
@@ -0,0 +1 @@
1
+ export {};