Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implementing Pearson Hashing in Hash Table (#529) #596

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: replace right shift by using bit mask
JackyYin committed Dec 20, 2020
commit a25febeb2535e863861f4c477112c43855f00b78
5 changes: 3 additions & 2 deletions src/data-structures/hash-table/HashTable.js
Original file line number Diff line number Diff line change
@@ -70,8 +70,9 @@ export default class HashTable {
hash = (hash << 8) | hashPerRound;
}

// Unsigned right shift to avoid negative number.
hash >>>= 1;
// Bit mask to clear the left most bit,
// so the result will be a positive number.
hash &= 0x7FFFFFFF;

// Reduce hash number so it would fit hash table size.
return hash % this.buckets.length;
20 changes: 10 additions & 10 deletions src/data-structures/hash-table/__test__/HashTable.test.js
Original file line number Diff line number Diff line change
@@ -12,18 +12,18 @@ describe('HashTable', () => {
it('should generate proper hash for specified keys', () => {
const hashTable = new HashTable();

expect(hashTable.hash('a')).toBe(9);
expect(hashTable.hash('b')).toBe(26);
expect(hashTable.hash('abc')).toBe(26);
expect(hashTable.hash('a')).toBe(18);
expect(hashTable.hash('b')).toBe(21);
expect(hashTable.hash('abc')).toBe(21);
});

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

expect(hashTable.hash('a')).toBe(2);
expect(hashTable.hash('a')).toBe(1);
expect(hashTable.hash('b')).toBe(2);
expect(hashTable.hash('c')).toBe(1);
expect(hashTable.hash('d')).toBe(2);
expect(hashTable.hash('c')).toBe(2);
expect(hashTable.hash('d')).toBe(1);

hashTable.set('a', 'sky-old');
hashTable.set('a', 'sky');
@@ -38,8 +38,8 @@ describe('HashTable', () => {
const stringifier = (value) => `${value.key}:${value.value}`;

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

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

expect(hashTable.getValues()).toEqual(['gamma', 'alpha', 'beta']);
expect(hashTable.getValues()).toEqual(['alpha', 'beta', 'gamma']);
});

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

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

expect(hashTable.getValues()).toEqual(['one', 'three', 'two']);
expect(hashTable.getValues()).toEqual(['three', 'one', 'two']);
});
});