Hashmap class

Currently supports collision resolution by chaining, hashing by modulo and string keys

Todo

Add load-factor based rehashing

Todo

Add hashing by multiplication option

Type Parameters

  • T

    The type of the values in the hashmap.

Hierarchy

  • Hashmap

Constructors

Properties

#dynamic_rehashing_enabled: boolean = false

Whether or not to use dynamic rehashing.

#hash_function: ((key: string) => number)

Type declaration

#hashing_method: "MODULO" | "MULTIPLICATION"

The hashing method to use.

#max_load_factor: null | number

Maximum load factor for dynamic rehashing.

This value is compared with the new load factor after insert/delete operations to determine whether or not to decrease the # of buckets.

#min_load_factor: null | number

Minimum load factor for dynamic rehashing.

This value is compared with the new load factor after insert/delete operations to determine whether or not to increase the # of buckets.

#multiplication_factor: null | number = null

Multiplication factor for hashing by multiplication (if used.)

#stop_rehash_loop: boolean = true

When this is true, insert and delete operations will not check for dynamic rehashing. This is done to prevent a rehash causing another rehash.

buckets: (null | LinkedList<[string, T]>)[] = []

The buckets of the hashmap

This is an array of LinkedLists of entries which represented as a tuples of [string, T]

When an entry is inserted, it will become the head of the corresponding LL

When all entries in an LL bucket are removed, the corresponding entry in the array becomes null; see delete

elements: number = 0

The number of entries present in the hashmap

Used to calculate load factor

keys: string[] = []

The keys used in the hashmap

rehashes: number = 0

Number of times the hashmap was dynamically rehashed (i.e. the number of buckets changed.)

Accessors

  • get valid_elements_range(): null | [number, number]
  • Returns

    a tuple [min, max] of two integers, representing the inclusive range of allowed number of elements/entries before dynamic rehashing. If an insertion or deletion operation would cause the number of elements to fall outside of this range, the number of buckets will be adjusted before the insertion/deletion.

    Returns

    null if dynamic rehashing is not enabled.

    Returns null | [number, number]

Methods

  • Private

    Default hash function used when user does not specify their own

    Based on Java's hashCode

    Returns

    The hash digest of key

    Parameters

    • key: Key

    Returns number

  • Rehashes the hash table if the load factor falls outside the min/max bounds.

    Returns

    true if rehashed, false otherwise

    Throws

    When dynamic hashing is not enabled.

    Returns boolean

  • Private

    Passes hash digest of key through modulo operation to get bucket index.

    Returns

    The index of the corresponding bucket in this.buckets

    Parameters

    • key: Key

      The key

    Returns number

  • Removes the entry with the specified key. This is an in-place operation.

    If the result of this operation would lead to an empty LL the LL will become null

    Throws

    NONEXISTANT_KEY

    Parameters

    • key: Key

    Returns void

  • Resizes the number of buckets of the hashmap and rehashes pre-existing elements to the new buckets.

    Parameters

    • desired: number

      The desired new value for the parameter.

    • on_buckets: boolean = false

      Whether to rehash based on load factor or number of buckets. If true, desired will be interpreted as the new number of buckets. If false, desired will be interpreted as the new desired load factor. false by default.

      When on_buckets is false, the new number of buckets is set to ceil(entries / desired).

    Returns void

Generated using TypeDoc