@frostpillar/frostpillar-btree 0.2.7 → 0.2.9

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 (38) hide show
  1. package/README-JA.md +93 -59
  2. package/README.md +93 -59
  3. package/dist/InMemoryBTree.d.cts +45 -0
  4. package/dist/btree/autoScale.d.cts +11 -0
  5. package/dist/btree/bulkLoad.d.cts +5 -0
  6. package/dist/btree/deleteRange.d.cts +3 -0
  7. package/dist/btree/entry-lookup.d.cts +8 -0
  8. package/dist/btree/integrity-helpers.d.cts +6 -0
  9. package/dist/btree/integrity.d.cts +2 -0
  10. package/dist/btree/mutations.d.cts +10 -0
  11. package/dist/btree/navigation.d.cts +23 -0
  12. package/dist/btree/node-ops.d.cts +15 -0
  13. package/dist/btree/rangeQuery.d.cts +7 -0
  14. package/dist/btree/rebalance-branch.d.cts +4 -0
  15. package/dist/btree/rebalance.d.cts +7 -0
  16. package/dist/btree/serialization.d.cts +18 -0
  17. package/dist/btree/split.d.cts +3 -0
  18. package/dist/btree/stats.d.cts +2 -0
  19. package/dist/btree/traversal.d.cts +7 -0
  20. package/dist/btree/types.d.cts +110 -0
  21. package/dist/{chunk-UGGWGP4E.js → chunk-OFXDCKLC.js} +4 -3
  22. package/dist/concurrency/ConcurrentInMemoryBTree.d.cts +49 -0
  23. package/dist/concurrency/coordinator.d.cts +21 -0
  24. package/dist/concurrency/helpers.d.cts +34 -0
  25. package/dist/concurrency/index.d.cts +2 -0
  26. package/dist/concurrency/syncLogValidation.d.cts +2 -0
  27. package/dist/concurrency/types.d.cts +54 -0
  28. package/dist/concurrency/writeOps.d.cts +48 -0
  29. package/dist/core.cjs +4 -3
  30. package/dist/core.d.cts +4 -0
  31. package/dist/core.js +1 -1
  32. package/dist/errors.d.cts +12 -0
  33. package/dist/frostpillar-btree-core.min.js +1 -1
  34. package/dist/frostpillar-btree.min.js +1 -1
  35. package/dist/index.cjs +4 -3
  36. package/dist/index.d.cts +6 -0
  37. package/dist/index.js +1 -1
  38. package/package.json +21 -9
@@ -1 +1 @@
1
- "use strict";var FrostpillarBTreeCore=(()=>{var $=Object.defineProperty;var Tn=Object.getOwnPropertyDescriptor;var sn=Object.getOwnPropertyNames;var cn=Object.prototype.hasOwnProperty;var fn=(e,n)=>{for(var t in n)$(e,t,{get:n[t],enumerable:!0})},dn=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let l of sn(n))!cn.call(e,l)&&l!==t&&$(e,l,{get:()=>n[l],enumerable:!(r=Tn(n,l))||r.enumerable});return e};var pn=e=>dn($({},"__esModule",{value:!0}),e);var Un={};fn(Un,{InMemoryBTree:()=>he});var c=class extends Error{constructor(n){super(n),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(n){super(n),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}};var S=(e,n)=>({kind:0,entries:e,entryOffset:0,parent:n,indexInParent:0,prev:null,next:null}),v=(e,n)=>{let t=[],r={kind:1,children:e,keys:t,childOffset:0,parent:n,indexInParent:0};for(let l=0;l<e.length;l+=1){let o=e[l];o.parent=r,o.indexInParent=l;let a={key:void 0,sequence:0};if(!x(o,a))throw new u("branch child has no min key");t.push(a)}return r},T=e=>e.entries.length-e.entryOffset,y=(e,n)=>e.entries[e.entryOffset+n],M=e=>{if(e.entryOffset>=e.entries.length)return;let n=e.entries[e.entryOffset];return e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),n},Y=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},Q=(e,n)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=n):e.entries.unshift(n)},P=(e,n)=>{let t=e.entries.length-e.entryOffset,r=e.entryOffset+n;n<t-1-n?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,r),e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length-=e.entryOffset,e.entryOffset=0)):(e.entries.copyWithin(r,r+1),e.entries.length-=1)},Z=(e,n,t)=>{let r=e.entryOffset+n;if(e.entryOffset>0&&n<e.entries.length-e.entryOffset>>>1)e.entries.copyWithin(e.entryOffset-1,e.entryOffset,r),e.entryOffset-=1,e.entries[r-1]=t;else{let l=e.entries.length;r>=l?e.entries.push(t):(e.entries.push(e.entries[l-1]),e.entries.copyWithin(r+1,r,l),e.entries[r]=t)}},j=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},me=1,k=e=>{if(e.childOffset>0){let n=e.childOffset<=me?0:me;e.children.copyWithin(n,e.childOffset),e.children.length-=e.childOffset-n,e.keys.copyWithin(n,e.childOffset),e.keys.length-=e.childOffset-n,e.childOffset=n;for(let t=n;t<e.children.length;t+=1)e.children[t].indexInParent=t}},g=e=>e.children.length-e.childOffset,ee=(e,n,t,r)=>{let l=e.childOffset+n,o=e.children.length-e.childOffset;if(e.childOffset>0&&n<o>>>1){e.children.copyWithin(e.childOffset-1,e.childOffset,l),e.keys.copyWithin(e.childOffset-1,e.childOffset,l),e.childOffset-=1,e.children[l-1]=t,e.keys[l-1]=r;for(let a=e.childOffset;a<l;a+=1)e.children[a].indexInParent=a;t.indexInParent=l-1}else{e.children.splice(l,0,t),e.keys.splice(l,0,r);for(let a=l;a<e.children.length;a+=1)e.children[a].indexInParent=a}},ne=(e,n)=>{let t=n-e.childOffset,r=e.children.length-e.childOffset;if(t<r-1-t){e.children.copyWithin(e.childOffset+1,e.childOffset,n),e.keys.copyWithin(e.childOffset+1,e.childOffset,n),e.childOffset+=1;for(let l=e.childOffset;l<=n;l+=1)e.children[l].indexInParent=l;e.childOffset>=e.children.length>>>1&&k(e)}else{e.children.copyWithin(n,n+1),e.keys.copyWithin(n,n+1),e.children.length-=1,e.keys.length-=1;for(let l=n;l<e.children.length;l+=1)e.children[l].indexInParent=l}};var te=64,re=64,z=3,X=16384,A=0;var Ve=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new c("Invalid duplicateKeys option.");return e},Be=e=>{if(e===void 0)return"standard";if(e!=="standard"&&e!=="lazy")throw new c("Invalid deleteRebalancePolicy option.");return e},p=e=>Object.freeze(e),I=(e,n,t)=>Object.freeze({key:e,entryId:n,value:t}),K=e=>e.kind===A,x=(e,n)=>{if(e.kind===A){if(e.entryOffset>=e.entries.length)return!1;let t=e.entries[e.entryOffset];return n.key=t.key,n.sequence=t.entryId,!0}return e.childOffset>=e.keys.length?!1:(n.key=e.keys[e.childOffset].key,n.sequence=e.keys[e.childOffset].sequence,!0)},_=(e,n,t)=>{if(e===void 0)return t;if(!Number.isInteger(e)||e<z||e>X)throw new c(`${n}: integer ${z}\u2013${X} required.`);return e};var Kn=(e,n,t,r)=>{let l=n.childOffset;if(l>=n.children.length)throw new u("branch has no children");let o=l,a=l,i=n.keys.length-1;for(;a<=i;){let s=a+i>>>1,f=n.keys[s],d=e(f.key,t);(d!==0?d:f.sequence<r?-1:f.sequence>r?1:0)<=0?(o=s,a=s+1):i=s-1}return n.children[o]},h=(e,n,t)=>{let r=e.compareKeys,l=e.root;for(;l.kind!==A;)l=Kn(r,l,n,t);return l},m=(e,n,t,r)=>{let l=e.compareKeys,o=n.entryOffset,a=n.entries.length;for(;o<a;){let i=o+a>>>1,s=n.entries[i],f=l(s.key,t);(f!==0?f:s.entryId<r?-1:s.entryId>r?1:0)<0?o=i+1:a=i}return o-n.entryOffset},E=(e,n,t,r)=>{let l=e.compareKeys,o=n.entryOffset,a=n.entries.length;for(;o<a;){let i=o+a>>>1,s=n.entries[i],f=l(s.key,t);(f!==0?f:s.entryId<r?-1:s.entryId>r?1:0)<=0?o=i+1:a=i}return o-n.entryOffset},xe=(e,n,t,r)=>{let l=e.compareKeys,o=n,a=32-Math.clz32(e.entryCount+1);for(;a>0&&o.next!==null&&T(o.next)>0;){let i=y(o.next,0),s=l(i.key,t);if(s>0||s===0&&i.entryId>r)break;o=o.next,a-=1}if(a===0&&o.next!==null&&T(o.next)>0){let i=y(o.next,0),s=l(i.key,t);if(s<0||s===0&&i.entryId<=r)return h(e,t,r)}return o},R=(e,n)=>{if(e.entryCount===0)return null;let t=h(e,n,0),r=m(e,t,n,0);if(r>=T(t)&&(t.next===null||(t=t.next,r=m(e,t,n,0),r>=T(t)))||e.compareKeys(y(t,r).key,n)!==0)return null;let l=e._cursor;return l.leaf=t,l.index=r,l},Ee=(e,n)=>{if(e.entryCount===0)return null;let t=h(e,n,Number.MAX_SAFE_INTEGER),r=E(e,t,n,Number.MAX_SAFE_INTEGER);if(r===0&&(t.prev===null||(t=t.prev,r=T(t),r===0))||(r-=1,e.compareKeys(y(t,r).key,n)!==0))return null;let l=e._cursor;return l.leaf=t,l.index=r,l},Ne=(e,n)=>R(e,n)!==null,Le=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=h(e,n,Number.MAX_SAFE_INTEGER),l=E(e,r,n,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(l<T(r)){let o=y(r,l);if(t(o.key,n)>0)return o.key;l+=1}else r=r.next,l=0;return null},we=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=h(e,n,0),l=m(e,r,n,0),o=r,a=l-1;for(;o!==null;){for(;a>=0;){let i=y(o,a);if(t(i.key,n)<0)return i.key;a-=1}o=o.prev,o!==null&&(a=T(o)-1)}return null},be=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=h(e,n,0),l=m(e,r,n,0),o=e._cursor;if(l<T(r)){if(t(y(r,l).key,n)===0)return o.leaf=r,o.index=l,o}else if(r.next!==null){let a=m(e,r.next,n,0);if(a<T(r.next)&&t(y(r.next,a).key,n)===0)return o.leaf=r.next,o.index=a,o}if(l>0)return o.leaf=r,o.index=l-1,o;if(r.prev!==null){let a=T(r.prev);if(a>0)return o.leaf=r.prev,o.index=a-1,o}return null};function le(e,n,t,r){let l=e(n,t);if(l>0)return!0;let o=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive";return o&&a&&l===0}var oe=(e,n,t,r)=>{if(e.entryCount===0)return null;let l=e.compareKeys;if(le(l,n,t,r))return null;let o=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive",i=o?Number.MAX_SAFE_INTEGER:0,s=h(e,n,i),f=o?E(e,s,n,Number.MAX_SAFE_INTEGER):m(e,s,n,0);return{leaf:s,index:f,compare:l,upperExclusive:a}},ae=(e,n,t,r)=>{let l=oe(e,n,t,r);if(l===null)return 0;let o=l.leaf,a=l.index,{compare:i,upperExclusive:s}=l,f=0;for(;o!==null;){let d=T(o);if(a>=d){o=o.next,a=0;continue}let V=y(o,d-1).key;if(W(V,t,i,s)){f+=d-a,o=o.next,a=0;continue}return f+=ie(e,o,t,s,d)-a,f}return f},W=(e,n,t,r)=>{let l=t(e,n);return r?l<0:l<=0},ie=(e,n,t,r,l)=>{let o=r?0:Number.MAX_SAFE_INTEGER,a=r?m(e,n,t,o):E(e,n,t,o);return a<l?a:l},hn=200,mn=(e,n,t,r,l)=>{let o=T(n.leaf);if(o-n.index>=hn&&n.leaf.next!==null){let i=y(n.leaf,o-1).key;if(W(i,r,n.compare,n.upperExclusive))return new Array(ae(e,t,r,l))}return[]},Ce=(e,n,t,r,l,o)=>{if(l)for(let a=n;a<t;a+=1)r[o++]=p(y(e,a));else for(let a=n;a<t;a+=1)r.push(p(y(e,a)));return o},Vn=(e,n,t,r)=>{let{compare:l,upperExclusive:o}=n,a=n.leaf,i=n.index,s=0,f=r.length>0;for(;a!==null;){let d=T(a);if(i>=d){a=a.next,i=0;continue}let V=y(a,d-1).key;if(W(V,t,l,o)){s=Ce(a,i,d,r,f,s),a=a.next,i=0;continue}let C=ie(e,a,t,o,d);Ce(a,i,C,r,f,s);return}},Se=(e,n,t,r)=>{let l=oe(e,n,t,r);if(l===null)return[];let o=mn(e,l,n,t,r);return Vn(e,l,t,o),o},ve=(e,n,t,r,l)=>{let o=oe(e,n,t,l);if(o===null)return;let a=o.leaf,i=o.index,{compare:s,upperExclusive:f}=o;for(;a!==null;){let d=T(a);if(i>=d){a=a.next,i=0;continue}let V=y(a,d-1).key;if(W(V,t,s,f)){for(let N=i;N<d;N+=1)r(p(y(a,N)));a=a.next,i=0;continue}let C=ie(e,a,t,f,d);for(let N=i;N<C;N+=1)r(p(y(a,N)));return}};var B=e=>{let n=e;for(;n.parent!==null;){let t=n.indexInParent;if(!x(n,n.parent.keys[t])||t!==n.parent.childOffset)return;n=n.parent}},G=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},ge=e=>{if(K(e))throw new u("expected branch, got leaf");return e},D=(e,n)=>{if(n<e.childOffset||n>=e.children.length)throw new u("child index out of range");ne(e,n)},Bn=(e,n,t)=>{let r=n.children.pop();if(r===void 0)throw new u("left branch borrow failed");n.keys.pop(),r.parent=e;let l={key:void 0,sequence:0};if(!x(r,l))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=r,e.keys[e.childOffset]=l,r.indexInParent=e.childOffset;else{e.children.unshift(r),e.keys.unshift(l);for(let a=0;a<e.children.length;a+=1)e.children[a].indexInParent=a}let o=G(e);o.keys[t]={key:l.key,sequence:l.sequence},B(e)},xn=(e,n,t)=>{let r=n.childOffset;if(r>=n.children.length)throw new u("right branch borrow failed");let l=n.children[r];n.childOffset+=1,n.childOffset>=n.children.length>>>1&&k(n),e.children.push(l),l.parent=e;let o={key:void 0,sequence:0};if(!x(l,o))throw new u("borrowed child has no min key");e.keys.push(o),l.indexInParent=e.children.length-1;let a=G(e);x(n,a.keys[t+1])},En=(e,n,t,r)=>{for(let o=n.childOffset;o<n.children.length;o+=1){let a=n.children[o];a.parent=t,a.indexInParent=t.children.length,t.children.push(a),t.keys.push(n.keys[o])}let l=G(n);D(l,r),F(e,l)},Nn=(e,n,t,r)=>{for(let o=t.childOffset;o<t.children.length;o+=1){let a=t.children[o];a.parent=n,a.indexInParent=n.children.length,n.children.push(a),n.keys.push(t.keys[o])}let l=G(n);D(l,r+1),F(e,l)},Ln=(e,n)=>{let t=n>e.childOffset?ge(e.children[n-1]):null,r=n+1<e.children.length?ge(e.children[n+1]):null;return{left:t,right:r}},F=(e,n)=>{let t=g(n);if(n===e.root){if(t===1){let i=n.children[n.childOffset];i.parent=null,e.root=i,K(i)&&(e.leftmostLeaf=i,e.rightmostLeaf=i)}return}if(t>=e.minBranchChildren)return;let r=n.parent;if(r===null)throw new u("branch has no parent");let l=n.indexInParent,{left:o,right:a}=Ln(r,l);if(a!==null&&g(a)>e.minBranchChildren){xn(n,a,l);return}if(o!==null&&g(o)>e.minBranchChildren){Bn(n,o,l);return}if(o!==null){En(e,n,o,l);return}if(a!==null){Nn(e,n,a,l);return}throw new u("no branch siblings to rebalance")};var Ie=e=>{if(!K(e))throw new u("expected leaf, got branch");return e},Oe=(e,n)=>{n.prev!==null?n.prev.next=n.next:n.next!==null&&(e.leftmostLeaf=n.next),n.next!==null?n.next.prev=n.prev:n.prev!==null&&(e.rightmostLeaf=n.prev),n.prev=null,n.next=null},ke=(e,n)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),e.entries.push(...n.entries.slice(n.entryOffset))},ue=e=>Math.max(1,Math.ceil(e/4)),ye=e=>e.deleteRebalancePolicy==="lazy"?ue(e.minLeafEntries):e.minLeafEntries,wn=(e,n)=>{let t=n>e.childOffset?Ie(e.children[n-1]):null,r=n+1<e.children.length?Ie(e.children[n+1]):null;return{left:t,right:r}},bn=(e,n,t,r,l,o)=>{if(o!==null&&T(o)>e.minLeafEntries){let a=M(o);if(a===void 0)throw new u("right leaf borrow failed");return n.entries.push(a),x(o,t.keys[r+1]),!0}if(l!==null&&T(l)>e.minLeafEntries){let a=l.entries.pop();if(a===void 0)throw new u("left leaf borrow failed");return Q(n,a),t.keys[r]={key:a.key,sequence:a.entryId},B(n),!0}return!1},Cn=(e,n,t,r,l,o)=>{if(l!==null){ke(l,n),Oe(e,n),D(t,r),F(e,t);return}if(o!==null){ke(n,o),Oe(e,o),D(t,r+1),F(e,t);return}throw new u("no leaf siblings to rebalance")},w=(e,n)=>{if(n===e.root){e.entryCount===0&&(e.leftmostLeaf=n,e.rightmostLeaf=n);return}if(T(n)>=ye(e))return;let t=n.parent;if(t===null)throw new u("Leaf node has no parent during rebalance.");let r=n.indexInParent,{left:l,right:o}=wn(t,r);bn(e,n,t,r,l,o)||Cn(e,n,t,r,l,o)};var Sn=(e,n,t)=>{let r=t?Number.MAX_SAFE_INTEGER:0,l=h(e,n,r),o=t?E(e,l,n,Number.MAX_SAFE_INTEGER):m(e,l,n,0);return o>=T(l)?l.next===null?null:{leaf:l.next,idx:0}:{leaf:l,idx:o}},vn=(e,n,t,r)=>{let l=T(n),o=y(n,l-1),a=e.compareKeys(o.key,t);if(r?a<0:a<=0)return l;let i=r?0:Number.MAX_SAFE_INTEGER,s=r?m(e,n,t,i):E(e,n,t,i);return s<l?s:l},gn=(e,n,t,r)=>{if(e.entryKeys!==null)for(let f=t;f<t+r;f+=1)e.entryKeys.delete(y(n,f).entryId);let l=n.entryOffset+t;n.entries.copyWithin(l,l+r),n.entries.length-=r,e.entryCount-=r;let o=T(n)===0;t===0&&!o&&n.parent!==null&&B(n);let a=T(n),i=ye(e),s=e.minLeafEntries+4;for(;s>0&&n!==e.root&&T(n)<i&&(w(e,n),!(n.parent!==null&&n.parent.children[n.indexInParent]!==n));)s-=1;return o&&T(n)>0&&n.parent!==null&&n.parent.children[n.indexInParent]===n&&B(n),a},In=(e,n)=>n.parent===null?n===e.root:n.parent.children[n.indexInParent]===n,Ae=(e,n,t,r)=>{if(e.entryCount===0||le(e.compareKeys,n,t,r))return 0;let l=r?.lowerBound==="exclusive",o=r?.upperBound==="exclusive",a=0,i=!0,s=null,f=0;for(;e.entryCount>0;){if(i){let U=Sn(e,n,l);if(U===null)break;s=U.leaf,f=U.idx,i=!1}if(f>=T(s))break;let d=T(s),V=vn(e,s,t,o),C=V-f;if(C===0)break;let N=gn(e,s,f,C);if(a+=C,V<d)break;if(!In(e,s)){i=!0;continue}if(T(s)>N){i=!0;continue}if(s.next===null)break;s=s.next,f=0}return a};var b=e=>Math.ceil(e/2),O=[{threshold:0,maxLeaf:32,maxBranch:32},{threshold:1e3,maxLeaf:64,maxBranch:64},{threshold:1e4,maxLeaf:128,maxBranch:128},{threshold:1e5,maxLeaf:256,maxBranch:128},{threshold:1e6,maxLeaf:512,maxBranch:256}],L=e=>{let n=O[0];for(let t=1;t<O.length&&e>=O[t].threshold;t+=1)n=O[t];return n},Te=e=>{for(let n=1;n<O.length;n+=1)if(e<O[n].threshold)return O[n].threshold;return Number.MAX_SAFE_INTEGER},On=(e,n)=>{if(n&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new c("autoScale conflicts with explicit capacity.");if(n){let t=L(0);return{maxLeafEntries:t.maxLeaf,maxBranchChildren:t.maxBranch}}return{maxLeafEntries:_(e.maxLeafEntries,"maxLeafEntries",te),maxBranchChildren:_(e.maxBranchChildren,"maxBranchChildren",re)}},Re=e=>{if(typeof e.compareKeys!="function")throw new c("compareKeys must be a function.");let n=e.autoScale===!0,{maxLeafEntries:t,maxBranchChildren:r}=On(e,n),l=Ve(e.duplicateKeys),o=Be(e.deleteRebalancePolicy),a=S([],null);return{compareKeys:e.compareKeys,maxLeafEntries:t,maxBranchChildren:r,duplicateKeys:l,minLeafEntries:b(t),minBranchChildren:b(r),root:a,leftmostLeaf:a,rightmostLeaf:a,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:n,deleteRebalancePolicy:o,_nextAutoScaleThreshold:n?Te(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:a,index:0}}},H=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:n,maxBranch:t}=L(e.entryCount);n>e.maxLeafEntries&&(e.maxLeafEntries=n,e.minLeafEntries=b(n)),t>e.maxBranchChildren&&(e.maxBranchChildren=t,e.minBranchChildren=b(t)),e._nextAutoScaleThreshold=Te(e.entryCount)},se=(e,n,t)=>{if(!e.autoScale)return;let r=L(0),l=_(n,"maxLeafEntries",te),o=_(t,"maxBranchChildren",re);if(l<r.maxLeaf||o<r.maxBranch)throw new c("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=l,e.maxBranchChildren=o,e.minLeafEntries=b(l),e.minBranchChildren=b(o)},Me=e=>{let n=L(0);e.maxLeafEntries=n.maxLeaf,e.maxBranchChildren=n.maxBranch,e.minLeafEntries=b(n.maxLeaf),e.minBranchChildren=b(n.maxBranch),e._nextAutoScaleThreshold=Te(0)};var Pe=(e,n,t)=>{let r=[],l=0;for(;l<e;){let o=e-l;if(o>n&&o-n<t){let i=Math.ceil(o/2);r.push(l+i),r.push(e);break}let a=l+n<e?l+n:e;r.push(a),l=a}return r},kn=(e,n,t,r)=>{let l=Pe(n.length,e.maxLeafEntries,e.minLeafEntries),o=new Array(l.length),a=0;for(let i=0;i<l.length;i+=1){let s=l[i],f=new Array(s-a);for(let d=a;d<s;d+=1){let V=r+d;f[d-a]=I(n[d].key,V,n[d].value),t[d]=V,e.entryKeys!==null&&e.entryKeys.set(V,n[d].key)}o[i]=S(f,null),a=s}return o},_e=(e,n)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let t=e.nextSequence;if(t+n.length>Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");let r=new Array(n.length),l=kn(e,n,r,t);e.nextSequence=t+n.length,e.entryCount=n.length;for(let o=0;o<l.length;o+=1)o>0&&(l[o].prev=l[o-1]),o<l.length-1&&(l[o].next=l[o+1]);if(e.leftmostLeaf=l[0],e.rightmostLeaf=l[l.length-1],l.length===1)e.root=l[0];else{let o=l;for(;o.length>1;){let a=Pe(o.length,e.maxBranchChildren,e.minBranchChildren),i=new Array(a.length),s=0;for(let f=0;f<a.length;f+=1)i[f]=v(o.slice(s,a[f]),null),s=a[f];o=i}e.root=o[0]}return H(e),r};var De=(e,n,t,r)=>{let l={key:void 0,sequence:0};if(!x(r,l))throw new u("inserted child has no min key");r.parent=n;let o=t.indexInParent-n.childOffset+1;ee(n,o,r,l),g(n)>e.maxBranchChildren&&An(e,n)},Fe=(e,n)=>{j(n);let t=Math.ceil(n.entries.length/2),r={kind:A,entries:n.entries.splice(t),entryOffset:0,parent:n.parent,indexInParent:0,prev:n,next:n.next};if(n.next!==null?n.next.prev=r:e.rightmostLeaf=r,n.next=r,n.parent===null){e.root=v([n,r],null);return}De(e,n.parent,n,r)},An=(e,n)=>{k(n);let t=Math.ceil(n.children.length/2),r=v(n.children.splice(t),n.parent);if(n.keys.splice(t),n.parent===null){e.root=v([n,r],null);return}De(e,n.parent,n,r)};var ce=(e,n,t)=>{let r=h(e,n,t),l=m(e,r,n,t);return l>=T(r)||y(r,l).entryId!==t?null:{leaf:r,index:l}},fe=(e,n)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let t=e.entryKeys.get(n);if(t===void 0)return null;let r=ce(e,t,n);return r===null?null:y(r.leaf,r.index)},de=(e,n,t)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(n);if(r===void 0)return null;let l=ce(e,r,n);if(l===null)return null;let o=y(l.leaf,l.index),a=I(o.key,o.entryId,t);return l.leaf.entries[l.leaf.entryOffset+l.index]=a,a},pe=(e,n)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let t=e.entryKeys.get(n);if(t===void 0)return null;let r=ce(e,t,n);if(r===null)return null;let l=y(r.leaf,r.index);return P(r.leaf,r.index),e.entryCount-=1,e.entryKeys.delete(n),r.index===0&&T(r.leaf)>0&&r.leaf.parent!==null&&B(r.leaf),r.leaf!==e.root&&T(r.leaf)<e.minLeafEntries&&w(e,r.leaf),l};var Rn=(e,n,t,r)=>{if(e.duplicateKeys==="allow")return null;if(r>0){let l=y(n,r-1);if(e.compareKeys(l.key,t)===0)return{leaf:n,physIndex:n.entryOffset+r-1,entry:l}}else if(n.prev!==null&&T(n.prev)>0){let l=n.prev,o=T(l),a=y(l,o-1);if(e.compareKeys(a.key,t)===0)return{leaf:l,physIndex:l.entryOffset+o-1,entry:a}}return null},qe=(e,n,t,r)=>{let l=e.nextSequence,o=E(e,n,t,l),a=Rn(e,n,t,o);if(a!==null){if(e.duplicateKeys==="reject")throw new c("Duplicate key rejected.");return a.leaf.entries[a.physIndex]=I(a.entry.key,a.entry.entryId,r),a.entry.entryId}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");return e.nextSequence+=1,Z(n,o,I(t,l,r)),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(l,t),o===0&&n.parent!==null&&B(n),T(n)>e.maxLeafEntries&&Fe(e,n),H(e),l},Je=(e,n,t)=>{let r=h(e,n,e.nextSequence);return qe(e,r,n,t)},ze=e=>{if(e.entryCount===0)return null;let n=M(e.leftmostLeaf);if(n===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(n.entryId),T(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&B(e.leftmostLeaf),e.leftmostLeaf!==e.root&&T(e.leftmostLeaf)<e.minLeafEntries&&w(e,e.leftmostLeaf),n},Xe=e=>{if(e.entryCount===0)return null;let n=Y(e.rightmostLeaf);if(n===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(n.entryId),e.rightmostLeaf!==e.root&&T(e.rightmostLeaf)<e.minLeafEntries&&w(e,e.rightmostLeaf),n},We=(e,n)=>{let t=R(e,n);if(t===null)return null;let r=t.leaf,l=t.index,o=y(r,l);return P(r,l),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(o.entryId),l===0&&T(r)>0&&r.parent!==null&&B(r),r!==e.root&&T(r)<e.minLeafEntries&&w(e,r),o},Ge=(e,n)=>{if(n.length===0)return[];let t=e.duplicateKeys!=="allow";for(let r=1;r<n.length;r+=1){let l=e.compareKeys(n[r-1].key,n[r].key);if(l>0)throw new c("putMany: entries not in ascending order.");if(t&&l===0)throw new c(e.duplicateKeys==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(e.entryCount>0){let r=new Array(n.length),l=h(e,n[0].key,e.nextSequence);for(let o=0;o<n.length;o+=1){let a=n[o],i=xe(e,l,a.key,e.nextSequence);r[o]=qe(e,i,a.key,a.value),l=i}return r}return _e(e,n)};var He=e=>{let n={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale,deleteRebalancePolicy:e.deleteRebalancePolicy};return e.autoScale||(n.maxLeafEntries=e.maxLeafEntries,n.maxBranchChildren=e.maxBranchChildren),n},Ue=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let o=T(t);for(let a=0;a<o;a+=1){let i=y(t,a);n[r++]=[i.key,i.value]}t=t.next}let l={maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.deleteRebalancePolicy!=="standard"&&(l.deleteRebalancePolicy=e.deleteRebalancePolicy),{version:1,config:l,entries:n}},Mn=1e6,Pn=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new c(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new c("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new c("BTreeJSON: entries must be array.");if(e.entries.length>Mn)throw new c("BTreeJSON: entry count exceeds maximum.");for(let n=0;n<e.entries.length;n+=1){let t=e.entries[n];if(!Array.isArray(t)||t.length!==2)throw new c(`BTreeJSON: bad entries[${n}].`)}},_n=e=>{let n=(t,r)=>{if(!Number.isInteger(r)||r<z||r>X)throw new c(`BTreeJSON: invalid ${t}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new c(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new c("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new c("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new c("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new c("BTreeJSON: invalid maxBranchChildren.");if(n("maxLeafEntries",e.maxLeafEntries),n("maxBranchChildren",e.maxBranchChildren),e.autoScale){let t=L(0);if(e.maxLeafEntries<t.maxLeaf||e.maxBranchChildren<t.maxBranch)throw new c("BTreeJSON: autoScale capacity below tier-0.")}},$e=e=>{Pn(e),_n(e.config)},Ye=(e,n)=>{let t=e.config.duplicateKeys!=="allow";for(let r=1;r<e.entries.length;r+=1){let l=n(e.entries[r-1][0],e.entries[r][0]);if(l>0)throw new c("fromJSON: entries not sorted.");if(t&&l===0)throw new c('fromJSON: duplicate keys require duplicateKeys "allow".')}},Qe=(e,n)=>{let t=e.config,r={compareKeys:n,duplicateKeys:t.duplicateKeys,enableEntryIdLookup:t.enableEntryIdLookup,autoScale:t.autoScale,deleteRebalancePolicy:t.deleteRebalancePolicy};return t.autoScale||(r.maxLeafEntries=t.maxLeafEntries,r.maxBranchChildren=t.maxBranchChildren),r};var Ze=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let l=T(t);for(let o=0;o<l;o+=1)n[r++]=p(y(t,o));t=t.next}return n},je=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let l=T(t);for(let o=0;o<l;o+=1)n[r++]=y(t,o);t=t.next}return n},en=(e,n,t)=>{let r=e.leftmostLeaf;for(;r!==null;){let l=T(r);for(let o=0;o<l;o+=1)n.call(t,p(y(r,o)));r=r.next}};var Ke=e=>{if(K(e)){if(e.entryOffset>=e.entries.length)return null;let n=e.entries[e.entryOffset];return{key:n.key,sequence:n.entryId}}return e.childOffset>=e.keys.length?null:{key:e.keys[e.childOffset].key,sequence:e.keys[e.childOffset].sequence}},J=(e,n,t,r,l)=>{let o=e(n,r);return o!==0?o:t<l?-1:t>l?1:0},nn=e=>{if(K(e)){if(e.entryOffset>=e.entries.length)return null;let n=e.entries[e.entries.length-1];return{key:n.key,sequence:n.entryId}}return e.childOffset>=e.children.length?null:nn(e.children[e.children.length-1])},q=e=>{if(!Number.isFinite(e))throw new c("compareKeys must return a finite number.");return e},Dn=(e,n)=>{if(q(e(n,n))!==0)throw new c("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},Fn=(e,n,t,r)=>{let l=Math.sign(q(e(n,t))),o=Math.sign(q(e(t,r)));if(l<0&&o<0&&Math.sign(q(e(n,r)))>=0)throw new c("compareKeys must satisfy transitivity for observed key triples.");if(l>0&&o>0&&Math.sign(q(e(n,r)))<=0)throw new c("compareKeys must satisfy transitivity for observed key triples.")},tn=(e,n)=>{try{Dn(e.compareKeys,n)}catch(t){throw t instanceof c?new u(t.message):t}},rn=(e,n,t,r)=>{try{Fn(e.compareKeys,n,t,r)}catch(l){throw l instanceof c?new u(l.message):l}},qn=(e,n,t)=>{let r=nn(n),l=Ke(t);if(r===null||l===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(J(e.compareKeys,r.key,r.sequence,l.key,l.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let o=T(n),a=T(t);if(e.duplicateKeys!=="allow"&&o>0&&a>0&&e.compareKeys(y(n,o-1).key,y(t,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")},Jn=(e,n,t,r)=>{if(!K(n))throw new u("Leaf linkage cursor reached non-leaf node.");if(r.has(n))throw new u("Cycle detected in leaf linkage.");if(n.prev!==t)throw new u("Leaf prev pointer mismatch.");t!==null&&K(t)&&qn(e,t,n)},ln=(e,n)=>{if(e.entryCount===0){if(!K(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let t=new Set,r=e.leftmostLeaf,l=null,o=0;for(;r!==null;)Jn(e,r,l,t),t.add(r),l=r,r=r.next,o+=1;if(l!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(o!==n)throw new u("Leaf chain count mismatch with tree traversal count.")};var zn=(e,n)=>{let t=T(n);for(let r=0;r<t;r+=1)tn(e,y(n,r).key);for(let r=1;r<t;r+=1)if(J(e.compareKeys,y(n,r-1).key,y(n,r-1).entryId,y(n,r).key,y(n,r).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<t;r+=1)if(e.compareKeys(y(n,r-1).key,y(n,r).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<t;r+=1){let l=y(n,r-2),o=y(n,r-1),a=y(n,r);rn(e,l.key,o.key,a.key)}if(t>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},Xn=(e,n,t)=>{zn(e,n);let r=T(n),l=e.autoScale?Math.ceil(L(0).maxLeaf/2):e.minLeafEntries;if(e.deleteRebalancePolicy==="lazy"&&(l=ue(l)),n!==e.root&&r<l)throw new u("Non-root leaf node violates minimum occupancy.");let o=r===0?null:y(n,0),a=r===0?null:y(n,r-1),i=o===null?null:{key:o.key,sequence:o.entryId},s=a===null?null:{key:a.key,sequence:a.entryId};return{minKey:i,maxKey:s,leafDepth:r===0?null:t,leafCount:1,branchCount:0,entryCount:r}},Wn=(e,n)=>{let t=n.children.length-n.childOffset;if(t===0)throw new u("Branch node has zero children.");let r=e.autoScale?Math.ceil(L(0).maxBranch/2):e.minBranchChildren;if(n!==e.root&&t<r)throw new u("Non-root branch node violates minimum occupancy.");if(t>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(n.keys.length!==n.children.length)throw new u("Branch keys array length does not match children array length.")},Gn=(e,n,t,r)=>{let l=n.children[t];if(l.parent!==n)throw new u("Child-parent pointer mismatch in branch node.");if(l.indexInParent!==t)throw new u("Child indexInParent does not match actual position in parent.");let o=on(e,l,r+1);if(o.minKey===null||o.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let a=n.keys[t],i=Ke(l);if(i===null||J(e.compareKeys,a.key,a.sequence,i.key,i.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return o},Hn=(e,n,t)=>{if(n.leafDepth!==null&&t.leafDepth!==null&&t.leafDepth!==n.leafDepth)throw new u("Leaf depth mismatch detected in tree.");if(n.leafDepth===null&&t.leafDepth!==null&&(n.leafDepth=t.leafDepth),n.previousChildMax!==null&&J(e.compareKeys,n.previousChildMax.key,n.previousChildMax.sequence,t.minKey.key,t.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");n.minKey===null&&(n.minKey=t.minKey),n.maxKey=t.maxKey,n.previousChildMax=t.maxKey,n.leafCount+=t.leafCount,n.branchCount+=t.branchCount,n.entryCount+=t.entryCount},on=(e,n,t)=>{if(K(n))return Xn(e,n,t);Wn(e,n);let r={leafDepth:null,leafCount:0,branchCount:1,entryCount:0,minKey:null,maxKey:null,previousChildMax:null};for(let l=n.childOffset;l<n.children.length;l+=1){let o=Gn(e,n,l,t);Hn(e,r,o)}return{minKey:r.minKey,maxKey:r.maxKey,leafDepth:r.leafDepth,leafCount:r.leafCount,branchCount:r.branchCount,entryCount:r.entryCount}},an=e=>{let n=on(e,e.root,0);if(n.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");ln(e,n.leafCount)};var un=e=>{if(K(e))return{height:1,leafCount:1,branchCount:0};let n=0,t=0,r=1;for(let l=e.childOffset;l<e.children.length;l+=1){let o=e.children[l],a=un(o);a.height>n&&(n=a.height),t+=a.leafCount,r+=a.branchCount}return{height:n+1,leafCount:t,branchCount:r}},yn=e=>{let n=un(e.root);return{height:n.height,leafCount:n.leafCount,branchCount:n.branchCount,entryCount:e.entryCount}};var he=class e{constructor(n){this.state=Re(n)}put(n,t){return Je(this.state,n,t)}putMany(n){return Ge(this.state,n)}remove(n){let t=We(this.state,n);return t===null?null:p(t)}removeById(n){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let t=pe(this.state,n);return t===null?null:p(t)}peekById(n){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let t=fe(this.state,n);return t===null?null:p(t)}updateById(n,t){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let r=de(this.state,n,t);return r===null?null:p(r)}popFirst(){let n=ze(this.state);return n===null?null:p(n)}peekFirst(){return this.state.entryCount===0?null:p(y(this.state.leftmostLeaf,0))}peekLast(){if(this.state.entryCount===0)return null;let n=this.state.rightmostLeaf;return p(y(n,T(n)-1))}popLast(){let n=Xe(this.state);return n===null?null:p(n)}clear(){let n=S([],null);this.state.root=n,this.state.leftmostLeaf=n,this.state.rightmostLeaf=n,this.state.entryCount=0,this.state._cursor.leaf=n,this.state._cursor.index=0,this.state.entryKeys?.clear(),this.state.autoScale&&Me(this.state)}get(n){let t=R(this.state,n);return t===null?null:y(t.leaf,t.index).value}hasKey(n){return Ne(this.state,n)}findFirst(n){let t=R(this.state,n);return t===null?null:p(y(t.leaf,t.index))}findLast(n){let t=Ee(this.state,n);return t===null?null:p(y(t.leaf,t.index))}nextHigherKey(n){return Le(this.state,n)}nextLowerKey(n){return we(this.state,n)}getPairOrNextLower(n){let t=be(this.state,n);return t===null?null:p(y(t.leaf,t.index))}count(n,t,r){return ae(this.state,n,t,r)}deleteRange(n,t,r){return Ae(this.state,n,t,r)}range(n,t,r){return Se(this.state,n,t,r)}*entries(){let n=this.state.leftmostLeaf;for(;n!==null;){let t=T(n);for(let r=0;r<t;r+=1)yield p(y(n,r));n=n.next}}*entriesReversed(){let n=this.state.rightmostLeaf;for(;n!==null;){let t=T(n);for(let r=t-1;r>=0;r-=1)yield p(y(n,r));n=n.prev}}*keys(){for(let n of this.entries())yield n.key}*values(){for(let n of this.entries())yield n.value}[Symbol.iterator](){return this.entries()}forEachRange(n,t,r,l){ve(this.state,n,t,r,l)}forEach(n,t){en(this.state,n,t)}snapshot(){return Ze(this.state)}clone(){let n=new e(He(this.state));return se(n.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0&&n.putMany(je(this.state)),n}toJSON(){return Ue(this.state)}static fromJSON(n,t){$e(n),Ye(n,t);let r=new e(Qe(n,t));if(se(r.state,n.config.maxLeafEntries,n.config.maxBranchChildren),n.entries.length>0){let l=new Array(n.entries.length);for(let o=0;o<n.entries.length;o+=1)l[o]={key:n.entries[o][0],value:n.entries[o][1]};r.putMany(l)}return r}size(){return this.state.entryCount}assertInvariants(){an(this.state)}getStats(){return yn(this.state)}};return pn(Un);})();
1
+ "use strict";var FrostpillarBTreeCore=(()=>{var $=Object.defineProperty;var Tn=Object.getOwnPropertyDescriptor;var sn=Object.getOwnPropertyNames;var cn=Object.prototype.hasOwnProperty;var fn=(e,n)=>{for(var t in n)$(e,t,{get:n[t],enumerable:!0})},dn=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let l of sn(n))!cn.call(e,l)&&l!==t&&$(e,l,{get:()=>n[l],enumerable:!(r=Tn(n,l))||r.enumerable});return e};var pn=e=>dn($({},"__esModule",{value:!0}),e);var Un={};fn(Un,{InMemoryBTree:()=>he});var c=class extends Error{constructor(n){super(n),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(n){super(n),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}};var S=(e,n)=>({kind:0,entries:e,entryOffset:0,parent:n,indexInParent:0,prev:null,next:null}),v=(e,n)=>{let t=[],r={kind:1,children:e,keys:t,childOffset:0,parent:n,indexInParent:0};for(let l=0;l<e.length;l+=1){let o=e[l];o.parent=r,o.indexInParent=l;let a={key:void 0,sequence:0};if(!x(o,a))throw new u("branch child has no min key");t.push(a)}return r},T=e=>e.entries.length-e.entryOffset,y=(e,n)=>e.entries[e.entryOffset+n],M=e=>{if(e.entryOffset>=e.entries.length)return;let n=e.entries[e.entryOffset];return e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),n},Y=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},Q=(e,n)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=n):e.entries.unshift(n)},P=(e,n)=>{let t=e.entries.length-e.entryOffset,r=e.entryOffset+n;n<t-1-n?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,r),e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length-=e.entryOffset,e.entryOffset=0)):(e.entries.copyWithin(r,r+1),e.entries.length-=1)},Z=(e,n,t)=>{let r=e.entryOffset+n;if(e.entryOffset>0&&n<e.entries.length-e.entryOffset>>>1)e.entries.copyWithin(e.entryOffset-1,e.entryOffset,r),e.entryOffset-=1,e.entries[r-1]=t;else{let l=e.entries.length;r>=l?e.entries.push(t):(e.entries.push(e.entries[l-1]),e.entries.copyWithin(r+1,r,l),e.entries[r]=t)}},j=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},me=1,k=e=>{if(e.childOffset>0){let n=e.childOffset<=me?0:me;e.children.copyWithin(n,e.childOffset),e.children.length-=e.childOffset-n,e.keys.copyWithin(n,e.childOffset),e.keys.length-=e.childOffset-n,e.childOffset=n;for(let t=n;t<e.children.length;t+=1)e.children[t].indexInParent=t}},g=e=>e.children.length-e.childOffset,ee=(e,n,t,r)=>{let l=e.childOffset+n,o=e.children.length-e.childOffset;if(e.childOffset>0&&n<o>>>1){e.children.copyWithin(e.childOffset-1,e.childOffset,l),e.keys.copyWithin(e.childOffset-1,e.childOffset,l),e.childOffset-=1,e.children[l-1]=t,e.keys[l-1]=r;for(let a=e.childOffset;a<l;a+=1)e.children[a].indexInParent=a;t.indexInParent=l-1}else{e.children.splice(l,0,t),e.keys.splice(l,0,r);for(let a=l;a<e.children.length;a+=1)e.children[a].indexInParent=a}},ne=(e,n)=>{let t=n-e.childOffset,r=e.children.length-e.childOffset;if(t<r-1-t){e.children.copyWithin(e.childOffset+1,e.childOffset,n),e.keys.copyWithin(e.childOffset+1,e.childOffset,n),e.childOffset+=1;for(let l=e.childOffset;l<=n;l+=1)e.children[l].indexInParent=l;e.childOffset>=e.children.length>>>1&&k(e)}else{e.children.copyWithin(n,n+1),e.keys.copyWithin(n,n+1),e.children.length-=1,e.keys.length-=1;for(let l=n;l<e.children.length;l+=1)e.children[l].indexInParent=l}};var te=64,re=64,z=3,X=16384,A=0;var Ve=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new c("Invalid duplicateKeys option.");return e},Be=e=>{if(e===void 0)return"standard";if(e!=="standard"&&e!=="lazy")throw new c("Invalid deleteRebalancePolicy option.");return e},p=e=>Object.freeze(e),I=(e,n,t)=>Object.freeze({key:e,entryId:n,value:t}),K=e=>e.kind===A,x=(e,n)=>{if(e.kind===A){if(e.entryOffset>=e.entries.length)return!1;let t=e.entries[e.entryOffset];return n.key=t.key,n.sequence=t.entryId,!0}return e.childOffset>=e.keys.length?!1:(n.key=e.keys[e.childOffset].key,n.sequence=e.keys[e.childOffset].sequence,!0)},_=(e,n,t)=>{if(e===void 0)return t;if(!Number.isInteger(e)||e<z||e>X)throw new c(`${n}: integer ${z}\u2013${X} required.`);return e};var Kn=(e,n,t,r)=>{let l=n.childOffset;if(l>=n.children.length)throw new u("branch has no children");let o=l,a=l,i=n.keys.length-1;for(;a<=i;){let s=a+i>>>1,f=n.keys[s],d=e(f.key,t);(d!==0?d:f.sequence<r?-1:f.sequence>r?1:0)<=0?(o=s,a=s+1):i=s-1}return n.children[o]},h=(e,n,t)=>{let r=e.compareKeys,l=e.root;for(;l.kind!==A;)l=Kn(r,l,n,t);return l},m=(e,n,t,r)=>{let l=e.compareKeys,o=n.entryOffset,a=n.entries.length;for(;o<a;){let i=o+a>>>1,s=n.entries[i],f=l(s.key,t);(f!==0?f:s.entryId<r?-1:s.entryId>r?1:0)<0?o=i+1:a=i}return o-n.entryOffset},E=(e,n,t,r)=>{let l=e.compareKeys,o=n.entryOffset,a=n.entries.length;for(;o<a;){let i=o+a>>>1,s=n.entries[i],f=l(s.key,t);(f!==0?f:s.entryId<r?-1:s.entryId>r?1:0)<=0?o=i+1:a=i}return o-n.entryOffset},xe=(e,n,t,r)=>{let l=e.compareKeys,o=n,a=32-Math.clz32(e.entryCount+1);for(;a>0&&o.next!==null&&T(o.next)>0;){let i=y(o.next,0),s=l(i.key,t);if(s>0||s===0&&i.entryId>r)break;o=o.next,a-=1}if(a===0&&o.next!==null&&T(o.next)>0){let i=y(o.next,0),s=l(i.key,t);if(s<0||s===0&&i.entryId<=r)return h(e,t,r)}return o},R=(e,n)=>{if(e.entryCount===0)return null;let t=h(e,n,0),r=m(e,t,n,0);if(r>=T(t)&&(t.next===null||(t=t.next,r=m(e,t,n,0),r>=T(t)))||e.compareKeys(y(t,r).key,n)!==0)return null;let l=e._cursor;return l.leaf=t,l.index=r,l},Ee=(e,n)=>{if(e.entryCount===0)return null;let t=h(e,n,Number.MAX_SAFE_INTEGER),r=E(e,t,n,Number.MAX_SAFE_INTEGER);if(r===0&&(t.prev===null||(t=t.prev,r=T(t),r===0))||(r-=1,e.compareKeys(y(t,r).key,n)!==0))return null;let l=e._cursor;return l.leaf=t,l.index=r,l},Ne=(e,n)=>R(e,n)!==null,Le=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=h(e,n,Number.MAX_SAFE_INTEGER),l=E(e,r,n,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(l<T(r)){let o=y(r,l);if(t(o.key,n)>0)return o.key;l+=1}else r=r.next,l=0;return null},we=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=h(e,n,0),l=m(e,r,n,0),o=r,a=l-1;for(;o!==null;){for(;a>=0;){let i=y(o,a);if(t(i.key,n)<0)return i.key;a-=1}o=o.prev,o!==null&&(a=T(o)-1)}return null},be=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=h(e,n,0),l=m(e,r,n,0),o=e._cursor;if(l<T(r)){if(t(y(r,l).key,n)===0)return o.leaf=r,o.index=l,o}else if(r.next!==null){let a=m(e,r.next,n,0);if(a<T(r.next)&&t(y(r.next,a).key,n)===0)return o.leaf=r.next,o.index=a,o}if(l>0)return o.leaf=r,o.index=l-1,o;if(r.prev!==null){let a=T(r.prev);if(a>0)return o.leaf=r.prev,o.index=a-1,o}return null};function le(e,n,t,r){let l=e(n,t);if(l>0)return!0;let o=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive";return o&&a&&l===0}var oe=(e,n,t,r)=>{if(e.entryCount===0)return null;let l=e.compareKeys;if(le(l,n,t,r))return null;let o=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive",i=o?Number.MAX_SAFE_INTEGER:0,s=h(e,n,i),f=o?E(e,s,n,Number.MAX_SAFE_INTEGER):m(e,s,n,0);return{leaf:s,index:f,compare:l,upperExclusive:a}},ae=(e,n,t,r)=>{let l=oe(e,n,t,r);if(l===null)return 0;let o=l.leaf,a=l.index,{compare:i,upperExclusive:s}=l,f=0;for(;o!==null;){let d=T(o);if(a>=d){o=o.next,a=0;continue}let B=y(o,d-1).key;if(W(B,t,i,s)){f+=d-a,o=o.next,a=0;continue}return f+=ie(e,o,t,s,d)-a,f}return f},W=(e,n,t,r)=>{let l=t(e,n);return r?l<0:l<=0},ie=(e,n,t,r,l)=>{let o=r?0:Number.MAX_SAFE_INTEGER,a=r?m(e,n,t,o):E(e,n,t,o);return a<l?a:l},hn=200,mn=(e,n,t,r,l)=>{let o=T(n.leaf);if(o-n.index>=hn&&n.leaf.next!==null){let i=y(n.leaf,o-1).key;if(W(i,r,n.compare,n.upperExclusive))return new Array(ae(e,t,r,l))}return[]},Ce=(e,n,t,r,l,o)=>{if(l)for(let a=n;a<t;a+=1)r[o++]=p(y(e,a));else for(let a=n;a<t;a+=1)r.push(p(y(e,a)));return o},Vn=(e,n,t,r)=>{let{compare:l,upperExclusive:o}=n,a=n.leaf,i=n.index,s=0,f=r.length>0;for(;a!==null;){let d=T(a);if(i>=d){a=a.next,i=0;continue}let B=y(a,d-1).key;if(W(B,t,l,o)){s=Ce(a,i,d,r,f,s),a=a.next,i=0;continue}let C=ie(e,a,t,o,d);Ce(a,i,C,r,f,s);return}},Se=(e,n,t,r)=>{let l=oe(e,n,t,r);if(l===null)return[];let o=mn(e,l,n,t,r);return Vn(e,l,t,o),o},ve=(e,n,t,r,l)=>{let o=oe(e,n,t,l);if(o===null)return;let a=o.leaf,i=o.index,{compare:s,upperExclusive:f}=o;for(;a!==null;){let d=T(a);if(i>=d){a=a.next,i=0;continue}let B=y(a,d-1).key;if(W(B,t,s,f)){for(let N=i;N<d;N+=1)r(p(y(a,N)));a=a.next,i=0;continue}let C=ie(e,a,t,f,d);for(let N=i;N<C;N+=1)r(p(y(a,N)));return}};var V=e=>{let n=e;for(;n.parent!==null;){let t=n.indexInParent;if(!x(n,n.parent.keys[t])||t!==n.parent.childOffset)return;n=n.parent}},G=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},ge=e=>{if(K(e))throw new u("expected branch, got leaf");return e},D=(e,n)=>{if(n<e.childOffset||n>=e.children.length)throw new u("child index out of range");ne(e,n)},Bn=(e,n,t)=>{let r=n.children.pop();if(r===void 0)throw new u("left branch borrow failed");n.keys.pop(),r.parent=e;let l={key:void 0,sequence:0};if(!x(r,l))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=r,e.keys[e.childOffset]=l,r.indexInParent=e.childOffset;else{e.children.unshift(r),e.keys.unshift(l);for(let a=0;a<e.children.length;a+=1)e.children[a].indexInParent=a}let o=G(e);o.keys[t]={key:l.key,sequence:l.sequence},V(e)},xn=(e,n,t)=>{let r=n.childOffset;if(r>=n.children.length)throw new u("right branch borrow failed");let l=n.children[r];n.childOffset+=1,n.childOffset>=n.children.length>>>1&&k(n),e.children.push(l),l.parent=e;let o={key:void 0,sequence:0};if(!x(l,o))throw new u("borrowed child has no min key");e.keys.push(o),l.indexInParent=e.children.length-1;let a=G(e);x(n,a.keys[t+1])},En=(e,n,t,r)=>{for(let o=n.childOffset;o<n.children.length;o+=1){let a=n.children[o];a.parent=t,a.indexInParent=t.children.length,t.children.push(a),t.keys.push(n.keys[o])}let l=G(n);D(l,r),F(e,l)},Nn=(e,n,t,r)=>{for(let o=t.childOffset;o<t.children.length;o+=1){let a=t.children[o];a.parent=n,a.indexInParent=n.children.length,n.children.push(a),n.keys.push(t.keys[o])}let l=G(n);D(l,r+1),F(e,l)},Ln=(e,n)=>{let t=n>e.childOffset?ge(e.children[n-1]):null,r=n+1<e.children.length?ge(e.children[n+1]):null;return{left:t,right:r}},F=(e,n)=>{let t=g(n);if(n===e.root){if(t===1){let i=n.children[n.childOffset];i.parent=null,e.root=i,K(i)&&(e.leftmostLeaf=i,e.rightmostLeaf=i)}return}if(t>=e.minBranchChildren)return;let r=n.parent;if(r===null)throw new u("branch has no parent");let l=n.indexInParent,{left:o,right:a}=Ln(r,l);if(a!==null&&g(a)>e.minBranchChildren){xn(n,a,l);return}if(o!==null&&g(o)>e.minBranchChildren){Bn(n,o,l);return}if(o!==null){En(e,n,o,l);return}if(a!==null){Nn(e,n,a,l);return}throw new u("no branch siblings to rebalance")};var Ie=e=>{if(!K(e))throw new u("expected leaf, got branch");return e},Oe=(e,n)=>{n.prev!==null?n.prev.next=n.next:n.next!==null&&(e.leftmostLeaf=n.next),n.next!==null?n.next.prev=n.prev:n.prev!==null&&(e.rightmostLeaf=n.prev),n.prev=null,n.next=null},ke=(e,n)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),e.entries.push(...n.entries.slice(n.entryOffset))},ue=e=>Math.max(1,Math.ceil(e/4)),ye=e=>e.deleteRebalancePolicy==="lazy"?ue(e.minLeafEntries):e.minLeafEntries,wn=(e,n)=>{let t=n>e.childOffset?Ie(e.children[n-1]):null,r=n+1<e.children.length?Ie(e.children[n+1]):null;return{left:t,right:r}},bn=(e,n,t,r,l,o)=>{if(o!==null&&T(o)>e.minLeafEntries){let a=M(o);if(a===void 0)throw new u("right leaf borrow failed");let i=T(n)===0;return n.entries.push(a),x(o,t.keys[r+1]),i&&V(n),!0}if(l!==null&&T(l)>e.minLeafEntries){let a=l.entries.pop();if(a===void 0)throw new u("left leaf borrow failed");return Q(n,a),t.keys[r]={key:a.key,sequence:a.entryId},V(n),!0}return!1},Cn=(e,n,t,r,l,o)=>{if(l!==null){ke(l,n),Oe(e,n),D(t,r),F(e,t);return}if(o!==null){let a=T(n)===0;ke(n,o),Oe(e,o),a&&V(n),D(t,r+1),F(e,t);return}throw new u("no leaf siblings to rebalance")},w=(e,n)=>{if(n===e.root){e.entryCount===0&&(e.leftmostLeaf=n,e.rightmostLeaf=n);return}if(T(n)>=ye(e))return;let t=n.parent;if(t===null)throw new u("Leaf node has no parent during rebalance.");let r=n.indexInParent,{left:l,right:o}=wn(t,r);bn(e,n,t,r,l,o)||Cn(e,n,t,r,l,o)};var Sn=(e,n,t)=>{let r=t?Number.MAX_SAFE_INTEGER:0,l=h(e,n,r),o=t?E(e,l,n,Number.MAX_SAFE_INTEGER):m(e,l,n,0);return o>=T(l)?l.next===null?null:{leaf:l.next,idx:0}:{leaf:l,idx:o}},vn=(e,n,t,r)=>{let l=T(n),o=y(n,l-1),a=e.compareKeys(o.key,t);if(r?a<0:a<=0)return l;let i=r?0:Number.MAX_SAFE_INTEGER,s=r?m(e,n,t,i):E(e,n,t,i);return s<l?s:l},gn=(e,n,t,r)=>{if(e.entryKeys!==null)for(let f=t;f<t+r;f+=1)e.entryKeys.delete(y(n,f).entryId);let l=n.entryOffset+t;n.entries.copyWithin(l,l+r),n.entries.length-=r,e.entryCount-=r;let o=T(n)===0;t===0&&!o&&n.parent!==null&&V(n);let a=T(n),i=ye(e),s=e.minLeafEntries+4;for(;s>0&&n!==e.root&&T(n)<i&&(w(e,n),!(n.parent!==null&&n.parent.children[n.indexInParent]!==n));)s-=1;return a},In=(e,n)=>n.parent===null?n===e.root:n.parent.children[n.indexInParent]===n,Ae=(e,n,t,r)=>{if(e.entryCount===0||le(e.compareKeys,n,t,r))return 0;let l=r?.lowerBound==="exclusive",o=r?.upperBound==="exclusive",a=0,i=!0,s=null,f=0;for(;e.entryCount>0;){if(i){let U=Sn(e,n,l);if(U===null)break;s=U.leaf,f=U.idx,i=!1}if(f>=T(s))break;let d=T(s),B=vn(e,s,t,o),C=B-f;if(C===0)break;let N=gn(e,s,f,C);if(a+=C,B<d)break;if(!In(e,s)){i=!0;continue}if(T(s)>N){i=!0;continue}if(s.next===null)break;s=s.next,f=0}return a};var b=e=>Math.ceil(e/2),O=[{threshold:0,maxLeaf:32,maxBranch:32},{threshold:1e3,maxLeaf:64,maxBranch:64},{threshold:1e4,maxLeaf:128,maxBranch:128},{threshold:1e5,maxLeaf:256,maxBranch:128},{threshold:1e6,maxLeaf:512,maxBranch:256}],L=e=>{let n=O[0];for(let t=1;t<O.length&&e>=O[t].threshold;t+=1)n=O[t];return n},Te=e=>{for(let n=1;n<O.length;n+=1)if(e<O[n].threshold)return O[n].threshold;return Number.MAX_SAFE_INTEGER},On=(e,n)=>{if(n&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new c("autoScale conflicts with explicit capacity.");if(n){let t=L(0);return{maxLeafEntries:t.maxLeaf,maxBranchChildren:t.maxBranch}}return{maxLeafEntries:_(e.maxLeafEntries,"maxLeafEntries",te),maxBranchChildren:_(e.maxBranchChildren,"maxBranchChildren",re)}},Re=e=>{if(typeof e.compareKeys!="function")throw new c("compareKeys must be a function.");let n=e.autoScale===!0,{maxLeafEntries:t,maxBranchChildren:r}=On(e,n),l=Ve(e.duplicateKeys),o=Be(e.deleteRebalancePolicy),a=S([],null);return{compareKeys:e.compareKeys,maxLeafEntries:t,maxBranchChildren:r,duplicateKeys:l,minLeafEntries:b(t),minBranchChildren:b(r),root:a,leftmostLeaf:a,rightmostLeaf:a,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:n,deleteRebalancePolicy:o,_nextAutoScaleThreshold:n?Te(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:a,index:0}}},H=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:n,maxBranch:t}=L(e.entryCount);n>e.maxLeafEntries&&(e.maxLeafEntries=n,e.minLeafEntries=b(n)),t>e.maxBranchChildren&&(e.maxBranchChildren=t,e.minBranchChildren=b(t)),e._nextAutoScaleThreshold=Te(e.entryCount)},se=(e,n,t)=>{if(!e.autoScale)return;let r=L(0),l=_(n,"maxLeafEntries",te),o=_(t,"maxBranchChildren",re);if(l<r.maxLeaf||o<r.maxBranch)throw new c("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=l,e.maxBranchChildren=o,e.minLeafEntries=b(l),e.minBranchChildren=b(o)},Me=e=>{let n=L(0);e.maxLeafEntries=n.maxLeaf,e.maxBranchChildren=n.maxBranch,e.minLeafEntries=b(n.maxLeaf),e.minBranchChildren=b(n.maxBranch),e._nextAutoScaleThreshold=Te(0)};var Pe=(e,n,t)=>{let r=[],l=0;for(;l<e;){let o=e-l;if(o>n&&o-n<t){let i=Math.ceil(o/2);r.push(l+i),r.push(e);break}let a=l+n<e?l+n:e;r.push(a),l=a}return r},kn=(e,n,t,r)=>{let l=Pe(n.length,e.maxLeafEntries,e.minLeafEntries),o=new Array(l.length),a=0;for(let i=0;i<l.length;i+=1){let s=l[i],f=new Array(s-a);for(let d=a;d<s;d+=1){let B=r+d;f[d-a]=I(n[d].key,B,n[d].value),t[d]=B,e.entryKeys!==null&&e.entryKeys.set(B,n[d].key)}o[i]=S(f,null),a=s}return o},_e=(e,n)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let t=e.nextSequence;if(t+n.length>Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");let r=new Array(n.length),l=kn(e,n,r,t);e.nextSequence=t+n.length,e.entryCount=n.length;for(let o=0;o<l.length;o+=1)o>0&&(l[o].prev=l[o-1]),o<l.length-1&&(l[o].next=l[o+1]);if(e.leftmostLeaf=l[0],e.rightmostLeaf=l[l.length-1],l.length===1)e.root=l[0];else{let o=l;for(;o.length>1;){let a=Pe(o.length,e.maxBranchChildren,e.minBranchChildren),i=new Array(a.length),s=0;for(let f=0;f<a.length;f+=1)i[f]=v(o.slice(s,a[f]),null),s=a[f];o=i}e.root=o[0]}return H(e),r};var De=(e,n,t,r)=>{let l={key:void 0,sequence:0};if(!x(r,l))throw new u("inserted child has no min key");r.parent=n;let o=t.indexInParent-n.childOffset+1;ee(n,o,r,l),g(n)>e.maxBranchChildren&&An(e,n)},Fe=(e,n)=>{j(n);let t=Math.ceil(n.entries.length/2),r={kind:A,entries:n.entries.splice(t),entryOffset:0,parent:n.parent,indexInParent:0,prev:n,next:n.next};if(n.next!==null?n.next.prev=r:e.rightmostLeaf=r,n.next=r,n.parent===null){e.root=v([n,r],null);return}De(e,n.parent,n,r)},An=(e,n)=>{k(n);let t=Math.ceil(n.children.length/2),r=v(n.children.splice(t),n.parent);if(n.keys.splice(t),n.parent===null){e.root=v([n,r],null);return}De(e,n.parent,n,r)};var ce=(e,n,t)=>{let r=h(e,n,t),l=m(e,r,n,t);return l>=T(r)||y(r,l).entryId!==t?null:{leaf:r,index:l}},fe=(e,n)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let t=e.entryKeys.get(n);if(t===void 0)return null;let r=ce(e,t,n);return r===null?null:y(r.leaf,r.index)},de=(e,n,t)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(n);if(r===void 0)return null;let l=ce(e,r,n);if(l===null)return null;let o=y(l.leaf,l.index),a=I(o.key,o.entryId,t);return l.leaf.entries[l.leaf.entryOffset+l.index]=a,a},pe=(e,n)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let t=e.entryKeys.get(n);if(t===void 0)return null;let r=ce(e,t,n);if(r===null)return null;let l=y(r.leaf,r.index);return P(r.leaf,r.index),e.entryCount-=1,e.entryKeys.delete(n),r.index===0&&T(r.leaf)>0&&r.leaf.parent!==null&&V(r.leaf),r.leaf!==e.root&&T(r.leaf)<e.minLeafEntries&&w(e,r.leaf),l};var Rn=(e,n,t,r)=>{if(e.duplicateKeys==="allow")return null;if(r>0){let l=y(n,r-1);if(e.compareKeys(l.key,t)===0)return{leaf:n,physIndex:n.entryOffset+r-1,entry:l}}else if(n.prev!==null&&T(n.prev)>0){let l=n.prev,o=T(l),a=y(l,o-1);if(e.compareKeys(a.key,t)===0)return{leaf:l,physIndex:l.entryOffset+o-1,entry:a}}return null},qe=(e,n,t,r)=>{let l=e.nextSequence,o=E(e,n,t,l),a=Rn(e,n,t,o);if(a!==null){if(e.duplicateKeys==="reject")throw new c("Duplicate key rejected.");return a.leaf.entries[a.physIndex]=I(a.entry.key,a.entry.entryId,r),a.entry.entryId}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");return e.nextSequence+=1,Z(n,o,I(t,l,r)),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(l,t),o===0&&n.parent!==null&&V(n),T(n)>e.maxLeafEntries&&Fe(e,n),H(e),l},Je=(e,n,t)=>{let r=h(e,n,e.nextSequence);return qe(e,r,n,t)},ze=e=>{if(e.entryCount===0)return null;let n=M(e.leftmostLeaf);if(n===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(n.entryId),T(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&V(e.leftmostLeaf),e.leftmostLeaf!==e.root&&T(e.leftmostLeaf)<e.minLeafEntries&&w(e,e.leftmostLeaf),n},Xe=e=>{if(e.entryCount===0)return null;let n=Y(e.rightmostLeaf);if(n===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(n.entryId),e.rightmostLeaf!==e.root&&T(e.rightmostLeaf)<e.minLeafEntries&&w(e,e.rightmostLeaf),n},We=(e,n)=>{let t=R(e,n);if(t===null)return null;let r=t.leaf,l=t.index,o=y(r,l);return P(r,l),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(o.entryId),l===0&&T(r)>0&&r.parent!==null&&V(r),r!==e.root&&T(r)<e.minLeafEntries&&w(e,r),o},Ge=(e,n)=>{if(n.length===0)return[];let t=e.duplicateKeys!=="allow";for(let r=1;r<n.length;r+=1){let l=e.compareKeys(n[r-1].key,n[r].key);if(l>0)throw new c("putMany: entries not in ascending order.");if(t&&l===0)throw new c(e.duplicateKeys==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(e.entryCount>0){let r=new Array(n.length),l=h(e,n[0].key,e.nextSequence);for(let o=0;o<n.length;o+=1){let a=n[o],i=xe(e,l,a.key,e.nextSequence);r[o]=qe(e,i,a.key,a.value),l=i}return r}return _e(e,n)};var He=e=>{let n={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale,deleteRebalancePolicy:e.deleteRebalancePolicy};return e.autoScale||(n.maxLeafEntries=e.maxLeafEntries,n.maxBranchChildren=e.maxBranchChildren),n},Ue=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let o=T(t);for(let a=0;a<o;a+=1){let i=y(t,a);n[r++]=[i.key,i.value]}t=t.next}let l={maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.deleteRebalancePolicy!=="standard"&&(l.deleteRebalancePolicy=e.deleteRebalancePolicy),{version:1,config:l,entries:n}},Mn=1e6,Pn=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new c(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new c("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new c("BTreeJSON: entries must be array.");if(e.entries.length>Mn)throw new c("BTreeJSON: entry count exceeds maximum.");for(let n=0;n<e.entries.length;n+=1){let t=e.entries[n];if(!Array.isArray(t)||t.length!==2)throw new c(`BTreeJSON: bad entries[${n}].`)}},_n=e=>{let n=(t,r)=>{if(!Number.isInteger(r)||r<z||r>X)throw new c(`BTreeJSON: invalid ${t}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new c(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new c("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new c("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new c("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new c("BTreeJSON: invalid maxBranchChildren.");if(n("maxLeafEntries",e.maxLeafEntries),n("maxBranchChildren",e.maxBranchChildren),e.autoScale){let t=L(0);if(e.maxLeafEntries<t.maxLeaf||e.maxBranchChildren<t.maxBranch)throw new c("BTreeJSON: autoScale capacity below tier-0.")}},$e=e=>{Pn(e),_n(e.config)},Ye=(e,n)=>{let t=e.config.duplicateKeys!=="allow";for(let r=1;r<e.entries.length;r+=1){let l=n(e.entries[r-1][0],e.entries[r][0]);if(l>0)throw new c("fromJSON: entries not sorted.");if(t&&l===0)throw new c('fromJSON: duplicate keys require duplicateKeys "allow".')}},Qe=(e,n)=>{let t=e.config,r={compareKeys:n,duplicateKeys:t.duplicateKeys,enableEntryIdLookup:t.enableEntryIdLookup,autoScale:t.autoScale,deleteRebalancePolicy:t.deleteRebalancePolicy};return t.autoScale||(r.maxLeafEntries=t.maxLeafEntries,r.maxBranchChildren=t.maxBranchChildren),r};var Ze=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let l=T(t);for(let o=0;o<l;o+=1)n[r++]=p(y(t,o));t=t.next}return n},je=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let l=T(t);for(let o=0;o<l;o+=1)n[r++]=y(t,o);t=t.next}return n},en=(e,n,t)=>{let r=e.leftmostLeaf;for(;r!==null;){let l=T(r);for(let o=0;o<l;o+=1)n.call(t,p(y(r,o)));r=r.next}};var Ke=e=>{if(K(e)){if(e.entryOffset>=e.entries.length)return null;let n=e.entries[e.entryOffset];return{key:n.key,sequence:n.entryId}}return e.childOffset>=e.keys.length?null:{key:e.keys[e.childOffset].key,sequence:e.keys[e.childOffset].sequence}},J=(e,n,t,r,l)=>{let o=e(n,r);return o!==0?o:t<l?-1:t>l?1:0},nn=e=>{if(K(e)){if(e.entryOffset>=e.entries.length)return null;let n=e.entries[e.entries.length-1];return{key:n.key,sequence:n.entryId}}return e.childOffset>=e.children.length?null:nn(e.children[e.children.length-1])},q=e=>{if(!Number.isFinite(e))throw new c("compareKeys must return a finite number.");return e},Dn=(e,n)=>{if(q(e(n,n))!==0)throw new c("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},Fn=(e,n,t,r)=>{let l=Math.sign(q(e(n,t))),o=Math.sign(q(e(t,r)));if(l<0&&o<0&&Math.sign(q(e(n,r)))>=0)throw new c("compareKeys must satisfy transitivity for observed key triples.");if(l>0&&o>0&&Math.sign(q(e(n,r)))<=0)throw new c("compareKeys must satisfy transitivity for observed key triples.")},tn=(e,n)=>{try{Dn(e.compareKeys,n)}catch(t){throw t instanceof c?new u(t.message):t}},rn=(e,n,t,r)=>{try{Fn(e.compareKeys,n,t,r)}catch(l){throw l instanceof c?new u(l.message):l}},qn=(e,n,t)=>{let r=nn(n),l=Ke(t);if(r===null||l===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(J(e.compareKeys,r.key,r.sequence,l.key,l.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let o=T(n),a=T(t);if(e.duplicateKeys!=="allow"&&o>0&&a>0&&e.compareKeys(y(n,o-1).key,y(t,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")},Jn=(e,n,t,r)=>{if(!K(n))throw new u("Leaf linkage cursor reached non-leaf node.");if(r.has(n))throw new u("Cycle detected in leaf linkage.");if(n.prev!==t)throw new u("Leaf prev pointer mismatch.");t!==null&&K(t)&&qn(e,t,n)},ln=(e,n)=>{if(e.entryCount===0){if(!K(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let t=new Set,r=e.leftmostLeaf,l=null,o=0;for(;r!==null;)Jn(e,r,l,t),t.add(r),l=r,r=r.next,o+=1;if(l!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(o!==n)throw new u("Leaf chain count mismatch with tree traversal count.")};var zn=(e,n)=>{let t=T(n);for(let r=0;r<t;r+=1)tn(e,y(n,r).key);for(let r=1;r<t;r+=1)if(J(e.compareKeys,y(n,r-1).key,y(n,r-1).entryId,y(n,r).key,y(n,r).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<t;r+=1)if(e.compareKeys(y(n,r-1).key,y(n,r).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<t;r+=1){let l=y(n,r-2),o=y(n,r-1),a=y(n,r);rn(e,l.key,o.key,a.key)}if(t>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},Xn=(e,n,t)=>{zn(e,n);let r=T(n),l=e.autoScale?Math.ceil(L(0).maxLeaf/2):e.minLeafEntries;if(e.deleteRebalancePolicy==="lazy"&&(l=ue(l)),n!==e.root&&r<l)throw new u("Non-root leaf node violates minimum occupancy.");let o=r===0?null:y(n,0),a=r===0?null:y(n,r-1),i=o===null?null:{key:o.key,sequence:o.entryId},s=a===null?null:{key:a.key,sequence:a.entryId};return{minKey:i,maxKey:s,leafDepth:r===0?null:t,leafCount:1,branchCount:0,entryCount:r}},Wn=(e,n)=>{let t=n.children.length-n.childOffset;if(t===0)throw new u("Branch node has zero children.");let r=e.autoScale?Math.ceil(L(0).maxBranch/2):e.minBranchChildren;if(n!==e.root&&t<r)throw new u("Non-root branch node violates minimum occupancy.");if(t>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(n.keys.length!==n.children.length)throw new u("Branch keys array length does not match children array length.")},Gn=(e,n,t,r)=>{let l=n.children[t];if(l.parent!==n)throw new u("Child-parent pointer mismatch in branch node.");if(l.indexInParent!==t)throw new u("Child indexInParent does not match actual position in parent.");let o=on(e,l,r+1);if(o.minKey===null||o.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let a=n.keys[t],i=Ke(l);if(i===null||J(e.compareKeys,a.key,a.sequence,i.key,i.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return o},Hn=(e,n,t)=>{if(n.leafDepth!==null&&t.leafDepth!==null&&t.leafDepth!==n.leafDepth)throw new u("Leaf depth mismatch detected in tree.");if(n.leafDepth===null&&t.leafDepth!==null&&(n.leafDepth=t.leafDepth),n.previousChildMax!==null&&J(e.compareKeys,n.previousChildMax.key,n.previousChildMax.sequence,t.minKey.key,t.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");n.minKey===null&&(n.minKey=t.minKey),n.maxKey=t.maxKey,n.previousChildMax=t.maxKey,n.leafCount+=t.leafCount,n.branchCount+=t.branchCount,n.entryCount+=t.entryCount},on=(e,n,t)=>{if(K(n))return Xn(e,n,t);Wn(e,n);let r={leafDepth:null,leafCount:0,branchCount:1,entryCount:0,minKey:null,maxKey:null,previousChildMax:null};for(let l=n.childOffset;l<n.children.length;l+=1){let o=Gn(e,n,l,t);Hn(e,r,o)}return{minKey:r.minKey,maxKey:r.maxKey,leafDepth:r.leafDepth,leafCount:r.leafCount,branchCount:r.branchCount,entryCount:r.entryCount}},an=e=>{let n=on(e,e.root,0);if(n.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");ln(e,n.leafCount)};var un=e=>{if(K(e))return{height:1,leafCount:1,branchCount:0};let n=0,t=0,r=1;for(let l=e.childOffset;l<e.children.length;l+=1){let o=e.children[l],a=un(o);a.height>n&&(n=a.height),t+=a.leafCount,r+=a.branchCount}return{height:n+1,leafCount:t,branchCount:r}},yn=e=>{let n=un(e.root);return{height:n.height,leafCount:n.leafCount,branchCount:n.branchCount,entryCount:e.entryCount}};var he=class e{constructor(n){this.state=Re(n)}put(n,t){return Je(this.state,n,t)}putMany(n){return Ge(this.state,n)}remove(n){let t=We(this.state,n);return t===null?null:p(t)}removeById(n){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let t=pe(this.state,n);return t===null?null:p(t)}peekById(n){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let t=fe(this.state,n);return t===null?null:p(t)}updateById(n,t){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let r=de(this.state,n,t);return r===null?null:p(r)}popFirst(){let n=ze(this.state);return n===null?null:p(n)}peekFirst(){return this.state.entryCount===0?null:p(y(this.state.leftmostLeaf,0))}peekLast(){if(this.state.entryCount===0)return null;let n=this.state.rightmostLeaf;return p(y(n,T(n)-1))}popLast(){let n=Xe(this.state);return n===null?null:p(n)}clear(){let n=S([],null);this.state.root=n,this.state.leftmostLeaf=n,this.state.rightmostLeaf=n,this.state.entryCount=0,this.state._cursor.leaf=n,this.state._cursor.index=0,this.state.entryKeys?.clear(),this.state.autoScale&&Me(this.state)}get(n){let t=R(this.state,n);return t===null?null:y(t.leaf,t.index).value}hasKey(n){return Ne(this.state,n)}findFirst(n){let t=R(this.state,n);return t===null?null:p(y(t.leaf,t.index))}findLast(n){let t=Ee(this.state,n);return t===null?null:p(y(t.leaf,t.index))}nextHigherKey(n){return Le(this.state,n)}nextLowerKey(n){return we(this.state,n)}getPairOrNextLower(n){let t=be(this.state,n);return t===null?null:p(y(t.leaf,t.index))}count(n,t,r){return ae(this.state,n,t,r)}deleteRange(n,t,r){return Ae(this.state,n,t,r)}range(n,t,r){return Se(this.state,n,t,r)}*entries(){let n=this.state.leftmostLeaf;for(;n!==null;){let t=T(n);for(let r=0;r<t;r+=1)yield p(y(n,r));n=n.next}}*entriesReversed(){let n=this.state.rightmostLeaf;for(;n!==null;){let t=T(n);for(let r=t-1;r>=0;r-=1)yield p(y(n,r));n=n.prev}}*keys(){for(let n of this.entries())yield n.key}*values(){for(let n of this.entries())yield n.value}[Symbol.iterator](){return this.entries()}forEachRange(n,t,r,l){ve(this.state,n,t,r,l)}forEach(n,t){en(this.state,n,t)}snapshot(){return Ze(this.state)}clone(){let n=new e(He(this.state));return se(n.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0&&n.putMany(je(this.state)),n}toJSON(){return Ue(this.state)}static fromJSON(n,t){$e(n),Ye(n,t);let r=new e(Qe(n,t));if(se(r.state,n.config.maxLeafEntries,n.config.maxBranchChildren),n.entries.length>0){let l=new Array(n.entries.length);for(let o=0;o<n.entries.length;o+=1)l[o]={key:n.entries[o][0],value:n.entries[o][1]};r.putMany(l)}return r}size(){return this.state.entryCount}assertInvariants(){an(this.state)}getStats(){return yn(this.state)}};return pn(Un);})();
@@ -1 +1 @@
1
- "use strict";var FrostpillarBTree=(()=>{var re=Object.defineProperty;var kt=Object.getOwnPropertyDescriptor;var Mt=Object.getOwnPropertyNames;var Ot=Object.prototype.hasOwnProperty;var Rt=(e,t)=>{for(var r in t)re(e,r,{get:t[r],enumerable:!0})},At=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Mt(t))!Ot.call(e,o)&&o!==r&&re(e,o,{get:()=>t[o],enumerable:!(n=kt(t,o))||n.enumerable});return e};var Pt=e=>At(re({},"__esModule",{value:!0}),e);var Er={};Rt(Er,{BTreeConcurrencyError:()=>f,BTreeInvariantError:()=>u,BTreeValidationError:()=>c,ConcurrentInMemoryBTree:()=>j,InMemoryBTree:()=>O});var c=class extends Error{constructor(t){super(t),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(t){super(t),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}},f=class extends Error{constructor(t){super(t),this.name="BTreeConcurrencyError",Object.setPrototypeOf(this,new.target.prototype)}};var g=(e,t)=>({kind:0,entries:e,entryOffset:0,parent:t,indexInParent:0,prev:null,next:null}),S=(e,t)=>{let r=[],n={kind:1,children:e,keys:r,childOffset:0,parent:t,indexInParent:0};for(let o=0;o<e.length;o+=1){let a=e[o];a.parent=n,a.indexInParent=o;let l={key:void 0,sequence:0};if(!E(a,l))throw new u("branch child has no min key");r.push(l)}return n},T=e=>e.entries.length-e.entryOffset,y=(e,t)=>e.entries[e.entryOffset+t],_=e=>{if(e.entryOffset>=e.entries.length)return;let t=e.entries[e.entryOffset];return e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),t},ne=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},oe=(e,t)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=t):e.entries.unshift(t)},F=(e,t)=>{let r=e.entries.length-e.entryOffset,n=e.entryOffset+t;t<r-1-t?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,n),e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length-=e.entryOffset,e.entryOffset=0)):(e.entries.copyWithin(n,n+1),e.entries.length-=1)},ae=(e,t,r)=>{let n=e.entryOffset+t;if(e.entryOffset>0&&t<e.entries.length-e.entryOffset>>>1)e.entries.copyWithin(e.entryOffset-1,e.entryOffset,n),e.entryOffset-=1,e.entries[n-1]=r;else{let o=e.entries.length;n>=o?e.entries.push(r):(e.entries.push(e.entries[o-1]),e.entries.copyWithin(n+1,n,o),e.entries[n]=r)}},le=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},Ee=1,R=e=>{if(e.childOffset>0){let t=e.childOffset<=Ee?0:Ee;e.children.copyWithin(t,e.childOffset),e.children.length-=e.childOffset-t,e.keys.copyWithin(t,e.childOffset),e.keys.length-=e.childOffset-t,e.childOffset=t;for(let r=t;r<e.children.length;r+=1)e.children[r].indexInParent=r}},C=e=>e.children.length-e.childOffset,ie=(e,t,r,n)=>{let o=e.childOffset+t,a=e.children.length-e.childOffset;if(e.childOffset>0&&t<a>>>1){e.children.copyWithin(e.childOffset-1,e.childOffset,o),e.keys.copyWithin(e.childOffset-1,e.childOffset,o),e.childOffset-=1,e.children[o-1]=r,e.keys[o-1]=n;for(let l=e.childOffset;l<o;l+=1)e.children[l].indexInParent=l;r.indexInParent=o-1}else{e.children.splice(o,0,r),e.keys.splice(o,0,n);for(let l=o;l<e.children.length;l+=1)e.children[l].indexInParent=l}},ue=(e,t)=>{let r=t-e.childOffset,n=e.children.length-e.childOffset;if(r<n-1-r){e.children.copyWithin(e.childOffset+1,e.childOffset,t),e.keys.copyWithin(e.childOffset+1,e.childOffset,t),e.childOffset+=1;for(let o=e.childOffset;o<=t;o+=1)e.children[o].indexInParent=o;e.childOffset>=e.children.length>>>1&&R(e)}else{e.children.copyWithin(t,t+1),e.keys.copyWithin(t,t+1),e.children.length-=1,e.keys.length-=1;for(let o=t;o<e.children.length;o+=1)e.children[o].indexInParent=o}};var D=64,q=64,W=3,G=16384,A=0;var we=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new c("Invalid duplicateKeys option.");return e},be=e=>{if(e===void 0)return"standard";if(e!=="standard"&&e!=="lazy")throw new c("Invalid deleteRebalancePolicy option.");return e},m=e=>Object.freeze(e),k=(e,t,r)=>Object.freeze({key:e,entryId:t,value:r}),h=e=>e.kind===A,E=(e,t)=>{if(e.kind===A){if(e.entryOffset>=e.entries.length)return!1;let r=e.entries[e.entryOffset];return t.key=r.key,t.sequence=r.entryId,!0}return e.childOffset>=e.keys.length?!1:(t.key=e.keys[e.childOffset].key,t.sequence=e.keys[e.childOffset].sequence,!0)},J=(e,t,r)=>{if(e===void 0)return r;if(!Number.isInteger(e)||e<W||e>G)throw new c(`${t}: integer ${W}\u2013${G} required.`);return e};var _t=(e,t,r,n)=>{let o=t.childOffset;if(o>=t.children.length)throw new u("branch has no children");let a=o,l=o,i=t.keys.length-1;for(;l<=i;){let s=l+i>>>1,d=t.keys[s],p=e(d.key,r);(p!==0?p:d.sequence<n?-1:d.sequence>n?1:0)<=0?(a=s,l=s+1):i=s-1}return t.children[a]},K=(e,t,r)=>{let n=e.compareKeys,o=e.root;for(;o.kind!==A;)o=_t(n,o,t,r);return o},B=(e,t,r,n)=>{let o=e.compareKeys,a=t.entryOffset,l=t.entries.length;for(;a<l;){let i=a+l>>>1,s=t.entries[i],d=o(s.key,r);(d!==0?d:s.entryId<n?-1:s.entryId>n?1:0)<0?a=i+1:l=i}return a-t.entryOffset},b=(e,t,r,n)=>{let o=e.compareKeys,a=t.entryOffset,l=t.entries.length;for(;a<l;){let i=a+l>>>1,s=t.entries[i],d=o(s.key,r);(d!==0?d:s.entryId<n?-1:s.entryId>n?1:0)<=0?a=i+1:l=i}return a-t.entryOffset},Le=(e,t,r,n)=>{let o=e.compareKeys,a=t,l=32-Math.clz32(e.entryCount+1);for(;l>0&&a.next!==null&&T(a.next)>0;){let i=y(a.next,0),s=o(i.key,r);if(s>0||s===0&&i.entryId>n)break;a=a.next,l-=1}if(l===0&&a.next!==null&&T(a.next)>0){let i=y(a.next,0),s=o(i.key,r);if(s<0||s===0&&i.entryId<=n)return K(e,r,n)}return a},P=(e,t)=>{if(e.entryCount===0)return null;let r=K(e,t,0),n=B(e,r,t,0);if(n>=T(r)&&(r.next===null||(r=r.next,n=B(e,r,t,0),n>=T(r)))||e.compareKeys(y(r,n).key,t)!==0)return null;let o=e._cursor;return o.leaf=r,o.index=n,o},Ne=(e,t)=>{if(e.entryCount===0)return null;let r=K(e,t,Number.MAX_SAFE_INTEGER),n=b(e,r,t,Number.MAX_SAFE_INTEGER);if(n===0&&(r.prev===null||(r=r.prev,n=T(r),n===0))||(n-=1,e.compareKeys(y(r,n).key,t)!==0))return null;let o=e._cursor;return o.leaf=r,o.index=n,o},ve=(e,t)=>P(e,t)!==null,Ie=(e,t)=>{if(e.entryCount===0)return null;let r=e.compareKeys,n=K(e,t,Number.MAX_SAFE_INTEGER),o=b(e,n,t,Number.MAX_SAFE_INTEGER);for(;n!==null;)if(o<T(n)){let a=y(n,o);if(r(a.key,t)>0)return a.key;o+=1}else n=n.next,o=0;return null},ge=(e,t)=>{if(e.entryCount===0)return null;let r=e.compareKeys,n=K(e,t,0),o=B(e,n,t,0),a=n,l=o-1;for(;a!==null;){for(;l>=0;){let i=y(a,l);if(r(i.key,t)<0)return i.key;l-=1}a=a.prev,a!==null&&(l=T(a)-1)}return null},Se=(e,t)=>{if(e.entryCount===0)return null;let r=e.compareKeys,n=K(e,t,0),o=B(e,n,t,0),a=e._cursor;if(o<T(n)){if(r(y(n,o).key,t)===0)return a.leaf=n,a.index=o,a}else if(n.next!==null){let l=B(e,n.next,t,0);if(l<T(n.next)&&r(y(n.next,l).key,t)===0)return a.leaf=n.next,a.index=l,a}if(o>0)return a.leaf=n,a.index=o-1,a;if(n.prev!==null){let l=T(n.prev);if(l>0)return a.leaf=n.prev,a.index=l-1,a}return null};function ye(e,t,r,n){let o=e(t,r);if(o>0)return!0;let a=n?.lowerBound==="exclusive",l=n?.upperBound==="exclusive";return a&&l&&o===0}var se=(e,t,r,n)=>{if(e.entryCount===0)return null;let o=e.compareKeys;if(ye(o,t,r,n))return null;let a=n?.lowerBound==="exclusive",l=n?.upperBound==="exclusive",i=a?Number.MAX_SAFE_INTEGER:0,s=K(e,t,i),d=a?b(e,s,t,Number.MAX_SAFE_INTEGER):B(e,s,t,0);return{leaf:s,index:d,compare:o,upperExclusive:l}},Te=(e,t,r,n)=>{let o=se(e,t,r,n);if(o===null)return 0;let a=o.leaf,l=o.index,{compare:i,upperExclusive:s}=o,d=0;for(;a!==null;){let p=T(a);if(l>=p){a=a.next,l=0;continue}let V=y(a,p-1).key;if($(V,r,i,s)){d+=p-l,a=a.next,l=0;continue}return d+=ce(e,a,r,s,p)-l,d}return d},$=(e,t,r,n)=>{let o=r(e,t);return n?o<0:o<=0},ce=(e,t,r,n,o)=>{let a=n?0:Number.MAX_SAFE_INTEGER,l=n?B(e,t,r,a):b(e,t,r,a);return l<o?l:o},Ft=200,Dt=(e,t,r,n,o)=>{let a=T(t.leaf);if(a-t.index>=Ft&&t.leaf.next!==null){let i=y(t.leaf,a-1).key;if($(i,n,t.compare,t.upperExclusive))return new Array(Te(e,r,n,o))}return[]},Ce=(e,t,r,n,o,a)=>{if(o)for(let l=t;l<r;l+=1)n[a++]=m(y(e,l));else for(let l=t;l<r;l+=1)n.push(m(y(e,l)));return a},qt=(e,t,r,n)=>{let{compare:o,upperExclusive:a}=t,l=t.leaf,i=t.index,s=0,d=n.length>0;for(;l!==null;){let p=T(l);if(i>=p){l=l.next,i=0;continue}let V=y(l,p-1).key;if($(V,r,o,a)){s=Ce(l,i,p,n,d,s),l=l.next,i=0;continue}let I=ce(e,l,r,a,p);Ce(l,i,I,n,d,s);return}},ke=(e,t,r,n)=>{let o=se(e,t,r,n);if(o===null)return[];let a=Dt(e,o,t,r,n);return qt(e,o,r,a),a},Me=(e,t,r,n,o)=>{let a=se(e,t,r,o);if(a===null)return;let l=a.leaf,i=a.index,{compare:s,upperExclusive:d}=a;for(;l!==null;){let p=T(l);if(i>=p){l=l.next,i=0;continue}let V=y(l,p-1).key;if($(V,r,s,d)){for(let L=i;L<p;L+=1)n(m(y(l,L)));l=l.next,i=0;continue}let I=ce(e,l,r,d,p);for(let L=i;L<I;L+=1)n(m(y(l,L)));return}};var x=e=>{let t=e;for(;t.parent!==null;){let r=t.indexInParent;if(!E(t,t.parent.keys[r])||r!==t.parent.childOffset)return;t=t.parent}},Q=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},Oe=e=>{if(h(e))throw new u("expected branch, got leaf");return e},z=(e,t)=>{if(t<e.childOffset||t>=e.children.length)throw new u("child index out of range");ue(e,t)},Jt=(e,t,r)=>{let n=t.children.pop();if(n===void 0)throw new u("left branch borrow failed");t.keys.pop(),n.parent=e;let o={key:void 0,sequence:0};if(!E(n,o))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=n,e.keys[e.childOffset]=o,n.indexInParent=e.childOffset;else{e.children.unshift(n),e.keys.unshift(o);for(let l=0;l<e.children.length;l+=1)e.children[l].indexInParent=l}let a=Q(e);a.keys[r]={key:o.key,sequence:o.sequence},x(e)},zt=(e,t,r)=>{let n=t.childOffset;if(n>=t.children.length)throw new u("right branch borrow failed");let o=t.children[n];t.childOffset+=1,t.childOffset>=t.children.length>>>1&&R(t),e.children.push(o),o.parent=e;let a={key:void 0,sequence:0};if(!E(o,a))throw new u("borrowed child has no min key");e.keys.push(a),o.indexInParent=e.children.length-1;let l=Q(e);E(t,l.keys[r+1])},Ut=(e,t,r,n)=>{for(let a=t.childOffset;a<t.children.length;a+=1){let l=t.children[a];l.parent=r,l.indexInParent=r.children.length,r.children.push(l),r.keys.push(t.keys[a])}let o=Q(t);z(o,n),U(e,o)},Xt=(e,t,r,n)=>{for(let a=r.childOffset;a<r.children.length;a+=1){let l=r.children[a];l.parent=t,l.indexInParent=t.children.length,t.children.push(l),t.keys.push(r.keys[a])}let o=Q(t);z(o,n+1),U(e,o)},Ht=(e,t)=>{let r=t>e.childOffset?Oe(e.children[t-1]):null,n=t+1<e.children.length?Oe(e.children[t+1]):null;return{left:r,right:n}},U=(e,t)=>{let r=C(t);if(t===e.root){if(r===1){let i=t.children[t.childOffset];i.parent=null,e.root=i,h(i)&&(e.leftmostLeaf=i,e.rightmostLeaf=i)}return}if(r>=e.minBranchChildren)return;let n=t.parent;if(n===null)throw new u("branch has no parent");let o=t.indexInParent,{left:a,right:l}=Ht(n,o);if(l!==null&&C(l)>e.minBranchChildren){zt(t,l,o);return}if(a!==null&&C(a)>e.minBranchChildren){Jt(t,a,o);return}if(a!==null){Ut(e,t,a,o);return}if(l!==null){Xt(e,t,l,o);return}throw new u("no branch siblings to rebalance")};var Re=e=>{if(!h(e))throw new u("expected leaf, got branch");return e},Ae=(e,t)=>{t.prev!==null?t.prev.next=t.next:t.next!==null&&(e.leftmostLeaf=t.next),t.next!==null?t.next.prev=t.prev:t.prev!==null&&(e.rightmostLeaf=t.prev),t.prev=null,t.next=null},Pe=(e,t)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),e.entries.push(...t.entries.slice(t.entryOffset))},de=e=>Math.max(1,Math.ceil(e/4)),fe=e=>e.deleteRebalancePolicy==="lazy"?de(e.minLeafEntries):e.minLeafEntries,jt=(e,t)=>{let r=t>e.childOffset?Re(e.children[t-1]):null,n=t+1<e.children.length?Re(e.children[t+1]):null;return{left:r,right:n}},Wt=(e,t,r,n,o,a)=>{if(a!==null&&T(a)>e.minLeafEntries){let l=_(a);if(l===void 0)throw new u("right leaf borrow failed");return t.entries.push(l),E(a,r.keys[n+1]),!0}if(o!==null&&T(o)>e.minLeafEntries){let l=o.entries.pop();if(l===void 0)throw new u("left leaf borrow failed");return oe(t,l),r.keys[n]={key:l.key,sequence:l.entryId},x(t),!0}return!1},Gt=(e,t,r,n,o,a)=>{if(o!==null){Pe(o,t),Ae(e,t),z(r,n),U(e,r);return}if(a!==null){Pe(t,a),Ae(e,a),z(r,n+1),U(e,r);return}throw new u("no leaf siblings to rebalance")},N=(e,t)=>{if(t===e.root){e.entryCount===0&&(e.leftmostLeaf=t,e.rightmostLeaf=t);return}if(T(t)>=fe(e))return;let r=t.parent;if(r===null)throw new u("Leaf node has no parent during rebalance.");let n=t.indexInParent,{left:o,right:a}=jt(r,n);Wt(e,t,r,n,o,a)||Gt(e,t,r,n,o,a)};var $t=(e,t,r)=>{let n=r?Number.MAX_SAFE_INTEGER:0,o=K(e,t,n),a=r?b(e,o,t,Number.MAX_SAFE_INTEGER):B(e,o,t,0);return a>=T(o)?o.next===null?null:{leaf:o.next,idx:0}:{leaf:o,idx:a}},Qt=(e,t,r,n)=>{let o=T(t),a=y(t,o-1),l=e.compareKeys(a.key,r);if(n?l<0:l<=0)return o;let i=n?0:Number.MAX_SAFE_INTEGER,s=n?B(e,t,r,i):b(e,t,r,i);return s<o?s:o},Yt=(e,t,r,n)=>{if(e.entryKeys!==null)for(let d=r;d<r+n;d+=1)e.entryKeys.delete(y(t,d).entryId);let o=t.entryOffset+r;t.entries.copyWithin(o,o+n),t.entries.length-=n,e.entryCount-=n;let a=T(t)===0;r===0&&!a&&t.parent!==null&&x(t);let l=T(t),i=fe(e),s=e.minLeafEntries+4;for(;s>0&&t!==e.root&&T(t)<i&&(N(e,t),!(t.parent!==null&&t.parent.children[t.indexInParent]!==t));)s-=1;return a&&T(t)>0&&t.parent!==null&&t.parent.children[t.indexInParent]===t&&x(t),l},Zt=(e,t)=>t.parent===null?t===e.root:t.parent.children[t.indexInParent]===t,_e=(e,t,r,n)=>{if(e.entryCount===0||ye(e.compareKeys,t,r,n))return 0;let o=n?.lowerBound==="exclusive",a=n?.upperBound==="exclusive",l=0,i=!0,s=null,d=0;for(;e.entryCount>0;){if(i){let te=$t(e,t,o);if(te===null)break;s=te.leaf,d=te.idx,i=!1}if(d>=T(s))break;let p=T(s),V=Qt(e,s,r,a),I=V-d;if(I===0)break;let L=Yt(e,s,d,I);if(l+=I,V<p)break;if(!Zt(e,s)){i=!0;continue}if(T(s)>L){i=!0;continue}if(s.next===null)break;s=s.next,d=0}return l};var v=e=>Math.ceil(e/2),M=[{threshold:0,maxLeaf:32,maxBranch:32},{threshold:1e3,maxLeaf:64,maxBranch:64},{threshold:1e4,maxLeaf:128,maxBranch:128},{threshold:1e5,maxLeaf:256,maxBranch:128},{threshold:1e6,maxLeaf:512,maxBranch:256}],w=e=>{let t=M[0];for(let r=1;r<M.length&&e>=M[r].threshold;r+=1)t=M[r];return t},pe=e=>{for(let t=1;t<M.length;t+=1)if(e<M[t].threshold)return M[t].threshold;return Number.MAX_SAFE_INTEGER},er=(e,t)=>{if(t&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new c("autoScale conflicts with explicit capacity.");if(t){let r=w(0);return{maxLeafEntries:r.maxLeaf,maxBranchChildren:r.maxBranch}}return{maxLeafEntries:J(e.maxLeafEntries,"maxLeafEntries",D),maxBranchChildren:J(e.maxBranchChildren,"maxBranchChildren",q)}},Fe=e=>{if(typeof e.compareKeys!="function")throw new c("compareKeys must be a function.");let t=e.autoScale===!0,{maxLeafEntries:r,maxBranchChildren:n}=er(e,t),o=we(e.duplicateKeys),a=be(e.deleteRebalancePolicy),l=g([],null);return{compareKeys:e.compareKeys,maxLeafEntries:r,maxBranchChildren:n,duplicateKeys:o,minLeafEntries:v(r),minBranchChildren:v(n),root:l,leftmostLeaf:l,rightmostLeaf:l,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:t,deleteRebalancePolicy:a,_nextAutoScaleThreshold:t?pe(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:l,index:0}}},Y=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:t,maxBranch:r}=w(e.entryCount);t>e.maxLeafEntries&&(e.maxLeafEntries=t,e.minLeafEntries=v(t)),r>e.maxBranchChildren&&(e.maxBranchChildren=r,e.minBranchChildren=v(r)),e._nextAutoScaleThreshold=pe(e.entryCount)},me=(e,t,r)=>{if(!e.autoScale)return;let n=w(0),o=J(t,"maxLeafEntries",D),a=J(r,"maxBranchChildren",q);if(o<n.maxLeaf||a<n.maxBranch)throw new c("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=o,e.maxBranchChildren=a,e.minLeafEntries=v(o),e.minBranchChildren=v(a)},De=e=>{let t=w(0);e.maxLeafEntries=t.maxLeaf,e.maxBranchChildren=t.maxBranch,e.minLeafEntries=v(t.maxLeaf),e.minBranchChildren=v(t.maxBranch),e._nextAutoScaleThreshold=pe(0)};var qe=(e,t,r)=>{let n=[],o=0;for(;o<e;){let a=e-o;if(a>t&&a-t<r){let i=Math.ceil(a/2);n.push(o+i),n.push(e);break}let l=o+t<e?o+t:e;n.push(l),o=l}return n},tr=(e,t,r,n)=>{let o=qe(t.length,e.maxLeafEntries,e.minLeafEntries),a=new Array(o.length),l=0;for(let i=0;i<o.length;i+=1){let s=o[i],d=new Array(s-l);for(let p=l;p<s;p+=1){let V=n+p;d[p-l]=k(t[p].key,V,t[p].value),r[p]=V,e.entryKeys!==null&&e.entryKeys.set(V,t[p].key)}a[i]=g(d,null),l=s}return a},Je=(e,t)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let r=e.nextSequence;if(r+t.length>Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");let n=new Array(t.length),o=tr(e,t,n,r);e.nextSequence=r+t.length,e.entryCount=t.length;for(let a=0;a<o.length;a+=1)a>0&&(o[a].prev=o[a-1]),a<o.length-1&&(o[a].next=o[a+1]);if(e.leftmostLeaf=o[0],e.rightmostLeaf=o[o.length-1],o.length===1)e.root=o[0];else{let a=o;for(;a.length>1;){let l=qe(a.length,e.maxBranchChildren,e.minBranchChildren),i=new Array(l.length),s=0;for(let d=0;d<l.length;d+=1)i[d]=S(a.slice(s,l[d]),null),s=l[d];a=i}e.root=a[0]}return Y(e),n};var ze=(e,t,r,n)=>{let o={key:void 0,sequence:0};if(!E(n,o))throw new u("inserted child has no min key");n.parent=t;let a=r.indexInParent-t.childOffset+1;ie(t,a,n,o),C(t)>e.maxBranchChildren&&rr(e,t)},Ue=(e,t)=>{le(t);let r=Math.ceil(t.entries.length/2),n={kind:A,entries:t.entries.splice(r),entryOffset:0,parent:t.parent,indexInParent:0,prev:t,next:t.next};if(t.next!==null?t.next.prev=n:e.rightmostLeaf=n,t.next=n,t.parent===null){e.root=S([t,n],null);return}ze(e,t.parent,t,n)},rr=(e,t)=>{R(t);let r=Math.ceil(t.children.length/2),n=S(t.children.splice(r),t.parent);if(t.keys.splice(r),t.parent===null){e.root=S([t,n],null);return}ze(e,t.parent,t,n)};var he=(e,t,r)=>{let n=K(e,t,r),o=B(e,n,t,r);return o>=T(n)||y(n,o).entryId!==r?null:{leaf:n,index:o}},Ke=(e,t)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(t);if(r===void 0)return null;let n=he(e,r,t);return n===null?null:y(n.leaf,n.index)},Be=(e,t,r)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let n=e.entryKeys.get(t);if(n===void 0)return null;let o=he(e,n,t);if(o===null)return null;let a=y(o.leaf,o.index),l=k(a.key,a.entryId,r);return o.leaf.entries[o.leaf.entryOffset+o.index]=l,l},Ve=(e,t)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(t);if(r===void 0)return null;let n=he(e,r,t);if(n===null)return null;let o=y(n.leaf,n.index);return F(n.leaf,n.index),e.entryCount-=1,e.entryKeys.delete(t),n.index===0&&T(n.leaf)>0&&n.leaf.parent!==null&&x(n.leaf),n.leaf!==e.root&&T(n.leaf)<e.minLeafEntries&&N(e,n.leaf),o};var nr=(e,t,r,n)=>{if(e.duplicateKeys==="allow")return null;if(n>0){let o=y(t,n-1);if(e.compareKeys(o.key,r)===0)return{leaf:t,physIndex:t.entryOffset+n-1,entry:o}}else if(t.prev!==null&&T(t.prev)>0){let o=t.prev,a=T(o),l=y(o,a-1);if(e.compareKeys(l.key,r)===0)return{leaf:o,physIndex:o.entryOffset+a-1,entry:l}}return null},Xe=(e,t,r,n)=>{let o=e.nextSequence,a=b(e,t,r,o),l=nr(e,t,r,a);if(l!==null){if(e.duplicateKeys==="reject")throw new c("Duplicate key rejected.");return l.leaf.entries[l.physIndex]=k(l.entry.key,l.entry.entryId,n),l.entry.entryId}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");return e.nextSequence+=1,ae(t,a,k(r,o,n)),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(o,r),a===0&&t.parent!==null&&x(t),T(t)>e.maxLeafEntries&&Ue(e,t),Y(e),o},He=(e,t,r)=>{let n=K(e,t,e.nextSequence);return Xe(e,n,t,r)},je=e=>{if(e.entryCount===0)return null;let t=_(e.leftmostLeaf);if(t===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),T(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&x(e.leftmostLeaf),e.leftmostLeaf!==e.root&&T(e.leftmostLeaf)<e.minLeafEntries&&N(e,e.leftmostLeaf),t},We=e=>{if(e.entryCount===0)return null;let t=ne(e.rightmostLeaf);if(t===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),e.rightmostLeaf!==e.root&&T(e.rightmostLeaf)<e.minLeafEntries&&N(e,e.rightmostLeaf),t},Ge=(e,t)=>{let r=P(e,t);if(r===null)return null;let n=r.leaf,o=r.index,a=y(n,o);return F(n,o),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(a.entryId),o===0&&T(n)>0&&n.parent!==null&&x(n),n!==e.root&&T(n)<e.minLeafEntries&&N(e,n),a},$e=(e,t)=>{if(t.length===0)return[];let r=e.duplicateKeys!=="allow";for(let n=1;n<t.length;n+=1){let o=e.compareKeys(t[n-1].key,t[n].key);if(o>0)throw new c("putMany: entries not in ascending order.");if(r&&o===0)throw new c(e.duplicateKeys==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(e.entryCount>0){let n=new Array(t.length),o=K(e,t[0].key,e.nextSequence);for(let a=0;a<t.length;a+=1){let l=t[a],i=Le(e,o,l.key,e.nextSequence);n[a]=Xe(e,i,l.key,l.value),o=i}return n}return Je(e,t)};var Qe=e=>{let t={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale,deleteRebalancePolicy:e.deleteRebalancePolicy};return e.autoScale||(t.maxLeafEntries=e.maxLeafEntries,t.maxBranchChildren=e.maxBranchChildren),t},Ye=e=>{let t=new Array(e.entryCount),r=e.leftmostLeaf,n=0;for(;r!==null;){let a=T(r);for(let l=0;l<a;l+=1){let i=y(r,l);t[n++]=[i.key,i.value]}r=r.next}let o={maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.deleteRebalancePolicy!=="standard"&&(o.deleteRebalancePolicy=e.deleteRebalancePolicy),{version:1,config:o,entries:t}},or=1e6,ar=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new c(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new c("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new c("BTreeJSON: entries must be array.");if(e.entries.length>or)throw new c("BTreeJSON: entry count exceeds maximum.");for(let t=0;t<e.entries.length;t+=1){let r=e.entries[t];if(!Array.isArray(r)||r.length!==2)throw new c(`BTreeJSON: bad entries[${t}].`)}},lr=e=>{let t=(r,n)=>{if(!Number.isInteger(n)||n<W||n>G)throw new c(`BTreeJSON: invalid ${r}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new c(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new c("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new c("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new c("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new c("BTreeJSON: invalid maxBranchChildren.");if(t("maxLeafEntries",e.maxLeafEntries),t("maxBranchChildren",e.maxBranchChildren),e.autoScale){let r=w(0);if(e.maxLeafEntries<r.maxLeaf||e.maxBranchChildren<r.maxBranch)throw new c("BTreeJSON: autoScale capacity below tier-0.")}},Ze=e=>{ar(e),lr(e.config)},et=(e,t)=>{let r=e.config.duplicateKeys!=="allow";for(let n=1;n<e.entries.length;n+=1){let o=t(e.entries[n-1][0],e.entries[n][0]);if(o>0)throw new c("fromJSON: entries not sorted.");if(r&&o===0)throw new c('fromJSON: duplicate keys require duplicateKeys "allow".')}},tt=(e,t)=>{let r=e.config,n={compareKeys:t,duplicateKeys:r.duplicateKeys,enableEntryIdLookup:r.enableEntryIdLookup,autoScale:r.autoScale,deleteRebalancePolicy:r.deleteRebalancePolicy};return r.autoScale||(n.maxLeafEntries=r.maxLeafEntries,n.maxBranchChildren=r.maxBranchChildren),n};var rt=e=>{let t=new Array(e.entryCount),r=e.leftmostLeaf,n=0;for(;r!==null;){let o=T(r);for(let a=0;a<o;a+=1)t[n++]=m(y(r,a));r=r.next}return t},nt=e=>{let t=new Array(e.entryCount),r=e.leftmostLeaf,n=0;for(;r!==null;){let o=T(r);for(let a=0;a<o;a+=1)t[n++]=y(r,a);r=r.next}return t},ot=(e,t,r)=>{let n=e.leftmostLeaf;for(;n!==null;){let o=T(n);for(let a=0;a<o;a+=1)t.call(r,m(y(n,a)));n=n.next}};var xe=e=>{if(h(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entryOffset];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.keys.length?null:{key:e.keys[e.childOffset].key,sequence:e.keys[e.childOffset].sequence}},H=(e,t,r,n,o)=>{let a=e(t,n);return a!==0?a:r<o?-1:r>o?1:0},at=e=>{if(h(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entries.length-1];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.children.length?null:at(e.children[e.children.length-1])},X=e=>{if(!Number.isFinite(e))throw new c("compareKeys must return a finite number.");return e},ir=(e,t)=>{if(X(e(t,t))!==0)throw new c("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},ur=(e,t,r,n)=>{let o=Math.sign(X(e(t,r))),a=Math.sign(X(e(r,n)));if(o<0&&a<0&&Math.sign(X(e(t,n)))>=0)throw new c("compareKeys must satisfy transitivity for observed key triples.");if(o>0&&a>0&&Math.sign(X(e(t,n)))<=0)throw new c("compareKeys must satisfy transitivity for observed key triples.")},lt=(e,t)=>{try{ir(e.compareKeys,t)}catch(r){throw r instanceof c?new u(r.message):r}},it=(e,t,r,n)=>{try{ur(e.compareKeys,t,r,n)}catch(o){throw o instanceof c?new u(o.message):o}},yr=(e,t,r)=>{let n=at(t),o=xe(r);if(n===null||o===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(H(e.compareKeys,n.key,n.sequence,o.key,o.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let a=T(t),l=T(r);if(e.duplicateKeys!=="allow"&&a>0&&l>0&&e.compareKeys(y(t,a-1).key,y(r,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")},sr=(e,t,r,n)=>{if(!h(t))throw new u("Leaf linkage cursor reached non-leaf node.");if(n.has(t))throw new u("Cycle detected in leaf linkage.");if(t.prev!==r)throw new u("Leaf prev pointer mismatch.");r!==null&&h(r)&&yr(e,r,t)},ut=(e,t)=>{if(e.entryCount===0){if(!h(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let r=new Set,n=e.leftmostLeaf,o=null,a=0;for(;n!==null;)sr(e,n,o,r),r.add(n),o=n,n=n.next,a+=1;if(o!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(a!==t)throw new u("Leaf chain count mismatch with tree traversal count.")};var Tr=(e,t)=>{let r=T(t);for(let n=0;n<r;n+=1)lt(e,y(t,n).key);for(let n=1;n<r;n+=1)if(H(e.compareKeys,y(t,n-1).key,y(t,n-1).entryId,y(t,n).key,y(t,n).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let n=1;n<r;n+=1)if(e.compareKeys(y(t,n-1).key,y(t,n).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let n=2;n<r;n+=1){let o=y(t,n-2),a=y(t,n-1),l=y(t,n);it(e,o.key,a.key,l.key)}if(r>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},cr=(e,t,r)=>{Tr(e,t);let n=T(t),o=e.autoScale?Math.ceil(w(0).maxLeaf/2):e.minLeafEntries;if(e.deleteRebalancePolicy==="lazy"&&(o=de(o)),t!==e.root&&n<o)throw new u("Non-root leaf node violates minimum occupancy.");let a=n===0?null:y(t,0),l=n===0?null:y(t,n-1),i=a===null?null:{key:a.key,sequence:a.entryId},s=l===null?null:{key:l.key,sequence:l.entryId};return{minKey:i,maxKey:s,leafDepth:n===0?null:r,leafCount:1,branchCount:0,entryCount:n}},dr=(e,t)=>{let r=t.children.length-t.childOffset;if(r===0)throw new u("Branch node has zero children.");let n=e.autoScale?Math.ceil(w(0).maxBranch/2):e.minBranchChildren;if(t!==e.root&&r<n)throw new u("Non-root branch node violates minimum occupancy.");if(r>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(t.keys.length!==t.children.length)throw new u("Branch keys array length does not match children array length.")},fr=(e,t,r,n)=>{let o=t.children[r];if(o.parent!==t)throw new u("Child-parent pointer mismatch in branch node.");if(o.indexInParent!==r)throw new u("Child indexInParent does not match actual position in parent.");let a=yt(e,o,n+1);if(a.minKey===null||a.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let l=t.keys[r],i=xe(o);if(i===null||H(e.compareKeys,l.key,l.sequence,i.key,i.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return a},pr=(e,t,r)=>{if(t.leafDepth!==null&&r.leafDepth!==null&&r.leafDepth!==t.leafDepth)throw new u("Leaf depth mismatch detected in tree.");if(t.leafDepth===null&&r.leafDepth!==null&&(t.leafDepth=r.leafDepth),t.previousChildMax!==null&&H(e.compareKeys,t.previousChildMax.key,t.previousChildMax.sequence,r.minKey.key,r.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");t.minKey===null&&(t.minKey=r.minKey),t.maxKey=r.maxKey,t.previousChildMax=r.maxKey,t.leafCount+=r.leafCount,t.branchCount+=r.branchCount,t.entryCount+=r.entryCount},yt=(e,t,r)=>{if(h(t))return cr(e,t,r);dr(e,t);let n={leafDepth:null,leafCount:0,branchCount:1,entryCount:0,minKey:null,maxKey:null,previousChildMax:null};for(let o=t.childOffset;o<t.children.length;o+=1){let a=fr(e,t,o,r);pr(e,n,a)}return{minKey:n.minKey,maxKey:n.maxKey,leafDepth:n.leafDepth,leafCount:n.leafCount,branchCount:n.branchCount,entryCount:n.entryCount}},st=e=>{let t=yt(e,e.root,0);if(t.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");ut(e,t.leafCount)};var Tt=e=>{if(h(e))return{height:1,leafCount:1,branchCount:0};let t=0,r=0,n=1;for(let o=e.childOffset;o<e.children.length;o+=1){let a=e.children[o],l=Tt(a);l.height>t&&(t=l.height),r+=l.leafCount,n+=l.branchCount}return{height:t+1,leafCount:r,branchCount:n}},ct=e=>{let t=Tt(e.root);return{height:t.height,leafCount:t.leafCount,branchCount:t.branchCount,entryCount:e.entryCount}};var O=class e{constructor(t){this.state=Fe(t)}put(t,r){return He(this.state,t,r)}putMany(t){return $e(this.state,t)}remove(t){let r=Ge(this.state,t);return r===null?null:m(r)}removeById(t){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let r=Ve(this.state,t);return r===null?null:m(r)}peekById(t){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let r=Ke(this.state,t);return r===null?null:m(r)}updateById(t,r){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let n=Be(this.state,t,r);return n===null?null:m(n)}popFirst(){let t=je(this.state);return t===null?null:m(t)}peekFirst(){return this.state.entryCount===0?null:m(y(this.state.leftmostLeaf,0))}peekLast(){if(this.state.entryCount===0)return null;let t=this.state.rightmostLeaf;return m(y(t,T(t)-1))}popLast(){let t=We(this.state);return t===null?null:m(t)}clear(){let t=g([],null);this.state.root=t,this.state.leftmostLeaf=t,this.state.rightmostLeaf=t,this.state.entryCount=0,this.state._cursor.leaf=t,this.state._cursor.index=0,this.state.entryKeys?.clear(),this.state.autoScale&&De(this.state)}get(t){let r=P(this.state,t);return r===null?null:y(r.leaf,r.index).value}hasKey(t){return ve(this.state,t)}findFirst(t){let r=P(this.state,t);return r===null?null:m(y(r.leaf,r.index))}findLast(t){let r=Ne(this.state,t);return r===null?null:m(y(r.leaf,r.index))}nextHigherKey(t){return Ie(this.state,t)}nextLowerKey(t){return ge(this.state,t)}getPairOrNextLower(t){let r=Se(this.state,t);return r===null?null:m(y(r.leaf,r.index))}count(t,r,n){return Te(this.state,t,r,n)}deleteRange(t,r,n){return _e(this.state,t,r,n)}range(t,r,n){return ke(this.state,t,r,n)}*entries(){let t=this.state.leftmostLeaf;for(;t!==null;){let r=T(t);for(let n=0;n<r;n+=1)yield m(y(t,n));t=t.next}}*entriesReversed(){let t=this.state.rightmostLeaf;for(;t!==null;){let r=T(t);for(let n=r-1;n>=0;n-=1)yield m(y(t,n));t=t.prev}}*keys(){for(let t of this.entries())yield t.key}*values(){for(let t of this.entries())yield t.value}[Symbol.iterator](){return this.entries()}forEachRange(t,r,n,o){Me(this.state,t,r,n,o)}forEach(t,r){ot(this.state,t,r)}snapshot(){return rt(this.state)}clone(){let t=new e(Qe(this.state));return me(t.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0&&t.putMany(nt(this.state)),t}toJSON(){return Ye(this.state)}static fromJSON(t,r){Ze(t),et(t,r);let n=new e(tt(t,r));if(me(n.state,t.config.maxLeafEntries,t.config.maxBranchChildren),t.entries.length>0){let o=new Array(t.entries.length);for(let a=0;a<t.entries.length;a+=1)o[a]={key:t.entries[a][0],value:t.entries[a][1]};n.putMany(o)}return n}size(){return this.state.entryCount}assertInvariants(){st(this.state)}getStats(){return ct(this.state)}};var mr=16,dt=1024,hr=1e5,ft=1e6,pt=e=>{let t=e.autoScale===!0,r=t?w(0):void 0;return JSON.stringify({duplicateKeys:e.duplicateKeys??"replace",maxLeafEntries:e.maxLeafEntries??(r?r.maxLeaf:D),maxBranchChildren:e.maxBranchChildren??(r?r.maxBranch:q),enableEntryIdLookup:e.enableEntryIdLookup===!0,autoScale:t})},mt=e=>{let t=e;throw new f(`Unsupported mutation type from shared store: ${String(t.type)}`)},Kr=e=>{for(let t of e)if(typeof t!="object"||t===null||!("key"in t)||!("value"in t))throw new f("Malformed putMany mutation: each entry must have key and value.")},Br=(e,t)=>{if(typeof e.configFingerprint!="string")throw new f("Malformed init mutation: missing configFingerprint.");if(t!==void 0&&e.configFingerprint!==t)throw new f("Config mismatch: store peers must share identical tree config.")},Vr=e=>{switch(e.type){case"put":if(!("key"in e)||!("value"in e))throw new f("Malformed put mutation: missing key or value.");break;case"remove":if(!("key"in e))throw new f("Malformed remove mutation: missing key.");break;case"removeById":if(!("entryId"in e))throw new f("Malformed removeById mutation: missing entryId.");break;case"updateById":if(!("entryId"in e)||!("value"in e))throw new f("Malformed updateById mutation: missing entryId or value.");break;case"putMany":if(!("entries"in e)||!Array.isArray(e.entries))throw new f("Malformed putMany mutation: missing entries array.");Kr(e.entries);break;case"deleteRange":if(!("startKey"in e)||!("endKey"in e))throw new f("Malformed deleteRange mutation: missing startKey or endKey.");break;default:break}},xr=(e,t)=>{switch(e.type){case"init":Br(e,t);break;case"put":case"remove":case"removeById":case"updateById":case"putMany":case"deleteRange":Vr(e);break;case"popFirst":case"popLast":case"clear":break;default:throw new f(`Unsupported mutation type from shared store: ${String(e.type)}`)}},ht=(e,t)=>{for(let r of e){if(typeof r!="object"||r===null)throw new f("Malformed mutation: expected an object.");xr(r,t)}},Kt=e=>{if(e===void 0)return mr;if(!Number.isInteger(e)||e<1||e>dt)throw new f(`maxRetries: integer 1\u2013${dt} required.`);return e},Bt=e=>{if(e===void 0)return hr;if(!Number.isInteger(e)||e<1||e>ft)throw new f(`maxSyncMutationsPerBatch: integer 1\u2013${ft} required.`);return e},Vt=e=>{if(e===void 0)return"strong";if(e!=="strong"&&e!=="local")throw new f("readMode: must be 'strong' or 'local'.");return e};function xt(e,t){if(typeof t!="object"||t===null)throw new f("Store contract: append() must return {applied, version}.");let r=t;if(typeof r.applied!="boolean")throw new f("Store contract: applied must be boolean.");if(typeof r.version!="bigint")throw new f("Store contract: version must be bigint.");if(r.applied&&r.version<=e)throw new f("Store contract: applied version must exceed expected.");if(!r.applied&&r.version<e)throw new f("Store contract: rejected version must be >= expected.")}var Z=(e,t,r)=>{switch(t.type){case"init":return r(),null;case"put":return e.put(t.key,t.value);case"putMany":return e.putMany(t.entries);case"remove":return e.remove(t.key);case"removeById":return e.removeById(t.entryId);case"updateById":return e.updateById(t.entryId,t.value);case"popFirst":return e.popFirst();case"popLast":return e.popLast();case"deleteRange":return e.deleteRange(t.startKey,t.endKey,t.options);case"clear":return e.clear(),null;default:return mt(t)}},Et=(e,t,r)=>n=>{if(e==="reject"&&n.hasKey(t))throw new c("Duplicate key rejected.");return{type:"put",key:t,value:r}},wt=e=>t=>t.hasKey(e)?{type:"remove",key:e}:null,bt=e=>t=>t.peekById(e)!==null?{type:"removeById",entryId:e}:null,Lt=(e,t)=>r=>r.peekById(e)!==null?{type:"updateById",entryId:e,value:t}:null,Nt=()=>e=>e.peekFirst()!==null?{type:"popFirst"}:null,vt=()=>e=>e.peekLast()!==null?{type:"popLast"}:null,It=(e,t,r)=>n=>{let o=t!=="allow";for(let a=1;a<e.length;a+=1){let l=r(e[a-1].key,e[a].key);if(l>0)throw new c("putMany: entries not in ascending order.");if(o&&l===0)throw new c(t==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(t==="reject"){for(let a of e)if(n.hasKey(a.key))throw new c("Duplicate key rejected.")}return{type:"putMany",entries:e}},gt=(e,t,r)=>n=>n.count(e,t,r)===0?null:{type:"deleteRange",startKey:e,endKey:t,options:r},St=()=>()=>({type:"clear"});var Ct=(e,t)=>{if(typeof e.version!="bigint")throw new f("Store contract: version must be bigint.");if(!Array.isArray(e.mutations))throw new f("Store contract: mutations must be an array.");if(e.mutations.length>t)throw new f(`Sync batch exceeded limit (${String(t)}).`)};var ee=class{constructor(t,r,n,o,a,l){this.tree=t,this.store=r,this.maxRetries=n,this.maxSyncMutationsPerBatch=o,this.configFingerprint=a,this.readMode=l,this.currentVersion=0n,this.operationQueue=Promise.resolve(),this.initSeen=!1,this.corrupted=!1}async syncUnlocked(){let t=await this.store.getLogEntriesSince(this.currentVersion);if(Ct(t,this.maxSyncMutationsPerBatch),!(t.version<=this.currentVersion)){ht(t.mutations,this.configFingerprint);try{let r=()=>{this.initSeen=!0};for(let n of t.mutations)Z(this.tree,n,r);this.currentVersion=t.version}catch(r){this.corrupted=!0;let n=r instanceof Error?r.message:String(r);throw new f(`Replay failure: instance is permanently corrupted. Discard and create a new instance. Cause: ${n}`)}}}runExclusive(t){let r=async()=>{if(this.corrupted)throw new f("Instance is permanently corrupted. Discard and create a new instance.");return t()},n=this.operationQueue.then(r,r);return this.operationQueue=n.then(()=>{},()=>{}),n}readOp(t){return this.runExclusive(async()=>(this.readMode==="strong"&&await this.syncUnlocked(),t(this.tree)))}async appendAndApply(t){for(let r=0;r<this.maxRetries;r+=1){await this.syncUnlocked();let n=t(this.tree);if(n===null)return null;let o=this.currentVersion,a=this.initSeen?[n]:[{type:"init",configFingerprint:this.configFingerprint},n],l=await this.store.append(o,a);if(xt(o,l),l.applied)try{let i=()=>{this.initSeen=!0};for(let d of a){if(d===n)break;Z(this.tree,d,i)}let s=Z(this.tree,n,i);return this.currentVersion=l.version,s}catch(i){this.corrupted=!0;let s=i instanceof Error?i.message:String(i);throw new f(`Local apply failure after successful append: instance is permanently corrupted. Discard and create a new instance. Cause: ${s}`)}}throw new f(`Mutation failed after ${String(this.maxRetries)} retries.`)}writeOp(t){return this.runExclusive(async()=>this.appendAndApply(t))}};var j=class{constructor(t){this.compareKeys=t.compareKeys,this.duplicateKeys=t.duplicateKeys??"replace";let r=new O({compareKeys:t.compareKeys,maxLeafEntries:t.maxLeafEntries,maxBranchChildren:t.maxBranchChildren,duplicateKeys:t.duplicateKeys,enableEntryIdLookup:t.enableEntryIdLookup,autoScale:t.autoScale,deleteRebalancePolicy:t.deleteRebalancePolicy});this.coord=new ee(r,t.store,Kt(t.maxRetries),Bt(t.maxSyncMutationsPerBatch),pt(t),Vt(t.readMode))}async sync(){await this.coord.runExclusive(async()=>{await this.coord.syncUnlocked()})}async syncThenRead(t){return this.coord.runExclusive(async()=>(await this.coord.syncUnlocked(),t(this.coord.tree)))}async put(t,r){return this.coord.writeOp(Et(this.duplicateKeys,t,r))}async remove(t){return this.coord.writeOp(wt(t))}async removeById(t){return this.coord.writeOp(bt(t))}async updateById(t,r){return this.coord.writeOp(Lt(t,r))}async popFirst(){return this.coord.writeOp(Nt())}async popLast(){return this.coord.writeOp(vt())}async putMany(t){return t.length===0?[]:this.coord.writeOp(It(t,this.duplicateKeys,this.compareKeys))}async deleteRange(t,r,n){return await this.coord.writeOp(gt(t,r,n))??0}async clear(){await this.coord.writeOp(St())}async get(t){return this.coord.readOp(r=>r.get(t))}async hasKey(t){return this.coord.readOp(r=>r.hasKey(t))}async findFirst(t){return this.coord.readOp(r=>r.findFirst(t))}async findLast(t){return this.coord.readOp(r=>r.findLast(t))}async range(t,r,n){return this.coord.readOp(o=>o.range(t,r,n))}async snapshot(){return this.coord.readOp(t=>t.snapshot())}async size(){return this.coord.readOp(t=>t.size())}async assertInvariants(){await this.coord.readOp(t=>t.assertInvariants())}async getStats(){return this.coord.readOp(t=>t.getStats())}async peekFirst(){return this.coord.readOp(t=>t.peekFirst())}async peekLast(){return this.coord.readOp(t=>t.peekLast())}async peekById(t){return this.coord.readOp(r=>r.peekById(t))}async count(t,r,n){return this.coord.readOp(o=>o.count(t,r,n))}async nextHigherKey(t){return this.coord.readOp(r=>r.nextHigherKey(t))}async nextLowerKey(t){return this.coord.readOp(r=>r.nextLowerKey(t))}async getPairOrNextLower(t){return this.coord.readOp(r=>r.getPairOrNextLower(t))}async entries(){return this.coord.readOp(t=>Array.from(t.entries()))}async entriesReversed(){return this.coord.readOp(t=>Array.from(t.entriesReversed()))}async keys(){return this.coord.readOp(t=>Array.from(t.keys()))}async values(){return this.coord.readOp(t=>Array.from(t.values()))}async forEach(t){await this.coord.readOp(r=>{r.forEach(t)})}async forEachRange(t,r,n,o){await this.coord.readOp(a=>{a.forEachRange(t,r,n,o)})}async*[Symbol.asyncIterator](){let t=await this.entries();for(let r of t)yield r}async clone(){return this.coord.readOp(t=>t.clone())}async toJSON(){return this.coord.readOp(t=>t.toJSON())}static fromJSON(t,r){return O.fromJSON(t,r)}};return Pt(Er);})();
1
+ "use strict";var FrostpillarBTree=(()=>{var re=Object.defineProperty;var kt=Object.getOwnPropertyDescriptor;var Mt=Object.getOwnPropertyNames;var Ot=Object.prototype.hasOwnProperty;var Rt=(e,t)=>{for(var r in t)re(e,r,{get:t[r],enumerable:!0})},At=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Mt(t))!Ot.call(e,o)&&o!==r&&re(e,o,{get:()=>t[o],enumerable:!(n=kt(t,o))||n.enumerable});return e};var Pt=e=>At(re({},"__esModule",{value:!0}),e);var Er={};Rt(Er,{BTreeConcurrencyError:()=>f,BTreeInvariantError:()=>u,BTreeValidationError:()=>c,ConcurrentInMemoryBTree:()=>j,InMemoryBTree:()=>O});var c=class extends Error{constructor(t){super(t),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(t){super(t),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}},f=class extends Error{constructor(t){super(t),this.name="BTreeConcurrencyError",Object.setPrototypeOf(this,new.target.prototype)}};var g=(e,t)=>({kind:0,entries:e,entryOffset:0,parent:t,indexInParent:0,prev:null,next:null}),S=(e,t)=>{let r=[],n={kind:1,children:e,keys:r,childOffset:0,parent:t,indexInParent:0};for(let o=0;o<e.length;o+=1){let a=e[o];a.parent=n,a.indexInParent=o;let l={key:void 0,sequence:0};if(!E(a,l))throw new u("branch child has no min key");r.push(l)}return n},T=e=>e.entries.length-e.entryOffset,y=(e,t)=>e.entries[e.entryOffset+t],_=e=>{if(e.entryOffset>=e.entries.length)return;let t=e.entries[e.entryOffset];return e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),t},ne=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},oe=(e,t)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=t):e.entries.unshift(t)},F=(e,t)=>{let r=e.entries.length-e.entryOffset,n=e.entryOffset+t;t<r-1-t?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,n),e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length-=e.entryOffset,e.entryOffset=0)):(e.entries.copyWithin(n,n+1),e.entries.length-=1)},ae=(e,t,r)=>{let n=e.entryOffset+t;if(e.entryOffset>0&&t<e.entries.length-e.entryOffset>>>1)e.entries.copyWithin(e.entryOffset-1,e.entryOffset,n),e.entryOffset-=1,e.entries[n-1]=r;else{let o=e.entries.length;n>=o?e.entries.push(r):(e.entries.push(e.entries[o-1]),e.entries.copyWithin(n+1,n,o),e.entries[n]=r)}},le=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},Ee=1,R=e=>{if(e.childOffset>0){let t=e.childOffset<=Ee?0:Ee;e.children.copyWithin(t,e.childOffset),e.children.length-=e.childOffset-t,e.keys.copyWithin(t,e.childOffset),e.keys.length-=e.childOffset-t,e.childOffset=t;for(let r=t;r<e.children.length;r+=1)e.children[r].indexInParent=r}},C=e=>e.children.length-e.childOffset,ie=(e,t,r,n)=>{let o=e.childOffset+t,a=e.children.length-e.childOffset;if(e.childOffset>0&&t<a>>>1){e.children.copyWithin(e.childOffset-1,e.childOffset,o),e.keys.copyWithin(e.childOffset-1,e.childOffset,o),e.childOffset-=1,e.children[o-1]=r,e.keys[o-1]=n;for(let l=e.childOffset;l<o;l+=1)e.children[l].indexInParent=l;r.indexInParent=o-1}else{e.children.splice(o,0,r),e.keys.splice(o,0,n);for(let l=o;l<e.children.length;l+=1)e.children[l].indexInParent=l}},ue=(e,t)=>{let r=t-e.childOffset,n=e.children.length-e.childOffset;if(r<n-1-r){e.children.copyWithin(e.childOffset+1,e.childOffset,t),e.keys.copyWithin(e.childOffset+1,e.childOffset,t),e.childOffset+=1;for(let o=e.childOffset;o<=t;o+=1)e.children[o].indexInParent=o;e.childOffset>=e.children.length>>>1&&R(e)}else{e.children.copyWithin(t,t+1),e.keys.copyWithin(t,t+1),e.children.length-=1,e.keys.length-=1;for(let o=t;o<e.children.length;o+=1)e.children[o].indexInParent=o}};var D=64,q=64,W=3,G=16384,A=0;var we=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new c("Invalid duplicateKeys option.");return e},be=e=>{if(e===void 0)return"standard";if(e!=="standard"&&e!=="lazy")throw new c("Invalid deleteRebalancePolicy option.");return e},m=e=>Object.freeze(e),k=(e,t,r)=>Object.freeze({key:e,entryId:t,value:r}),h=e=>e.kind===A,E=(e,t)=>{if(e.kind===A){if(e.entryOffset>=e.entries.length)return!1;let r=e.entries[e.entryOffset];return t.key=r.key,t.sequence=r.entryId,!0}return e.childOffset>=e.keys.length?!1:(t.key=e.keys[e.childOffset].key,t.sequence=e.keys[e.childOffset].sequence,!0)},J=(e,t,r)=>{if(e===void 0)return r;if(!Number.isInteger(e)||e<W||e>G)throw new c(`${t}: integer ${W}\u2013${G} required.`);return e};var _t=(e,t,r,n)=>{let o=t.childOffset;if(o>=t.children.length)throw new u("branch has no children");let a=o,l=o,i=t.keys.length-1;for(;l<=i;){let s=l+i>>>1,d=t.keys[s],p=e(d.key,r);(p!==0?p:d.sequence<n?-1:d.sequence>n?1:0)<=0?(a=s,l=s+1):i=s-1}return t.children[a]},K=(e,t,r)=>{let n=e.compareKeys,o=e.root;for(;o.kind!==A;)o=_t(n,o,t,r);return o},B=(e,t,r,n)=>{let o=e.compareKeys,a=t.entryOffset,l=t.entries.length;for(;a<l;){let i=a+l>>>1,s=t.entries[i],d=o(s.key,r);(d!==0?d:s.entryId<n?-1:s.entryId>n?1:0)<0?a=i+1:l=i}return a-t.entryOffset},b=(e,t,r,n)=>{let o=e.compareKeys,a=t.entryOffset,l=t.entries.length;for(;a<l;){let i=a+l>>>1,s=t.entries[i],d=o(s.key,r);(d!==0?d:s.entryId<n?-1:s.entryId>n?1:0)<=0?a=i+1:l=i}return a-t.entryOffset},Le=(e,t,r,n)=>{let o=e.compareKeys,a=t,l=32-Math.clz32(e.entryCount+1);for(;l>0&&a.next!==null&&T(a.next)>0;){let i=y(a.next,0),s=o(i.key,r);if(s>0||s===0&&i.entryId>n)break;a=a.next,l-=1}if(l===0&&a.next!==null&&T(a.next)>0){let i=y(a.next,0),s=o(i.key,r);if(s<0||s===0&&i.entryId<=n)return K(e,r,n)}return a},P=(e,t)=>{if(e.entryCount===0)return null;let r=K(e,t,0),n=B(e,r,t,0);if(n>=T(r)&&(r.next===null||(r=r.next,n=B(e,r,t,0),n>=T(r)))||e.compareKeys(y(r,n).key,t)!==0)return null;let o=e._cursor;return o.leaf=r,o.index=n,o},Ne=(e,t)=>{if(e.entryCount===0)return null;let r=K(e,t,Number.MAX_SAFE_INTEGER),n=b(e,r,t,Number.MAX_SAFE_INTEGER);if(n===0&&(r.prev===null||(r=r.prev,n=T(r),n===0))||(n-=1,e.compareKeys(y(r,n).key,t)!==0))return null;let o=e._cursor;return o.leaf=r,o.index=n,o},ve=(e,t)=>P(e,t)!==null,Ie=(e,t)=>{if(e.entryCount===0)return null;let r=e.compareKeys,n=K(e,t,Number.MAX_SAFE_INTEGER),o=b(e,n,t,Number.MAX_SAFE_INTEGER);for(;n!==null;)if(o<T(n)){let a=y(n,o);if(r(a.key,t)>0)return a.key;o+=1}else n=n.next,o=0;return null},ge=(e,t)=>{if(e.entryCount===0)return null;let r=e.compareKeys,n=K(e,t,0),o=B(e,n,t,0),a=n,l=o-1;for(;a!==null;){for(;l>=0;){let i=y(a,l);if(r(i.key,t)<0)return i.key;l-=1}a=a.prev,a!==null&&(l=T(a)-1)}return null},Se=(e,t)=>{if(e.entryCount===0)return null;let r=e.compareKeys,n=K(e,t,0),o=B(e,n,t,0),a=e._cursor;if(o<T(n)){if(r(y(n,o).key,t)===0)return a.leaf=n,a.index=o,a}else if(n.next!==null){let l=B(e,n.next,t,0);if(l<T(n.next)&&r(y(n.next,l).key,t)===0)return a.leaf=n.next,a.index=l,a}if(o>0)return a.leaf=n,a.index=o-1,a;if(n.prev!==null){let l=T(n.prev);if(l>0)return a.leaf=n.prev,a.index=l-1,a}return null};function ye(e,t,r,n){let o=e(t,r);if(o>0)return!0;let a=n?.lowerBound==="exclusive",l=n?.upperBound==="exclusive";return a&&l&&o===0}var se=(e,t,r,n)=>{if(e.entryCount===0)return null;let o=e.compareKeys;if(ye(o,t,r,n))return null;let a=n?.lowerBound==="exclusive",l=n?.upperBound==="exclusive",i=a?Number.MAX_SAFE_INTEGER:0,s=K(e,t,i),d=a?b(e,s,t,Number.MAX_SAFE_INTEGER):B(e,s,t,0);return{leaf:s,index:d,compare:o,upperExclusive:l}},Te=(e,t,r,n)=>{let o=se(e,t,r,n);if(o===null)return 0;let a=o.leaf,l=o.index,{compare:i,upperExclusive:s}=o,d=0;for(;a!==null;){let p=T(a);if(l>=p){a=a.next,l=0;continue}let x=y(a,p-1).key;if($(x,r,i,s)){d+=p-l,a=a.next,l=0;continue}return d+=ce(e,a,r,s,p)-l,d}return d},$=(e,t,r,n)=>{let o=r(e,t);return n?o<0:o<=0},ce=(e,t,r,n,o)=>{let a=n?0:Number.MAX_SAFE_INTEGER,l=n?B(e,t,r,a):b(e,t,r,a);return l<o?l:o},Ft=200,Dt=(e,t,r,n,o)=>{let a=T(t.leaf);if(a-t.index>=Ft&&t.leaf.next!==null){let i=y(t.leaf,a-1).key;if($(i,n,t.compare,t.upperExclusive))return new Array(Te(e,r,n,o))}return[]},Ce=(e,t,r,n,o,a)=>{if(o)for(let l=t;l<r;l+=1)n[a++]=m(y(e,l));else for(let l=t;l<r;l+=1)n.push(m(y(e,l)));return a},qt=(e,t,r,n)=>{let{compare:o,upperExclusive:a}=t,l=t.leaf,i=t.index,s=0,d=n.length>0;for(;l!==null;){let p=T(l);if(i>=p){l=l.next,i=0;continue}let x=y(l,p-1).key;if($(x,r,o,a)){s=Ce(l,i,p,n,d,s),l=l.next,i=0;continue}let I=ce(e,l,r,a,p);Ce(l,i,I,n,d,s);return}},ke=(e,t,r,n)=>{let o=se(e,t,r,n);if(o===null)return[];let a=Dt(e,o,t,r,n);return qt(e,o,r,a),a},Me=(e,t,r,n,o)=>{let a=se(e,t,r,o);if(a===null)return;let l=a.leaf,i=a.index,{compare:s,upperExclusive:d}=a;for(;l!==null;){let p=T(l);if(i>=p){l=l.next,i=0;continue}let x=y(l,p-1).key;if($(x,r,s,d)){for(let L=i;L<p;L+=1)n(m(y(l,L)));l=l.next,i=0;continue}let I=ce(e,l,r,d,p);for(let L=i;L<I;L+=1)n(m(y(l,L)));return}};var V=e=>{let t=e;for(;t.parent!==null;){let r=t.indexInParent;if(!E(t,t.parent.keys[r])||r!==t.parent.childOffset)return;t=t.parent}},Q=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},Oe=e=>{if(h(e))throw new u("expected branch, got leaf");return e},z=(e,t)=>{if(t<e.childOffset||t>=e.children.length)throw new u("child index out of range");ue(e,t)},Jt=(e,t,r)=>{let n=t.children.pop();if(n===void 0)throw new u("left branch borrow failed");t.keys.pop(),n.parent=e;let o={key:void 0,sequence:0};if(!E(n,o))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=n,e.keys[e.childOffset]=o,n.indexInParent=e.childOffset;else{e.children.unshift(n),e.keys.unshift(o);for(let l=0;l<e.children.length;l+=1)e.children[l].indexInParent=l}let a=Q(e);a.keys[r]={key:o.key,sequence:o.sequence},V(e)},zt=(e,t,r)=>{let n=t.childOffset;if(n>=t.children.length)throw new u("right branch borrow failed");let o=t.children[n];t.childOffset+=1,t.childOffset>=t.children.length>>>1&&R(t),e.children.push(o),o.parent=e;let a={key:void 0,sequence:0};if(!E(o,a))throw new u("borrowed child has no min key");e.keys.push(a),o.indexInParent=e.children.length-1;let l=Q(e);E(t,l.keys[r+1])},Ut=(e,t,r,n)=>{for(let a=t.childOffset;a<t.children.length;a+=1){let l=t.children[a];l.parent=r,l.indexInParent=r.children.length,r.children.push(l),r.keys.push(t.keys[a])}let o=Q(t);z(o,n),U(e,o)},Xt=(e,t,r,n)=>{for(let a=r.childOffset;a<r.children.length;a+=1){let l=r.children[a];l.parent=t,l.indexInParent=t.children.length,t.children.push(l),t.keys.push(r.keys[a])}let o=Q(t);z(o,n+1),U(e,o)},Ht=(e,t)=>{let r=t>e.childOffset?Oe(e.children[t-1]):null,n=t+1<e.children.length?Oe(e.children[t+1]):null;return{left:r,right:n}},U=(e,t)=>{let r=C(t);if(t===e.root){if(r===1){let i=t.children[t.childOffset];i.parent=null,e.root=i,h(i)&&(e.leftmostLeaf=i,e.rightmostLeaf=i)}return}if(r>=e.minBranchChildren)return;let n=t.parent;if(n===null)throw new u("branch has no parent");let o=t.indexInParent,{left:a,right:l}=Ht(n,o);if(l!==null&&C(l)>e.minBranchChildren){zt(t,l,o);return}if(a!==null&&C(a)>e.minBranchChildren){Jt(t,a,o);return}if(a!==null){Ut(e,t,a,o);return}if(l!==null){Xt(e,t,l,o);return}throw new u("no branch siblings to rebalance")};var Re=e=>{if(!h(e))throw new u("expected leaf, got branch");return e},Ae=(e,t)=>{t.prev!==null?t.prev.next=t.next:t.next!==null&&(e.leftmostLeaf=t.next),t.next!==null?t.next.prev=t.prev:t.prev!==null&&(e.rightmostLeaf=t.prev),t.prev=null,t.next=null},Pe=(e,t)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),e.entries.push(...t.entries.slice(t.entryOffset))},de=e=>Math.max(1,Math.ceil(e/4)),fe=e=>e.deleteRebalancePolicy==="lazy"?de(e.minLeafEntries):e.minLeafEntries,jt=(e,t)=>{let r=t>e.childOffset?Re(e.children[t-1]):null,n=t+1<e.children.length?Re(e.children[t+1]):null;return{left:r,right:n}},Wt=(e,t,r,n,o,a)=>{if(a!==null&&T(a)>e.minLeafEntries){let l=_(a);if(l===void 0)throw new u("right leaf borrow failed");let i=T(t)===0;return t.entries.push(l),E(a,r.keys[n+1]),i&&V(t),!0}if(o!==null&&T(o)>e.minLeafEntries){let l=o.entries.pop();if(l===void 0)throw new u("left leaf borrow failed");return oe(t,l),r.keys[n]={key:l.key,sequence:l.entryId},V(t),!0}return!1},Gt=(e,t,r,n,o,a)=>{if(o!==null){Pe(o,t),Ae(e,t),z(r,n),U(e,r);return}if(a!==null){let l=T(t)===0;Pe(t,a),Ae(e,a),l&&V(t),z(r,n+1),U(e,r);return}throw new u("no leaf siblings to rebalance")},N=(e,t)=>{if(t===e.root){e.entryCount===0&&(e.leftmostLeaf=t,e.rightmostLeaf=t);return}if(T(t)>=fe(e))return;let r=t.parent;if(r===null)throw new u("Leaf node has no parent during rebalance.");let n=t.indexInParent,{left:o,right:a}=jt(r,n);Wt(e,t,r,n,o,a)||Gt(e,t,r,n,o,a)};var $t=(e,t,r)=>{let n=r?Number.MAX_SAFE_INTEGER:0,o=K(e,t,n),a=r?b(e,o,t,Number.MAX_SAFE_INTEGER):B(e,o,t,0);return a>=T(o)?o.next===null?null:{leaf:o.next,idx:0}:{leaf:o,idx:a}},Qt=(e,t,r,n)=>{let o=T(t),a=y(t,o-1),l=e.compareKeys(a.key,r);if(n?l<0:l<=0)return o;let i=n?0:Number.MAX_SAFE_INTEGER,s=n?B(e,t,r,i):b(e,t,r,i);return s<o?s:o},Yt=(e,t,r,n)=>{if(e.entryKeys!==null)for(let d=r;d<r+n;d+=1)e.entryKeys.delete(y(t,d).entryId);let o=t.entryOffset+r;t.entries.copyWithin(o,o+n),t.entries.length-=n,e.entryCount-=n;let a=T(t)===0;r===0&&!a&&t.parent!==null&&V(t);let l=T(t),i=fe(e),s=e.minLeafEntries+4;for(;s>0&&t!==e.root&&T(t)<i&&(N(e,t),!(t.parent!==null&&t.parent.children[t.indexInParent]!==t));)s-=1;return l},Zt=(e,t)=>t.parent===null?t===e.root:t.parent.children[t.indexInParent]===t,_e=(e,t,r,n)=>{if(e.entryCount===0||ye(e.compareKeys,t,r,n))return 0;let o=n?.lowerBound==="exclusive",a=n?.upperBound==="exclusive",l=0,i=!0,s=null,d=0;for(;e.entryCount>0;){if(i){let te=$t(e,t,o);if(te===null)break;s=te.leaf,d=te.idx,i=!1}if(d>=T(s))break;let p=T(s),x=Qt(e,s,r,a),I=x-d;if(I===0)break;let L=Yt(e,s,d,I);if(l+=I,x<p)break;if(!Zt(e,s)){i=!0;continue}if(T(s)>L){i=!0;continue}if(s.next===null)break;s=s.next,d=0}return l};var v=e=>Math.ceil(e/2),M=[{threshold:0,maxLeaf:32,maxBranch:32},{threshold:1e3,maxLeaf:64,maxBranch:64},{threshold:1e4,maxLeaf:128,maxBranch:128},{threshold:1e5,maxLeaf:256,maxBranch:128},{threshold:1e6,maxLeaf:512,maxBranch:256}],w=e=>{let t=M[0];for(let r=1;r<M.length&&e>=M[r].threshold;r+=1)t=M[r];return t},pe=e=>{for(let t=1;t<M.length;t+=1)if(e<M[t].threshold)return M[t].threshold;return Number.MAX_SAFE_INTEGER},er=(e,t)=>{if(t&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new c("autoScale conflicts with explicit capacity.");if(t){let r=w(0);return{maxLeafEntries:r.maxLeaf,maxBranchChildren:r.maxBranch}}return{maxLeafEntries:J(e.maxLeafEntries,"maxLeafEntries",D),maxBranchChildren:J(e.maxBranchChildren,"maxBranchChildren",q)}},Fe=e=>{if(typeof e.compareKeys!="function")throw new c("compareKeys must be a function.");let t=e.autoScale===!0,{maxLeafEntries:r,maxBranchChildren:n}=er(e,t),o=we(e.duplicateKeys),a=be(e.deleteRebalancePolicy),l=g([],null);return{compareKeys:e.compareKeys,maxLeafEntries:r,maxBranchChildren:n,duplicateKeys:o,minLeafEntries:v(r),minBranchChildren:v(n),root:l,leftmostLeaf:l,rightmostLeaf:l,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:t,deleteRebalancePolicy:a,_nextAutoScaleThreshold:t?pe(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:l,index:0}}},Y=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:t,maxBranch:r}=w(e.entryCount);t>e.maxLeafEntries&&(e.maxLeafEntries=t,e.minLeafEntries=v(t)),r>e.maxBranchChildren&&(e.maxBranchChildren=r,e.minBranchChildren=v(r)),e._nextAutoScaleThreshold=pe(e.entryCount)},me=(e,t,r)=>{if(!e.autoScale)return;let n=w(0),o=J(t,"maxLeafEntries",D),a=J(r,"maxBranchChildren",q);if(o<n.maxLeaf||a<n.maxBranch)throw new c("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=o,e.maxBranchChildren=a,e.minLeafEntries=v(o),e.minBranchChildren=v(a)},De=e=>{let t=w(0);e.maxLeafEntries=t.maxLeaf,e.maxBranchChildren=t.maxBranch,e.minLeafEntries=v(t.maxLeaf),e.minBranchChildren=v(t.maxBranch),e._nextAutoScaleThreshold=pe(0)};var qe=(e,t,r)=>{let n=[],o=0;for(;o<e;){let a=e-o;if(a>t&&a-t<r){let i=Math.ceil(a/2);n.push(o+i),n.push(e);break}let l=o+t<e?o+t:e;n.push(l),o=l}return n},tr=(e,t,r,n)=>{let o=qe(t.length,e.maxLeafEntries,e.minLeafEntries),a=new Array(o.length),l=0;for(let i=0;i<o.length;i+=1){let s=o[i],d=new Array(s-l);for(let p=l;p<s;p+=1){let x=n+p;d[p-l]=k(t[p].key,x,t[p].value),r[p]=x,e.entryKeys!==null&&e.entryKeys.set(x,t[p].key)}a[i]=g(d,null),l=s}return a},Je=(e,t)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let r=e.nextSequence;if(r+t.length>Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");let n=new Array(t.length),o=tr(e,t,n,r);e.nextSequence=r+t.length,e.entryCount=t.length;for(let a=0;a<o.length;a+=1)a>0&&(o[a].prev=o[a-1]),a<o.length-1&&(o[a].next=o[a+1]);if(e.leftmostLeaf=o[0],e.rightmostLeaf=o[o.length-1],o.length===1)e.root=o[0];else{let a=o;for(;a.length>1;){let l=qe(a.length,e.maxBranchChildren,e.minBranchChildren),i=new Array(l.length),s=0;for(let d=0;d<l.length;d+=1)i[d]=S(a.slice(s,l[d]),null),s=l[d];a=i}e.root=a[0]}return Y(e),n};var ze=(e,t,r,n)=>{let o={key:void 0,sequence:0};if(!E(n,o))throw new u("inserted child has no min key");n.parent=t;let a=r.indexInParent-t.childOffset+1;ie(t,a,n,o),C(t)>e.maxBranchChildren&&rr(e,t)},Ue=(e,t)=>{le(t);let r=Math.ceil(t.entries.length/2),n={kind:A,entries:t.entries.splice(r),entryOffset:0,parent:t.parent,indexInParent:0,prev:t,next:t.next};if(t.next!==null?t.next.prev=n:e.rightmostLeaf=n,t.next=n,t.parent===null){e.root=S([t,n],null);return}ze(e,t.parent,t,n)},rr=(e,t)=>{R(t);let r=Math.ceil(t.children.length/2),n=S(t.children.splice(r),t.parent);if(t.keys.splice(r),t.parent===null){e.root=S([t,n],null);return}ze(e,t.parent,t,n)};var he=(e,t,r)=>{let n=K(e,t,r),o=B(e,n,t,r);return o>=T(n)||y(n,o).entryId!==r?null:{leaf:n,index:o}},Ke=(e,t)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(t);if(r===void 0)return null;let n=he(e,r,t);return n===null?null:y(n.leaf,n.index)},Be=(e,t,r)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let n=e.entryKeys.get(t);if(n===void 0)return null;let o=he(e,n,t);if(o===null)return null;let a=y(o.leaf,o.index),l=k(a.key,a.entryId,r);return o.leaf.entries[o.leaf.entryOffset+o.index]=l,l},Ve=(e,t)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(t);if(r===void 0)return null;let n=he(e,r,t);if(n===null)return null;let o=y(n.leaf,n.index);return F(n.leaf,n.index),e.entryCount-=1,e.entryKeys.delete(t),n.index===0&&T(n.leaf)>0&&n.leaf.parent!==null&&V(n.leaf),n.leaf!==e.root&&T(n.leaf)<e.minLeafEntries&&N(e,n.leaf),o};var nr=(e,t,r,n)=>{if(e.duplicateKeys==="allow")return null;if(n>0){let o=y(t,n-1);if(e.compareKeys(o.key,r)===0)return{leaf:t,physIndex:t.entryOffset+n-1,entry:o}}else if(t.prev!==null&&T(t.prev)>0){let o=t.prev,a=T(o),l=y(o,a-1);if(e.compareKeys(l.key,r)===0)return{leaf:o,physIndex:o.entryOffset+a-1,entry:l}}return null},Xe=(e,t,r,n)=>{let o=e.nextSequence,a=b(e,t,r,o),l=nr(e,t,r,a);if(l!==null){if(e.duplicateKeys==="reject")throw new c("Duplicate key rejected.");return l.leaf.entries[l.physIndex]=k(l.entry.key,l.entry.entryId,n),l.entry.entryId}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");return e.nextSequence+=1,ae(t,a,k(r,o,n)),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(o,r),a===0&&t.parent!==null&&V(t),T(t)>e.maxLeafEntries&&Ue(e,t),Y(e),o},He=(e,t,r)=>{let n=K(e,t,e.nextSequence);return Xe(e,n,t,r)},je=e=>{if(e.entryCount===0)return null;let t=_(e.leftmostLeaf);if(t===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),T(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&V(e.leftmostLeaf),e.leftmostLeaf!==e.root&&T(e.leftmostLeaf)<e.minLeafEntries&&N(e,e.leftmostLeaf),t},We=e=>{if(e.entryCount===0)return null;let t=ne(e.rightmostLeaf);if(t===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),e.rightmostLeaf!==e.root&&T(e.rightmostLeaf)<e.minLeafEntries&&N(e,e.rightmostLeaf),t},Ge=(e,t)=>{let r=P(e,t);if(r===null)return null;let n=r.leaf,o=r.index,a=y(n,o);return F(n,o),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(a.entryId),o===0&&T(n)>0&&n.parent!==null&&V(n),n!==e.root&&T(n)<e.minLeafEntries&&N(e,n),a},$e=(e,t)=>{if(t.length===0)return[];let r=e.duplicateKeys!=="allow";for(let n=1;n<t.length;n+=1){let o=e.compareKeys(t[n-1].key,t[n].key);if(o>0)throw new c("putMany: entries not in ascending order.");if(r&&o===0)throw new c(e.duplicateKeys==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(e.entryCount>0){let n=new Array(t.length),o=K(e,t[0].key,e.nextSequence);for(let a=0;a<t.length;a+=1){let l=t[a],i=Le(e,o,l.key,e.nextSequence);n[a]=Xe(e,i,l.key,l.value),o=i}return n}return Je(e,t)};var Qe=e=>{let t={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale,deleteRebalancePolicy:e.deleteRebalancePolicy};return e.autoScale||(t.maxLeafEntries=e.maxLeafEntries,t.maxBranchChildren=e.maxBranchChildren),t},Ye=e=>{let t=new Array(e.entryCount),r=e.leftmostLeaf,n=0;for(;r!==null;){let a=T(r);for(let l=0;l<a;l+=1){let i=y(r,l);t[n++]=[i.key,i.value]}r=r.next}let o={maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.deleteRebalancePolicy!=="standard"&&(o.deleteRebalancePolicy=e.deleteRebalancePolicy),{version:1,config:o,entries:t}},or=1e6,ar=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new c(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new c("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new c("BTreeJSON: entries must be array.");if(e.entries.length>or)throw new c("BTreeJSON: entry count exceeds maximum.");for(let t=0;t<e.entries.length;t+=1){let r=e.entries[t];if(!Array.isArray(r)||r.length!==2)throw new c(`BTreeJSON: bad entries[${t}].`)}},lr=e=>{let t=(r,n)=>{if(!Number.isInteger(n)||n<W||n>G)throw new c(`BTreeJSON: invalid ${r}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new c(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new c("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new c("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new c("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new c("BTreeJSON: invalid maxBranchChildren.");if(t("maxLeafEntries",e.maxLeafEntries),t("maxBranchChildren",e.maxBranchChildren),e.autoScale){let r=w(0);if(e.maxLeafEntries<r.maxLeaf||e.maxBranchChildren<r.maxBranch)throw new c("BTreeJSON: autoScale capacity below tier-0.")}},Ze=e=>{ar(e),lr(e.config)},et=(e,t)=>{let r=e.config.duplicateKeys!=="allow";for(let n=1;n<e.entries.length;n+=1){let o=t(e.entries[n-1][0],e.entries[n][0]);if(o>0)throw new c("fromJSON: entries not sorted.");if(r&&o===0)throw new c('fromJSON: duplicate keys require duplicateKeys "allow".')}},tt=(e,t)=>{let r=e.config,n={compareKeys:t,duplicateKeys:r.duplicateKeys,enableEntryIdLookup:r.enableEntryIdLookup,autoScale:r.autoScale,deleteRebalancePolicy:r.deleteRebalancePolicy};return r.autoScale||(n.maxLeafEntries=r.maxLeafEntries,n.maxBranchChildren=r.maxBranchChildren),n};var rt=e=>{let t=new Array(e.entryCount),r=e.leftmostLeaf,n=0;for(;r!==null;){let o=T(r);for(let a=0;a<o;a+=1)t[n++]=m(y(r,a));r=r.next}return t},nt=e=>{let t=new Array(e.entryCount),r=e.leftmostLeaf,n=0;for(;r!==null;){let o=T(r);for(let a=0;a<o;a+=1)t[n++]=y(r,a);r=r.next}return t},ot=(e,t,r)=>{let n=e.leftmostLeaf;for(;n!==null;){let o=T(n);for(let a=0;a<o;a+=1)t.call(r,m(y(n,a)));n=n.next}};var xe=e=>{if(h(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entryOffset];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.keys.length?null:{key:e.keys[e.childOffset].key,sequence:e.keys[e.childOffset].sequence}},H=(e,t,r,n,o)=>{let a=e(t,n);return a!==0?a:r<o?-1:r>o?1:0},at=e=>{if(h(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entries.length-1];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.children.length?null:at(e.children[e.children.length-1])},X=e=>{if(!Number.isFinite(e))throw new c("compareKeys must return a finite number.");return e},ir=(e,t)=>{if(X(e(t,t))!==0)throw new c("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},ur=(e,t,r,n)=>{let o=Math.sign(X(e(t,r))),a=Math.sign(X(e(r,n)));if(o<0&&a<0&&Math.sign(X(e(t,n)))>=0)throw new c("compareKeys must satisfy transitivity for observed key triples.");if(o>0&&a>0&&Math.sign(X(e(t,n)))<=0)throw new c("compareKeys must satisfy transitivity for observed key triples.")},lt=(e,t)=>{try{ir(e.compareKeys,t)}catch(r){throw r instanceof c?new u(r.message):r}},it=(e,t,r,n)=>{try{ur(e.compareKeys,t,r,n)}catch(o){throw o instanceof c?new u(o.message):o}},yr=(e,t,r)=>{let n=at(t),o=xe(r);if(n===null||o===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(H(e.compareKeys,n.key,n.sequence,o.key,o.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let a=T(t),l=T(r);if(e.duplicateKeys!=="allow"&&a>0&&l>0&&e.compareKeys(y(t,a-1).key,y(r,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")},sr=(e,t,r,n)=>{if(!h(t))throw new u("Leaf linkage cursor reached non-leaf node.");if(n.has(t))throw new u("Cycle detected in leaf linkage.");if(t.prev!==r)throw new u("Leaf prev pointer mismatch.");r!==null&&h(r)&&yr(e,r,t)},ut=(e,t)=>{if(e.entryCount===0){if(!h(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let r=new Set,n=e.leftmostLeaf,o=null,a=0;for(;n!==null;)sr(e,n,o,r),r.add(n),o=n,n=n.next,a+=1;if(o!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(a!==t)throw new u("Leaf chain count mismatch with tree traversal count.")};var Tr=(e,t)=>{let r=T(t);for(let n=0;n<r;n+=1)lt(e,y(t,n).key);for(let n=1;n<r;n+=1)if(H(e.compareKeys,y(t,n-1).key,y(t,n-1).entryId,y(t,n).key,y(t,n).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let n=1;n<r;n+=1)if(e.compareKeys(y(t,n-1).key,y(t,n).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let n=2;n<r;n+=1){let o=y(t,n-2),a=y(t,n-1),l=y(t,n);it(e,o.key,a.key,l.key)}if(r>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},cr=(e,t,r)=>{Tr(e,t);let n=T(t),o=e.autoScale?Math.ceil(w(0).maxLeaf/2):e.minLeafEntries;if(e.deleteRebalancePolicy==="lazy"&&(o=de(o)),t!==e.root&&n<o)throw new u("Non-root leaf node violates minimum occupancy.");let a=n===0?null:y(t,0),l=n===0?null:y(t,n-1),i=a===null?null:{key:a.key,sequence:a.entryId},s=l===null?null:{key:l.key,sequence:l.entryId};return{minKey:i,maxKey:s,leafDepth:n===0?null:r,leafCount:1,branchCount:0,entryCount:n}},dr=(e,t)=>{let r=t.children.length-t.childOffset;if(r===0)throw new u("Branch node has zero children.");let n=e.autoScale?Math.ceil(w(0).maxBranch/2):e.minBranchChildren;if(t!==e.root&&r<n)throw new u("Non-root branch node violates minimum occupancy.");if(r>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(t.keys.length!==t.children.length)throw new u("Branch keys array length does not match children array length.")},fr=(e,t,r,n)=>{let o=t.children[r];if(o.parent!==t)throw new u("Child-parent pointer mismatch in branch node.");if(o.indexInParent!==r)throw new u("Child indexInParent does not match actual position in parent.");let a=yt(e,o,n+1);if(a.minKey===null||a.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let l=t.keys[r],i=xe(o);if(i===null||H(e.compareKeys,l.key,l.sequence,i.key,i.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return a},pr=(e,t,r)=>{if(t.leafDepth!==null&&r.leafDepth!==null&&r.leafDepth!==t.leafDepth)throw new u("Leaf depth mismatch detected in tree.");if(t.leafDepth===null&&r.leafDepth!==null&&(t.leafDepth=r.leafDepth),t.previousChildMax!==null&&H(e.compareKeys,t.previousChildMax.key,t.previousChildMax.sequence,r.minKey.key,r.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");t.minKey===null&&(t.minKey=r.minKey),t.maxKey=r.maxKey,t.previousChildMax=r.maxKey,t.leafCount+=r.leafCount,t.branchCount+=r.branchCount,t.entryCount+=r.entryCount},yt=(e,t,r)=>{if(h(t))return cr(e,t,r);dr(e,t);let n={leafDepth:null,leafCount:0,branchCount:1,entryCount:0,minKey:null,maxKey:null,previousChildMax:null};for(let o=t.childOffset;o<t.children.length;o+=1){let a=fr(e,t,o,r);pr(e,n,a)}return{minKey:n.minKey,maxKey:n.maxKey,leafDepth:n.leafDepth,leafCount:n.leafCount,branchCount:n.branchCount,entryCount:n.entryCount}},st=e=>{let t=yt(e,e.root,0);if(t.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");ut(e,t.leafCount)};var Tt=e=>{if(h(e))return{height:1,leafCount:1,branchCount:0};let t=0,r=0,n=1;for(let o=e.childOffset;o<e.children.length;o+=1){let a=e.children[o],l=Tt(a);l.height>t&&(t=l.height),r+=l.leafCount,n+=l.branchCount}return{height:t+1,leafCount:r,branchCount:n}},ct=e=>{let t=Tt(e.root);return{height:t.height,leafCount:t.leafCount,branchCount:t.branchCount,entryCount:e.entryCount}};var O=class e{constructor(t){this.state=Fe(t)}put(t,r){return He(this.state,t,r)}putMany(t){return $e(this.state,t)}remove(t){let r=Ge(this.state,t);return r===null?null:m(r)}removeById(t){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let r=Ve(this.state,t);return r===null?null:m(r)}peekById(t){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let r=Ke(this.state,t);return r===null?null:m(r)}updateById(t,r){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let n=Be(this.state,t,r);return n===null?null:m(n)}popFirst(){let t=je(this.state);return t===null?null:m(t)}peekFirst(){return this.state.entryCount===0?null:m(y(this.state.leftmostLeaf,0))}peekLast(){if(this.state.entryCount===0)return null;let t=this.state.rightmostLeaf;return m(y(t,T(t)-1))}popLast(){let t=We(this.state);return t===null?null:m(t)}clear(){let t=g([],null);this.state.root=t,this.state.leftmostLeaf=t,this.state.rightmostLeaf=t,this.state.entryCount=0,this.state._cursor.leaf=t,this.state._cursor.index=0,this.state.entryKeys?.clear(),this.state.autoScale&&De(this.state)}get(t){let r=P(this.state,t);return r===null?null:y(r.leaf,r.index).value}hasKey(t){return ve(this.state,t)}findFirst(t){let r=P(this.state,t);return r===null?null:m(y(r.leaf,r.index))}findLast(t){let r=Ne(this.state,t);return r===null?null:m(y(r.leaf,r.index))}nextHigherKey(t){return Ie(this.state,t)}nextLowerKey(t){return ge(this.state,t)}getPairOrNextLower(t){let r=Se(this.state,t);return r===null?null:m(y(r.leaf,r.index))}count(t,r,n){return Te(this.state,t,r,n)}deleteRange(t,r,n){return _e(this.state,t,r,n)}range(t,r,n){return ke(this.state,t,r,n)}*entries(){let t=this.state.leftmostLeaf;for(;t!==null;){let r=T(t);for(let n=0;n<r;n+=1)yield m(y(t,n));t=t.next}}*entriesReversed(){let t=this.state.rightmostLeaf;for(;t!==null;){let r=T(t);for(let n=r-1;n>=0;n-=1)yield m(y(t,n));t=t.prev}}*keys(){for(let t of this.entries())yield t.key}*values(){for(let t of this.entries())yield t.value}[Symbol.iterator](){return this.entries()}forEachRange(t,r,n,o){Me(this.state,t,r,n,o)}forEach(t,r){ot(this.state,t,r)}snapshot(){return rt(this.state)}clone(){let t=new e(Qe(this.state));return me(t.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0&&t.putMany(nt(this.state)),t}toJSON(){return Ye(this.state)}static fromJSON(t,r){Ze(t),et(t,r);let n=new e(tt(t,r));if(me(n.state,t.config.maxLeafEntries,t.config.maxBranchChildren),t.entries.length>0){let o=new Array(t.entries.length);for(let a=0;a<t.entries.length;a+=1)o[a]={key:t.entries[a][0],value:t.entries[a][1]};n.putMany(o)}return n}size(){return this.state.entryCount}assertInvariants(){st(this.state)}getStats(){return ct(this.state)}};var mr=16,dt=1024,hr=1e5,ft=1e6,pt=e=>{let t=e.autoScale===!0,r=t?w(0):void 0;return JSON.stringify({duplicateKeys:e.duplicateKeys??"replace",maxLeafEntries:e.maxLeafEntries??(r?r.maxLeaf:D),maxBranchChildren:e.maxBranchChildren??(r?r.maxBranch:q),enableEntryIdLookup:e.enableEntryIdLookup===!0,autoScale:t})},mt=e=>{let t=e;throw new f(`Unsupported mutation type from shared store: ${String(t.type)}`)},Kr=e=>{for(let t of e)if(typeof t!="object"||t===null||!("key"in t)||!("value"in t))throw new f("Malformed putMany mutation: each entry must have key and value.")},Br=(e,t)=>{if(typeof e.configFingerprint!="string")throw new f("Malformed init mutation: missing configFingerprint.");if(t!==void 0&&e.configFingerprint!==t)throw new f("Config mismatch: store peers must share identical tree config.")},Vr=e=>{switch(e.type){case"put":if(!("key"in e)||!("value"in e))throw new f("Malformed put mutation: missing key or value.");break;case"remove":if(!("key"in e))throw new f("Malformed remove mutation: missing key.");break;case"removeById":if(!("entryId"in e))throw new f("Malformed removeById mutation: missing entryId.");break;case"updateById":if(!("entryId"in e)||!("value"in e))throw new f("Malformed updateById mutation: missing entryId or value.");break;case"putMany":if(!("entries"in e)||!Array.isArray(e.entries))throw new f("Malformed putMany mutation: missing entries array.");Kr(e.entries);break;case"deleteRange":if(!("startKey"in e)||!("endKey"in e))throw new f("Malformed deleteRange mutation: missing startKey or endKey.");break;default:break}},xr=(e,t)=>{switch(e.type){case"init":Br(e,t);break;case"put":case"remove":case"removeById":case"updateById":case"putMany":case"deleteRange":Vr(e);break;case"popFirst":case"popLast":case"clear":break;default:throw new f(`Unsupported mutation type from shared store: ${String(e.type)}`)}},ht=(e,t)=>{for(let r of e){if(typeof r!="object"||r===null)throw new f("Malformed mutation: expected an object.");xr(r,t)}},Kt=e=>{if(e===void 0)return mr;if(!Number.isInteger(e)||e<1||e>dt)throw new f(`maxRetries: integer 1\u2013${dt} required.`);return e},Bt=e=>{if(e===void 0)return hr;if(!Number.isInteger(e)||e<1||e>ft)throw new f(`maxSyncMutationsPerBatch: integer 1\u2013${ft} required.`);return e},Vt=e=>{if(e===void 0)return"strong";if(e!=="strong"&&e!=="local")throw new f("readMode: must be 'strong' or 'local'.");return e};function xt(e,t){if(typeof t!="object"||t===null)throw new f("Store contract: append() must return {applied, version}.");let r=t;if(typeof r.applied!="boolean")throw new f("Store contract: applied must be boolean.");if(typeof r.version!="bigint")throw new f("Store contract: version must be bigint.");if(r.applied&&r.version<=e)throw new f("Store contract: applied version must exceed expected.");if(!r.applied&&r.version<e)throw new f("Store contract: rejected version must be >= expected.")}var Z=(e,t,r)=>{switch(t.type){case"init":return r(),null;case"put":return e.put(t.key,t.value);case"putMany":return e.putMany(t.entries);case"remove":return e.remove(t.key);case"removeById":return e.removeById(t.entryId);case"updateById":return e.updateById(t.entryId,t.value);case"popFirst":return e.popFirst();case"popLast":return e.popLast();case"deleteRange":return e.deleteRange(t.startKey,t.endKey,t.options);case"clear":return e.clear(),null;default:return mt(t)}},Et=(e,t,r)=>n=>{if(e==="reject"&&n.hasKey(t))throw new c("Duplicate key rejected.");return{type:"put",key:t,value:r}},wt=e=>t=>t.hasKey(e)?{type:"remove",key:e}:null,bt=e=>t=>t.peekById(e)!==null?{type:"removeById",entryId:e}:null,Lt=(e,t)=>r=>r.peekById(e)!==null?{type:"updateById",entryId:e,value:t}:null,Nt=()=>e=>e.peekFirst()!==null?{type:"popFirst"}:null,vt=()=>e=>e.peekLast()!==null?{type:"popLast"}:null,It=(e,t,r)=>n=>{let o=t!=="allow";for(let a=1;a<e.length;a+=1){let l=r(e[a-1].key,e[a].key);if(l>0)throw new c("putMany: entries not in ascending order.");if(o&&l===0)throw new c(t==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(t==="reject"){for(let a of e)if(n.hasKey(a.key))throw new c("Duplicate key rejected.")}return{type:"putMany",entries:e}},gt=(e,t,r)=>n=>n.count(e,t,r)===0?null:{type:"deleteRange",startKey:e,endKey:t,options:r},St=()=>()=>({type:"clear"});var Ct=(e,t)=>{if(typeof e.version!="bigint")throw new f("Store contract: version must be bigint.");if(!Array.isArray(e.mutations))throw new f("Store contract: mutations must be an array.");if(e.mutations.length>t)throw new f(`Sync batch exceeded limit (${String(t)}).`)};var ee=class{constructor(t,r,n,o,a,l){this.tree=t,this.store=r,this.maxRetries=n,this.maxSyncMutationsPerBatch=o,this.configFingerprint=a,this.readMode=l,this.currentVersion=0n,this.operationQueue=Promise.resolve(),this.initSeen=!1,this.corrupted=!1}async syncUnlocked(){let t=await this.store.getLogEntriesSince(this.currentVersion);if(Ct(t,this.maxSyncMutationsPerBatch),!(t.version<=this.currentVersion)){ht(t.mutations,this.configFingerprint);try{let r=()=>{this.initSeen=!0};for(let n of t.mutations)Z(this.tree,n,r);this.currentVersion=t.version}catch(r){this.corrupted=!0;let n=r instanceof Error?r.message:String(r);throw new f(`Replay failure: instance is permanently corrupted. Discard and create a new instance. Cause: ${n}`)}}}runExclusive(t){let r=async()=>{if(this.corrupted)throw new f("Instance is permanently corrupted. Discard and create a new instance.");return t()},n=this.operationQueue.then(r,r);return this.operationQueue=n.then(()=>{},()=>{}),n}readOp(t){return this.runExclusive(async()=>(this.readMode==="strong"&&await this.syncUnlocked(),t(this.tree)))}async appendAndApply(t){for(let r=0;r<this.maxRetries;r+=1){await this.syncUnlocked();let n=t(this.tree);if(n===null)return null;let o=this.currentVersion,a=this.initSeen?[n]:[{type:"init",configFingerprint:this.configFingerprint},n],l=await this.store.append(o,a);if(xt(o,l),l.applied)try{let i=()=>{this.initSeen=!0};for(let d of a){if(d===n)break;Z(this.tree,d,i)}let s=Z(this.tree,n,i);return this.currentVersion=l.version,s}catch(i){this.corrupted=!0;let s=i instanceof Error?i.message:String(i);throw new f(`Local apply failure after successful append: instance is permanently corrupted. Discard and create a new instance. Cause: ${s}`)}}throw new f(`Mutation failed after ${String(this.maxRetries)} retries.`)}writeOp(t){return this.runExclusive(async()=>this.appendAndApply(t))}};var j=class{constructor(t){this.compareKeys=t.compareKeys,this.duplicateKeys=t.duplicateKeys??"replace";let r=new O({compareKeys:t.compareKeys,maxLeafEntries:t.maxLeafEntries,maxBranchChildren:t.maxBranchChildren,duplicateKeys:t.duplicateKeys,enableEntryIdLookup:t.enableEntryIdLookup,autoScale:t.autoScale,deleteRebalancePolicy:t.deleteRebalancePolicy});this.coord=new ee(r,t.store,Kt(t.maxRetries),Bt(t.maxSyncMutationsPerBatch),pt(t),Vt(t.readMode))}async sync(){await this.coord.runExclusive(async()=>{await this.coord.syncUnlocked()})}async syncThenRead(t){return this.coord.runExclusive(async()=>(await this.coord.syncUnlocked(),t(this.coord.tree)))}async put(t,r){return this.coord.writeOp(Et(this.duplicateKeys,t,r))}async remove(t){return this.coord.writeOp(wt(t))}async removeById(t){return this.coord.writeOp(bt(t))}async updateById(t,r){return this.coord.writeOp(Lt(t,r))}async popFirst(){return this.coord.writeOp(Nt())}async popLast(){return this.coord.writeOp(vt())}async putMany(t){return t.length===0?[]:this.coord.writeOp(It(t,this.duplicateKeys,this.compareKeys))}async deleteRange(t,r,n){return await this.coord.writeOp(gt(t,r,n))??0}async clear(){await this.coord.writeOp(St())}async get(t){return this.coord.readOp(r=>r.get(t))}async hasKey(t){return this.coord.readOp(r=>r.hasKey(t))}async findFirst(t){return this.coord.readOp(r=>r.findFirst(t))}async findLast(t){return this.coord.readOp(r=>r.findLast(t))}async range(t,r,n){return this.coord.readOp(o=>o.range(t,r,n))}async snapshot(){return this.coord.readOp(t=>t.snapshot())}async size(){return this.coord.readOp(t=>t.size())}async assertInvariants(){await this.coord.readOp(t=>t.assertInvariants())}async getStats(){return this.coord.readOp(t=>t.getStats())}async peekFirst(){return this.coord.readOp(t=>t.peekFirst())}async peekLast(){return this.coord.readOp(t=>t.peekLast())}async peekById(t){return this.coord.readOp(r=>r.peekById(t))}async count(t,r,n){return this.coord.readOp(o=>o.count(t,r,n))}async nextHigherKey(t){return this.coord.readOp(r=>r.nextHigherKey(t))}async nextLowerKey(t){return this.coord.readOp(r=>r.nextLowerKey(t))}async getPairOrNextLower(t){return this.coord.readOp(r=>r.getPairOrNextLower(t))}async entries(){return this.coord.readOp(t=>Array.from(t.entries()))}async entriesReversed(){return this.coord.readOp(t=>Array.from(t.entriesReversed()))}async keys(){return this.coord.readOp(t=>Array.from(t.keys()))}async values(){return this.coord.readOp(t=>Array.from(t.values()))}async forEach(t){await this.coord.readOp(r=>{r.forEach(t)})}async forEachRange(t,r,n,o){await this.coord.readOp(a=>{a.forEachRange(t,r,n,o)})}async*[Symbol.asyncIterator](){let t=await this.entries();for(let r of t)yield r}async clone(){return this.coord.readOp(t=>t.clone())}async toJSON(){return this.coord.readOp(t=>t.toJSON())}static fromJSON(t,r){return O.fromJSON(t,r)}};return Pt(Er);})();
package/dist/index.cjs CHANGED
@@ -836,8 +836,10 @@ var tryBorrowFromLeafSibling = (state, leaf, parent, leafIndex, leftSibling, rig
836
836
  const borrowed = leafShiftEntry(rightSibling);
837
837
  if (borrowed === void 0)
838
838
  throw new BTreeInvariantError("right leaf borrow failed");
839
+ const leafWasEmpty = leafEntryCount(leaf) === 0;
839
840
  leaf.entries.push(borrowed);
840
841
  writeMinKeyTo(rightSibling, parent.keys[leafIndex + 1]);
842
+ if (leafWasEmpty) updateMinKeyInAncestors(leaf);
841
843
  return true;
842
844
  }
843
845
  if (leftSibling !== null && leafEntryCount(leftSibling) > state.minLeafEntries) {
@@ -860,8 +862,10 @@ var mergeLeafWithSibling = (state, leaf, parent, leafIndex, leftSibling, rightSi
860
862
  return;
861
863
  }
862
864
  if (rightSibling !== null) {
865
+ const leafWasEmpty = leafEntryCount(leaf) === 0;
863
866
  mergeLeafEntries(leaf, rightSibling);
864
867
  detachLeafFromChain(state, rightSibling);
868
+ if (leafWasEmpty) updateMinKeyInAncestors(leaf);
865
869
  removeChildFromBranch(parent, leafIndex + 1);
866
870
  rebalanceAfterBranchRemoval(state, parent);
867
871
  return;
@@ -932,9 +936,6 @@ var spliceLeafAndRebalance = (state, leaf, idx, removeCount) => {
932
936
  break;
933
937
  safetyGuard -= 1;
934
938
  }
935
- if (leafEmptied && leafEntryCount(leaf) > 0 && leaf.parent !== null && leaf.parent.children[leaf.indexInParent] === leaf) {
936
- updateMinKeyInAncestors(leaf);
937
- }
938
939
  return countAfterSplice;
939
940
  };
940
941
  var isLeafStillValid = (state, leaf) => leaf.parent === null ? leaf === state.root : leaf.parent.children[leaf.indexInParent] === leaf;
@@ -0,0 +1,6 @@
1
+ export { InMemoryBTree } from './InMemoryBTree.cjs';
2
+ export type { BTreeEntry, BTreeJSON, BTreeStats, DeleteRebalancePolicy, DuplicateKeyPolicy, EntryId, InMemoryBTreeConfig, RangeBounds, } from './InMemoryBTree.cjs';
3
+ export { ConcurrentInMemoryBTree } from './concurrency/index.cjs';
4
+ export type { BTreeMutation, ConcurrentInMemoryBTreeConfig, ReadMode, SharedTreeLog, SharedTreeStore, } from './concurrency/index.cjs';
5
+ export { BTreeConcurrencyError, BTreeInvariantError, BTreeValidationError, } from './errors.cjs';
6
+ export type { KeyComparator } from './btree/types.cjs';
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  DEFAULT_MAX_LEAF_ENTRIES,
7
7
  InMemoryBTree,
8
8
  computeAutoScaleTier
9
- } from "./chunk-UGGWGP4E.js";
9
+ } from "./chunk-OFXDCKLC.js";
10
10
 
11
11
  // src/concurrency/helpers.ts
12
12
  var DEFAULT_MAX_RETRIES = 16;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frostpillar/frostpillar-btree",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "A tiny, zero-dependency in-memory B+ tree for TypeScript, Node.js, and browser JavaScript.",
5
5
  "type": "module",
6
6
  "author": "Hajime Sano",
@@ -8,14 +8,24 @@
8
8
  "types": "./dist/index.d.ts",
9
9
  "exports": {
10
10
  ".": {
11
- "types": "./dist/index.d.ts",
12
- "import": "./dist/index.js",
13
- "require": "./dist/index.cjs"
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
14
19
  },
15
20
  "./core": {
16
- "types": "./dist/core.d.ts",
17
- "import": "./dist/core.js",
18
- "require": "./dist/core.cjs"
21
+ "import": {
22
+ "types": "./dist/core.d.ts",
23
+ "default": "./dist/core.js"
24
+ },
25
+ "require": {
26
+ "types": "./dist/core.d.cts",
27
+ "default": "./dist/core.cjs"
28
+ }
19
29
  }
20
30
  },
21
31
  "sideEffects": false,
@@ -23,6 +33,7 @@
23
33
  "dist/**/*.js",
24
34
  "dist/**/*.cjs",
25
35
  "dist/**/*.d.ts",
36
+ "dist/**/*.d.cts",
26
37
  "README.md",
27
38
  "README-JA.md",
28
39
  "LICENSE"
@@ -50,7 +61,7 @@
50
61
  "@types/node": "^24.0.0",
51
62
  "@typescript-eslint/eslint-plugin": "^8.56.1",
52
63
  "@typescript-eslint/parser": "^8.56.1",
53
- "esbuild": "^0.27.3",
64
+ "esbuild": "^0.28.1",
54
65
  "eslint": "^10.0.3",
55
66
  "eslint-config-prettier": "^10.1.8",
56
67
  "prettier": "^3.2.5",
@@ -60,10 +71,11 @@
60
71
  "typescript-eslint": "^8.56.1"
61
72
  },
62
73
  "scripts": {
63
- "build": "rm -rf dist && pnpm build:esm && pnpm build:cjs && pnpm build:types",
74
+ "build": "rm -rf dist && pnpm build:esm && pnpm build:cjs && pnpm build:types && pnpm build:types:cjs",
64
75
  "build:esm": "esbuild src/index.ts src/core.ts --bundle --splitting --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.bundle.json --outdir=dist",
65
76
  "build:cjs": "esbuild src/index.ts src/core.ts --bundle --platform=node --format=cjs --target=es2022 --tsconfig=tsconfig.bundle.json --outdir=dist --out-extension:.js=.cjs",
66
77
  "build:types": "tsc --project tsconfig.build.json",
78
+ "build:types:cjs": "node ./scripts/build-cts-types.mjs",
67
79
  "build:bundle": "esbuild src/index.ts --bundle --minify --target=es2020 --tsconfig=tsconfig.bundle.json --platform=browser --format=iife --global-name=FrostpillarBTree --outfile=dist/frostpillar-btree.min.js",
68
80
  "build:bundle:core": "esbuild src/InMemoryBTree.ts --bundle --minify --target=es2020 --tsconfig=tsconfig.bundle.json --platform=browser --format=iife --global-name=FrostpillarBTreeCore --outfile=dist/frostpillar-btree-core.min.js",
69
81
  "lint": "eslint .",