# Class: SharedMap

## SharedMap(maxSize, keySize, objSize) → {SharedMap}

SharedMap zero-dependency high-performance unordered Vanilla JS implementation of SharedMap, a synchronous multi-threading capable, fine-grain-locked with deadlock recovery, static memory allocated, coalesced-chaining HashMap, backed by SharedArrayBuffer that supports deleting and is capable of auto-defragmenting itself on delete unless almost full compatible with both Node.js and SharedArrayBuffer-enabled browsers

## Constructor

#### new SharedMap(maxSize, keySize, objSize) → {SharedMap}

Creates a new SharedMap

##### Parameters:

Name | Type | Description
--- | --- | ---
`maxSize` | number | Maximum number of entries
`keySize` | number | Maximum length of keys in UTF-16 codepoints
`objSize` | number | Maximum length of values in UTF-16 codepoints

Author:
  - Momtchil Momtchev
Source:
  - index.js, line 96
See:
  - http://github.com/mmomtchev/SharedMap

##### Returns:

Type
  SharedMap

### Members

#### length

Number of elements present

Source:
  - index.js, line 133

#### size

Maximum number of elements allowed

Source:
  - index.js, line 142

### Methods

#### clear() → {void}

Clear the SharedMap

Source:
  - index.js, line 741

##### Returns:

Type
  void

#### delete(key, optopt) → {void}

Delete an element, fully thread-safe, acquires an exlusive lock and it is very expensive

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`key` | string |  |
`opt` | SharedMapOptions | <optional> | options, { lockExclusive: true } if manually calling lockExlusive

Source:
  - index.js, line 574

##### Throws:

- when the key does not exit
  Type
    RangeError
- when calling map.delete(key, value, { lockWrite: true, lockExclusive: false })
  Type
    Error

##### Returns:

Type
  void

#### get(key, optopt) → {string|undefined}

Get an element, fully thread-safe, multiple get/set can execute in parallel

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`key` | string |  |
`opt` | SharedMapOptions | <optional> | options, { lockWrite: true } if manually calling lockWrite

Source:
  - index.js, line 512

##### Returns:

Type
  string | undefined

#### has(key, optopt) → {boolean}

Find an element, fully thread-safe, identical to get(key) !== undefined

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`key` | string |  |
`opt` | SharedMapOptions | <optional> | options, { lockWrite: true } if manually calling lockWrite

Source:
  - index.js, line 550

##### Returns:

Type
  boolean

#### (generator) keys(optopt) → {Iterable}

A generator that can be used to iterate over the keys, thread-safe but allows additions and deletions during the iteration

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`opt` | SharedMapOptions | <optional> | options, { lockWrite: true } if manually calling lockWrite

Source:
  - index.js, line 646

##### Returns:

Type
  Iterable

#### lockExclusive() → {void}

Acquire an exclusive lock, All operations that need it, automatically acquire it, Use only if you need to block all other threads from accessing the map; The thread holding the lock can then call map.set(k, v, {lockHeld: true})

Source:
  - index.js, line 216

##### Returns:

Type
  void

#### lockWrite() → {void}

Acquire a write lock, All operations that need it, automatically acquire it, Use only if you need to block all other threads from writing to the map, The thread holding the lock can then call map.set(k, v, {lockHeld: true})

Source:
  - index.js, line 284

##### Returns:

Type
  void

##### Example

    myMap.lockWrite();
    for (let k of myMap.keys({lockWrite: true}))
      myMap.set(k,
        myMap.get(k, {lockWrite: true}).toUpperCase(),
        {lockWrite: true});
    myMap.unlockWrite();

#### map(cb, thisArgopt) → {Array}

A thread-safe map(). Doesn't block additions or deletions between two calls of the callback, all map operations are guaranteed atomic, map.get(index)=currentValue is guaranteed while the callback runs, You shall not manipulate the map in the callback, use an explicitly-locked keys() in this case (look at the example for lockWrite)

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`cb` | mapCallback |  | callback
`thisArg` | * | <optional> | callback will have its this set to thisArg

Source:
  - index.js, line 678

##### Returns:

Type
  Array

#### reduce(cb, initialValue) → {*}

A thread-safe reduce(). Doesn't block additions or deletions between two calls of the callback, map.get(key)=currentValue is guaranteed while the callback runs, You shall not manipulate the map in the callback, use an explicitly-locked keys() in this case (look at the example for lockWrite)

##### Parameters:

Name | Type | Description
--- | --- | ---
`cb` | reduceCallback | callback
`initialValue` | * | initial value of the accumulator

Source:
  - index.js, line 719

##### Returns:

Type
  *

#### set(key, value, optopt) → {void}

Add/replace an element, fully thread-safe, multiple get/set can execute in parallel

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`key` | string |  |
`value` | string | number |  |
`opt` | SharedMapOptions | <optional> | options, { lockWrite: true } if manually calling lockWrite

Source:
  - index.js, line 442

##### Throws:

- when the map is full
  Type
    RangeError
- when the input values do not fit
  Type
    RangeError
- when the input values are of a wrong type
  Type
    TypeError

##### Returns:

Type
  void

#### unlockExclusive() → {void}

Release the exclusive lock

Source:
  - index.js, line 224

##### Returns:

Type
  void

#### unlockWrite() → {void}

Release the write lock

Source:
  - index.js, line 293

##### Returns:

Type
  void