mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Add getValues() method to HashTable and update LinkedList READMEs.
This commit is contained in:
parent
46daaf51c5
commit
1b0e27ab86
@ -12,7 +12,7 @@ consists of a set of sequentially linked records called nodes. Each node contain
|
||||
two fields, called links, that are references to the previous and to the next
|
||||
node in the sequence of nodes. The beginning and ending nodes' previous and next
|
||||
links, respectively, point to some kind of terminator, typically a sentinel
|
||||
node or null, to facilitate traversal of the list. If there is only one
|
||||
node or null, to facilitate the traversal of the list. If there is only one
|
||||
sentinel node, then the list is circularly linked via the sentinel node. It can
|
||||
be conceptualized as two singly linked lists formed from the same data items,
|
||||
but in opposite sequential orders.
|
||||
|
@ -107,14 +107,15 @@ export default class HashTable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of all the stored values in the hash table in the order of
|
||||
* the keys map.
|
||||
* Gets the list of all the stored values in the hash table.
|
||||
*
|
||||
* @return {*[]}
|
||||
*/
|
||||
getValues() {
|
||||
const keys = this.getKeys();
|
||||
|
||||
return keys.map(key => this.buckets[this.hash(key)].head.value.value);
|
||||
return this.buckets.reduce((values, bucket) => {
|
||||
const bucketValues = bucket.toArray()
|
||||
.map((linkedListNode) => linkedListNode.value.value);
|
||||
return values.concat(bucketValues);
|
||||
}, []);
|
||||
}
|
||||
}
|
||||
|
@ -94,6 +94,24 @@ describe('HashTable', () => {
|
||||
hashTable.set('b', 'beta');
|
||||
hashTable.set('c', 'gamma');
|
||||
|
||||
expect(hashTable.getValues()).toEqual(['alpha', 'beta', 'gamma']);
|
||||
expect(hashTable.getValues()).toEqual(['gamma', 'alpha', 'beta']);
|
||||
});
|
||||
|
||||
it('should get all the values from empty hash table', () => {
|
||||
const hashTable = new HashTable();
|
||||
expect(hashTable.getValues()).toEqual([]);
|
||||
});
|
||||
|
||||
it('should get all the values in case of hash collision', () => {
|
||||
const hashTable = new HashTable(3);
|
||||
|
||||
// Keys `ab` and `ba` in current implementation should result in one hash (one bucket).
|
||||
// We need to make sure that several items from one bucket will be serialized.
|
||||
hashTable.set('ab', 'one');
|
||||
hashTable.set('ba', 'two');
|
||||
|
||||
hashTable.set('ac', 'three');
|
||||
|
||||
expect(hashTable.getValues()).toEqual(['one', 'two', 'three']);
|
||||
});
|
||||
});
|
||||
|
@ -4,7 +4,8 @@ _Read this in other languages:_
|
||||
[_简体中文_](README.zh-CN.md),
|
||||
[_Русский_](README.ru-RU.md),
|
||||
[_日本語_](README.ja-JP.md),
|
||||
[_Português_](README.pt-BR.md)
|
||||
[_Português_](README.pt-BR.md),
|
||||
[_한국어_](README.ko-KR.md)
|
||||
|
||||
In computer science, a **linked list** is a linear collection
|
||||
of data elements, in which linear order is not given by
|
||||
|
Loading…
Reference in New Issue
Block a user