Skip to content

Commit

Permalink
Add in-place set functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomguithereal committed Apr 13, 2017
1 parent 94c1194 commit d3e6122
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 1 deletion.
110 changes: 109 additions & 1 deletion set.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,112 @@ exports.symmetricDifference = function(A, B) {
return S;
};

// TODO: subset, superset, in-place
/**
* Function returning whether A is a subset of B.
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
* @return {boolean}
*/
exports.isSubset = function(A, B) {
var iterator = A.values(),
step;

while ((step = iterator.next(), !step.done)) {
if (!B.has(step.value))
return false;
}

return true;
};

/**
* Function returning whether A is a superset of B.
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
* @return {boolean}
*/
exports.isSuperset = function(A, B) {
return exports.isSubset(B, A);
};

/**
* Function adding the items of set B to the set A.
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
*/
exports.add = function(A, B) {
var iterator = B.values(),
step;

while ((step = iterator.next(), !step.done))
A.add(step.value);

return;
};

/**
* Function subtracting the items of set B from the set A.
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
*/
exports.subtract = function(A, B) {
var iterator = B.values(),
step;

while ((step = iterator.next(), !step.done))
A.delete(step.value);

return;
};

/**
* Function intersecting the items of A & B.
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
*/
exports.intersect = function(A, B) {
var iterator = A.values(),
step;

while ((step = iterator.next(), !step.done)) {
if (!B.has(step.value))
A.delete(step.value);
}

return;
};

/**
* Function disjuncting the items of A & B.
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
*/
exports.disjunct = function(A, B) {
var iterator = A.values(),
step;

var toRemove = [];

while ((step = iterator.next(), !step.done)) {
if (B.has(step.value))
toRemove.push(step.value);
}

iterator = B.values();

while ((step = iterator.next(), !step.done)) {
if (!A.has(step.value))
A.add(step.value);
}

for (var i = 0, l = toRemove.length; i < l; i++)
A.delete(toRemove[i]);

return;
};
62 changes: 62 additions & 0 deletions test/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,66 @@ describe('Set functions', function() {
assert.deepEqual(Array.from(S), [1, 2, 4, 5]);
});
});

describe('#.isSubset', function() {
it('should properly return if the first set is a subset of the second.', function() {
var A = new Set([1, 2]),
B = new Set([1, 2, 3]),
C = new Set([2, 4]);

assert.strictEqual(functions.isSubset(A, B), true);
assert.strictEqual(functions.isSubset(C, B), false);
});
});

describe('#.isSuperset', function() {
it('should properly return if the first set is a subset of the second.', function() {
var A = new Set([1, 2]),
B = new Set([1, 2, 3]),
C = new Set([2, 4]);

assert.strictEqual(functions.isSuperset(B, A), true);
assert.strictEqual(functions.isSuperset(B, C), false);
});
});

describe('#.add', function() {
it('should properly add the second set to the first.', function() {
var A = new Set([1, 2]);

functions.add(A, new Set([2, 3]));

assert.deepEqual(Array.from(A), [1, 2, 3]);
});
});

describe('#.subtract', function() {
it('should properly subtract the second set to the first.', function() {
var A = new Set([1, 2]);

functions.subtract(A, new Set([2, 3]));

assert.deepEqual(Array.from(A), [1]);
});
});

describe('#.intersect', function() {
it('should properly intersect the second set to the first.', function() {
var A = new Set([1, 2]);

functions.intersect(A, new Set([2, 3]));

assert.deepEqual(Array.from(A), [2]);
});
});

describe('#.disjunct', function() {
it('should properly disjunct the second set to the first.', function() {
var A = new Set([1, 2]);

functions.disjunct(A, new Set([2, 3]));

assert.deepEqual(Array.from(A), [1, 3]);
});
});
});

0 comments on commit d3e6122

Please sign in to comment.