UNPKG

typescript-ds-lib

Version:

A collection of TypeScript data structure implementations

72 lines 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Set = void 0; const base_collection_1 = require("./base-collection"); const binary_search_tree_1 = require("./binary-search-tree"); class Set extends base_collection_1.BaseCollection { tree; constructor(comparator = (a, b) => a < b) { super(); this.tree = new binary_search_tree_1.BinarySearchTree(comparator); } /** * Adds a value to the set if it's not already present. */ insert(value) { this.tree.insert(value); } /** * Adds multiple values to the set if they're not already present. */ insertList(values) { for (const value of values) { this.tree.insert(value); } } /** * Checks if a value exists in the set. */ find(value) { return this.tree.find(value); } has(value) { return this.find(value); } /** * Removes a value from the set. */ remove(value) { this.tree.remove(value); } forEach(callback) { this.tree.forEach(callback); } /** * Removes all elements from the set. */ clear() { this.tree = new binary_search_tree_1.BinarySearchTree(); } /** * Returns true if the set contains no elements. */ isEmpty() { return this.tree.size() === 0; } /** * Returns the number of elements in the set. */ size() { return this.tree.size(); } /** * Checks if two sets are equal. */ equals(other) { if (!other || !(other instanceof Set)) return false; return this.tree.equals(other.tree); } } exports.Set = Set; //# sourceMappingURL=set.js.map