Options
All
  • Public
  • Public/Protected
  • All
Menu

A tree-based dictionary that only allows one item with the same key.

import { TreeDict } from "scl"

The following table summarises the worst-case time complexity of the most commonly used properies of this class. For more information, see the documentation of the respective property.

Property name Worst case
add() O(log(n))
clear() O(1)
equalKeys() O(log(n))
delete() O(log(n))
deleteAll() O(log(n))
deleteAt() O(log(n))
size O(1)

When a new entry is added with a key that is already taken, the dictionary will replace the corresponding entry with the new one.

const d = new TreeDict<number, string>()
d.emplace(1, 'foo') // ok
assert.strictEqual(d.getValue(1), 'foo')
d.emplace(1, 'bar') // ok; replaced
assert.strictEqual(d.getValue(1), 'bar')

If you need to throw an error when a key is already taken, simply use has().

The items in a tree-based dictionary are guaranteed to be sorted on their keys.

d.emplace(2, 'two')
d.emplace(1, 'one')
d.emplace(3, 'three')
assert.deepEqual([...d], [[1, 'one'], [2, 'two'], [3, 'three']])
see

HashDict for a fast, unordered version of this collection.

see

TreeMultiDict when you need to store multiple items.

Type parameters

  • K

    The type of key of a given entry.

  • V

    The type of value associated with the given key.

Hierarchy

  • RBTreeDict<K, V>
    • TreeDict

Index

Constructors

constructor

  • new TreeDict<K, V>(opts?: RBTreeDict<K, V> | Iterable<[K, V]> | TreeDictOptions<K, V>): TreeDict<K, V>
  • Construct a new tree-based dictionary.

    const d = new TreeDict<number, string>()
    

    Similar to JavaScript's built-in map type, the constructor accepts a list of key-value pairs that will immediately be added to the resulting dictionary.

    const d = new TreeDict<number, string>([
      [1, 'one'],
      [2, 'two']
    ])
    

    The dictionary can be tweaked by providing a [[TreeDictOptions]]-object, which allows to configure things like the default compare function and value equality.

    const d = new TreeDict<number, string>({
      compareKeys: (a, b) => a < b,
      valuesEqual: (a, b) => a === b,
      elements: [[1, 'one'], [2, 'two']]
    })
    

    Type parameters

    • K

    • V

    Parameters

    • opts: RBTreeDict<K, V> | Iterable<[K, V]> | TreeDictOptions<K, V> = ...

    Returns TreeDict<K, V>

Accessors

size

  • get size(): number

Methods

[Symbol.iterator]

  • [Symbol.iterator](): IterableIterator<[K, V]>

add

  • add(element: [K, V], hint?: any): AddResult<[K, V]>

clear

  • clear(): void

clone

  • clone(): RBTreeDict<K, V>

delete

  • delete(element: [K, V]): boolean
  • Parameters

    • element: [K, V]

    Returns boolean

deleteAll

  • deleteAll(element: [K, V]): number

deleteAt

  • deleteAt(position: Cursor<[K, V]>): void

deleteKey

  • deleteKey(key: K): number

emplace

  • emplace(key: K, value: V): [boolean, Cursor<[K, V]>]

equalKeys

  • equalKeys(key: K): Range<[K, V]>

findKey

  • findKey(key: K): null | Cursor<[K, V]>

getValue

  • getValue(key: K): undefined | V

has

  • has(element: [K, V]): boolean
  • Parameters

    • element: [K, V]

    Returns boolean

hasKey

  • hasKey(key: K): boolean

toRange

  • toRange(): Range<[K, V]>

Legend

  • Property
  • Method
  • Accessor
  • Property
  • Method
  • Inherited property
  • Inherited method

Generated using TypeDoc