Modify HashTable (#447)

Add a getValues() method to the HashTable data structure
This commit is contained in:
JD Medina 2020-12-10 23:54:37 -08:00 committed by GitHub
parent c3d22956b7
commit 46daaf51c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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']);
});
});