Skip to content

Commit

Permalink
OverlappingFieldsCanBeMergedRule: refactor 'PairSet' (#3099)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed May 16, 2021
1 parent b029ef8 commit 7fb6c78
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/validation/rules/OverlappingFieldsCanBeMergedRule.js
Expand Up @@ -785,15 +785,16 @@ function subfieldConflicts(
* not matter. We do this by maintaining a sort of double adjacency sets.
*/
class PairSet {
_data: ObjMap<ObjMap<boolean>>;
_data: Map<string, Map<string, boolean>>;

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;
}
Expand All @@ -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;
}
}

0 comments on commit 7fb6c78

Please sign in to comment.