Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(patterns): add multi map #808

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions packages/patterns/src/multi-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { forEach, chain } from '@wixc3/common';

/**
* Maps two keys to a value
* @example
* ```ts
* const m = new MultiMap([['a', 1, 'one'],['a', 2, 'two'])
* m.add('a', 3, 'three')
*
* m.has('a', 1) // => true
* m.has('a',2) // => true
* m.has('a',3) // => true
* m.has('a',4) // => false
*
* m.get('a', 1) // => 'one'
* m.get('a',2) // => 'two'
* m.get('a',3) // => 'three'
* m.get('a',4) // => undefined
*
* m.delete('a', 1) // => true
* m.has('a', 1) // => false
*
* ```
*/
export class MultiMap<K1, K2, V> implements Iterable<[[K1, K2], V]> {
private map = new Map<K1, Map<K2, V>>();

constructor(entries?: Iterable<[K1, K2, V]>) {
forEach(entries, ([key1, key2, val]: [K1, K2, V]) => {
this.set(key1, key2, val);
});
}

public get size(): number {
return chain(this.map)
.map(([_, { size }]) => size)
.reduce((sum: number, size: number) => sum + size, 0).value;
}

public get(key1: K1, key2: K2): V | undefined {
return this.map.get(key1)?.get(key2);
}

public set(key1: K1, key2: K2, value: V): this {
const innerMap = this.map.get(key1);
if (innerMap) {
innerMap.set(key2, value);
} else {
this.map.set(key1, new Map([[key2, value]]));
}
return this;
}

public clear(): void {
this.map.clear();
}

public delete(key1: K1, key2: K2): boolean {
const innerMap = this.map.get(key1);
if (innerMap) {
const wasInSet = innerMap.delete(key2);
if (innerMap.size === 0) {
this.map.delete(key1);
}
return wasInSet;
}
return false;
}

public has(key1: K1, key2: K2): boolean {
const innerMap = this.map.get(key1);
return innerMap ? innerMap.has(key2) : false;
}

public [Symbol.iterator](): IterableIterator<[[K1, K2], V]> {
return this.entries();
}

public *entries(): IterableIterator<[[K1, K2], V]> {
const { map } = this;
for (const [key1, innerMap] of map.entries()) {
for (const [key2, value] of innerMap.entries()) {
yield [[key1, key2], value];
}
}
}

public *values(): IterableIterator<V> {
const { map } = this;
for (const innerMap of map.values()) {
yield* innerMap.values();
}
}

public *keys(): IterableIterator<[K1, K2]> {
const { map } = this;
for (const [key1, innerMap] of map.entries()) {
for (const key2 of innerMap.keys()) {
yield [key1, key2];
}
}
}

public getInnerMap(key1: K1): Map<K2, V> | undefined {
return this.map.get(key1);
}

public deleteInnerMap(key1: K1): boolean {
return this.map.delete(key1);
}
}

export function isMultiMap<K1, K2, V>(x: any): x is MultiMap<K1, K2, V> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use unknown instead of any

return x instanceof MultiMap;
}
53 changes: 19 additions & 34 deletions packages/patterns/src/set-multi-map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { forEach, chain } from '@wixc3/common';
import { forEach } from '@wixc3/common';
import { MultiMap } from './multi-map';

/**
* Maps keys to a set of values
Expand All @@ -13,7 +14,7 @@ import { forEach, chain } from '@wixc3/common';
* ```
*/
export class SetMultiMap<K, V> implements Iterable<[K, V]> {
private map = new Map<K, Set<V>>();
private map = new MultiMap<K, V, boolean>();

constructor(entries?: Iterable<[K, V]>) {
forEach(entries, ([key, val]: [K, V]) => {
Expand All @@ -22,22 +23,16 @@ export class SetMultiMap<K, V> implements Iterable<[K, V]> {
}

public get size(): number {
return chain(this.map)
.map(([_, { size }]) => size)
.reduce((sum: number, size: number) => sum + size, 0).value;
return this.map.size;
}

public get(key: K): ReadonlySet<V> | undefined {
return this.map.get(key);
const innerKeys = this.map.getInnerMap(key)?.keys();
return innerKeys ? new Set(innerKeys) : undefined;
}

public add(key: K, value: V): this {
const valueSet = this.map.get(key);
if (valueSet) {
valueSet.add(value);
} else {
this.map.set(key, new Set([value]));
}
this.map.set(key, value, true);
return this;
}

Expand All @@ -46,28 +41,19 @@ export class SetMultiMap<K, V> implements Iterable<[K, V]> {
}

public delete(key: K, value: V): boolean {
const valueSet = this.map.get(key);
if (valueSet) {
const wasInSet = valueSet.delete(value);
if (valueSet.size === 0) {
this.map.delete(key);
}
return wasInSet;
}
return false;
return this.map.delete(key, value);
}

public deleteKey(key: K): boolean {
return this.map.delete(key);
return this.map.deleteInnerMap(key);
}

public has(key: K, value: V): boolean {
const valueSet = this.map.get(key);
return valueSet ? valueSet.has(value) : false;
return this.map.has(key, value);
}

public hasKey(key: K): boolean {
const existingSet = this.map.get(key);
const existingSet = this.get(key);
return !!existingSet && existingSet.size > 0;
}

Expand All @@ -77,22 +63,21 @@ export class SetMultiMap<K, V> implements Iterable<[K, V]> {

public *entries(): IterableIterator<[K, V]> {
const { map } = this;
for (const [key, valueSet] of map.entries()) {
for (const value of valueSet) {
yield [key, value];
}
}
yield* map.keys();
}

public *values(): IterableIterator<V> {
const { map } = this;
for (const valueSet of map.values()) {
yield* valueSet.values();
for (const [_key, value] of map.keys()) {
yield value;
}
}

public keys(): IterableIterator<K> {
return this.map.keys();
public *keys(): IterableIterator<K> {
const { map } = this;
for (const [key, _value] of map.keys()) {
yield key;
}
}
}

Expand Down