Skip to content

structure: set #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions data_structures/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { HashTable } from "./hashing/hash_table";

/**
* This is an implementation of the Set data structure based on a HashTable.
*
* @template T The type of the elements in the set.
*/
export class Set<T> {
private hashTable: HashTable<T, null>;

constructor() {
this.hashTable = new HashTable();
}

/**
* Adds a new element to the set.
*
* @param value The value to add to the set.
*/
add(value: T): void {
this.hashTable.set(value, null);
}

/**
* Removes an element from the set.
*
* @param value The value to remove from the set.
*/
delete(value: T): void {
this.hashTable.delete(value);
}

/**
* Checks if the set contains a given value.
*
* @param value The value to check for.
* @returns Whether the set contains the value.
*/
has(value: T): boolean {
return this.hashTable.has(value);
}

/**
* Removes all elements from the set.
*/
clear(): void {
this.hashTable.clear();
}

/**
* Returns an array of all the values in the set.
*
* @returns An array of all the values in the set.
*/
values(): T[] {
return this.hashTable.keys();
}

/**
* Returns the number of elements in the set.
*
* @returns The number of elements in the set.
*/
getSize(): number {
return this.hashTable.getSize();
}
}