MCPcopy Create free account
hub / github.com/angular/angular / UniqueValueMultiKeyMap

Class UniqueValueMultiKeyMap

packages/core/src/render3/list_reconciliation.ts:420–486  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

418 * values (in the entire collection) are unique.
419 */
420export class UniqueValueMultiKeyMap<K, V> {
421 // A map from a key to the first value corresponding to this key.
422 private kvMap = new Map<K, V>();
423 // A map that acts as a linked list of values - each value maps to the next value in this "linked
424 // list" (this only works if values are unique). Allocated lazily to avoid memory consumption when
425 // there are no duplicated values.
426 private _vMap: Map<V, V> | undefined = undefined;
427
428 has(key: K): boolean {
429 return this.kvMap.has(key);
430 }
431
432 delete(key: K): boolean {
433 if (!this.has(key)) return false;
434
435 const value = this.kvMap.get(key)!;
436 if (this._vMap !== undefined && this._vMap.has(value)) {
437 this.kvMap.set(key, this._vMap.get(value)!);
438 this._vMap.delete(value);
439 } else {
440 this.kvMap.delete(key);
441 }
442
443 return true;
444 }
445
446 get(key: K): V | undefined {
447 return this.kvMap.get(key);
448 }
449
450 set(key: K, value: V): void {
451 if (this.kvMap.has(key)) {
452 let prevValue = this.kvMap.get(key)!;
453
454 // Note: we don't use `assertNotSame`, because the value needs to be stringified even if
455 // there is no error which can freeze the browser for large values (see #58509).
456 if (ngDevMode && prevValue === value) {
457 throw new Error(`Detected a duplicated value ${value} for the key ${key}`);
458 }
459
460 if (this._vMap === undefined) {
461 this._vMap = new Map();
462 }
463
464 const vMap = this._vMap;
465 while (vMap.has(prevValue)) {
466 prevValue = vMap.get(prevValue)!;
467 }
468 vMap.set(prevValue, value);
469 } else {
470 this.kvMap.set(key, value);
471 }
472 }
473
474 forEach(cb: (v: V, k: K) => void) {
475 for (let [key, value] of this.kvMap) {
476 cb(value, key);
477 if (this._vMap !== undefined) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected