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 09b4b69

Browse files
committedDec 18, 2020
fix: typo and handle range out
1 parent 11953b2 commit 09b4b69

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed
 

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,22 @@ export default class HashTable {
5656
// where charCodeAt(i) is the i-th character code of the key, n is the length of the key and
5757
// PRIME is just any prime number like 31.
5858
let hash = 0;
59-
let hashPerRound;
6059

6160
// Produce a 32-bits hash number.
6261
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]];
62+
// charCodeAt may return number from 0 to 65536,
63+
// should remap to range 0 to 255.
64+
let hashPerRound = table[(key.charCodeAt(0) + i) % 256];
65+
for (let j = 0; j < key.length; j += 1) {
66+
hashPerRound = table[hashPerRound ^ (key.charCodeAt(j) % 256)];
6667
}
6768

6869
// Left shift by 8-bits and OR the per-rounded hash.
6970
hash = (hash << 8) | hashPerRound;
7071
}
7172

72-
// Add max signed 32-bit number.
73-
hash += ~(1 << 31);
73+
// Unsigned right shift to avoid negative number.
74+
hash >>>= 1;
7475

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

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

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

15-
expect(hashTable.hash('a')).toBe(17);
16-
expect(hashTable.hash('b')).toBe(20);
17-
expect(hashTable.hash('abc')).toBe(17);
15+
expect(hashTable.hash('a')).toBe(20);
16+
expect(hashTable.hash('b')).toBe(31);
17+
expect(hashTable.hash('abc')).toBe(19);
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(2);
24-
expect(hashTable.hash('b')).toBe(0);
23+
expect(hashTable.hash('a')).toBe(1);
24+
expect(hashTable.hash('b')).toBe(1);
2525
expect(hashTable.hash('c')).toBe(0);
2626
expect(hashTable.hash('d')).toBe(2);
2727

@@ -37,9 +37,9 @@ describe('HashTable', () => {
3737

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

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');
40+
expect(hashTable.buckets[0].toString(stringifier)).toBe('c:earth');
41+
expect(hashTable.buckets[1].toString(stringifier)).toBe('a:sky,b:sea');
42+
expect(hashTable.buckets[2].toString(stringifier)).toBe('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(['beta', 'gamma', 'alpha']);
97+
expect(hashTable.getValues()).toEqual(['gamma', 'alpha', 'beta']);
9898
});
9999

100100
it('should get all the values from empty hash table', () => {
@@ -111,6 +111,6 @@ describe('HashTable', () => {
111111

112112
hashTable.set('ac', 'three');
113113

114-
expect(hashTable.getValues()).toEqual(['two', 'one', 'three']);
114+
expect(hashTable.getValues()).toEqual(['one', 'two', 'three']);
115115
});
116116
});

0 commit comments

Comments
 (0)
Please sign in to comment.