Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/data-structures/disjoint-set/DisjointSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default class DisjointSet {
*/
constructor(keyCallback) {
this.keyCallback = keyCallback;
this.items = {};
// Item keys must not collide with inherited Object properties.
this.items = Object.create(null);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/data-structures/disjoint-set/DisjointSetItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default class DisjointSetItem {
this.keyCallback = keyCallback;
/** @var {DisjointSetItem} this.parent */
this.parent = null;
this.children = {};
// Store __proto__ as a child key instead of changing the registry's prototype.
this.children = Object.create(null);
}

/**
Expand Down
45 changes: 45 additions & 0 deletions src/data-structures/disjoint-set/__test__/DisjointSet.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
import DisjointSet from '../DisjointSet';

describe('DisjointSet', () => {
const prototypeKeys = ['__proto__', 'constructor', 'toString', 'hasOwnProperty', 'valueOf'];

it.each(prototypeKeys)('should treat an absent prototype-named key %s as missing', (key) => {
const disjointSet = new DisjointSet();

expect(disjointSet.find(key)).toBeNull();
expect(() => disjointSet.union(key, 'other')).toThrow('One or two values are not in sets');
expect(() => disjointSet.inSameSet(key, 'other')).toThrow('One or two values are not in sets');
});

it.each(prototypeKeys)('should register and merge a prototype-named root %s', (key) => {
const disjointSet = new DisjointSet();
disjointSet.makeSet(key).makeSet('other');

expect(disjointSet.find(key)).toBe(key);
expect(disjointSet.inSameSet(key, 'other')).toBe(false);

disjointSet.union(key, 'other');

expect(disjointSet.find('other')).toBe(key);
expect(disjointSet.inSameSet(key, 'other')).toBe(true);
});

it.each(prototypeKeys)('should preserve membership when re-adding merged key %s', (key) => {
const disjointSet = new DisjointSet();
disjointSet.makeSet('root').makeSet(key).union('root', key);
disjointSet.makeSet(key);

expect(disjointSet.find(key)).toBe('root');
expect(disjointSet.inSameSet('root', key)).toBe(true);
});

it.each(prototypeKeys)('should support custom key extractors returning %s', (key) => {
const keyCallback = (value) => value.key;
const disjointSet = new DisjointSet(keyCallback);
const item = { key };
const other = { key: 'other' };

expect(disjointSet.find(item)).toBeNull();
disjointSet.makeSet(item).makeSet(other).union(other, item);

expect(disjointSet.find({ key })).toBe('other');
expect(disjointSet.inSameSet(item, other)).toBe(true);
});

it('should throw error when trying to union and check not existing sets', () => {
function mergeNotExistingSets() {
const disjointSet = new DisjointSet();
Expand Down
28 changes: 28 additions & 0 deletions src/data-structures/disjoint-set/__test__/DisjointSetItem.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
import DisjointSetItem from '../DisjointSetItem';

describe('DisjointSetItem', () => {
it.each(['__proto__', 'constructor', 'toString', 'hasOwnProperty', 'valueOf'])(
'should include a prototype-named child %s in children and rank',
(key) => {
const root = new DisjointSetItem('root');
const child = new DisjointSetItem(key);
const grandchild = new DisjointSetItem('grandchild');
child.addChild(grandchild);
root.addChild(child);

expect(root.getChildren()).toEqual([child]);
expect(root.getRank()).toBe(2);
expect(child.getRoot()).toBe(root);
expect(grandchild.getRoot()).toBe(root);
},
);

it('should retain children whose custom key is __proto__', () => {
const keyCallback = (value) => value.key;
const root = new DisjointSetItem({ key: 'root' }, keyCallback);
const child = new DisjointSetItem({ key: '__proto__' }, keyCallback);

child.setParent(root);

expect(root.getChildren()).toEqual([child]);
expect(root.getRank()).toBe(1);
expect(child.getRoot()).toBe(root);
});

it('should do basic manipulation with disjoint set item', () => {
const itemA = new DisjointSetItem('A');
const itemB = new DisjointSetItem('B');
Expand Down