Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sorted-btree

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sorted-btree - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

2

b+tree.min.js

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

var __extends=this&&this.__extends||function(){var r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var i in t)t.hasOwnProperty(i)&&(e[i]=t[i])})(e,t)};return function(e,t){function i(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(i.prototype=t.prototype,new i)}}();!function(e){"object"==typeof module&&"object"==typeof module.exports?e(require,exports):"function"==typeof define&&define.amd&&define(["require","exports"],e)}(function(e,t){"use strict";function r(e,t){var i=e-t;return i==i?i:(e=e&&e.valueOf())<(t=t&&t.valueOf())?-1:t<e?1:e==t?0:i}Object.defineProperty(t,"__esModule",{value:!0}),t.EmptyBTree=t.defaultComparator=void 0,t.defaultComparator=r;var i=(Object.defineProperty(n.prototype,"size",{get:function(){return this._size},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"length",{get:function(){return this._size},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"isEmpty",{get:function(){return 0===this._size},enumerable:!1,configurable:!0}),n.prototype.clear=function(){this._root=y,this._size=0},n.prototype.forEach=function(i,e){var r=this;return void 0!==e&&(i=i.bind(e)),this.forEachPair(function(e,t){return i(t,e,r)})},n.prototype.forEachPair=function(e,t){var i=this.minKey(),r=this.maxKey();return this.forRange(i,r,!0,e,t)},n.prototype.get=function(e,t){return this._root.get(e,t,this)},n.prototype.set=function(e,t,i){this._root.isShared&&(this._root=this._root.clone());i=this._root.set(e,t,i,this);return!0===i||!1===i?i:(this._root=new a([this._root,i]),!0)},n.prototype.has=function(e){return 0!==this.forRange(e,e,!0,void 0)},n.prototype.delete=function(e){return 0!==this.editRange(e,e,!0,p)},n.prototype.with=function(e,t,i){var r=this.clone();return r.set(e,t,i)||i?r:this},n.prototype.withPairs=function(e,t){var i=this.clone();return 0!==i.setPairs(e,t)||t?i:this},n.prototype.withKeys=function(e,t){for(var i=this.clone(),r=!1,n=0;n<e.length;n++)r=i.set(e[n],void 0,!1)||r;return t&&!r?this:i},n.prototype.without=function(e,t){return this.withoutRange(e,e,!0,t)},n.prototype.withoutKeys=function(e,t){var i=this.clone();return i.deleteKeys(e)||!t?i:this},n.prototype.withoutRange=function(e,t,i,r){var n=this.clone();return 0===n.deleteRange(e,t,i)&&r?this:n},n.prototype.filter=function(r,e){var n,t=this.greedyClone();return t.editAll(function(e,t,i){if(!r(e,t,i))return n=f}),!n&&e?this:t},n.prototype.mapValues=function(r){var n={},e=this.greedyClone();return e.editAll(function(e,t,i){return n.value=r(t,e,i),n}),e},n.prototype.reduce=function(e,t){for(var i,r=0,n=t,o=this.entries(this.minKey(),g);!(i=o.next()).done;)n=e(n,i.value,r++,this);return n},n.prototype.entries=function(e,t){var i=this.findPath(e);if(void 0===i)return u();var r=i.nodequeue,n=i.nodeindex,o=i.leaf,s=void 0!==t?1:0,h=void 0===e?-1:o.indexOf(e,0,this._compare)-1;return u(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>=r.length){s=3;continue e}if(++n[e]<r[e].length)break}for(;0<e;e--)r[e-1]=r[e][n[e]].children,n[e-1]=0;o=r[0][n[0]],h=-1,s=void 0!==t?1:0;continue;case 3:return{done:!0,value:void 0}}})},n.prototype.entriesReversed=function(e,t,i){if(void 0===e&&(i=void 0)===(e=this.maxKey()))return u();var r=this.findPath(e)||this.findPath(this.maxKey()),n=r.nodequeue,o=r.nodeindex,s=r.leaf;m(!n[0]||s===n[0][o[0]],"wat!");var h=s.indexOf(e,0,this._compare);i||0<this._compare(s.keys[h],e)||h++;var a=void 0!==t?1:0;return u(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}}})},n.prototype.findPath=function(e){var t,i,r=this._root;if(r.isLeaf)i=t=v;else{t=[],i=[];for(var n=0;!r.isLeaf;n++){if(t[n]=r.children,i[n]=void 0===e?0:r.indexOf(e,0,this._compare),i[n]>=t[n].length)return;r=t[n][i[n]]}t.reverse(),i.reverse()}return{nodequeue:t,nodeindex:i,leaf:r}},n.prototype.keys=function(e){var t=this.entries(e,g);return u(function(){var e=t.next();return e.value&&(e.value=e.value[0]),e})},n.prototype.values=function(e){var t=this.entries(e,g);return u(function(){var e=t.next();return e.value&&(e.value=e.value[1]),e})},Object.defineProperty(n.prototype,"maxNodeSize",{get:function(){return this._maxNodeSize},enumerable:!1,configurable:!0}),n.prototype.minKey=function(){return this._root.minKey()},n.prototype.maxKey=function(){return this._root.maxKey()},n.prototype.clone=function(){this._root.isShared=!0;var e=new n(void 0,this._compare,this._maxNodeSize);return e._root=this._root,e._size=this._size,e},n.prototype.greedyClone=function(e){var t=new n(void 0,this._compare,this._maxNodeSize);return t._root=this._root.greedyClone(e),t._size=this._size,t},n.prototype.toArray=function(e){void 0===e&&(e=2147483647);var t=this.minKey(),i=this.maxKey();return void 0!==t?this.getRange(t,i,!0,e):[]},n.prototype.keysArray=function(){var i=[];return this._root.forRange(this.minKey(),this.maxKey(),!0,!1,this,0,function(e,t){i.push(e)}),i},n.prototype.valuesArray=function(){var i=[];return this._root.forRange(this.minKey(),this.maxKey(),!0,!1,this,0,function(e,t){i.push(t)}),i},n.prototype.toString=function(){return this.toArray().toString()},n.prototype.setIfNotPresent=function(e,t){return this.set(e,t,!1)},n.prototype.nextHigherPair=function(e){var t=this.entries(e,g),i=t.next();return!i.done&&void 0!==e&&this._compare(i.value[0],e)<=0&&(i=t.next()),i.value},n.prototype.nextHigherKey=function(e){e=this.nextHigherPair(e);return e&&e[0]},n.prototype.nextLowerPair=function(e){return this.entriesReversed(e,g,!0).next().value},n.prototype.nextLowerKey=function(e){e=this.nextLowerPair(e);return e&&e[0]},n.prototype.changeIfPresent=function(e,i){return 0!==this.editRange(e,e,!0,function(e,t){return{value:i}})},n.prototype.getRange=function(e,t,i,r){void 0===r&&(r=67108863);var n=[];return this._root.forRange(e,t,i,!1,this,0,function(e,t){return n.push([e,t]),n.length>r?c:void 0}),n},n.prototype.setPairs=function(e,t){for(var i=0,r=0;r<e.length;r++)this.set(e[r][0],e[r][1],t)&&i++;return i},n.prototype.forRange=function(e,t,i,r,n){r=this._root.forRange(e,t,i,!1,this,n||0,r);return"number"==typeof r?r:r.break},n.prototype.editRange=function(e,t,i,r,n){var o=this._root;o.isShared&&(this._root=o=o.clone());try{var s=o.forRange(e,t,i,!0,this,n||0,r);return"number"==typeof s?s:s.break}finally{for(;o.keys.length<=1&&!o.isLeaf;)this._root=o=0===o.keys.length?y:o.children[0]}},n.prototype.editAll=function(e,t){return this.editRange(this.minKey(),this.maxKey(),!0,e,t)},n.prototype.deleteRange=function(e,t,i){return this.editRange(e,t,i,p)},n.prototype.deleteKeys=function(e){for(var t=0,i=0;t<e.length;t++)this.delete(e[t])&&i++;return i},Object.defineProperty(n.prototype,"height",{get:function(){for(var e=this._root,t=-1;null!=e;t++)e=e.children;return t},enumerable:!1,configurable:!0}),n.prototype.freeze=function(){this.clear=this.set=this.editRange=function(){throw new Error("Attempted to modify a frozen BTree")}},n.prototype.unfreeze=function(){delete this.clear,delete this.set,delete this.editRange},Object.defineProperty(n.prototype,"isFrozen",{get:function(){return this.hasOwnProperty("editRange")},enumerable:!1,configurable:!0}),n.prototype.checkValid=function(){var e=this._root.checkValid(0,this,0);m(e===this.size,"size mismatch: counted ",e,"but stored",this.size)},n);function n(e,t,i){this._root=y,this._size=0,this._maxNodeSize=4<=i?Math.min(i,256):32,this._compare=t||r,e&&this.setPairs(e)}function u(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}t.default=i,Symbol&&Symbol.iterator&&(i.prototype[Symbol.iterator]=i.prototype.entries),i.prototype.where=i.prototype.filter,i.prototype.setRange=i.prototype.setPairs,i.prototype.add=i.prototype.set;var o=(Object.defineProperty(s.prototype,"isLeaf",{get:function(){return void 0===this.children},enumerable:!1,configurable:!0}),s.prototype.maxKey=function(){return this.keys[this.keys.length-1]},s.prototype.indexOf=function(e,t,i){for(var r=this.keys,n=0,o=r.length,s=o>>1;n<o;){var h=i(r[s],e);if(h<0)n=s+1;else{if(!(0<h)){if(0===h)return s;if(e==e)return r.length;throw new Error("BTree: NaN was used as a key")}o=s}s=n+o>>1}return s^t},s.prototype.minKey=function(){return this.keys[0]},s.prototype.clone=function(){var e=this.values;return new s(this.keys.slice(0),e===d?e:e.slice(0))},s.prototype.greedyClone=function(e){return this.isShared&&!e?this:this.clone()},s.prototype.get=function(e,t,i){i=this.indexOf(e,-1,i._compare);return i<0?t:this.values[i]},s.prototype.checkValid=function(e,t,i){var r=this.keys.length,n=this.values.length;return m(this.values===d?r<=n:r===n,"keys/values length mismatch: depth",e,"with lengths",r,n,"and baseIndex",i),m(0==e||0<r,"empty leaf at depth",e,"and baseIndex",i),r},s.prototype.set=function(e,t,i,r){var n=this.indexOf(e,-1,r._compare);if(n<0){if(n=~n,r._size++,this.keys.length<r._maxNodeSize)return this.insertInLeaf(n,e,t,r);var o=this.splitOffRightSide(),s=this;return n>this.keys.length&&(n-=this.keys.length,s=o),s.insertInLeaf(n,e,t,r),o}return!1!==i&&(void 0!==t&&this.reifyValues(),this.keys[n]=e,this.values[n]=t),!1},s.prototype.reifyValues=function(){return this.values===d?this.values=this.values.slice(0,this.keys.length):this.values},s.prototype.insertInLeaf=function(e,t,i,r){if(this.keys.splice(e,0,t),this.values===d){for(;d.length<r._maxNodeSize;)d.push(void 0);if(void 0===i)return!0;this.values=d.slice(0,this.keys.length-1)}return this.values.splice(e,0,i),!0},s.prototype.takeFromRight=function(e){var t=this.values;e.values===d?t!==d&&t.push(void 0):(t=this.reifyValues()).push(e.values.shift()),this.keys.push(e.keys.shift())},s.prototype.takeFromLeft=function(e){var t=this.values;e.values===d?t!==d&&t.unshift(void 0):(t=this.reifyValues()).unshift(e.values.pop()),this.keys.unshift(e.keys.pop())},s.prototype.splitOffRightSide=function(){var e=this.keys.length>>1;return new s(this.keys.splice(e),this.values===d?d:this.values.splice(e))},s.prototype.forRange=function(e,t,i,r,n,o,s){var h,a,u=n._compare;if(t===e){if(!i)return o;if(a=(h=this.indexOf(e,-1,u))+1,h<0)return o}else h=this.indexOf(e,0,u),(a=this.indexOf(t,-1,u))<0?a=~a:!0===i&&a++;var l=this.keys,f=this.values;if(void 0!==s)for(var p=h;p<a;p++){var c=l[p],y=s(c,f[p],o++);if(void 0!==y){if(!0===r){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!==d&&this.values.splice(p,1),n._size--,p--,a--):y.hasOwnProperty("value")&&(f[p]=y.value)}if(void 0!==y.break)return y}}else o+=a-h;return o},s.prototype.mergeSibling=function(e,t){if(this.keys.push.apply(this.keys,e.keys),this.values===d){if(e.values===d)return;this.values=this.values.slice(0,this.keys.length)}this.values.push.apply(this.values,e.reifyValues())},s);function s(e,t){void 0===e&&(e=[]),this.keys=e,this.values=t||d,this.isShared=void 0}var h,a=(__extends(l,h=o),l.prototype.clone=function(){for(var e=this.children.slice(0),t=0;t<e.length;t++)e[t].isShared=!0;return new l(e,this.keys.slice(0))},l.prototype.greedyClone=function(e){if(this.isShared&&!e)return this;for(var t=new l(this.children.slice(0),this.keys.slice(0)),i=0;i<t.children.length;i++)t.children[i]=t.children[i].greedyClone();return t},l.prototype.minKey=function(){return this.children[0].minKey()},l.prototype.get=function(e,t,i){var r=this.indexOf(e,0,i._compare),n=this.children;return r<n.length?n[r].get(e,t,i):void 0},l.prototype.checkValid=function(e,t,i){var r=this.keys.length,n=this.children.length;m(r===n,"keys/children length mismatch: depth",e,"lengths",r,n,"baseIndex",i),m(1<r||0<e,"internal node has length",r,"at depth",e,"baseIndex",i);for(var o=0,s=this.children,h=this.keys,a=0,u=0;u<n;u++)o+=s[u].checkValid(e+1,t,i+o),m((a+=s[u].keys.length)<=o,"wtf",i),m(0===u||s[u-1].constructor===s[u].constructor,"type mismatch, baseIndex:",i),s[u].maxKey()!=h[u]&&m(!1,"keys[",u,"] =",h[u],"is wrong, should be ",s[u].maxKey(),"at depth",e,"baseIndex",i),0===u||t._compare(h[u-1],h[u])<0||m(!1,"sort violation at depth",e,"index",u,"keys",h[u-1],h[u]);r=0===a;return(r||a>t.maxNodeSize*n)&&m(!1,r?"too few":"too many","children (",a,o,") at depth",e,"maxNodeSize:",t.maxNodeSize,"children.length:",n,"baseIndex:",i),o},l.prototype.set=function(e,t,i,r){var n,o=this.children,s=r._maxNodeSize,h=r._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()));r=u.set(e,t,i,r);if(!1===r)return!1;if(this.keys[a]=u.maxKey(),!0===r)return!0;if(this.keys.length<s)return this.insert(a+1,r),!0;u=this.splitOffRightSide(),s=this;return 0<h(r.maxKey(),this.maxKey())&&(s=u,a-=this.keys.length),s.insert(a+1,r),u},l.prototype.insert=function(e,t){this.children.splice(e,0,t),this.keys.splice(e,0,t.maxKey())},l.prototype.splitOffRightSide=function(){var e=this.children.length>>1;return new l(this.children.splice(e),this.keys.splice(e))},l.prototype.takeFromRight=function(e){this.keys.push(e.keys.shift()),this.children.push(e.children.shift())},l.prototype.takeFromLeft=function(e){this.keys.unshift(e.keys.pop()),this.children.unshift(e.children.pop())},l.prototype.forRange=function(e,t,i,r,n,o,s){var h=n._compare,a=this.keys,u=this.children,l=this.indexOf(e,0,h),f=l,p=Math.min(t===e?l:this.indexOf(t,0,h),a.length-1);if(r){if(f<=p)try{for(;f<=p;f++){u[f].isShared&&(u[f]=u[f].clone());var c=u[f].forRange(e,t,i,r,n,o,s);if(a[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):(a.splice(f,1),u.splice(f,1)));0!==u.length&&0===u[0].keys.length&&m(!1,"emptiness bug")}}else for(;f<=p;f++){if("number"!=typeof(c=u[f].forRange(e,t,i,r,n,o,s)))return c;o=c}return o},l.prototype.tryMerge=function(e,t){var i=this.children;return 0<=e&&e+1<i.length&&i[e].keys.length+i[e+1].keys.length<=t&&(i[e].isShared&&(i[e]=i[e].clone()),i[e].mergeSibling(i[e+1],t),i.splice(e+1,1),this.keys.splice(e+1,1),this.keys[e]=i[e].maxKey(),!0)},l.prototype.mergeSibling=function(e,t){var i=this.keys.length;this.keys.push.apply(this.keys,e.keys),this.children.push.apply(this.children,e.children),this.tryMerge(i-1,t)},l);function l(e,t){var i=this;if(!t){t=[];for(var r=0;r<e.length;r++)t[r]=e[r].maxKey()}return(i=h.call(this,t)||this).children=e,i}var d=[],f={delete:!0},p=function(){return f},c={break:!0},y=((o=new o).isShared=!0,o),v=[],g=[];function m(e){for(var t=[],i=1;i<arguments.length;i++)t[i-1]=arguments[i];if(!e)throw t.unshift("B+ tree"),new Error(t.join(" "))}t.EmptyBTree=((i=new i).freeze(),i)});
"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)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)};return function(e,t){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){var r=e-t;return r==r?r:(e=e&&e.valueOf())<(t=t&&t.valueOf())?-1:t<e?1:e==t?0:r}Object.defineProperty(exports,"__esModule",{value:!0}),exports.EmptyBTree=exports.defaultComparator=void 0,exports.defaultComparator=defaultComparator;var BTree=function(){function r(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(r.prototype,"size",{get:function(){return this._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._size},enumerable:!1,configurable:!0}),r.prototype.clear=function(){this._root=EmptyLeaf,this._size=0},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);return!0===r||!1===r?r:(this._root=new BNodeInternal([this._root,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||0<this._compare(s.keys[h],e)||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._size=this._size,e},r.prototype.greedyClone=function(e){var t=new r(void 0,this._compare,this._maxNodeSize);return t._root=this._root.greedyClone(e),t._size=this._size,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){var t=this.entries(e,ReusedArray),r=t.next();return!r.done&&void 0!==e&&this._compare(r.value[0],e)<=0&&(r=t.next()),r.value},r.prototype.nextHigherKey=function(e){e=this.nextHigherPair(e);return e&&e[0]},r.prototype.nextLowerPair=function(e){return this.entriesReversed(e,ReusedArray,!0).next().value},r.prototype.nextLowerKey=function(e){e=this.nextLowerPair(e);return e&&e[0]},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(;o.keys.length<=1&&!o.isLeaf;)this._root=o=0===o.keys.length?EmptyLeaf:o.children[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;null!=e;t++)e=e.children;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(){var e=this._root.checkValid(0,this,0);check(e===this.size,"size mismatch: counted ",e,"but stored",this.size)},r}();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 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.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.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 h,a,u=n._compare;if(t===e){if(!r)return o;if(a=(h=this.indexOf(e,-1,u))+1,h<0)return o}else h=this.indexOf(e,0,u),(a=this.indexOf(t,-1,u))<0?a=~a:!0===r&&a++;var l=this.keys,f=this.values;if(void 0!==s)for(var p=h;p<a;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--,a--):y.hasOwnProperty("value")&&(f[p]=y.value)}if(void 0!==y.break)return y}}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(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.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.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,h=this.keys,a=0,u=0;u<n;u++)o+=s[u].checkValid(e+1,t,r+o),check((a+=s[u].keys.length)<=o,"wtf",r),check(0===u||s[u-1].constructor===s[u].constructor,"type mismatch, baseIndex:",r),s[u].maxKey()!=h[u]&&check(!1,"keys[",u,"] =",h[u],"is wrong, should be ",s[u].maxKey(),"at depth",e,"baseIndex",r),0===u||t._compare(h[u-1],h[u])<0||check(!1,"sort violation at depth",e,"index",u,"keys",h[u-1],h[u]);i=0===a;return(i||a>t.maxNodeSize*n)&&check(!1,i?"too few":"too many","children (",a,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,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()));i=u.set(e,t,r,i);if(!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())},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 h=n._compare,a=this.keys,u=this.children,l=this.indexOf(e,0,h),f=l,p=Math.min(t===e?l:this.indexOf(t,0,h),a.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(a[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):(a.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}();
{
"name": "sorted-btree",
"version": "1.3.0",
"version": "1.4.0",
"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",

@@ -39,3 +39,3 @@ B+ tree

- Throws an exception if you try to use `NaN` as a key, but infinity is allowed.
- No dependencies. 15K minified.
- No dependencies. 16K minified.
- Includes a lattice of interfaces for TypeScript users (see below)

@@ -376,2 +376,6 @@

### v1.4.0 ###
- Now built as CommonJS module instead of UMD module, for better compatibility with webpack. No semantic change.
### v1.3.0 ###

@@ -378,0 +382,0 @@

@@ -1,81 +0,71 @@

(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/** A super-inefficient sorted list for testing purposes */
var SortedArray = /** @class */ (function () {
function SortedArray(entries, compare) {
this.cmp = compare || (function (a, b) { return a < b ? -1 : a > b ? 1 : a === b ? 0 : a - b; });
this.a = [];
if (entries !== undefined)
for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
var e = entries_1[_i];
this.set(e[0], e[1]);
}
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/** A super-inefficient sorted list for testing purposes */
var SortedArray = /** @class */ (function () {
function SortedArray(entries, compare) {
this.cmp = compare || (function (a, b) { return a < b ? -1 : a > b ? 1 : a === b ? 0 : a - b; });
this.a = [];
if (entries !== undefined)
for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
var e = entries_1[_i];
this.set(e[0], e[1]);
}
Object.defineProperty(SortedArray.prototype, "size", {
get: function () { return this.a.length; },
enumerable: false,
configurable: true
});
SortedArray.prototype.get = function (key, defaultValue) {
var pair = this.a[this.indexOf(key, -1)];
return pair === undefined ? defaultValue : pair[1];
};
SortedArray.prototype.set = function (key, value, overwrite) {
var i = this.indexOf(key, -1);
if (i <= -1)
this.a.splice(~i, 0, [key, value]);
else
this.a[i] = [key, value];
return i <= -1;
};
SortedArray.prototype.has = function (key) {
return this.indexOf(key, -1) >= 0;
};
SortedArray.prototype.delete = function (key) {
var i = this.indexOf(key, -1);
if (i > -1)
this.a.splice(i, 1);
return i > -1;
};
SortedArray.prototype.clear = function () { this.a = []; };
SortedArray.prototype.getArray = function () { return this.a; };
SortedArray.prototype.minKey = function () { return this.a[0][0]; };
SortedArray.prototype.maxKey = function () { return this.a[this.a.length - 1][0]; };
SortedArray.prototype.forEach = function (callbackFn) {
var _this = this;
this.a.forEach(function (pair) { return callbackFn(pair[1], pair[0], _this); });
};
// a.values() used to implement IMap<K,V> but it's not actually available in Node v10.4
SortedArray.prototype[Symbol.iterator] = function () { return this.a.values(); };
SortedArray.prototype.entries = function () { return this.a.values(); };
SortedArray.prototype.keys = function () { return this.a.map(function (pair) { return pair[0]; }).values(); };
SortedArray.prototype.values = function () { return this.a.map(function (pair) { return pair[1]; }).values(); };
SortedArray.prototype.indexOf = function (key, failXor) {
var lo = 0, hi = this.a.length, mid = hi >> 1;
while (lo < hi) {
var c = this.cmp(this.a[mid][0], key);
if (c < 0)
lo = mid + 1;
else if (c > 0) // keys[mid] > key
hi = mid;
else if (c === 0)
return mid;
else
throw new Error("Problem: compare failed");
mid = (lo + hi) >> 1;
}
Object.defineProperty(SortedArray.prototype, "size", {
get: function () { return this.a.length; },
enumerable: false,
configurable: true
});
SortedArray.prototype.get = function (key, defaultValue) {
var pair = this.a[this.indexOf(key, -1)];
return pair === undefined ? defaultValue : pair[1];
};
SortedArray.prototype.set = function (key, value, overwrite) {
var i = this.indexOf(key, -1);
if (i <= -1)
this.a.splice(~i, 0, [key, value]);
else
this.a[i] = [key, value];
return i <= -1;
};
SortedArray.prototype.has = function (key) {
return this.indexOf(key, -1) >= 0;
};
SortedArray.prototype.delete = function (key) {
var i = this.indexOf(key, -1);
if (i > -1)
this.a.splice(i, 1);
return i > -1;
};
SortedArray.prototype.clear = function () { this.a = []; };
SortedArray.prototype.getArray = function () { return this.a; };
SortedArray.prototype.minKey = function () { return this.a[0][0]; };
SortedArray.prototype.maxKey = function () { return this.a[this.a.length - 1][0]; };
SortedArray.prototype.forEach = function (callbackFn) {
var _this = this;
this.a.forEach(function (pair) { return callbackFn(pair[1], pair[0], _this); });
};
// a.values() used to implement IMap<K,V> but it's not actually available in Node v10.4
SortedArray.prototype[Symbol.iterator] = function () { return this.a.values(); };
SortedArray.prototype.entries = function () { return this.a.values(); };
SortedArray.prototype.keys = function () { return this.a.map(function (pair) { return pair[0]; }).values(); };
SortedArray.prototype.values = function () { return this.a.map(function (pair) { return pair[1]; }).values(); };
SortedArray.prototype.indexOf = function (key, failXor) {
var lo = 0, hi = this.a.length, mid = hi >> 1;
while (lo < hi) {
var c = this.cmp(this.a[mid][0], key);
if (c < 0)
lo = mid + 1;
else if (c > 0) // keys[mid] > key
hi = mid;
else if (c === 0)
return mid;
else
throw new Error("Problem: compare failed");
mid = (lo + hi) >> 1;
}
return mid ^ failXor;
};
return SortedArray;
}());
exports.default = SortedArray;
});
return mid ^ failXor;
};
return SortedArray;
}());
exports.default = SortedArray;

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc