Options
All
  • Public
  • Public/Protected
  • All
Menu

A tree-based dictionary that can store multile items with the same key, but only if the values differ.

import { TreeMultiDict } 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(n)
delete() O(log(n))
deleteAll() O(n)
deleteAt() O(log(n))
size O(1)

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

const d = new TreeMultiDict<number, string>()
d.emplace(1, 'foo') // ok
assert.strictEqual(d.getValues(1), 'foo')
d.emplace(1, 'bar') // ok
d.emplace(1, 'foo') // ok
const values = [...d.getValues(1)]
assert.lengthOf(values, 3)
assert.deepInclude(value, [1, 'foo']) // appears two times
assert.deepInclude(value, [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. This is not true for values with the same, which generally appear in the order by which they were inserted.

const d = new TreeMultiDict<number, string>()
d.emplace(2, 'two')
d.emplace(1, 'one')
d.emplace(3, 'three')
assert.deepEqual([...d], [[1, 'one'], [2, 'two'], [3, 'three']])
see

HashMultiDict for a fast, unordered version of this collection.

see

TreeDict when you need your keys to be unique.

Type parameters

  • K

    The type of key of a given entry.

  • V

    The type of value associated with the given key.

Hierarchy

  • RBTreeMultiDict<K, V>
    • TreeMultiDict

Index

Constructors

constructor

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

    const d = new TreeMultiDict<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 TreeMultiDict<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 TreeMultiDict<number, string>({
      compare: (a, b) => a < b,
      valuesEqual: (a, b) => a === b,
      elements: [[1, 'one'], [2, 'two']]
    })
    

    Type parameters

    • K

    • V

    Parameters

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

    Returns TreeMultiDict<K, V>

Accessors

size

  • get size(): number

Methods

[Symbol.iterator]

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

add

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

clear

  • clear(): void

clone

  • clone(): RBTreeMultiDict<K, V>

delete

  • delete(element: [K, V]): 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]>

getValues

  • getValues(key: K): IterableIterator<V>

has

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

hasKey

  • hasKey(key: K): boolean

toRange

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

Legend

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

Generated using TypeDoc