A hash-based dictionary for strings, which wraps a native JavaScript object to provide very fast lookup, insertion and deletion.

import { StringDict } from "scl"

When two items are added with the same key, the second item will override the first.

const d = new StringDict<number>()
d.add(1, 'one') // ok
d.add(1, 'two') // ok; replaced
assert.strictEqual(d.getValue(1), 'two')

All operations in the dictionary are in O(1).

Type Parameters

  • V

Implements

Constructors

  • Construct a new string-based dictionary.

    const d = new StringDict<number>()
    

    You can also constrcut this dictionary from any iterable, like so:

    const d = new StringDict([
    ['one', 1],
    ['two', 2]
    ])

    Type Parameters

    • V

    Parameters

    • Optionaliterable: Iterable<[string, V], any, any>

    Returns StringDict<V>

Accessors

  • get size(): number
  • Count the amount of elements in the collection.

    ⚠️ In most cases, this should be an O(1) operation. However, there are cases where this can be an O(n) operation. Therefore, it is recommended to always cache the result in a local variable.

    Returns number

Methods

  • Returns an object which is able to sift through the values in this collection.

    The order by which the elements are traversed depends on the kind of collection. For unordered collections, the iteration order is unspecified and may even differ between two iterations on the same collection.

    Returns IterableIterator<[string, V], any, any>

  • Add an element to the collection. If the element already exists, update its value.

    The location where the element is placed depends on the collection type, and in the generic case there is no guarantee on the location where it is inserted.

    This method returns a pair with the first element indicating whether the element was added, while the second element refers to the actual location of the element.

    Parameters

    Returns [boolean, Cursor<[string, V]>]

  • Remove all elements from this collection, effectively setting the collection to the empty collection.

    Returns void

  • Remove an element from the collection. If multiple elements are matched, the collection picks one of them.

    Parameters

    Returns boolean

    true if the element was found, false otherwise.

  • Remove an element from the collection. If multiple elements are matched, the collection removes all of them.

    Parameters

    • element: [string, V]

    Returns number

    The amount of elements that was removed.

  • Remove the element pointed to by the iterator result from this collection.

    Parameters

    • cursor: ObjectCursor<V>

    Returns void

  • Delete a pair from the underlying collection that has the given key as key.

    Returns the amount of items that have been deleted.

    Parameters

    • key: string

    Returns 0 | 1

  • Creates a new pair and inserts it in the underlying collection.

    Parameters

    • key: string
    • value: V

    Returns [boolean, Cursor<[string, V]>]

  • Similar to Dict.getValue, except that it returns the pair that was inserted in the collection.

    Parameters

    • key: string

    Returns ObjectCursor<V>

  • Get the value that is associated with the given key.

    Parameters

    • key: string

    Returns any

  • Checks if the collection holds the given element.

    Parameters

    • element: Pair<string, V>

      The element to check membership of.

    Returns boolean

    True if the collections holds the given element, false otherwise.

  • Checks whether there a pair in this collection that has the given key.

    Parameters

    • key: string

    Returns boolean