UNPKG

mathsteps-experimental-fork

Version:

Step by step math solutions. Experimental Fork

61 lines (59 loc) 1.62 kB
class DisjointSet { parent; rank; constructor() { this.parent = /* @__PURE__ */ new Map(); this.rank = /* @__PURE__ */ new Map(); } // Find the representative of the set containing x with path compression find(x) { if (!this.parent.has(x)) { this.parent.set(x, x); this.rank.set(x, 0); } if (this.parent.get(x) !== x) { this.parent.set(x, this.find(this.parent.get(x))); } return this.parent.get(x); } // Union the sets containing x and y by rank union(x, y) { const rootX = this.find(x); const rootY = this.find(y); if (rootX !== rootY) { const rankX = this.rank.get(rootX); const rankY = this.rank.get(rootY); if (rankX > rankY) { this.parent.set(rootY, rootX); } else if (rankX < rankY) { this.parent.set(rootX, rootY); } else { this.parent.set(rootY, rootX); this.rank.set(rootX, rankX + 1); } } } } class EqualityCache { customEqualityFn; cache; constructor(customEqualityFn = null, cache = new DisjointSet()) { this.customEqualityFn = customEqualityFn; this.cache = cache; } getFromCachedEquality(str1) { return this.cache.find(str1); } getCachedEquality(str1, str2, customEqualityFn = this.customEqualityFn) { const root1 = this.cache.find(str1); const root2 = this.cache.find(str2); if (root1 === root2) return true; const result = customEqualityFn ? customEqualityFn(str1, str2) : false; if (result) { this.cache.union(str1, str2); } return result; } } export { DisjointSet, EqualityCache };