mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-14 06:52:59 +08:00
Modify HashTable (#447)
Add a getValues() method to the HashTable data structure
This commit is contained in:
parent
c3d22956b7
commit
46daaf51c5
@ -105,4 +105,16 @@ export default class HashTable {
|
|||||||
getKeys() {
|
getKeys() {
|
||||||
return Object.keys(this.keys);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,4 +86,14 @@ describe('HashTable', () => {
|
|||||||
expect(hashTable.has('b')).toBe(true);
|
expect(hashTable.has('b')).toBe(true);
|
||||||
expect(hashTable.has('x')).toBe(false);
|
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']);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user