2018-11-15 07:56:52 -06:00
|
|
|
/**
|
|
|
|
* Performs a shallow comparison of two sets with the same item type.
|
|
|
|
*/
|
|
|
|
export function equal<T>(a: Set<T>, b: Set<T>): boolean {
|
|
|
|
if (a.size !== b.size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const it = a.values();
|
|
|
|
while (true) {
|
|
|
|
const { value, done } = it.next();
|
|
|
|
if (done) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-21 07:42:53 -06:00
|
|
|
if (!b.has(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-15 07:56:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-21 07:42:53 -06:00
|
|
|
* Returns a new set with items in both sets using shallow comparison.
|
2018-11-15 07:56:52 -06:00
|
|
|
*/
|
|
|
|
export function intersect<T>(a: Set<T>, b: Set<T>): Set<T> {
|
2018-11-21 07:42:53 -06:00
|
|
|
const result = new Set<T>();
|
2018-11-15 07:56:52 -06:00
|
|
|
const it = b.values();
|
|
|
|
while (true) {
|
|
|
|
const { value, done } = it.next();
|
|
|
|
if (done) {
|
2018-11-21 07:42:53 -06:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (a.has(value)) {
|
|
|
|
result.add(value);
|
2018-11-15 07:56:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|