From 112de0119305d7a95b8c79595e57313957cc7834 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Sun, 16 May 2021 14:52:52 +0300 Subject: [PATCH] OverlappingFieldsCanBeMergedRule: refactor 'PairSet' --- .../rules/OverlappingFieldsCanBeMergedRule.js | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/validation/rules/OverlappingFieldsCanBeMergedRule.js b/src/validation/rules/OverlappingFieldsCanBeMergedRule.js index a0e6773f73..8f7281cd7d 100644 --- a/src/validation/rules/OverlappingFieldsCanBeMergedRule.js +++ b/src/validation/rules/OverlappingFieldsCanBeMergedRule.js @@ -785,15 +785,16 @@ function subfieldConflicts( * not matter. We do this by maintaining a sort of double adjacency sets. */ class PairSet { - _data: ObjMap>; + _data: Map>; constructor() { - this._data = Object.create(null); + this._data = new Map(); } has(a: string, b: string, areMutuallyExclusive: boolean): boolean { - const first = this._data[a]; - const result = first && first[b]; + const [key1, key2] = a < b ? [a, b] : [b, a]; + + const result = this._data.get(key1)?.get(key2); if (result === undefined) { return false; } @@ -807,16 +808,13 @@ class PairSet { } add(a: string, b: string, areMutuallyExclusive: boolean): void { - this._pairSetAdd(a, b, areMutuallyExclusive); - this._pairSetAdd(b, a, areMutuallyExclusive); - } + const [key1, key2] = a < b ? [a, b] : [b, a]; - _pairSetAdd(a: string, b: string, areMutuallyExclusive: boolean): void { - let map = this._data[a]; - if (!map) { - map = Object.create(null); - this._data[a] = map; + const map = this._data.get(key1); + if (map === undefined) { + this._data.set(key1, new Map([[key2, areMutuallyExclusive]])); + } else { + map.set(key2, areMutuallyExclusive); } - map[b] = areMutuallyExclusive; } }