Skip to content

Commit 1232075

Browse files
authored
feat: Add BiDirectionalDeepMap (#18)
1 parent 17e21a5 commit 1232075

9 files changed

+2384
-1134
lines changed

README.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ The following supplemental comparisons/methods are included:
5656
```typescript
5757
// COMPARISONS
5858
const set1 = new DeepSet([{ a: 1 }, { b: 2 }]);
59-
const set2 = new DeepSet([{ a: 1 }, { b: 2 }]);
59+
const set2 = new DeepSet([{ b: 2 }, { a: 1 }]);
6060
set1.equals(set2); // true
6161

6262
const set3 = new DeepSet([{ a: 1 }]);
@@ -100,7 +100,7 @@ The `options` argument is a superset of the options defined for [object-hash](ht
100100
[...set.values()]; // [{ val: 1, other: 2 }]
101101
```
102102
103-
- `mapValueTransformer` - a custom function that transforms Map values prior to hashing. This is only relevant to the `.equals` and `.contains` operations from the `Comparable` interface. It does not affect the values that are stored.
103+
- `mapValueTransformer` - a custom function that transforms Map values prior to hashing. This is only relevant to the `.equals`/`.contains` operations from the [Comparable interface](#comparable-interface), as well as the [Bi-Directional DeepMap](#bi-directional-deepmap). It does not affect the values that are stored.
104104
105105
```typescript
106106
type MyType = { val: number; other: number };
@@ -140,9 +140,24 @@ The `options` argument is a superset of the options defined for [object-hash](ht
140140
set.size; // 1
141141
```
142142
143+
## Bi-Directional DeepMap
144+
145+
This library also exposes a `BiDirectionalDeepMap` class, which supports O(1) lookups by both keys and values. It provides the following extended API:
146+
147+
- _`hasValue(val: V): boolean`_: Returns true if `val` exists as a value in the map
148+
- _`getKeyByValue(val: V): K | undefined`_: Returns the key associated with `val` if it exists
149+
- _`deleteByValue(val: V): boolean`_: Removes the key-value pair whose value is `val` and returns true if found
150+
151+
### Caveats
152+
153+
Note that this "two-way" map has the traditional caveats:
154+
155+
- There is a ~2x memory footprint
156+
- Keys and values must be 1-to-1, meaning each key must have a distinct value and vice versa. This implementation will error if attempting to set a key-value pair whose _value_ is already present in the map with a different _key_.
157+
143158
## Static Utility Methods
144159
145-
- _`areEqual(values, options?)`_: Returns `true` if all elements in `values` are equal. This can be useful when you need to quickly
160+
- _`areEqual(values, options?)`_: Returns true if all elements in `values` are equal. This can be useful when you need to quickly
146161
test equality of more than 2 values, or when you want to specify an equality transform (via `options.transformer`).
147162
148163
## Notes/Caveats

0 commit comments

Comments
 (0)