Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 09b4b69b824cb06ec7d9b334d4688aeb509fe499
Choose a base ref
..
head repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e48f70db2b78434cbb0548575281f1f78ca7268f
Choose a head ref
Showing with 11 additions and 11 deletions.
  1. +1 −1 src/data-structures/hash-table/HashTable.js
  2. +10 −10 src/data-structures/hash-table/__test__/HashTable.test.js
2 changes: 1 addition & 1 deletion src/data-structures/hash-table/HashTable.js
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ export default class HashTable {
// charCodeAt may return number from 0 to 65536,
// should remap to range 0 to 255.
let hashPerRound = table[(key.charCodeAt(0) + i) % 256];
for (let j = 0; j < key.length; j += 1) {
for (let j = 1; j < key.length; j += 1) {
hashPerRound = table[hashPerRound ^ (key.charCodeAt(j) % 256)];
}

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,17 +12,17 @@ describe('HashTable', () => {
it('should generate proper hash for specified keys', () => {
const hashTable = new HashTable();

expect(hashTable.hash('a')).toBe(20);
expect(hashTable.hash('b')).toBe(31);
expect(hashTable.hash('abc')).toBe(19);
expect(hashTable.hash('a')).toBe(9);
expect(hashTable.hash('b')).toBe(26);
expect(hashTable.hash('abc')).toBe(26);
});

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

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

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

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

expect(hashTable.buckets[0].toString(stringifier)).toBe('c:earth');
expect(hashTable.buckets[1].toString(stringifier)).toBe('a:sky,b:sea');
expect(hashTable.buckets[2].toString(stringifier)).toBe('d:ocean');
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.get('a')).toBe('sky');
expect(hashTable.get('d')).toBe('ocean');
@@ -111,6 +111,6 @@ describe('HashTable', () => {

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

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