Your HashTable class is shaping up nicely! It includes a custom error class InvalidTableDimError and methods to manage a simple hash table. However, there are some improvements and additions we could make to enhance its functionality and usability:
- Add Methods for Inserting and Deleting: Right now, you can only query positions, but you can't insert or delete key-value pairs.
- Hash Function: Implement a simple hash function to convert keys to an index for the table.
- Collision Handling: Use a probing method to handle collisions.
- Expand Error Handling: Make sure your methods are robust and handle various edge cases gracefully.
Here's an improved version of your HashTable class:
class InvalidTableDimError extends Error { ; }
class HashTable<K, V> {
private table: Map<number, [K, V] | "D"> = new Map();
private m: number;
constructor(dim: number) {
if (dim <= 0) throw new InvalidTableDimError();
this.m = dim;
}
private hash(key: K): number {
let hash = 0;
const keyStr = key.toString();
for (let i = 0; i < keyStr.length; i++) {
hash = (hash << 5) - hash + keyStr.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
return Math.abs(hash) % this.m;
}
private probe(index: number, key: K): number {
let i = index;
let step = 1; // Linear probing
while (this.table.has(i) && this.table.get(i) != "D" && this.table.get(i)![0] !== key) {
i = (i + step) % this.m;
}
return i;
}
insert(key: K, value: V): void {
const index = this.hash(key);
const pos = this.probe(index, key);
this.table.set(pos, [key, value]);
}
find(key: K): V | null {
const index = this.hash(key);
let i = index;
let step = 1; // Linear probing
while (this.table.has(i)) {
const entry = this.table.get(i);
if (entry != "D" && entry![0] === key) {
return entry![1];
}
i = (i + step) % this.m;
}
return null;
}
delete(key: K): void {
const index = this.hash(key);
let i = index;
let step = 1; // Linear probing
while (this.table.has(i)) {
const entry = this.table.get(i);
if (entry != "D" && entry![0] === key) {
this.table.set(i, "D");
return;
}
i = (i + step) % this.m;
}
}
pos(n: number): [K, V] | null {
if (this.table.has(n)) {
return this.table.get(n) != "D" ? this.table.get(n)! : null;
}
return null;
}
}
// Testing the HashTable
const hashTable = new HashTable<string, number>(10);
hashTable.insert('apple', 1);
hashTable.insert('banana', 2);
console.log(hashTable.find('apple')); // Should print: 1
console.log(hashTable.find('banana')); // Should print: 2
console.log(hashTable.find('cherry')); // Should print: null
hashTable.delete('banana');
console.log(hashTable.find('banana')); // Should print: null
Changes and Additions:
- Private Methods for Hashing and Probing: I added
hash and probe methods.
- Insert Method: For inserting key-value pairs.
- Find Method: For finding a value by key.
- Delete Method: For deleting a key-value pair by key.
- Linear Probing: For collision resolution in
probe, insert, find, and delete.
Now you have a more complete HashTable class that supports insertion, deletion, and lookup by key.