digitree
    Preparing search index...

    Class BTree<TKey, TEntry>

    Represents a lightweight B+(ish)Tree (data at leaves, but no linked list of leaves). Allows for efficient storage and retrieval of data in a sorted manner.

    Type Parameters

    • TKey

      The type of keys used for indexing the entries. This might be an element of TEntry, or TEntry itself.

    • TEntry

      The type of entries stored in the B-tree.

    Index

    Constructors

    • Type Parameters

      • TKey
      • TEntry

      Parameters

      • OptionalkeyFromEntry: (entry: TEntry) => TKey = ...

        a function to extract the key from an entry. The default assumes the key is the entry itself.

      • Optionalcompare: (a: TKey, b: TKey) => number = ...

        a comparison function for keys. The default uses < and > operators.

      Returns BTree<TKey, TEntry>

    Methods

    • Invokes user-provided comperator to compare two keys. Inner-loop code, so this doesn't do backflips to iron out ES's idiosyncrasies (undefined quirks, infinity, nulls, etc.), but does ensure deterministic comparison. If you want to eak out more performance at the risk of corruption, you can override this method and omit the consistency check.

      Parameters

      Returns number

    • Deletes the entry at the given path. The on property of the path will be cleared.

      Parameters

      Returns boolean

      true if the delete succeeded (the key was found); false otherwise.

    • Attempts to find the given key

      Parameters

      Returns Path<TKey, TEntry>

      Path to the key or the "crack" before it. If on is true on the resulting path, the key was found. If on is false, next() and prior() can attempt to move to the nearest match.

    • Retrieves the entry for the given key. Use find instead for a path to the key, the nearest match, or as a basis for navigation.

      Parameters

      Returns undefined | TEntry

      the entry for the given key if found; undefined otherwise.

    • Computed (not stored) count. Computes the sum using leaf-node lengths. O(n/af) where af is average fill.

      Parameters

      • Optionalfrom: { ascending?: boolean; path: Path<TKey, TEntry> }

        if provided, the count will start from the given path (inclusive). If ascending is false, the count will start from the end of the tree. Ascending is true by default.

      Returns number

    • Adds a value to the tree. Be sure to check the result, as the tree does not allow duplicate keys. Added entries are frozen to ensure immutability

      Parameters

      Returns Path<TKey, TEntry>

      path to the new (on = true) or conflicting (on = false) row.

    • Parameters

      Returns boolean

      true if the given path remains valid; false if the tree has been mutated, invalidating the path.

    • Inserts or updates depending on the existence of the given key, using callbacks to generate the new value.

      Parameters

      • newEntry: TEntry

        the new entry to insert if the key doesn't exist.

      • getUpdated: (existing: TEntry) => TEntry

        a callback to generate an updated entry if the key does exist. WARNING: mutation in this callback will cause merge to error.

      Returns [path: Path<TKey, TEntry>, wasUpdate: boolean]

      path to new entry and whether an update or insert attempted. If getUpdated callback returns a row that is already present, the resulting path will not be on.

    • Updates the entry at the given path to the given value. Deletes and inserts if the key changes.

      Parameters

      Returns [path: Path<TKey, TEntry>, wasUpdate: boolean]

      path to resulting entry and whether it was an update (as opposed to an insert). * on = true if update/insert succeeded. * wasUpdate = true if updated; false if inserted. * Returned path is on entry * on = false if update/insert failed. * wasUpdate = true, given path is not on an entry * else newEntry's new key already present; returned path is "near" existing entry

    • Inserts the entry if it doesn't exist, or updates it if it does. The entry is frozen to ensure immutability.

      Parameters

      Returns Path<TKey, TEntry>

      path to the new entry. on = true if existing; on = false if new.