@gabrielrufino/cube 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/cd.yml +26 -0
- package/.github/workflows/ci.yml +23 -0
- package/LICENSE +24 -0
- package/README.md +1695 -0
- package/build/Algorithms/index.d.ts +3 -0
- package/build/Algorithms/index.js +24 -0
- package/build/Array/IArray.d.ts +9 -0
- package/build/Array/IArray.js +2 -0
- package/build/Array/index.d.ts +12 -0
- package/build/Array/index.js +79 -0
- package/build/BinarySearchTree/BinarySearchTreeNode.d.ts +7 -0
- package/build/BinarySearchTree/BinarySearchTreeNode.js +22 -0
- package/build/BinarySearchTree/IBinarySearchNodeOptions.d.ts +5 -0
- package/build/BinarySearchTree/IBinarySearchNodeOptions.js +2 -0
- package/build/BinarySearchTree/IBinarySearchTree.d.ts +14 -0
- package/build/BinarySearchTree/IBinarySearchTree.js +2 -0
- package/build/BinarySearchTree/IBinarySearchTreeData.d.ts +7 -0
- package/build/BinarySearchTree/IBinarySearchTreeData.js +2 -0
- package/build/BinarySearchTree/index.d.ts +24 -0
- package/build/BinarySearchTree/index.js +199 -0
- package/build/DataStructure/index.d.ts +6 -0
- package/build/DataStructure/index.js +32 -0
- package/build/Deck/IDeck.d.ts +9 -0
- package/build/Deck/IDeck.js +2 -0
- package/build/Deck/index.d.ts +12 -0
- package/build/Deck/index.js +76 -0
- package/build/Dictionary/IDictionary.d.ts +16 -0
- package/build/Dictionary/IDictionary.js +2 -0
- package/build/Dictionary/IDictionaryData.d.ts +4 -0
- package/build/Dictionary/IDictionaryData.js +2 -0
- package/build/Dictionary/index.d.ts +21 -0
- package/build/Dictionary/index.js +110 -0
- package/build/DoublyLinkedList/IDoublyLinkedList.d.ts +16 -0
- package/build/DoublyLinkedList/IDoublyLinkedList.js +2 -0
- package/build/DoublyLinkedList/Node.d.ts +6 -0
- package/build/DoublyLinkedList/Node.js +11 -0
- package/build/DoublyLinkedList/index.d.ts +32 -0
- package/build/DoublyLinkedList/index.js +221 -0
- package/build/Graph/GraphNodeNotFoundError.d.ts +3 -0
- package/build/Graph/GraphNodeNotFoundError.js +27 -0
- package/build/Graph/GraphSearchNodeStates.d.ts +6 -0
- package/build/Graph/GraphSearchNodeStates.js +10 -0
- package/build/Graph/IGraph.d.ts +16 -0
- package/build/Graph/IGraph.js +2 -0
- package/build/Graph/IGraphOptions.d.ts +7 -0
- package/build/Graph/IGraphOptions.js +2 -0
- package/build/Graph/index.d.ts +20 -0
- package/build/Graph/index.js +187 -0
- package/build/HashTable/IHashTable.d.ts +9 -0
- package/build/HashTable/IHashTable.js +2 -0
- package/build/HashTable/IHashTableData.d.ts +4 -0
- package/build/HashTable/IHashTableData.js +2 -0
- package/build/HashTable/IHashTableInputs.d.ts +4 -0
- package/build/HashTable/IHashTableInputs.js +2 -0
- package/build/HashTable/IHashTableOptions.d.ts +4 -0
- package/build/HashTable/IHashTableOptions.js +2 -0
- package/build/HashTable/index.d.ts +16 -0
- package/build/HashTable/index.js +83 -0
- package/build/HashTableLinearProbing/HashTableLinearProbingElement.d.ts +6 -0
- package/build/HashTableLinearProbing/HashTableLinearProbingElement.js +19 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbing.d.ts +9 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbing.js +2 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingData.d.ts +5 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingData.js +2 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingInputs.d.ts +4 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingInputs.js +2 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingOptions.d.ts +4 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingOptions.js +2 -0
- package/build/HashTableLinearProbing/index.d.ts +20 -0
- package/build/HashTableLinearProbing/index.js +120 -0
- package/build/HashTableSeparateChaining/HashTableSeparateChainingElement.d.ts +6 -0
- package/build/HashTableSeparateChaining/HashTableSeparateChainingElement.js +21 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChaining.d.ts +10 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChaining.js +2 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingData.d.ts +5 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingData.js +2 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingInputs.d.ts +4 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingInputs.js +2 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingOptions.d.ts +4 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingOptions.js +2 -0
- package/build/HashTableSeparateChaining/index.d.ts +19 -0
- package/build/HashTableSeparateChaining/index.js +104 -0
- package/build/LinkedList/ILinkedList.d.ts +13 -0
- package/build/LinkedList/ILinkedList.js +2 -0
- package/build/LinkedList/ILinkedListItem.d.ts +5 -0
- package/build/LinkedList/ILinkedListItem.js +2 -0
- package/build/LinkedList/Node.d.ts +5 -0
- package/build/LinkedList/Node.js +10 -0
- package/build/LinkedList/index.d.ts +24 -0
- package/build/LinkedList/index.js +169 -0
- package/build/List/index.d.ts +1 -0
- package/build/List/index.js +8 -0
- package/build/MaxHeap/IMaxHeap.d.ts +9 -0
- package/build/MaxHeap/IMaxHeap.js +2 -0
- package/build/MaxHeap/IMaxHeapOptions.d.ts +5 -0
- package/build/MaxHeap/IMaxHeapOptions.js +2 -0
- package/build/MaxHeap/index.d.ts +19 -0
- package/build/MaxHeap/index.js +102 -0
- package/build/MinHeap/IMinHeap.d.ts +9 -0
- package/build/MinHeap/IMinHeap.js +2 -0
- package/build/MinHeap/IMinHeapOptions.d.ts +5 -0
- package/build/MinHeap/IMinHeapOptions.js +2 -0
- package/build/MinHeap/index.d.ts +19 -0
- package/build/MinHeap/index.js +102 -0
- package/build/Queue/IQueue.d.ts +8 -0
- package/build/Queue/IQueue.js +2 -0
- package/build/Queue/index.d.ts +11 -0
- package/build/Queue/index.js +74 -0
- package/build/Set/ISet.d.ts +15 -0
- package/build/Set/ISet.js +2 -0
- package/build/Set/index.d.ts +21 -0
- package/build/Set/index.js +132 -0
- package/build/Stack/IStack.d.ts +8 -0
- package/build/Stack/IStack.js +2 -0
- package/build/Stack/index.d.ts +11 -0
- package/build/Stack/index.js +64 -0
- package/build/index.d.ts +17 -0
- package/build/index.js +40 -0
- package/coverage/clover.xml +706 -0
- package/coverage/coverage-final.json +19 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +327 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov-report/src/Algorithms/index.html +117 -0
- package/coverage/lcov-report/src/Algorithms/index.ts.html +149 -0
- package/coverage/lcov-report/src/Array/index.html +117 -0
- package/coverage/lcov-report/src/Array/index.ts.html +290 -0
- package/coverage/lcov-report/src/DataStructure/index.html +117 -0
- package/coverage/lcov-report/src/DataStructure/index.ts.html +131 -0
- package/coverage/lcov-report/src/Deck/index.html +117 -0
- package/coverage/lcov-report/src/Deck/index.ts.html +239 -0
- package/coverage/lcov-report/src/Dictionary/index.html +117 -0
- package/coverage/lcov-report/src/Dictionary/index.ts.html +350 -0
- package/coverage/lcov-report/src/DoublyLinkedList/Node.ts.html +113 -0
- package/coverage/lcov-report/src/DoublyLinkedList/index.html +132 -0
- package/coverage/lcov-report/src/DoublyLinkedList/index.ts.html +836 -0
- package/coverage/lcov-report/src/HashTable/index.html +117 -0
- package/coverage/lcov-report/src/HashTable/index.ts.html +320 -0
- package/coverage/lcov-report/src/HashTableLinearProbing/index.html +117 -0
- package/coverage/lcov-report/src/HashTableLinearProbing/index.ts.html +95 -0
- package/coverage/lcov-report/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts.html +149 -0
- package/coverage/lcov-report/src/HashTableSeparateChaining/index.html +132 -0
- package/coverage/lcov-report/src/HashTableSeparateChaining/index.ts.html +398 -0
- package/coverage/lcov-report/src/LinkedList/Node.ts.html +110 -0
- package/coverage/lcov-report/src/LinkedList/index.html +132 -0
- package/coverage/lcov-report/src/LinkedList/index.ts.html +641 -0
- package/coverage/lcov-report/src/List/index.html +117 -0
- package/coverage/lcov-report/src/List/index.ts.html +89 -0
- package/coverage/lcov-report/src/Queue/index.html +117 -0
- package/coverage/lcov-report/src/Queue/index.ts.html +218 -0
- package/coverage/lcov-report/src/Set/index.html +117 -0
- package/coverage/lcov-report/src/Set/index.ts.html +488 -0
- package/coverage/lcov-report/src/Stack/index.html +117 -0
- package/coverage/lcov-report/src/Stack/index.ts.html +215 -0
- package/coverage/lcov-report/src/index.html +117 -0
- package/coverage/lcov-report/src/index.ts.html +125 -0
- package/coverage/lcov.info +1346 -0
- package/package.json +42 -0
- package/src/Algorithms/index.spec.ts +15 -0
- package/src/Algorithms/index.ts +21 -0
- package/src/Array/IArray.ts +10 -0
- package/src/Array/index.spec.ts +151 -0
- package/src/Array/index.ts +68 -0
- package/src/BinarySearchTree/BinarySearchTreeNode.spec.ts +29 -0
- package/src/BinarySearchTree/BinarySearchTreeNode.ts +23 -0
- package/src/BinarySearchTree/IBinarySearchNodeOptions.ts +6 -0
- package/src/BinarySearchTree/IBinarySearchTree.ts +16 -0
- package/src/BinarySearchTree/IBinarySearchTreeData.ts +9 -0
- package/src/BinarySearchTree/index.spec.ts +486 -0
- package/src/BinarySearchTree/index.ts +198 -0
- package/src/DataStructure/index.spec.ts +25 -0
- package/src/DataStructure/index.ts +15 -0
- package/src/Deck/IDeck.ts +10 -0
- package/src/Deck/index.spec.ts +131 -0
- package/src/Deck/index.ts +51 -0
- package/src/Dictionary/IDictionary.ts +18 -0
- package/src/Dictionary/IDictionaryData.ts +5 -0
- package/src/Dictionary/index.spec.ts +310 -0
- package/src/Dictionary/index.ts +88 -0
- package/src/DoublyLinkedList/IDoublyLinkedList.ts +18 -0
- package/src/DoublyLinkedList/Node.ts +9 -0
- package/src/DoublyLinkedList/index.spec.ts +478 -0
- package/src/DoublyLinkedList/index.ts +250 -0
- package/src/Graph/GraphNodeNotFoundError.ts +7 -0
- package/src/Graph/GraphSearchNodeStates.ts +8 -0
- package/src/Graph/IGraph.ts +15 -0
- package/src/Graph/IGraphOptions.ts +6 -0
- package/src/Graph/index.spec.ts +318 -0
- package/src/Graph/index.ts +170 -0
- package/src/HashTable/IHashTable.ts +11 -0
- package/src/HashTable/IHashTableData.ts +5 -0
- package/src/HashTable/IHashTableInputs.ts +5 -0
- package/src/HashTable/IHashTableOptions.ts +5 -0
- package/src/HashTable/index.spec.ts +136 -0
- package/src/HashTable/index.ts +78 -0
- package/src/HashTableLinearProbing/HashTableLinearProbingElement.spec.ts +28 -0
- package/src/HashTableLinearProbing/HashTableLinearProbingElement.ts +18 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbing.ts +11 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbingData.ts +7 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbingInputs.ts +5 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbingOptions.ts +5 -0
- package/src/HashTableLinearProbing/index.spec.ts +266 -0
- package/src/HashTableLinearProbing/index.ts +128 -0
- package/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts +21 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChaining.ts +12 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingData.ts +7 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingInputs.ts +5 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingOptions.ts +5 -0
- package/src/HashTableSeparateChaining/index.spec.ts +173 -0
- package/src/HashTableSeparateChaining/index.ts +104 -0
- package/src/LinkedList/ILinkedList.ts +15 -0
- package/src/LinkedList/ILinkedListItem.ts +6 -0
- package/src/LinkedList/Node.ts +8 -0
- package/src/LinkedList/index.spec.ts +355 -0
- package/src/LinkedList/index.ts +185 -0
- package/src/List/index.ts +1 -0
- package/src/MaxHeap/IMaxHeap.ts +10 -0
- package/src/MaxHeap/IMaxHeapOptions.ts +6 -0
- package/src/MaxHeap/index.spec.ts +161 -0
- package/src/MaxHeap/index.ts +91 -0
- package/src/MinHeap/IMinHeap.ts +10 -0
- package/src/MinHeap/IMinHeapOptions.ts +6 -0
- package/src/MinHeap/index.spec.ts +161 -0
- package/src/MinHeap/index.ts +91 -0
- package/src/Queue/IQueue.ts +9 -0
- package/src/Queue/index.spec.ts +92 -0
- package/src/Queue/index.ts +44 -0
- package/src/Set/ISet.ts +17 -0
- package/src/Set/index.spec.ts +246 -0
- package/src/Set/index.ts +134 -0
- package/src/Stack/IStack.ts +9 -0
- package/src/Stack/index.spec.ts +108 -0
- package/src/Stack/index.ts +43 -0
- package/src/index.ts +17 -0
- package/tsconfig.json +104 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import {describe, it, expect} from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
import HashTableSeparateChaining from './';
|
|
4
|
+
import LinkedList from '../LinkedList';
|
|
5
|
+
import HashTableSeparateChainingElement from './HashTableSeparateChainingElement';
|
|
6
|
+
|
|
7
|
+
describe('HashTableSeparateChaining', () => {
|
|
8
|
+
it('Should create an empty hash table separate chaining without problems', () => {
|
|
9
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining<number>();
|
|
10
|
+
|
|
11
|
+
expect(hashTableSeparateChaining.data).toEqual({});
|
|
12
|
+
expect(hashTableSeparateChaining.size).toBe(0);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('Should create an filled hash table separate chaining without problems', () => {
|
|
16
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining<number>({
|
|
17
|
+
first: 1,
|
|
18
|
+
tsrif: -1,
|
|
19
|
+
second: 2,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
expect(hashTableSeparateChaining.data).toEqual({
|
|
23
|
+
36: new LinkedList(
|
|
24
|
+
new HashTableSeparateChainingElement('second', 2),
|
|
25
|
+
),
|
|
26
|
+
52: new LinkedList(
|
|
27
|
+
new HashTableSeparateChainingElement('first', 1),
|
|
28
|
+
new HashTableSeparateChainingElement('tsrif', -1),
|
|
29
|
+
),
|
|
30
|
+
});
|
|
31
|
+
expect(hashTableSeparateChaining.size).toBe(2);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('.put()', () => {
|
|
35
|
+
it('Should insert a new element in the hash table separate chaining', () => {
|
|
36
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining({
|
|
37
|
+
first: 1,
|
|
38
|
+
});
|
|
39
|
+
hashTableSeparateChaining.put('second', 2);
|
|
40
|
+
|
|
41
|
+
expect(hashTableSeparateChaining.data).toEqual({
|
|
42
|
+
36: new LinkedList(
|
|
43
|
+
new HashTableSeparateChainingElement('second', 2),
|
|
44
|
+
),
|
|
45
|
+
52: new LinkedList(
|
|
46
|
+
new HashTableSeparateChainingElement('first', 1),
|
|
47
|
+
),
|
|
48
|
+
});
|
|
49
|
+
expect(hashTableSeparateChaining.size).toBe(2);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('Should insert the new element in an existent linked list on collisions', () => {
|
|
53
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining({
|
|
54
|
+
first: 1,
|
|
55
|
+
second: 2,
|
|
56
|
+
});
|
|
57
|
+
hashTableSeparateChaining.put('tsrif', -1);
|
|
58
|
+
|
|
59
|
+
expect(hashTableSeparateChaining.data).toEqual({
|
|
60
|
+
36: new LinkedList(
|
|
61
|
+
new HashTableSeparateChainingElement('second', 2),
|
|
62
|
+
),
|
|
63
|
+
52: new LinkedList(
|
|
64
|
+
new HashTableSeparateChainingElement('first', 1),
|
|
65
|
+
new HashTableSeparateChainingElement('tsrif', -1),
|
|
66
|
+
),
|
|
67
|
+
});
|
|
68
|
+
expect(hashTableSeparateChaining.size).toBe(2);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('Should return the inserted element', () => {
|
|
72
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining();
|
|
73
|
+
const returned = hashTableSeparateChaining.put('first', 1);
|
|
74
|
+
|
|
75
|
+
expect(returned).toBe(1);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('.get()', () => {
|
|
80
|
+
it('Should get an element by key', () => {
|
|
81
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining({
|
|
82
|
+
first: 1,
|
|
83
|
+
second: 2,
|
|
84
|
+
});
|
|
85
|
+
const returned = hashTableSeparateChaining.get('first');
|
|
86
|
+
|
|
87
|
+
expect(returned).toEqual(new HashTableSeparateChainingElement('first', 1));
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('Should return null when the key is not in the hash table separate chaining', () => {
|
|
91
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining({
|
|
92
|
+
first: 1,
|
|
93
|
+
second: 2,
|
|
94
|
+
});
|
|
95
|
+
const returned = hashTableSeparateChaining.get('third');
|
|
96
|
+
|
|
97
|
+
expect(returned).toBeNull();
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('.remove()', () => {
|
|
102
|
+
it('Should remove an existent key value pair', () => {
|
|
103
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining({
|
|
104
|
+
first: 1,
|
|
105
|
+
second: 2,
|
|
106
|
+
});
|
|
107
|
+
hashTableSeparateChaining.remove('first');
|
|
108
|
+
|
|
109
|
+
expect(hashTableSeparateChaining.data).toEqual({
|
|
110
|
+
36: new LinkedList(new HashTableSeparateChainingElement('second', 2)),
|
|
111
|
+
});
|
|
112
|
+
expect(hashTableSeparateChaining.size).toBe(1);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('Should change nothing if the key is not in the hash table separate chaining', () => {
|
|
116
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining({
|
|
117
|
+
first: 1,
|
|
118
|
+
second: 2,
|
|
119
|
+
});
|
|
120
|
+
hashTableSeparateChaining.remove('third');
|
|
121
|
+
|
|
122
|
+
expect(hashTableSeparateChaining.data).toEqual({
|
|
123
|
+
36: new LinkedList(new HashTableSeparateChainingElement('second', 2)),
|
|
124
|
+
52: new LinkedList(new HashTableSeparateChainingElement('first', 1)),
|
|
125
|
+
});
|
|
126
|
+
expect(hashTableSeparateChaining.size).toBe(2);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('Should return the removed linked list', () => {
|
|
130
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining({
|
|
131
|
+
first: 1,
|
|
132
|
+
second: 2,
|
|
133
|
+
});
|
|
134
|
+
const returned = hashTableSeparateChaining.remove('first');
|
|
135
|
+
|
|
136
|
+
expect(returned).toEqual(new HashTableSeparateChainingElement('first', 1));
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('Should return null when remove a non existent key', () => {
|
|
140
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining({
|
|
141
|
+
first: 1,
|
|
142
|
+
second: 2,
|
|
143
|
+
});
|
|
144
|
+
const returned = hashTableSeparateChaining.remove('third');
|
|
145
|
+
|
|
146
|
+
expect(returned).toBeNull();
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe('Conversion to primitive', () => {
|
|
151
|
+
it('Should return separated by comma linked list in string conversion', () => {
|
|
152
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining({
|
|
153
|
+
first: 1,
|
|
154
|
+
tsrif: -1,
|
|
155
|
+
second: 2,
|
|
156
|
+
});
|
|
157
|
+
const string = String(hashTableSeparateChaining);
|
|
158
|
+
|
|
159
|
+
expect(string).toBe('[ [Head] { second => 2}, [Head] { first => 1} => { tsrif => -1} ]');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('Should return the size in number conversion', () => {
|
|
163
|
+
const hashTableSeparateChaining = new HashTableSeparateChaining({
|
|
164
|
+
first: 1,
|
|
165
|
+
tsrif: -1,
|
|
166
|
+
second: 2,
|
|
167
|
+
});
|
|
168
|
+
const string = Number(hashTableSeparateChaining);
|
|
169
|
+
|
|
170
|
+
expect(string).toBe(2);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import HashTableSeparateChainingElement from './HashTableSeparateChainingElement';
|
|
2
|
+
import IHashTableSeparateChaining from './IHashTableSeparateChaining';
|
|
3
|
+
import IHashTableSeparateChainingData from './IHashTableSeparateChainingData';
|
|
4
|
+
import IHashTableSeparateChainingInputs from './IHashTableSeparateChainingInputs';
|
|
5
|
+
import LinkedList from '../LinkedList';
|
|
6
|
+
|
|
7
|
+
export default class HashTableSeparateChaining<T = number> implements IHashTableSeparateChaining<T> {
|
|
8
|
+
private _maxSize: number;
|
|
9
|
+
private _data: LinkedList<HashTableSeparateChainingElement<T>>[];
|
|
10
|
+
|
|
11
|
+
constructor(
|
|
12
|
+
inputs: IHashTableSeparateChainingInputs<T> = {},
|
|
13
|
+
{maxSize} = {maxSize: 100},
|
|
14
|
+
) {
|
|
15
|
+
this._maxSize = maxSize;
|
|
16
|
+
this._data = new Array(this._maxSize);
|
|
17
|
+
|
|
18
|
+
for (const [key, value] of Object.entries(inputs)) {
|
|
19
|
+
this.put(key, value);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get data(): IHashTableSeparateChainingData<T> {
|
|
24
|
+
return this._data
|
|
25
|
+
.map((value, key) => ({key, value}))
|
|
26
|
+
.reduce((accumulator, current) => ({
|
|
27
|
+
...accumulator,
|
|
28
|
+
[current.key]: current.value,
|
|
29
|
+
}), {});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get size(): number {
|
|
33
|
+
return Reflect.ownKeys(this.data).length;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public put(key: string, value: T): T {
|
|
37
|
+
const linkedList = this._getLinkedListByKey(key);
|
|
38
|
+
const element = new HashTableSeparateChainingElement(key, value);
|
|
39
|
+
|
|
40
|
+
if (linkedList) {
|
|
41
|
+
linkedList.push(element);
|
|
42
|
+
} else {
|
|
43
|
+
const position = this._hashCode(key);
|
|
44
|
+
this._data[position] = new LinkedList(element);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public get(key: string): HashTableSeparateChainingElement<T> | null {
|
|
51
|
+
const linkedList = this._getLinkedListByKey(key);
|
|
52
|
+
|
|
53
|
+
if (linkedList) {
|
|
54
|
+
const element = linkedList.data.find(element => element.value.key === key);
|
|
55
|
+
return element?.value || null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public remove(key: string): HashTableSeparateChainingElement<T> | null {
|
|
62
|
+
const linkedList = this._getLinkedListByKey(key);
|
|
63
|
+
|
|
64
|
+
if (linkedList) {
|
|
65
|
+
const index = linkedList.data.findIndex(element => element.value.key === key);
|
|
66
|
+
const element = linkedList.removeFromPosition(index);
|
|
67
|
+
|
|
68
|
+
if (linkedList.isEmpty) {
|
|
69
|
+
const position = this._hashCode(key);
|
|
70
|
+
Reflect.deleteProperty(this._data, position);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return element || null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private _getLinkedListByKey(key: string): LinkedList<HashTableSeparateChainingElement<T>> | null {
|
|
80
|
+
const position = this._hashCode(key);
|
|
81
|
+
return Reflect.get(this.data, position) || null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private _hashCode(key: string): number {
|
|
85
|
+
const code = key
|
|
86
|
+
.split('')
|
|
87
|
+
.map(character => character.charCodeAt(0))
|
|
88
|
+
.reduce((previous, current) => previous + current, 0);
|
|
89
|
+
|
|
90
|
+
return code % this._maxSize;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private [Symbol.toPrimitive](type: string): string | number | null {
|
|
94
|
+
if (type === 'string') {
|
|
95
|
+
return `[ ${Object.values(this.data).map(String).join(', ')} ]`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (type === 'number') {
|
|
99
|
+
return this.size;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import ILinkedListItem from './ILinkedListItem';
|
|
2
|
+
|
|
3
|
+
interface ILinkedList<T> {
|
|
4
|
+
get data(): ILinkedListItem<T>[];
|
|
5
|
+
get size(): number;
|
|
6
|
+
get isEmpty(): boolean;
|
|
7
|
+
push(_element: T): T;
|
|
8
|
+
getFromPosition(_position: number): ILinkedListItem<T> | undefined;
|
|
9
|
+
positionOf(_element: T): number | undefined;
|
|
10
|
+
insertInPosition(_element: T, _position: number): T | undefined;
|
|
11
|
+
remove(_element: T): T | undefined;
|
|
12
|
+
removeFromPosition(_position: number): T | undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default ILinkedList;
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import {describe, it, expect} from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
import LinkedList from './';
|
|
4
|
+
|
|
5
|
+
describe('LinkedList', () => {
|
|
6
|
+
it('Should create an empty linked list without problems', () => {
|
|
7
|
+
const linkedList = new LinkedList();
|
|
8
|
+
|
|
9
|
+
expect(linkedList.data).toEqual([]);
|
|
10
|
+
expect(linkedList.size).toBe(0);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('Should create a filled linked list without problems', () => {
|
|
14
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
15
|
+
|
|
16
|
+
expect(linkedList.data).toEqual([
|
|
17
|
+
{
|
|
18
|
+
value: 1,
|
|
19
|
+
next: 2,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
value: 2,
|
|
23
|
+
next: 3,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
value: 3,
|
|
27
|
+
next: 4,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
value: 4,
|
|
31
|
+
next: null,
|
|
32
|
+
},
|
|
33
|
+
]);
|
|
34
|
+
expect(linkedList.size).toBe(4);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('.isEmpty', () => {
|
|
38
|
+
it('Should return true when there is no element in the linked list', () => {
|
|
39
|
+
const linkedList = new LinkedList();
|
|
40
|
+
|
|
41
|
+
expect(linkedList.isEmpty).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('Should return false when the is at least one element in the linked list', () => {
|
|
45
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
46
|
+
|
|
47
|
+
expect(linkedList.isEmpty).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('.push()', () => {
|
|
52
|
+
it('Should push a new element', () => {
|
|
53
|
+
const linkedList = new LinkedList();
|
|
54
|
+
|
|
55
|
+
linkedList.push(1);
|
|
56
|
+
linkedList.push(2);
|
|
57
|
+
linkedList.push(3);
|
|
58
|
+
linkedList.push(4);
|
|
59
|
+
|
|
60
|
+
expect(linkedList.data).toEqual([
|
|
61
|
+
{
|
|
62
|
+
value: 1,
|
|
63
|
+
next: 2,
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
value: 2,
|
|
67
|
+
next: 3,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
value: 3,
|
|
71
|
+
next: 4,
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
value: 4,
|
|
75
|
+
next: null,
|
|
76
|
+
},
|
|
77
|
+
]);
|
|
78
|
+
expect(linkedList.size).toBe(4);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('Should return the pushed element', () => {
|
|
82
|
+
const linkedList = new LinkedList();
|
|
83
|
+
const returned = linkedList.push(1);
|
|
84
|
+
|
|
85
|
+
expect(returned).toBe(1);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('.removeFromPosition()', () => {
|
|
90
|
+
it('Should return undefined when given a negative position', () => {
|
|
91
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
92
|
+
const returned = linkedList.removeFromPosition(-1);
|
|
93
|
+
|
|
94
|
+
expect(returned).toBeUndefined();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('Should return undefined when given a position equal size', () => {
|
|
98
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
99
|
+
const returned = linkedList.removeFromPosition(linkedList.size);
|
|
100
|
+
|
|
101
|
+
expect(returned).toBeUndefined();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('Should return undefined when given a position larger than size', () => {
|
|
105
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
106
|
+
const returned = linkedList.removeFromPosition(linkedList.size + 1);
|
|
107
|
+
|
|
108
|
+
expect(returned).toBeUndefined();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('Should remove item from the head', () => {
|
|
112
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
113
|
+
linkedList.removeFromPosition(0);
|
|
114
|
+
|
|
115
|
+
expect(linkedList.data).toEqual([
|
|
116
|
+
{
|
|
117
|
+
value: 2,
|
|
118
|
+
next: 3,
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
value: 3,
|
|
122
|
+
next: 4,
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
value: 4,
|
|
126
|
+
next: null,
|
|
127
|
+
},
|
|
128
|
+
]);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('Should remove item from the middle of the linked list', () => {
|
|
132
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
133
|
+
linkedList.removeFromPosition(2);
|
|
134
|
+
|
|
135
|
+
expect(linkedList.data).toEqual([
|
|
136
|
+
{
|
|
137
|
+
value: 1,
|
|
138
|
+
next: 2,
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
value: 2,
|
|
142
|
+
next: 4,
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
value: 4,
|
|
146
|
+
next: null,
|
|
147
|
+
},
|
|
148
|
+
]);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('Should remove item from the last position of the linked list', () => {
|
|
152
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
153
|
+
linkedList.removeFromPosition(linkedList.size - 1);
|
|
154
|
+
|
|
155
|
+
expect(linkedList.data).toEqual([
|
|
156
|
+
{
|
|
157
|
+
value: 1,
|
|
158
|
+
next: 2,
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
value: 2,
|
|
162
|
+
next: 3,
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
value: 3,
|
|
166
|
+
next: null,
|
|
167
|
+
},
|
|
168
|
+
]);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('.getFromPosition()', () => {
|
|
173
|
+
it('Should get then element at a valid position', () => {
|
|
174
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
175
|
+
const returned = linkedList.getFromPosition(2);
|
|
176
|
+
|
|
177
|
+
expect(returned).toEqual({
|
|
178
|
+
value: 3,
|
|
179
|
+
next: 4,
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe('.insertInPosition()', () => {
|
|
185
|
+
it('Should return undefined when receive a negative position', () => {
|
|
186
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
187
|
+
const returned = linkedList.insertInPosition(5, -1);
|
|
188
|
+
|
|
189
|
+
expect(returned).toBeUndefined();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('Should return undefined when receive a position larger than the size', () => {
|
|
193
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
194
|
+
const returned = linkedList.insertInPosition(5, 10);
|
|
195
|
+
|
|
196
|
+
expect(returned).toBeUndefined();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('Should insert an element in the head when receive position 0', () => {
|
|
200
|
+
const linkedList = new LinkedList(1);
|
|
201
|
+
linkedList.insertInPosition(0, 0);
|
|
202
|
+
|
|
203
|
+
expect(linkedList.data).toEqual([
|
|
204
|
+
{
|
|
205
|
+
value: 0,
|
|
206
|
+
next: 1,
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
value: 1,
|
|
210
|
+
next: null,
|
|
211
|
+
},
|
|
212
|
+
]);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('Should insert an element in the middle of the linked list', () => {
|
|
216
|
+
const linkedList = new LinkedList(1, 3);
|
|
217
|
+
linkedList.insertInPosition(2, 1);
|
|
218
|
+
|
|
219
|
+
expect(linkedList.data).toEqual([
|
|
220
|
+
{
|
|
221
|
+
value: 1,
|
|
222
|
+
next: 2,
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
value: 2,
|
|
226
|
+
next: 3,
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
value: 3,
|
|
230
|
+
next: null,
|
|
231
|
+
},
|
|
232
|
+
]);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('Should insert an element in the penultimate position', () => {
|
|
236
|
+
const linkedList = new LinkedList(1, 2, 4);
|
|
237
|
+
linkedList.insertInPosition(3, linkedList.size - 1);
|
|
238
|
+
|
|
239
|
+
expect(linkedList.data).toEqual([
|
|
240
|
+
{
|
|
241
|
+
value: 1,
|
|
242
|
+
next: 2,
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
value: 2,
|
|
246
|
+
next: 3,
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
value: 3,
|
|
250
|
+
next: 4,
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
value: 4,
|
|
254
|
+
next: null,
|
|
255
|
+
},
|
|
256
|
+
]);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('Should insert an element in the end of the linked list, just like a push', () => {
|
|
260
|
+
const linkedList = new LinkedList(1, 2);
|
|
261
|
+
linkedList.insertInPosition(3, linkedList.size);
|
|
262
|
+
|
|
263
|
+
expect(linkedList.data).toEqual([
|
|
264
|
+
{
|
|
265
|
+
value: 1,
|
|
266
|
+
next: 2,
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
value: 2,
|
|
270
|
+
next: 3,
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
value: 3,
|
|
274
|
+
next: null,
|
|
275
|
+
},
|
|
276
|
+
]);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it('Should increase the size by one', () => {
|
|
280
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
281
|
+
const {size} = linkedList;
|
|
282
|
+
linkedList.insertInPosition(5, linkedList.size);
|
|
283
|
+
|
|
284
|
+
expect(linkedList.size).toBe(size + 1);
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
describe('.positionOf()', () => {
|
|
289
|
+
it('Should return undefined when receive an element not found in linked list', () => {
|
|
290
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
291
|
+
const position = linkedList.positionOf(5);
|
|
292
|
+
|
|
293
|
+
expect(position).toBeUndefined();
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it('Should return the correct position of an element', () => {
|
|
297
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
298
|
+
const position = linkedList.positionOf(3);
|
|
299
|
+
|
|
300
|
+
expect(position).toBe(2);
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
describe('.remove()', () => {
|
|
305
|
+
it('Should remove an element from linked list', () => {
|
|
306
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
307
|
+
linkedList.remove(3);
|
|
308
|
+
|
|
309
|
+
expect(linkedList.data).toEqual([
|
|
310
|
+
{
|
|
311
|
+
value: 1,
|
|
312
|
+
next: 2,
|
|
313
|
+
},
|
|
314
|
+
{
|
|
315
|
+
value: 2,
|
|
316
|
+
next: 4,
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
value: 4,
|
|
320
|
+
next: null,
|
|
321
|
+
},
|
|
322
|
+
]);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it('Should return the removed element', () => {
|
|
326
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
327
|
+
const returned = linkedList.remove(3);
|
|
328
|
+
|
|
329
|
+
expect(returned).toBe(3);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it('Should return undefined when the element doesn\'t belongs to the linked list', () => {
|
|
333
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
334
|
+
const returned = linkedList.remove(5);
|
|
335
|
+
|
|
336
|
+
expect(returned).toBeUndefined();
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
describe('Conversion to primitive', () => {
|
|
341
|
+
it('Should return arrow-separated elements in string conversion', () => {
|
|
342
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
343
|
+
const string = String(linkedList);
|
|
344
|
+
|
|
345
|
+
expect(string).toBe('[Head] 1 => 2 => 3 => 4');
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it('Should return the linked list size in number conversion', () => {
|
|
349
|
+
const linkedList = new LinkedList(1, 2, 3, 4);
|
|
350
|
+
const number = Number(linkedList);
|
|
351
|
+
|
|
352
|
+
expect(number).toBe(4);
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
});
|