Options
All
  • Public
  • Public/Protected
  • All
Menu

A simple hash-based dictionary that only allows one item with same key.

import { HashDict } from "scl"

Most methods in this collection, given that a proper hashing function is set up, are in Θ(1 + n/k). If you're out of luck, this characteristic can grow to O(n).

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 HashDict<number, string>()
d.emplace(1, 'foo')
assert.strictEqual(d.getValue(1), 'foo')
d.emplace(1, 'bar')
assert.strictEqual(d.getValue(1), 'bar')

Type parameters

  • K

    The type of key of a given entry.

  • V

    The type of value associated with the given key.

Hierarchy

  • DictBase<K, V>
    • HashDict

Index

Constructors

constructor

  • new HashDict<K, V>(opts?: Iterable<[K, V]> | HashIndex<[K, V], K> | HashDictOptions<K, V>): HashDict<K, V>
  • Construct a new hash-based dictionary.

    const d = new HashDict<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 HashDict<number, string>([
      [1, 'one'],
      [2, 'two']
    ])
    

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

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

    Type parameters

    • K

    • V

    Parameters

    • opts: Iterable<[K, V]> | HashIndex<[K, V], K> | HashDictOptions<K, V> = ...

    Returns HashDict<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

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