UNPKG

@contextjs/collections

Version:

Strongly-typed collection classes for TypeScript, including List, Dictionary, Queue, Stack, and HashSet.

32 lines (31 loc) 695 B
export class HashSet { items = []; equals; constructor(equals) { this.equals = equals ?? ((a, b) => a === b); } add(item) { if (!this.has(item)) this.items.push(item); } remove(item) { const index = this.items.findIndex(x => this.equals(x, item)); if (index >= 0) this.items.splice(index, 1); } has(item) { return this.items.some(x => this.equals(x, item)); } clear() { this.items.length = 0; } get count() { return this.items.length; } get isEmpty() { return this.items.length === 0; } toArray() { return [...this.items]; } }