-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
35 lines (31 loc) · 998 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { LinkedList } from "../linked-list/index.ts";
import { djb2 } from "../../algorithms/hash/djb2";
export class HashTable<T> {
n: number;
hash: (string: string) => number;
buckets: LinkedList<T>[];
constructor(n: number, hash = djb2) {
this.n = n;
this.hash = hash;
this.buckets = new Array(this.n)
.fill(undefined)
.map(() => new LinkedList());
}
set(key: string, value: T) {
this.buckets[this.hash(key) % this.n].addTail(value);
}
get(key: string) {
return this.buckets[this.hash(key) % this.n];
}
}
const decoder = new TextDecoder("utf-8");
// Dictionary used taken from
// wget https://cdn.cs50.net/2019/fall/psets/5/speller/speller.zip
const data = await Deno.readFile("./dictionary");
const lines = decoder.decode(data).split("\n");
const hashTable = new HashTable<string>(lines.length);
// Load a dictionary
for (const line of decoder.decode(data).split("\n")) {
hashTable.set(line, line);
}
console.log(hashTable.get("hello"));