ReversibleKeyMap
A
Map
extended call with support of two keys instead of one, the support of O(1) access to data linked to one of the two keys, with a non-relevant key order.
One ReversibleKeyMap
object takes two keys in argument when setting a value. Keys are reversible: Order of the two keys is not relevant.
You can also access data stored under one key (in the form of a map storing a key => value
data) in a constant time.
This documentation will use TypeScript typing. Remove any type to restore "pure" JavaScript.
Getting started
// With a standard Map;classic_map.set'classic', 'map'; // <type_key1, type_key2, type_value>;reversible_map.set'one', 2, ; // Key order is not importantreversible_map.set3, 'four', ; reversible_map.get3, 'four'; // => ['my_values', 'in_this_array']reversible_map.get'four', 3; // => ['my_values', 'in_this_array']reversible_map.get2, 'one'; // => ['value', 'other']
Keys can be of any type (like in the Map
object).
;other_map.setdocument.getElementById'hello', document.getElementById'other-click', myEventListener;
The number of keys allowed is unlimited and their order is not relevant.
To install the package, use:
$ npm install reversible-key-map
Usage
Main method are get
, set
, getAllFrom
, has
and hasCouple
.
Instanciation
; ;// You can instanciate with an entries array;
Set values
map.set"one", "two", 3;map.set"four", "one", 5;map.set"six", "seven", 8;// Setters are chainablemap.set"nine", "ten", 11.set"twelve", "thirteen", 14;
Get values
map.get"two", "one"; // => 3map.get"four", "one"; // => 5map.get"six", "one"; // => undefined// You can all values linked to one keymap.getAllFrom"one"; // => Map(2) {"two" => 3, "four" => 5}
Iteration
Iteration through .entries()
, Symbol.iterator
, .values()
, .keysCouple()
ensure that getting a value with those methods will not give you duplicates.
With .keys()
, a call to .getAllFrom(key)
within the loop will give you the same value twice.
/// Iterate through the map;// Using Symbol.iterator | .entries()for of map // Iterate through values onlyfor of map.values // Iterate through keys couples onlyfor of map.keysCouples // Iterate through keysfor of map.keys
Appartenance
map.hasCouple"one", "two"; // => truemap.hasCouple"six", "one"; // => false// Test if one key existsmap.has"one"; // => truemap.has"hello"; // => false
Delete values
/// Delete valuesmap.delete"one", "two"; // Deletions are also chainable.// Delete all values linked to one keymap.deleteAllFrom"one"; console.log...map; // [["six", "seven"], 8] [["nine", "ten"], 11] [["twelve", "thirteen"], 14]
Full reference: ReversibleKeyMap
Map using a couple of keys to reference a value. You can get a map of values using ONE key, and a value using TWO keys.
Key order is not important.
Index
Constructors
Properties
Accessors
Methods
- __@iterator
- clear
- delete
- deleteAllFrom
- entries
- forEach
- get
- getAllFrom
- has
- hasCouple
- keys
- keysCouples
- set
- values
Constructors
constructor
⊕ new ReversibleKeyMap(it?: Iterable
<[[K1
, K2
], T
]>): ReversibleKeyMap
⊕ new ReversibleKeyMap(it?: Iterable
<[[K2
, K1
], T
]>): ReversibleKeyMap
Creates an instance of ReversibleKeyMap. You can give an iterable providing a tuple containaing a tuple of two keys and one value to initialize the instance.
Parameters:
Name | Type |
---|---|
Optional it |
Iterable <[[K1 , K2 ], T ]> |
Returns: ReversibleKeyMap
Properties
<Protected>
map
● map: Map
<K1
| K2
, Map
<K1
| K2
, T
>> = new Map
Accessors
__@toStringTag
get __@toStringTag(): string
Returns: string
count
get count(): number
Get map size using the VALUES. Warning, it have O(n) complexity.
readonly:
Returns: number
size
get size(): number
Get map size using the KEYS. Warning, it count EVERY key. It means that one set("a", "b", val)
will produce a result of 2
. 0(1) complexity.
readonly:
Returns: number
Methods
__@iterator
▸ __@iterator(): IterableIterator
<[[K1
| K2
, K1
| K2
], T
]>
Returns: IterableIterator
<[[K1
| K2
, K1
| K2
], T
]>
clear
▸ clear(): void
Delete all existing values in current instance.
Returns: void
delete
▸ delete(k1: K1
, k2: K2
): this
▸ delete(k1: K2
, k2: K1
): this
Delete a double key pair.
Parameters:
Name | Type | Description |
---|---|---|
k1 | K1 |
- |
k2 | K2 |
- |
Returns: this
deleteAllFrom
▸ deleteAllFrom(k1: K1
| K2
): this
Delete everythinh linked to key k1.
Parameters:
Name | Type | Description |
---|---|---|
k1 | K1 | K2 |
- |
Returns: this
entries
▸ entries(): IterableIterator
<[[K1
| K2
, K1
| K2
], T
]>
Returns: IterableIterator
<[[K1
| K2
, K1
| K2
], T
]>
forEach
▸ forEach<This
>(callback: function
, thisArg?: This
): void
Apply function callback
for each [key1, key2] => value
pair in ReversibleKeyMap object. Insertion order / key order is not guaranteed. If thisArg
is provided, it will be used as this
value for each callback call.
template: This
Type parameters:
This
Parameters:
Name | Type | Description |
---|---|---|
callback | function |
- |
Optional thisArg |
This |
Returns: void
get
▸ get(k1: K1
, k2: K2
): T
▸ get(k1: K2
, k2: K1
): T
Get one value according to a couple of keys. Order of keys does NOT have any influence.
Parameters:
Name | Type | Description |
---|---|---|
k1 | K1 |
- |
k2 | K2 |
Returns: T
getAllFrom
▸ getAllFrom(k1: K1
): Map
<K2
, T
>
▸ getAllFrom(k1: K2
): Map
<K1
, T
>
Give all keys and values mapped to k1. Results are returned wrapped into a Map object.
Parameters:
Name | Type | Description |
---|---|---|
k1 | K1 |
- |
Returns: Map
<K2
, T
>
has
▸ has(k1: K1
| K2
): boolean
Return true if key k1 is present.
Parameters:
Name | Type | Description |
---|---|---|
k1 | K1 | K2 |
- |
Returns: boolean
hasCouple
▸ hasCouple(k1: K1
, k2: K2
): boolean
▸ hasCouple(k1: K2
, k2: K1
): boolean
Return true if couple [k1, k2] is present.
Parameters:
Name | Type | Description |
---|---|---|
k1 | K1 |
- |
k2 | K2 |
- |
Returns: boolean
keys
▸ keys(): IterableIterator
<K1
| K2
>
Get all keys existing in double key map. Do NOT return key couples, see keysCouples
method.
Returns: IterableIterator
<K1
| K2
>
keysCouples
▸ keysCouples(): IterableIterator
<[K1
| K2
, K1
| K2
]>
Get existing key couples in double key maps.
Returns: IterableIterator
<[K1
| K2
, K1
| K2
]>
set
▸ set(k1: K1
, k2: K2
, value: T
): this
▸ set(k1: K2
, k2: K1
, value: T
): this
Set one value according to two keys.
Parameters:
Name | Type | Description |
---|---|---|
k1 | K1 |
- |
k2 | K2 |
- |
value | T |
- |
Returns: this
Current instance.
values
▸ values(): IterableIterator
<T
>
Get each value inside map.
Returns: IterableIterator
<T
>