Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aa5fd54

Browse files
committed
change hash fn to use prime num to minimize collisions, add separate chaining collision handling
1 parent 6a1186c commit aa5fd54

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

‎dist/datastructures/HashTable.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
class HashTable {
44
constructor() {
5-
this.hash = (value,max) => {
5+
this.hash = (key,arrLen) => {
66
// naive hash fn for illustrative purposes
7-
if (max <= 0)
7+
if (arrLen <= 0)
88
throw TypeError('Max hash value must be a positive integer');
9-
return value.split('').reduce((a, b) => a * b.charCodeAt(0), 1) % max;
9+
let total = 0;
10+
let prime = 31;
11+
for (let i = 0; i < Math.min(key.length, 100); i++) {
12+
let char = key[i];
13+
let value = char.charCodeAt(0) - 96;
14+
total = (total * prime + value) % arrLen;
15+
}
16+
return total;
17+
// return value.split('').reduce((a, b) => a * b.charCodeAt(0), 1) % max;
1018
};
1119
}
1220
}

‎dist/datastructures/HashTable.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ describe('basic hash table', () => {
1212
});
1313
test('hash fn takes string and returns number between 0 and specified max', () => {
1414
const max = 50;
15-
expect(HT.hash('test', max)).toBeLessThan(50);
15+
let str = '';
16+
for (let i = 0; i < 100; i++) {
17+
str += String.fromCharCode(Math.floor(Math.random() * (200 - 1) + 1));
18+
}
19+
expect(HT.hash(str, max)).toBeLessThan(50);
1620
});
1721
test('hash fn throws error if max argument is <= 0', () => {
1822
let max = 0;

‎src/datastructures/HashTable.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ describe('basic hash table', () => {
99
});
1010
test('hash fn takes string and returns number between 0 and specified max', () => {
1111
const max = 50;
12-
expect(HT.hash('test', max)).toBeLessThan(50);
12+
let str = '';
13+
for (let i = 0; i < 100; i++) {
14+
str += String.fromCharCode(Math.floor(Math.random() * (200 - 1) + 1));
15+
}
16+
expect(HT.hash(str, max)).toBeLessThan(50);
1317
});
1418
test('hash fn throws error if max argument is <= 0', () => {
1519
let max = 0;

‎src/datastructures/HashTable.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
class HashTable<T> {
2-
constructor() {
3-
1+
class HashTable {
2+
keyMap: [string, string][][];
3+
constructor(size = 83) {
4+
this.keyMap = new Array(size);
45
}
5-
hash = (value: string,max: number): number => {
6+
hash = (key: string): number => {
67
// naive hash fn for illustrative purposes
7-
if (max <= 0) throw TypeError('Max hash value must be a positive integer');
8-
return value.split('').reduce((a, b) => a * b.charCodeAt(0), 1) % max;
8+
let total = 0;
9+
let prime = 73;
10+
for (let i = 0; i < Math.min(key.length, 100); i++) {
11+
let char = key[i];
12+
let value = char.charCodeAt(0) - 96;
13+
total = (total * prime + value) % this.keyMap.length;
14+
}
15+
return total;
16+
// return value.split('').reduce((a, b) => a * b.charCodeAt(0), 1) % max;
17+
}
18+
set = (key: string, value: string): void => {
19+
let index = this.hash(key);
20+
if (!this.keyMap[index]) {
21+
this.keyMap[index] = [] as [string, string][];
22+
}
23+
this.keyMap[index].push([key, value]);
924
}
1025
}
1126

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /