Class MathMap<K>Alpha

A DefaultMap with methods for performing basic math operations that mutate the data.

Type Parameters

  • K

Hierarchy (view full)

Constructors

  • Create a new DefaultMap with a specified default value.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • K

    Parameters

    • defaultValue: number

      The default value to use for missing keys

    • Optional entries: null | Iterable<readonly [unknown, unknown]>

    Returns MathMap<K>

    Example

    const map = new DefaultMap(0);
    
  • Create a new DefaultMap with a specified function to generate default values.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • K

    Parameters

    • defaultValueFn: ((key) => number)

      The function to generate default values

        • (key): number
        • Parameters

          • key: K

          Returns number

    • Optional entries: null | Iterable<readonly [unknown, unknown]>

    Returns MathMap<K>

    Example

    const map = new DefaultMap(key => key.toUpperCase());
    

Properties

[toStringTag]: string
size: number

Returns

the number of elements in the Map.

[species]: MapConstructor

Methods

  • Returns an iterable of entries in the map.

    Returns IterableIterator<[K, number]>

  • Add an addend to the value for a given key.

    Parameters

    • key: K

      The key to update the value of

    • addend: number

      The value to add to the current value

    Returns number

    The updated value

    Example

    map.add('a', 1);
    
  • Returns void

  • Parameters

    • key: K

    Returns boolean

    true if an element in the Map existed and has been removed, or false if the element does not exist.

  • Divide the value for a given key by a divisor.

    Parameters

    • key: K

      The key to update the value of

    • divisor: number

      The value to divide the current value by

    Returns number

    The updated value

    Example

    map.divide('a', 1);
    
  • Returns an iterable of key, value pairs for every entry in the map.

    Returns IterableIterator<[K, number]>

  • Executes a provided function once per each key/value pair in the Map, in insertion order.

    Parameters

    • callbackfn: ((value, key, map) => void)
        • (value, key, map): void
        • Parameters

          • value: number
          • key: K
          • map: Map<K, number>

          Returns void

    • Optional thisArg: any

    Returns void

  • Get the value for a given key, or the default value if it's missing.

    Time complexity: O(1)

    Space complexity: O(1)

    Parameters

    • key: K

      The key to get the value for

    Returns number

    The value for key, or the default value if it's missing

    Example

    map.get('a');
    
  • Parameters

    • key: K

    Returns boolean

    boolean indicating whether an element with the specified key exists or not.

  • Returns an iterable of keys in the map

    Returns IterableIterator<K>

  • Multiply a multiplicand by the value for a given key.

    Parameters

    • key: K

      The key to update the value of

    • multiplicand: number

      The value to multiply the current value by

    Returns number

    The updated value

    Example

    map.multiply('a', 1);
    
  • Raise the value for a given key to an exponent.

    Parameters

    • key: K

      The key to update the value of

    • exponent: number

      The exponent to raise the current value to

    Returns number

    The updated value

    Example

    map.pow('a', 2);
    
  • Take the _n_th root of the value for a given key.

    Parameters

    • key: K

      The key to get the _n_th root of

    • root: number

      The root to get

    Returns number

    The nth root of the value for key

    Example

    map.root('a', 2);
    
  • Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.

    Parameters

    • key: K
    • value: number

    Returns this

  • Square root of the value for a given key.

    Parameters

    • key: K

      The key to get the square root of

    Returns number

    The square root of the value for key

    Example

    map.sqrt('a');
    
  • Subtract a subtrahend from the value for a given key.

    Parameters

    • key: K

      The key to update the value of

    • subtrahend: number

      The value to subtract from the current value

    Returns number

    The updated value

    Example

    map.subtract('a', 1);
    
  • Returns an iterable of values in the map

    Returns IterableIterator<number>