{"version":3,"file":"btree.cjs","names":["_root","BNode","BNodeInternal"],"sources":["../../../src/utils/btree.ts"],"sourcesContent":["// This file was copied from https://github.com/qwertie/btree-typescript/tree/master and adapted to our needs.\n// We removed methods that we don't need.\n\n// B+ tree by David Piepgrass. License: MIT\n// Informative microbenchmarks & stuff:\n// http://www.jayconrod.com/posts/52/a-tour-of-v8-object-representation (very educational)\n// https://blog.mozilla.org/luke/2012/10/02/optimizing-javascript-variable-access/ (local vars are faster than properties)\n// http://benediktmeurer.de/2017/12/13/an-introduction-to-speculative-optimization-in-v8/ (other stuff)\n// https://jsperf.com/js-in-operator-vs-alternatives (avoid 'in' operator; `.p!==undefined` faster than `hasOwnProperty('p')` in all browsers)\n// https://jsperf.com/instanceof-vs-typeof-vs-constructor-vs-member (speed of type tests varies wildly across browsers)\n// https://jsperf.com/detecting-arrays-new (a.constructor===Array is best across browsers, assuming a is an object)\n// https://jsperf.com/shallow-cloning-methods (a constructor is faster than Object.create; hand-written clone faster than Object.assign)\n// https://jsperf.com/ways-to-fill-an-array (slice-and-replace is fastest)\n// https://jsperf.com/math-min-max-vs-ternary-vs-if (Math.min/max is slow on Edge)\n// https://jsperf.com/array-vs-property-access-speed (v.x/v.y is faster than a[0]/a[1] in major browsers IF hidden class is constant)\n// https://jsperf.com/detect-not-null-or-undefined (`x==null` slightly slower than `x===null||x===undefined` on all browsers)\n// Overall, microbenchmarks suggest Firefox is the fastest browser for JavaScript and Edge is the slowest.\n// Lessons from https://v8project.blogspot.com/2017/09/elements-kinds-in-v8.html:\n//   - Avoid holes in arrays. Avoid `new Array(N)`, it will be \"holey\" permanently.\n//   - Don't read outside bounds of an array (it scans prototype chain).\n//   - Small integer arrays are stored differently from doubles\n//   - Adding non-numbers to an array deoptimizes it permanently into a general array\n//   - Objects can be used like arrays (e.g. have length property) but are slower\n//   - V8 source (NewElementsCapacity in src/objects.h): arrays grow by 50% + 16 elements\n\n/**\n * Mutable B+ tree used by BTreeIndex for sorted value buckets. Keys use the\n * supplied comparator, which must return a number that is not NaN; BTreeIndex\n * checks every comparator result. Point operations cost O(log size). This fork has\n * no copy-on-write sharing, cloning, optional-value storage, early-exit range\n * callbacks, or in-place range edits: only the operations BTreeIndex uses,\n * plus `has()` and the `get()` fallback that the Map oracle observes\n * (tests/btree-map-oracle.test.ts).\n * @author David Piepgrass\n */\nexport class BTree<K = any, V = any> {\n  private _root: BNode<K, V> = new BNode<K, V>()\n  _size = 0\n  _maxNodeSize: number\n\n  /**\n   * provides a total order over keys (and a strict partial order over the type K)\n   * @returns a negative value if a < b, 0 if a === b and a positive value if a > b\n   */\n  _compare: (a: K, b: K) => number\n\n  /**\n   * Initializes an empty B+ tree.\n   * @param compare Custom function to compare pairs of elements in the tree.\n   * @param maxNodeSize Branching factor (maximum items or children per node)\n   *   Must be in range 4..256. If undefined or <4 then default is used; if >256 then 256.\n   */\n  public constructor(compare: (a: K, b: K) => number, maxNodeSize?: number) {\n    this._maxNodeSize = maxNodeSize! >= 4 ? Math.min(maxNodeSize!, 256) : 32\n    this._compare = compare\n  }\n\n  /** Gets the number of key-value pairs in the tree. */\n  get size() {\n    return this._size\n  }\n\n  /** Releases the tree so that its size is 0. */\n  clear() {\n    this._root = new BNode<K, V>()\n    this._size = 0\n  }\n\n  /**\n   * Finds a pair in the tree and returns the associated value.\n   * @param defaultValue a value to return if the key was not found.\n   * @returns the value, or defaultValue if the key was not found.\n   * @description Computational complexity: O(log size)\n   */\n  get(key: K, defaultValue?: V): V | undefined {\n    return this._root.get(key, defaultValue, this)\n  }\n\n  /** Returns true if the key exists in the B+ tree. */\n  has(key: K): boolean {\n    const missing = {} as V\n    return this.get(key, missing) !== missing\n  }\n\n  /**\n   * Adds or overwrites a key-value pair in the B+ tree. Overwriting also\n   * replaces the stored key.\n   * @returns true if a new key-value pair was added.\n   * @description Computational complexity: O(log size)\n   */\n  set(key: K, value: V): boolean {\n    const result = this._root.set(key, value, this)\n    if (result === true || result === false) return result\n    // Root node has split, so create a new root node.\n    this._root = new BNodeInternal<K, V>([this._root, result])\n    return true\n  }\n\n  /**\n   * Removes a single key-value pair from the B+ tree.\n   * @returns true if a pair was found and removed, false otherwise.\n   * @description Computational complexity: O(log size)\n   */\n  delete(key: K): boolean {\n    const size = this._size\n    let root = this._root\n    root.forRange(key, key, true, true, this)\n    // Collapse roots left with at most one child by the deletion.\n    while (root.keys.length <= 1 && !root.isLeaf) {\n      this._root = root =\n        root.keys.length === 0\n          ? new BNode<K, V>()\n          : (root as any as BNodeInternal<K, V>).children[0]!\n    }\n    return this._size !== size\n  }\n\n  /** Gets the lowest key in the tree. Complexity: O(log size) */\n  minKey(): K | undefined {\n    return this._root.minKey()\n  }\n\n  /** Gets the highest key in the tree. Complexity: O(1) */\n  maxKey(): K | undefined {\n    return this._root.maxKey()\n  }\n\n  /** Returns the next pair whose key is larger than the specified key (or undefined if there is none).\n   * If key === undefined, this function returns the lowest pair.\n   */\n  nextHigherPair(key: K | undefined): [K, V] | undefined {\n    return key === undefined\n      ? this._root.minPair()\n      : this._root.getPairOrNextHigher(key, this._compare)\n  }\n\n  /** Returns the next pair whose key is smaller than the specified key (or undefined if there is none).\n   *  If key === undefined, this function returns the highest pair.\n   */\n  nextLowerPair(key: K | undefined): [K, V] | undefined {\n    return key === undefined\n      ? this._root.maxPair()\n      : this._root.getPairOrNextLower(key, this._compare)\n  }\n\n  /**\n   * Scans the specified range of keys, in ascending order by key.\n   * Note: the callback `onFound` must not insert or remove items in the\n   * collection. Doing so may cause incorrect data to be sent to the\n   * callback afterward.\n   * @param low The first key scanned will be greater than or equal to `low`.\n   * @param high Scanning stops when a key larger than this is reached.\n   * @param includeHigh If the `high` key is present, `onFound` is called for\n   *        that final pair if and only if this parameter is true.\n   * @description Computational complexity: O(number of items scanned + log size)\n   */\n  forRange(\n    low: K,\n    high: K,\n    includeHigh: boolean,\n    onFound: (k: K, v: V) => void,\n  ): void {\n    this._root.forRange(low, high, includeHigh, false, this, onFound)\n  }\n}\n\n/** Leaf node / base class. **************************************************/\nclass BNode<K, V> {\n  // If this is an internal node, _keys[i] is the highest key in children[i].\n  keys: Array<K>\n  values: Array<V>\n  get isLeaf() {\n    return (this as any).children === undefined\n  }\n\n  constructor(keys: Array<K> = [], values: Array<V> = []) {\n    this.keys = keys\n    this.values = values\n  }\n\n  // /////////////////////////////////////////////////////////////////////////\n  // Shared methods /////////////////////////////////////////////////////////\n\n  maxKey() {\n    return this.keys[this.keys.length - 1]\n  }\n\n  // If key not found, returns i^failXor where i is the insertion index.\n  // Callers that don't care whether there was a match will set failXor=0.\n  indexOf(key: K, failXor: number, cmp: (a: K, b: K) => number): number {\n    const keys = this.keys\n    let lo = 0,\n      hi = keys.length,\n      mid = hi >> 1\n    while (lo < hi) {\n      const c = cmp(keys[mid]!, key)\n      if (c < 0) lo = mid + 1\n      else if (c > 0)\n        // key < keys[mid]\n        hi = mid\n      else return mid\n      mid = (lo + hi) >> 1\n    }\n    return mid ^ failXor\n  }\n\n  // ///////////////////////////////////////////////////////////////////////////\n  // Leaf Node: misc //////////////////////////////////////////////////////////\n\n  minKey(): K | undefined {\n    return this.keys[0]\n  }\n\n  /** Returns the pair at index `i`, or undefined when `i` is out of range. */\n  pairAt(i: number): [K, V] | undefined {\n    return i >= 0 && i < this.keys.length\n      ? [this.keys[i]!, this.values[i]!]\n      : undefined\n  }\n\n  minPair(): [K, V] | undefined {\n    return this.pairAt(0)\n  }\n\n  maxPair(): [K, V] | undefined {\n    return this.pairAt(this.keys.length - 1)\n  }\n\n  get(key: K, defaultValue: V | undefined, tree: BTree<K, V>): V | undefined {\n    const i = this.indexOf(key, -1, tree._compare)\n    return i < 0 ? defaultValue : this.values[i]\n  }\n\n  // Strictly lower / higher neighbours of `key` within this leaf.\n  getPairOrNextLower(\n    key: K,\n    compare: (a: K, b: K) => number,\n  ): [K, V] | undefined {\n    const i = this.indexOf(key, -1, compare)\n    return this.pairAt(i < 0 ? ~i - 1 : i - 1)\n  }\n\n  getPairOrNextHigher(\n    key: K,\n    compare: (a: K, b: K) => number,\n  ): [K, V] | undefined {\n    const i = this.indexOf(key, -1, compare)\n    return this.pairAt(i < 0 ? ~i : i + 1)\n  }\n\n  // ///////////////////////////////////////////////////////////////////////////\n  // Leaf Node: set & node splitting //////////////////////////////////////////\n\n  set(key: K, value: V, tree: BTree<K, V>): boolean | BNode<K, V> {\n    let i = this.indexOf(key, -1, tree._compare)\n    if (i >= 0) {\n      // Key already exists. Overwrite both key and value.\n      this.keys[i] = key\n      this.values[i] = value\n      return false\n    }\n    i = ~i\n    tree._size++\n    let target: BNode<K, V> = this\n    let newRightSibling: BNode<K, V> | undefined\n    if (this.keys.length >= tree._maxNodeSize) {\n      // This leaf node is full and must split\n      newRightSibling = this.splitOffRightSide()\n      if (i > this.keys.length) {\n        i -= this.keys.length\n        target = newRightSibling\n      }\n    }\n    target.keys.splice(i, 0, key)\n    target.values.splice(i, 0, value)\n    return newRightSibling ?? true\n  }\n\n  takeFromRight(rhs: BNode<K, V>) {\n    // Reminder: parent node must update its copy of key for this node\n    this.values.push(rhs.values.shift()!)\n    this.keys.push(rhs.keys.shift()!)\n  }\n\n  splitOffRightSide(): BNode<K, V> {\n    // Reminder: parent node must update its copy of key for this node\n    const half = this.keys.length >> 1\n    return new BNode<K, V>(this.keys.splice(half), this.values.splice(half))\n  }\n\n  // ///////////////////////////////////////////////////////////////////////////\n  // Leaf Node: scanning & deletions //////////////////////////////////////////\n\n  // Visits [low, high] (or [low, high)) with `onFound`, or deletes that range\n  // when `deleteMode` is set.\n  forRange(\n    low: K,\n    high: K,\n    includeHigh: boolean,\n    deleteMode: boolean,\n    tree: BTree<K, V>,\n    onFound?: (k: K, v: V) => void,\n  ): void {\n    const cmp = tree._compare\n    let iLow, iHigh\n    if (high === low) {\n      // A point range (as used by delete) needs only one search.\n      if (!includeHigh) return\n      iHigh = (iLow = this.indexOf(low, -1, cmp)) + 1\n      if (iLow < 0) return\n    } else {\n      iLow = this.indexOf(low, 0, cmp)\n      iHigh = this.indexOf(high, -1, cmp)\n      if (iHigh < 0) iHigh = ~iHigh\n      else if (includeHigh) iHigh++\n    }\n    if (deleteMode) {\n      if (iHigh > iLow) {\n        this.keys.splice(iLow, iHigh - iLow)\n        this.values.splice(iLow, iHigh - iLow)\n        tree._size -= iHigh - iLow\n      }\n    } else {\n      for (let i = iLow; i < iHigh; i++)\n        onFound!(this.keys[i]!, this.values[i]!)\n    }\n  }\n\n  /** Adds entire contents of right-hand sibling (rhs is left unchanged) */\n  mergeSibling(rhs: BNode<K, V>, _: number) {\n    this.keys.push.apply(this.keys, rhs.keys)\n    this.values.push.apply(this.values, rhs.values)\n  }\n}\n\n/** Internal node (non-leaf node) ********************************************/\nclass BNodeInternal<K, V> extends BNode<K, V> {\n  // Note: conventionally B+ trees have one fewer key than the number of\n  // children, but I find it easier to keep the array lengths equal: each\n  // keys[i] caches the value of children[i].maxKey().\n  children: Array<BNode<K, V>>\n\n  constructor(children: Array<BNode<K, V>>, keys?: Array<K>) {\n    super(keys ?? children.map((child) => child.maxKey()!))\n    this.children = children\n  }\n\n  minKey() {\n    return this.children[0]!.minKey()\n  }\n\n  minPair(): [K, V] | undefined {\n    return this.children[0]!.minPair()\n  }\n\n  maxPair(): [K, V] | undefined {\n    return this.children[this.children.length - 1]!.maxPair()\n  }\n\n  get(key: K, defaultValue: V | undefined, tree: BTree<K, V>): V | undefined {\n    const i = this.indexOf(key, 0, tree._compare),\n      children = this.children\n    return i < children.length\n      ? children[i]!.get(key, defaultValue, tree)\n      : defaultValue\n  }\n\n  getPairOrNextLower(\n    key: K,\n    compare: (a: K, b: K) => number,\n  ): [K, V] | undefined {\n    const i = this.indexOf(key, 0, compare),\n      children = this.children\n    if (i >= children.length) return this.maxPair()\n    return (\n      children[i]!.getPairOrNextLower(key, compare) ??\n      (i > 0 ? children[i - 1]!.maxPair() : undefined)\n    )\n  }\n\n  getPairOrNextHigher(\n    key: K,\n    compare: (a: K, b: K) => number,\n  ): [K, V] | undefined {\n    const i = this.indexOf(key, 0, compare),\n      children = this.children\n    if (i >= children.length) return undefined\n    return (\n      children[i]!.getPairOrNextHigher(key, compare) ??\n      children[i + 1]?.minPair()\n    )\n  }\n\n  // ///////////////////////////////////////////////////////////////////////////\n  // Internal Node: set & node splitting //////////////////////////////////////\n\n  set(key: K, value: V, tree: BTree<K, V>): boolean | BNodeInternal<K, V> {\n    const c = this.children,\n      max = tree._maxNodeSize,\n      cmp = tree._compare\n    let i = Math.min(this.indexOf(key, 0, cmp), c.length - 1)\n    const child = c[i]!\n\n    if (child.keys.length >= max) {\n      // child is full; inserting anything else will cause a split.\n      // Shifting an item to the left sibling may avoid a split. We can do a\n      // shift if that sibling is not full and if the current key can still be\n      // placed in the same node after the shift. A right shift would need a\n      // key above child.maxKey(), and that only reaches the last child.\n      let other: BNode<K, V> | undefined\n      if (\n        i > 0 &&\n        (other = c[i - 1]!).keys.length < max &&\n        cmp(child.keys[0]!, key) < 0\n      ) {\n        other.takeFromRight(child)\n        this.keys[i - 1] = other.maxKey()!\n      }\n    }\n\n    const result = child.set(key, value, tree)\n    if (result === false) return false\n    this.keys[i] = child.maxKey()!\n    if (result === true) return true\n\n    // The child has split and `result` is a new right child... does it fit?\n    let target: BNodeInternal<K, V> = this\n    let newRightSibling: BNodeInternal<K, V> | undefined\n    if (this.keys.length >= max) {\n      // no, we must split also\n      newRightSibling = this.splitOffRightSide()\n      // The new child follows i; no comparison is needed after mutation.\n      if (i + 1 >= this.keys.length) {\n        target = newRightSibling\n        i -= this.keys.length\n      }\n    }\n    target.children.splice(i + 1, 0, result)\n    target.keys.splice(i + 1, 0, result.maxKey()!)\n    return newRightSibling ?? true\n  }\n\n  /**\n   * Split this node.\n   * Modifies this to remove the second half of the items, returning a separate node containing them.\n   */\n  splitOffRightSide() {\n    const half = this.children.length >> 1\n    return new BNodeInternal<K, V>(\n      this.children.splice(half),\n      this.keys.splice(half),\n    )\n  }\n\n  takeFromRight(rhs: BNode<K, V>) {\n    // Reminder: parent node must update its copy of key for this node\n    this.keys.push(rhs.keys.shift()!)\n    this.children.push((rhs as BNodeInternal<K, V>).children.shift()!)\n  }\n\n  // ///////////////////////////////////////////////////////////////////////////\n  // Internal Node: scanning & deletions //////////////////////////////////////\n\n  forRange(\n    low: K,\n    high: K,\n    includeHigh: boolean,\n    deleteMode: boolean,\n    tree: BTree<K, V>,\n    onFound?: (k: K, v: V) => void,\n  ): void {\n    const cmp = tree._compare\n    const keys = this.keys,\n      children = this.children\n    let iLow = this.indexOf(low, 0, cmp),\n      i = iLow\n    // A point range (as used by delete) needs only one search.\n    const iHigh = Math.min(\n      high === low ? iLow : this.indexOf(high, 0, cmp),\n      keys.length - 1,\n    )\n    for (; i <= iHigh; i++) {\n      children[i]!.forRange(low, high, includeHigh, deleteMode, tree, onFound)\n      // Note: if children[i] is empty then keys[i]=undefined.\n      //       This is an invalid state, but it is fixed below.\n      if (deleteMode) keys[i] = children[i]!.maxKey()!\n    }\n    if (deleteMode) {\n      // Deletions may have occurred, so look for opportunities to merge nodes.\n      const half = tree._maxNodeSize >> 1\n      if (iLow > 0) iLow--\n      for (i = iHigh; i >= iLow; i--) {\n        if (children[i]!.keys.length <= half) {\n          if (children[i]!.keys.length !== 0) {\n            this.tryMerge(i, tree._maxNodeSize)\n          } else {\n            // child is empty! delete it!\n            keys.splice(i, 1)\n            children.splice(i, 1)\n          }\n        }\n      }\n    }\n  }\n\n  /** Merges child i with child i+1 if their combined size is not too large */\n  tryMerge(i: number, maxSize: number): void {\n    const children = this.children\n    if (\n      i >= 0 &&\n      i + 1 < children.length &&\n      children[i]!.keys.length + children[i + 1]!.keys.length <= maxSize\n    ) {\n      children[i]!.mergeSibling(children[i + 1]!, maxSize)\n      children.splice(i + 1, 1)\n      this.keys.splice(i + 1, 1)\n      this.keys[i] = children[i]!.maxKey()!\n    }\n  }\n\n  /**\n   * Move children from `rhs` into this.\n   * `rhs` must be part of this tree, and be removed from it after this call.\n   */\n  mergeSibling(rhs: BNode<K, V>, maxNodeSize: number) {\n    const oldLength = this.keys.length\n    this.keys.push.apply(this.keys, rhs.keys)\n    const rhsChildren = (rhs as any as BNodeInternal<K, V>).children\n    this.children.push.apply(this.children, rhsChildren)\n\n    // If our children are themselves almost empty due to a mass-delete,\n    // they may need to be merged too (but only the oldLength-1 and its\n    // right sibling should need this).\n    this.tryMerge(oldLength - 1, maxNodeSize)\n  }\n}\n"],"mappings":"AAmCA,IAAa,QAAb,MAAqC;;;;;;;CAiBnC,YAAmB,SAAiC,aAAsB;EAhB7C,KAAAA,KAAA,IAAI,MAAY;EACrC,KAAA,QAAA;EAgBN,KAAK,eAAe,eAAgB,IAAI,KAAK,IAAI,aAAc,GAAG,IAAI;EACtE,KAAK,WAAW;CAClB;;CAGA,IAAI,OAAO;EACT,OAAO,KAAK;CACd;;CAGA,QAAQ;EACN,KAAKA,KAAQ,IAAI,MAAY;EAC7B,KAAK,QAAQ;CACf;;;;;;;CAQA,IAAI,KAAQ,cAAiC;EAC3C,OAAO,KAAKA,GAAM,IAAI,KAAK,cAAc,IAAI;CAC/C;;CAGA,IAAI,KAAiB;EACnB,MAAM,UAAU,CAAC;EACjB,OAAO,KAAK,IAAI,KAAK,OAAO,MAAM;CACpC;;;;;;;CAQA,IAAI,KAAQ,OAAmB;EAC7B,MAAM,SAAS,KAAKA,GAAM,IAAI,KAAK,OAAO,IAAI;EAC9C,IAAI,WAAW,QAAQ,WAAW,OAAO,OAAO;EAEhD,KAAKA,KAAQ,IAAI,cAAoB,CAAC,KAAKA,IAAO,MAAM,CAAC;EACzD,OAAO;CACT;;;;;;CAOA,OAAO,KAAiB;EACtB,MAAM,OAAO,KAAK;EAClB,IAAI,OAAO,KAAKA;EAChB,KAAK,SAAS,KAAK,KAAK,MAAM,MAAM,IAAI;EAExC,OAAO,KAAK,KAAK,UAAU,KAAK,CAAC,KAAK,QACpC,KAAKA,KAAQ,OACX,KAAK,KAAK,WAAW,IACjB,IAAI,MAAY,IACf,KAAoC,SAAS;EAEtD,OAAO,KAAK,UAAU;CACxB;;CAGA,SAA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