Add getValues() method to HashTable and update LinkedList READMEs.

This commit is contained in:
Oleksii Trekhleb 2020-12-11 09:14:48 +01:00
parent 46daaf51c5
commit 1b0e27ab86
4 changed files with 28 additions and 8 deletions

View File

@ -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 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 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 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 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, be conceptualized as two singly linked lists formed from the same data items,
but in opposite sequential orders. but in opposite sequential orders.

View File

@ -107,14 +107,15 @@ export default class HashTable {
} }
/** /**
* Gets the list of all the stored values in the hash table in the order of * Gets the list of all the stored values in the hash table.
* the keys map.
* *
* @return {*[]} * @return {*[]}
*/ */
getValues() { getValues() {
const keys = this.getKeys(); return this.buckets.reduce((values, bucket) => {
const bucketValues = bucket.toArray()
return keys.map(key => this.buckets[this.hash(key)].head.value.value); .map((linkedListNode) => linkedListNode.value.value);
return values.concat(bucketValues);
}, []);
} }
} }

View File

@ -94,6 +94,24 @@ describe('HashTable', () => {
hashTable.set('b', 'beta'); hashTable.set('b', 'beta');
hashTable.set('c', 'gamma'); 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']);
}); });
}); });

View File

@ -4,7 +4,8 @@ _Read this in other languages:_
[_简体中文_](README.zh-CN.md), [_简体中文_](README.zh-CN.md),
[_Русский_](README.ru-RU.md), [_Русский_](README.ru-RU.md),
[_日本語_](README.ja-JP.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 In computer science, a **linked list** is a linear collection
of data elements, in which linear order is not given by of data elements, in which linear order is not given by