Skip to content

Commit

Permalink
Modify HashTable (#447)
Browse files Browse the repository at this point in the history
Add a getValues() method to the HashTable data structure
  • Loading branch information
JD Medina authored Dec 11, 2020
1 parent c3d2295 commit 46daaf5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/data-structures/hash-table/HashTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,16 @@ export default class HashTable {
getKeys() {
return Object.keys(this.keys);
}

/**
* Gets the list of all the stored values in the hash table in the order of
* the keys map.
*
* @return {*[]}
*/
getValues() {
const keys = this.getKeys();

return keys.map(key => this.buckets[this.hash(key)].head.value.value);
}
}
10 changes: 10 additions & 0 deletions src/data-structures/hash-table/__test__/HashTable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,14 @@ describe('HashTable', () => {
expect(hashTable.has('b')).toBe(true);
expect(hashTable.has('x')).toBe(false);
});

it('should get all the values', () => {
const hashTable = new HashTable(3);

hashTable.set('a', 'alpha');
hashTable.set('b', 'beta');
hashTable.set('c', 'gamma');

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

0 comments on commit 46daaf5

Please sign in to comment.