feat: completed

This commit is contained in:
Mike 2024-02-02 08:23:19 -05:00
parent 95d3793c20
commit 073de11e88

View file

@ -35,8 +35,14 @@ class HashSet {
set(key) { set(key) {
let _hash = this.hash(key); let _hash = this.hash(key);
let bucketIndex = _hash % this.capacity; let bucketIndex = _hash % this.capacity;
// Check if we need to add more buckets
this.capacityWatcher(); this.capacityWatcher();
if (this.buckets[bucketIndex] == undefined) {
if (
this.buckets[bucketIndex] == undefined ||
this.buckets[bucketIndex] == null
) {
let linkedList = new LinkedList(); let linkedList = new LinkedList();
linkedList.append(key); linkedList.append(key);
@ -45,6 +51,7 @@ class HashSet {
let keyExists = this.buckets[bucketIndex].contains(key); let keyExists = this.buckets[bucketIndex].contains(key);
if (keyExists) { if (keyExists) {
// no dups
return; return;
} else { } else {
this.buckets[bucketIndex].append(key); this.buckets[bucketIndex].append(key);
@ -55,12 +62,13 @@ class HashSet {
get(key) { get(key) {
for (let i = 0; i < this.buckets.length; i++) { for (let i = 0; i < this.buckets.length; i++) {
if (this.buckets[i]) { if (this.buckets[i]) {
let value = this.buckets[i].find(key); let _key = this.buckets[i].find(key);
if (value) return true; if (_key) return true;
} }
} }
return false; return false;
} }
remove(key) { remove(key) {
for (let i = 0; i < this.buckets.length; i++) { for (let i = 0; i < this.buckets.length; i++) {
if (this.buckets[i]) { if (this.buckets[i]) {
@ -71,6 +79,7 @@ class HashSet {
} }
return false; return false;
} }
length() { length() {
let count = 0; let count = 0;
@ -81,10 +90,12 @@ class HashSet {
} }
return count; return count;
} }
clear() { clear() {
this.capacity = this.initialCapacity; this.capacity = this.initialCapacity;
this.buckets = Array(this.capacity); this.buckets = Array(this.capacity);
} }
keys() { keys() {
let _keys = []; let _keys = [];