Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 11953b2

Browse files
committedDec 18, 2020
fix: implementing Pearson Hashing in Hash Table (trekhleb#529)
1 parent e220450 commit 11953b2

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed
 

‎src/data-structures/hash-table/HashTable.js

+39-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ import LinkedList from '../linked-list/LinkedList';
66
// are being handled.
77
const defaultHashTableSize = 32;
88

9+
// This is the table used for the Pearson hashing.
10+
// Which contains shuffled number from 0 to 255.
11+
const table = [
12+
84, 124, 126, 180, 160, 221, 178, 97, 71, 13, 102, 167, 209, 153, 232, 237,
13+
243, 24, 205, 228, 182, 83, 111, 245, 86, 99, 59, 26, 172, 225, 9, 94,
14+
37, 179, 138, 250, 154, 142, 132, 36, 134, 233, 188, 100, 29, 57, 161, 17,
15+
41, 136, 198, 61, 241, 46, 201, 137, 214, 10, 45, 109, 145, 204, 15, 119,
16+
21, 16, 252, 170, 47, 12, 95, 246, 249, 116, 7, 64, 149, 219, 171, 120,
17+
212, 216, 80, 74, 186, 192, 210, 190, 85, 78, 196, 181, 150, 110, 176, 127,
18+
140, 22, 98, 82, 18, 53, 230, 240, 139, 62, 70, 151, 157, 159, 213, 207,
19+
56, 81, 147, 168, 49, 229, 189, 235, 152, 130, 222, 215, 141, 6, 175, 2,
20+
244, 28, 224, 96, 91, 27, 129, 227, 199, 251, 69, 242, 223, 158, 8, 23,
21+
19, 31, 236, 25, 68, 254, 112, 247, 208, 148, 38, 114, 40, 55, 88, 231,
22+
32, 93, 163, 253, 90, 30, 89, 146, 113, 200, 177, 101, 135, 169, 128, 48,
23+
174, 66, 4, 155, 14, 166, 60, 193, 162, 0, 58, 79, 92, 239, 72, 203,
24+
123, 115, 67, 217, 106, 52, 173, 20, 35, 50, 5, 44, 11, 143, 206, 73,
25+
42, 34, 75, 131, 184, 220, 125, 156, 202, 238, 164, 103, 77, 248, 195, 183,
26+
211, 65, 226, 105, 43, 191, 3, 54, 234, 187, 194, 117, 121, 107, 1, 218,
27+
185, 76, 133, 33, 39, 255, 197, 63, 51, 144, 122, 87, 104, 118, 165, 108,
28+
];
29+
930
export default class HashTable {
1031
/**
1132
* @param {number} hashTableSize
@@ -25,20 +46,31 @@ export default class HashTable {
2546
* @return {number}
2647
*/
2748
hash(key) {
28-
// For simplicity reasons we will just use character codes sum of all characters of the key
29-
// to calculate the hash.
49+
// Using Pearson hashing algorithm to generate the hash number.
3050
//
31-
// But you may also use more sophisticated approaches like polynomial string hash to reduce the
51+
// But you may also use other approaches like polynomial string hash to reduce the
3252
// number of collisions:
3353
//
3454
// hash = charCodeAt(0) * PRIME^(n-1) + charCodeAt(1) * PRIME^(n-2) + ... + charCodeAt(n-1)
3555
//
3656
// where charCodeAt(i) is the i-th character code of the key, n is the length of the key and
3757
// PRIME is just any prime number like 31.
38-
const hash = Array.from(key).reduce(
39-
(hashAccumulator, keySymbol) => (hashAccumulator + keySymbol.charCodeAt(0)),
40-
0,
41-
);
58+
let hash = 0;
59+
let hashPerRound;
60+
61+
// Produce a 32-bits hash number.
62+
for (let i = 0; i < 4; i += 1) {
63+
hashPerRound = table[(key.charCodeAt(0) + i) % 256];
64+
for (let j = 0; j < key.legnth; j += 1) {
65+
hashPerRound = table[hashPerRound ^ key[j]];
66+
}
67+
68+
// Left shift by 8-bits and OR the per-rounded hash.
69+
hash = (hash << 8) | hashPerRound;
70+
}
71+
72+
// Add max signed 32-bit number.
73+
hash += ~(1 << 31);
4274

4375
// Reduce hash number so it would fit hash table size.
4476
return hash % this.buckets.length;

‎src/data-structures/hash-table/__test__/HashTable.test.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ describe('HashTable', () => {
1212
it('should generate proper hash for specified keys', () => {
1313
const hashTable = new HashTable();
1414

15-
expect(hashTable.hash('a')).toBe(1);
16-
expect(hashTable.hash('b')).toBe(2);
17-
expect(hashTable.hash('abc')).toBe(6);
15+
expect(hashTable.hash('a')).toBe(17);
16+
expect(hashTable.hash('b')).toBe(20);
17+
expect(hashTable.hash('abc')).toBe(17);
1818
});
1919

2020
it('should set, read and delete data with collisions', () => {
2121
const hashTable = new HashTable(3);
2222

23-
expect(hashTable.hash('a')).toBe(1);
24-
expect(hashTable.hash('b')).toBe(2);
23+
expect(hashTable.hash('a')).toBe(2);
24+
expect(hashTable.hash('b')).toBe(0);
2525
expect(hashTable.hash('c')).toBe(0);
26-
expect(hashTable.hash('d')).toBe(1);
26+
expect(hashTable.hash('d')).toBe(2);
2727

2828
hashTable.set('a', 'sky-old');
2929
hashTable.set('a', 'sky');
@@ -37,9 +37,9 @@ describe('HashTable', () => {
3737

3838
const stringifier = (value) => `${value.key}:${value.value}`;
3939

40-
expect(hashTable.buckets[0].toString(stringifier)).toBe('c:earth');
41-
expect(hashTable.buckets[1].toString(stringifier)).toBe('a:sky,d:ocean');
42-
expect(hashTable.buckets[2].toString(stringifier)).toBe('b:sea');
40+
expect(hashTable.buckets[0].toString(stringifier)).toBe('b:sea,c:earth');
41+
expect(hashTable.buckets[1].toString(stringifier)).toBe('');
42+
expect(hashTable.buckets[2].toString(stringifier)).toBe('a:sky,d:ocean');
4343

4444
expect(hashTable.get('a')).toBe('sky');
4545
expect(hashTable.get('d')).toBe('ocean');
@@ -94,7 +94,7 @@ describe('HashTable', () => {
9494
hashTable.set('b', 'beta');
9595
hashTable.set('c', 'gamma');
9696

97-
expect(hashTable.getValues()).toEqual(['gamma', 'alpha', 'beta']);
97+
expect(hashTable.getValues()).toEqual(['beta', 'gamma', 'alpha']);
9898
});
9999

100100
it('should get all the values from empty hash table', () => {
@@ -105,13 +105,12 @@ describe('HashTable', () => {
105105
it('should get all the values in case of hash collision', () => {
106106
const hashTable = new HashTable(3);
107107

108-
// Keys `ab` and `ba` in current implementation should result in one hash (one bucket).
109108
// We need to make sure that several items from one bucket will be serialized.
110109
hashTable.set('ab', 'one');
111110
hashTable.set('ba', 'two');
112111

113112
hashTable.set('ac', 'three');
114113

115-
expect(hashTable.getValues()).toEqual(['one', 'two', 'three']);
114+
expect(hashTable.getValues()).toEqual(['two', 'one', 'three']);
116115
});
117116
});

0 commit comments

Comments
 (0)
Please sign in to comment.