MCPcopy Create free account
hub / github.com/denoland/std / MultiMap

Class MultiMap

data_structures/unstable_multimap.ts:60–791  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

58 * ```
59 */
60export class MultiMap<K, V> implements Iterable<[K, V]> {
61 #map = new Map<K, V[]>();
62 #valueCount = 0;
63
64 /**
65 * Creates a new instance.
66 *
67 * @experimental **UNSTABLE**: New API, yet to be vetted.
68 *
69 * @param entries An iterable of key-value pairs for the initial entries.
70 * Duplicate values for the same key are preserved in insertion order.
71 *
72 * @example Creating an empty map
73 * ```ts
74 * import { MultiMap } from "@std/data-structures/unstable-multimap";
75 * import { assertEquals } from "@std/assert";
76 *
77 * const map = new MultiMap<string, number>();
78 * assertEquals(map.size, 0);
79 * ```
80 *
81 * @example Creating a map from an iterable
82 * ```ts
83 * import { MultiMap } from "@std/data-structures/unstable-multimap";
84 * import { assertEquals } from "@std/assert";
85 *
86 * const map = new MultiMap([["a", 1], ["a", 2], ["b", 3]]);
87 * assertEquals(map.get("a"), [1, 2]);
88 * ```
89 */
90 constructor(entries?: Iterable<readonly [K, V]> | null) {
91 if (entries) {
92 for (const [key, value] of entries) {
93 this.add(key, value);
94 }
95 }
96 }
97
98 /**
99 * The number of distinct keys in the map.
100 *
101 * @experimental **UNSTABLE**: New API, yet to be vetted.
102 *
103 * @returns The number of distinct keys in the map.
104 *
105 * @example Usage
106 * ```ts
107 * import { MultiMap } from "@std/data-structures/unstable-multimap";
108 * import { assertEquals } from "@std/assert";
109 *
110 * const map = new MultiMap([["a", 1], ["a", 2], ["b", 3]]);
111 * assertEquals(map.size, 2);
112 * ```
113 */
114 get size(): number {
115 return this.#map.size;
116 }
117

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected