Class BTree<K, V>

A mapping is a Collection indexed by keys that may have associated values.

Abstract

Template

V

Type Parameters

  • K = any

  • V = any

Hierarchy

Constructors

  • Initializes an empty B+ tree.

    Type Parameters

    • K = any

    • V = any

    Parameters

    • Optional entries: Iterable<[K, V]>

      The initial key value pairs.

    • Optional compareFn: Comparer<any>

      Custom function to compare pairs of elements in the tree. If not specified, defaultComparator will be used which is valid as long as K extends DefaultComparable.

    • Optional maxNodeSize: number = 64

      Branching factor (maximum items or children per node) Must be in range 4..256. If undefined or <4 then default is used; if >256 then 256.

    Returns BTree<K, V>

Properties

_compare: Comparer<any>

provides a total order over keys (and a strict partial order over the type K)

Returns

a negative value if a < b, 0 if a === b and a positive value if a > b

_maxNodeSize: number
count: number = 0

Accessors

  • get height(): number
  • Gets the height of the tree: the number of internal nodes between the BTree object and its leaf nodes (zero if there are no internal nodes).

    Returns number

  • get maxNodeSize(): number
  • Returns the maximum number of children/values before nodes will split.

    Returns number

  • get size(): number
  • Gets the number of key-value pairs in the tree.

    Returns number

Methods

  • Returns the key-value pair associated with the supplied key if it exists or the pair associated with the next lower pair otherwise. If there is no next lower pair, undefined is returned.

    Parameters

    • key: K

      The key to search for.

    • Optional reusedArray: [K, V]

      Optional array used repeatedly to store key-value pairs, to avoid creating a new array each time you call this method.

    Returns [K, V]

  • Performs a deep cloning of the tree, immediately duplicating any nodes that are not currently marked as shared, in order to avoid marking any additional nodes as shared.

    Parameters

    • force: boolean = false

      Clone all nodes, even shared ones.

    Returns BTree<K, V>

  • Returns true if the key exists in the B+ tree, false if not.

    Description

    Computational complexity: O(log size)

    Parameters

    • key: K

      Key to detect

    Returns boolean

  • Returns an iterator that provides items in order (ascending order if the collection's comparator uses ascending order, as is the default.)

    Parameters

    • Optional lowestKey: K

      First key to be iterated, or undefined to start at minKey(). If the specified key doesn't exist then iteration starts at the next higher key (according to the comparator).

    • Optional reusedArray: (K | V)[]

      Optional array used repeatedly to store key-value pairs, to avoid creating a new array on every iteration.

    Returns IterableIterator<[K, V]>

  • Adds all pairs from a list of key-value pairs.

    Returns

    The number of pairs added to the collection.

    Description

    Computational complexity: O(pairs.length * log(size + pairs.length))

    Parameters

    • entries: Iterable<[K, V]>

      Pairs to add to this tree. If there are duplicate keys, later pairs currently overwrite earlier ones (e.g. [[0,1],[0,7]] associates 0 with 7.)

    Returns number

  • Returns the key-value pair associated with the supplied key if it exists or the pair associated with the next lower pair otherwise. If there is no next lower pair, undefined is returned.

    Parameters

    • key: K

      The key to search for.

    • Optional reusedArray: [K, V]

      Optional array used repeatedly to store key-value pairs, to avoid creating a new array each time you call this method.

    Returns [K, V]

  • Makes the object read-only to ensure it is not accidentally modified. Freezing does not have to be permanent; unfreeze() reverses the effect. This is accomplished by replacing mutator functions with a function that throws an Error. Compared to using a property (e.g. this.isFrozen) this implementation gives better performance in non-frozen BTrees.

    Returns void

  • Finds a pair in the tree and returns the associated value.

    Returns

    the value, or defaultValue if the key was not found.

    Description

    Computational complexity: O(log size)

    Parameters

    • key: K
    • Optional defaultValue: V

      a value to return if the key was not found.

    Returns V

  • Returns a new iterator for iterating the keys of each pair in ascending order.

    Parameters

    • Optional firstKey: K

    Returns IterableIterator<K>

  • Builds an array of pairs from the specified range of keys, sorted by key. Each returned pair is also an array: pair[0] is the key, pair[1] is the value.

    Description

    Computational complexity: O(result.length + log size)

    Parameters

    • low: K

      The first key in the array will be greater than or equal to low.

    • high: K

      This method returns when a key larger than this is reached.

    • Optional includeHigh: boolean

      If the high key is present, its pair will be included in the output if and only if this parameter is true. Note: if the low key is present, it is always included in the output.

    • maxLength: number = 0x3ffffff

      Length limit. getRange will stop scanning the tree when the array reaches this size.

    Returns [K, V][]

  • Returns the next pair whose key is larger than the specified key (or undefined if there is none). If key === undefined, this function returns the lowest pair.

    Parameters

    • key: K

      The key to search for.

    • Optional reusedArray: [K, V]

      Optional array used repeatedly to store key-value pairs, to avoid creating a new array on every iteration.

    Returns [K, V]

  • Scans the specified range of keys, in ascending order by key. Note: the callback onFound must not insert or remove items in the collection. Doing so may cause incorrect data to be sent to the callback afterward.

    Returns

    The number of values found, or R if the callback returned {done:R} to stop early.

    Description

    Computational complexity: O(number of items scanned + log size)

    Type Parameters

    • R = number

    Parameters

    • low: K

      The first key scanned will be greater than or equal to low.

    • high: K

      Scanning stops when a key larger than this is reached.

    • includeHigh: boolean

      If the high key is present, onFound is called for that final pair if and only if this parameter is true.

    • Optional onFound: Iteratee<K, V, any>

      A function that is called for each key-value pair. This function can return {done:R} to stop early with result R.

    • Optional initialCounter: number

      Initial third argument of onFound. This value increases by one each time onFound is called. Default: 0

    Returns number | R

  • Scans and potentially modifies values for a subsequence of keys. Note: the callback onFound should ideally be a pure function. Specifically, it must not insert items, call clone(), or change the collection except via return value; out-of-band editing may cause an exception or may cause incorrect data to be sent to the callback (duplicate or missed items). It must not cause a clone() of the collection, otherwise the clone could be modified by changes requested by the callback.

    Returns

    The number of values scanned, or R if the callback returned {done:R} to stop early.

    Description

    Computational complexity: O(number of items scanned + log size) Note: if the tree has been cloned with clone(), any shared nodes are copied before onFound is called. This takes O(n) time where n is proportional to the amount of shared data scanned.

    Type Parameters

    • R = V

    Parameters

    • low: K

      The first key scanned will be greater than or equal to low.

    • high: K

      Scanning stops when a key larger than this is reached.

    • includeHigh: boolean

      If the high key is present, onFound is called for that final pair if and only if this parameter is true.

    • onFound: ((k: K, v: V, counter: number) => any)

      A function that is called for each key-value pair. This function can return {value:v} to change the value associated with the current key, {delete:true} to delete the current pair, {done:R} to stop early with result R, or it can return nothing (undefined or {}) to cause no effect and continue iterating. {done:R} can be combined with one of the other two commands. The third argument counter is the number of items iterated previously; it equals 0 when onFound is called the first time.

        • (k: K, v: V, counter: number): any
        • Parameters

          • k: K
          • v: V
          • counter: number

          Returns any

    • Optional initialCounter: number

    Returns number | R

  • Removes a single key-value pair from the B+ tree.

    Returns

    true if a pair was found and removed, false otherwise.

    Description

    Computational complexity: O(log size)

    Parameters

    • key: K

      Key to find

    Returns boolean

  • Removes a series of keys from the collection.

    Parameters

    • keys: K[]

    Returns number

  • Removes a range of key-value pairs from the B+ tree.

    Returns

    The number of key-value pairs that were deleted.

    Description

    Computational complexity: O(log size + number of items deleted)

    Parameters

    • low: K

      The first key scanned will be greater than or equal to low.

    • high: K

      Scanning stops when a key larger than this is reached.

    • includeHigh: boolean

      Specifies whether the high key, if present, is deleted.

    Returns number

  • Returns an iterator that provides items in reversed order.

    Parameters

    • Optional highestKey: K

      Key at which to start iterating, or undefined to start at maxKey(). If the specified key doesn't exist then iteration starts at the next lower key (according to the comparator).

    • Optional reusedArray: (K | V)[]

      Optional array used repeatedly to store key-value pairs, to avoid creating a new array on every iteration.

    • Optional skipHighest: boolean

      Iff this flag is true and the highestKey exists in the collection, the pair matching highestKey is skipped, not iterated.

    Returns IterableIterator<[K, V]>

  • Adds or overwrites a key-value pair in the B+ tree.

    Returns

    true if a new key-value pair was added.

    Description

    Computational complexity: O(log size) Note: when overwriting a previous entry, the key is updated as well as the value. This has no effect unless the new key has data that does not affect its sort order.

    Parameters

    • key: K

      the key is used to determine the sort order of data in the tree.

    • value: V

      data to associate with the key (optional)

    Returns boolean

  • Gets an array filled with the contents of the tree, sorted by key

    Returns [K, V][]

  • Returns the next pair whose key is smaller than the specified key (or undefined if there is none). If key === undefined, this function returns the highest pair.

    Parameters

    • key: K

      The key to search for.

    • Optional reusedArray: [K, V]

      Optional array used repeatedly to store key-value pairs, to avoid creating a new array each time you call this method.

    Returns [K, V]

  • Returns a new iterator for iterating the values of each pair in order by key.

    Parameters

    • Optional firstKey: K

    Returns IterableIterator<V>

Generated using TypeDoc