grafana/public/app/features/explore/utils/set.ts

35 lines
694 B
TypeScript
Raw Normal View History

/**
* 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 (b.has(value)) {
return false;
}
if (done) {
return true;
}
}
}
/**
* Returns the first set with items in the second set through shallow comparison.
*/
export function intersect<T>(a: Set<T>, b: Set<T>): Set<T> {
const it = b.values();
while (true) {
const { value, done } = it.next();
if (!a.has(value)) {
a.delete(value);
}
if (done) {
return a;
}
}
}