@gabrielrufino/cube 1.0.1 → 1.0.6

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.
Files changed (123) hide show
  1. package/package.json +6 -6
  2. package/.github/workflows/cd.yml +0 -26
  3. package/.github/workflows/ci.yml +0 -23
  4. package/coverage/clover.xml +0 -706
  5. package/coverage/coverage-final.json +0 -19
  6. package/coverage/lcov-report/base.css +0 -224
  7. package/coverage/lcov-report/block-navigation.js +0 -87
  8. package/coverage/lcov-report/favicon.png +0 -0
  9. package/coverage/lcov-report/index.html +0 -327
  10. package/coverage/lcov-report/prettify.css +0 -1
  11. package/coverage/lcov-report/prettify.js +0 -2
  12. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  13. package/coverage/lcov-report/sorter.js +0 -196
  14. package/coverage/lcov-report/src/Algorithms/index.html +0 -117
  15. package/coverage/lcov-report/src/Algorithms/index.ts.html +0 -149
  16. package/coverage/lcov-report/src/Array/index.html +0 -117
  17. package/coverage/lcov-report/src/Array/index.ts.html +0 -290
  18. package/coverage/lcov-report/src/DataStructure/index.html +0 -117
  19. package/coverage/lcov-report/src/DataStructure/index.ts.html +0 -131
  20. package/coverage/lcov-report/src/Deck/index.html +0 -117
  21. package/coverage/lcov-report/src/Deck/index.ts.html +0 -239
  22. package/coverage/lcov-report/src/Dictionary/index.html +0 -117
  23. package/coverage/lcov-report/src/Dictionary/index.ts.html +0 -350
  24. package/coverage/lcov-report/src/DoublyLinkedList/Node.ts.html +0 -113
  25. package/coverage/lcov-report/src/DoublyLinkedList/index.html +0 -132
  26. package/coverage/lcov-report/src/DoublyLinkedList/index.ts.html +0 -836
  27. package/coverage/lcov-report/src/HashTable/index.html +0 -117
  28. package/coverage/lcov-report/src/HashTable/index.ts.html +0 -320
  29. package/coverage/lcov-report/src/HashTableLinearProbing/index.html +0 -117
  30. package/coverage/lcov-report/src/HashTableLinearProbing/index.ts.html +0 -95
  31. package/coverage/lcov-report/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts.html +0 -149
  32. package/coverage/lcov-report/src/HashTableSeparateChaining/index.html +0 -132
  33. package/coverage/lcov-report/src/HashTableSeparateChaining/index.ts.html +0 -398
  34. package/coverage/lcov-report/src/LinkedList/Node.ts.html +0 -110
  35. package/coverage/lcov-report/src/LinkedList/index.html +0 -132
  36. package/coverage/lcov-report/src/LinkedList/index.ts.html +0 -641
  37. package/coverage/lcov-report/src/List/index.html +0 -117
  38. package/coverage/lcov-report/src/List/index.ts.html +0 -89
  39. package/coverage/lcov-report/src/Queue/index.html +0 -117
  40. package/coverage/lcov-report/src/Queue/index.ts.html +0 -218
  41. package/coverage/lcov-report/src/Set/index.html +0 -117
  42. package/coverage/lcov-report/src/Set/index.ts.html +0 -488
  43. package/coverage/lcov-report/src/Stack/index.html +0 -117
  44. package/coverage/lcov-report/src/Stack/index.ts.html +0 -215
  45. package/coverage/lcov-report/src/index.html +0 -117
  46. package/coverage/lcov-report/src/index.ts.html +0 -125
  47. package/coverage/lcov.info +0 -1346
  48. package/src/Algorithms/index.spec.ts +0 -15
  49. package/src/Algorithms/index.ts +0 -21
  50. package/src/Array/IArray.ts +0 -10
  51. package/src/Array/index.spec.ts +0 -151
  52. package/src/Array/index.ts +0 -68
  53. package/src/BinarySearchTree/BinarySearchTreeNode.spec.ts +0 -29
  54. package/src/BinarySearchTree/BinarySearchTreeNode.ts +0 -23
  55. package/src/BinarySearchTree/IBinarySearchNodeOptions.ts +0 -6
  56. package/src/BinarySearchTree/IBinarySearchTree.ts +0 -16
  57. package/src/BinarySearchTree/IBinarySearchTreeData.ts +0 -9
  58. package/src/BinarySearchTree/index.spec.ts +0 -486
  59. package/src/BinarySearchTree/index.ts +0 -198
  60. package/src/DataStructure/index.spec.ts +0 -25
  61. package/src/DataStructure/index.ts +0 -15
  62. package/src/Deck/IDeck.ts +0 -10
  63. package/src/Deck/index.spec.ts +0 -131
  64. package/src/Deck/index.ts +0 -51
  65. package/src/Dictionary/IDictionary.ts +0 -18
  66. package/src/Dictionary/IDictionaryData.ts +0 -5
  67. package/src/Dictionary/index.spec.ts +0 -310
  68. package/src/Dictionary/index.ts +0 -88
  69. package/src/DoublyLinkedList/IDoublyLinkedList.ts +0 -18
  70. package/src/DoublyLinkedList/Node.ts +0 -9
  71. package/src/DoublyLinkedList/index.spec.ts +0 -478
  72. package/src/DoublyLinkedList/index.ts +0 -250
  73. package/src/Graph/GraphNodeNotFoundError.ts +0 -7
  74. package/src/Graph/GraphSearchNodeStates.ts +0 -8
  75. package/src/Graph/IGraph.ts +0 -15
  76. package/src/Graph/IGraphOptions.ts +0 -6
  77. package/src/Graph/index.spec.ts +0 -318
  78. package/src/Graph/index.ts +0 -170
  79. package/src/HashTable/IHashTable.ts +0 -11
  80. package/src/HashTable/IHashTableData.ts +0 -5
  81. package/src/HashTable/IHashTableInputs.ts +0 -5
  82. package/src/HashTable/IHashTableOptions.ts +0 -5
  83. package/src/HashTable/index.spec.ts +0 -136
  84. package/src/HashTable/index.ts +0 -78
  85. package/src/HashTableLinearProbing/HashTableLinearProbingElement.spec.ts +0 -28
  86. package/src/HashTableLinearProbing/HashTableLinearProbingElement.ts +0 -18
  87. package/src/HashTableLinearProbing/IHashTableLinearProbing.ts +0 -11
  88. package/src/HashTableLinearProbing/IHashTableLinearProbingData.ts +0 -7
  89. package/src/HashTableLinearProbing/IHashTableLinearProbingInputs.ts +0 -5
  90. package/src/HashTableLinearProbing/IHashTableLinearProbingOptions.ts +0 -5
  91. package/src/HashTableLinearProbing/index.spec.ts +0 -266
  92. package/src/HashTableLinearProbing/index.ts +0 -128
  93. package/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts +0 -21
  94. package/src/HashTableSeparateChaining/IHashTableSeparateChaining.ts +0 -12
  95. package/src/HashTableSeparateChaining/IHashTableSeparateChainingData.ts +0 -7
  96. package/src/HashTableSeparateChaining/IHashTableSeparateChainingInputs.ts +0 -5
  97. package/src/HashTableSeparateChaining/IHashTableSeparateChainingOptions.ts +0 -5
  98. package/src/HashTableSeparateChaining/index.spec.ts +0 -173
  99. package/src/HashTableSeparateChaining/index.ts +0 -104
  100. package/src/LinkedList/ILinkedList.ts +0 -15
  101. package/src/LinkedList/ILinkedListItem.ts +0 -6
  102. package/src/LinkedList/Node.ts +0 -8
  103. package/src/LinkedList/index.spec.ts +0 -355
  104. package/src/LinkedList/index.ts +0 -185
  105. package/src/List/index.ts +0 -1
  106. package/src/MaxHeap/IMaxHeap.ts +0 -10
  107. package/src/MaxHeap/IMaxHeapOptions.ts +0 -6
  108. package/src/MaxHeap/index.spec.ts +0 -161
  109. package/src/MaxHeap/index.ts +0 -91
  110. package/src/MinHeap/IMinHeap.ts +0 -10
  111. package/src/MinHeap/IMinHeapOptions.ts +0 -6
  112. package/src/MinHeap/index.spec.ts +0 -161
  113. package/src/MinHeap/index.ts +0 -91
  114. package/src/Queue/IQueue.ts +0 -9
  115. package/src/Queue/index.spec.ts +0 -92
  116. package/src/Queue/index.ts +0 -44
  117. package/src/Set/ISet.ts +0 -17
  118. package/src/Set/index.spec.ts +0 -246
  119. package/src/Set/index.ts +0 -134
  120. package/src/Stack/IStack.ts +0 -9
  121. package/src/Stack/index.spec.ts +0 -108
  122. package/src/Stack/index.ts +0 -43
  123. package/src/index.ts +0 -17
@@ -1,78 +0,0 @@
1
- import IHashTable from './IHashTable';
2
- import IHashTableData from './IHashTableData';
3
- import IHashTableInputs from './IHashTableInputs';
4
- import IHashTableOptions from './IHashTableOptions';
5
-
6
- export default class HashTable<T = number> implements IHashTable<T> {
7
- private _maxSize: number;
8
- private _data: T[];
9
-
10
- constructor(
11
- inputs: IHashTableInputs<T> = {},
12
- {maxSize = 100}: IHashTableOptions = {maxSize: 100},
13
- ) {
14
- this._maxSize = maxSize;
15
- this._data = new Array(this._maxSize);
16
-
17
- for (const [key, value] of Object.entries(inputs)) {
18
- this.put(key, value);
19
- }
20
- }
21
-
22
- get data(): IHashTableData<T> {
23
- return this._data
24
- .map((value, index) => ({index, value}))
25
- .reduce((accumulator, current) => ({
26
- ...accumulator,
27
- [current.index]: current.value,
28
- }), {});
29
- }
30
-
31
- get size(): number {
32
- return Reflect.ownKeys(this.data).length;
33
- }
34
-
35
- public put(key: string, value: T): T {
36
- const position = this._hashCode(key);
37
- this._data[position] = value;
38
- return value;
39
- }
40
-
41
- public get(key: string): T | null {
42
- const position = this._hashCode(key);
43
- return this.data[position] || null;
44
- }
45
-
46
- public remove(key: string): T | null {
47
- const value = this.get(key);
48
-
49
- if (value) {
50
- const position = this._hashCode(key);
51
- Reflect.deleteProperty(this._data, position);
52
- return value;
53
- }
54
-
55
- return null;
56
- }
57
-
58
- private _hashCode(key: string): number {
59
- const code = key
60
- .split('')
61
- .map(character => character.charCodeAt(0))
62
- .reduce((previous, current) => previous + current, 0);
63
-
64
- return code % this._maxSize;
65
- }
66
-
67
- private [Symbol.toPrimitive](type: string): string | number | null {
68
- if (type === 'string') {
69
- return `[ ${Object.entries(this.data).map(([key, value]) => `${key} => ${value}`).join(', ')} ]`;
70
- }
71
-
72
- if (type === 'number') {
73
- return this.size;
74
- }
75
-
76
- return null;
77
- }
78
- }
@@ -1,28 +0,0 @@
1
- import {describe, it, expect} from '@jest/globals';
2
-
3
- import HashTableLinearProbingElement from './HashTableLinearProbingElement';
4
-
5
- describe('HashTableLinearProbingElement', () => {
6
- it('Should create a hash table linear probing element without problems', () => {
7
- const hashTableLinearProbingElement = new HashTableLinearProbingElement('key', 10);
8
-
9
- expect(hashTableLinearProbingElement.key).toBe('key');
10
- expect(hashTableLinearProbingElement.value).toBe(10);
11
- });
12
-
13
- describe('Conversion to primitive', () => {
14
- it('Should return the key value pair separated by arrow in string conversion', () => {
15
- const hashTableLinearProbingElement = new HashTableLinearProbingElement('key', 'value');
16
- const string = String(hashTableLinearProbingElement);
17
-
18
- expect(string).toBe('{ key => value }');
19
- });
20
-
21
- it('Should return the number 2 in number conversion', () => {
22
- const hashTableLinearProbingElement = new HashTableLinearProbingElement('key', 'value');
23
- const number = Number(hashTableLinearProbingElement);
24
-
25
- expect(number).toBe(2);
26
- });
27
- });
28
- });
@@ -1,18 +0,0 @@
1
- /* eslint-disable no-unused-vars */
2
- /* eslint-disable no-useless-constructor */
3
-
4
- export default class HashTableLinearProbingElement<T> {
5
- constructor(
6
- public key: string,
7
- public value: T,
8
- ) {}
9
-
10
- [Symbol.toPrimitive](type: 'string' | 'number'): string | number | null {
11
- const options = {
12
- string: `{ ${this.key} => ${this.value} }`,
13
- number: 2,
14
- };
15
-
16
- return options[type] || null;
17
- }
18
- }
@@ -1,11 +0,0 @@
1
- import IHashTableLinearProbingData from './IHashTableLinearProbingData';
2
-
3
- interface IHashTableLinearProbing<T> {
4
- get data(): IHashTableLinearProbingData<T>;
5
- get size(): number;
6
- put(_key: string, _value: T): T | null;
7
- get(_key: string): T | null;
8
- remove(_key: string): T | null;
9
- }
10
-
11
- export default IHashTableLinearProbing;
@@ -1,7 +0,0 @@
1
- import HashTableLinearProbingElement from './HashTableLinearProbingElement';
2
-
3
- interface IHashTableLinearProbingData<T> {
4
- [index: number]: HashTableLinearProbingElement<T>
5
- }
6
-
7
- export default IHashTableLinearProbingData;
@@ -1,5 +0,0 @@
1
- interface IHashTableLinearProbingInputs<T> {
2
- [key: string]: T;
3
- }
4
-
5
- export default IHashTableLinearProbingInputs;
@@ -1,5 +0,0 @@
1
- interface IHashTableLinearProbingOptions {
2
- maxSize: number;
3
- }
4
-
5
- export default IHashTableLinearProbingOptions;
@@ -1,266 +0,0 @@
1
- import {describe, it, expect} from '@jest/globals';
2
-
3
- import HashTableLinearProbing from './';
4
- import HashTableLinearProbingElement from './HashTableLinearProbingElement';
5
-
6
- describe('HashTableLinearProbing', () => {
7
- it('Should create an empty hash table linear probing without problems', () => {
8
- const hashTableLinearProbing = new HashTableLinearProbing();
9
-
10
- expect(hashTableLinearProbing.data).toEqual({});
11
- expect(hashTableLinearProbing.size).toBe(0);
12
- });
13
-
14
- it('Should create a filled hash table linear probing without problems', () => {
15
- const hashTableLinearProbing = new HashTableLinearProbing({
16
- first: 1,
17
- second: 2,
18
- });
19
-
20
- expect(hashTableLinearProbing.data).toEqual({
21
- 36: new HashTableLinearProbingElement('second', 2),
22
- 52: new HashTableLinearProbingElement('first', 1),
23
- });
24
- expect(hashTableLinearProbing.size).toBe(2);
25
- });
26
-
27
- describe('.put()', () => {
28
- it('Should insert a new element', () => {
29
- const hashTableLinearProbing = new HashTableLinearProbing({
30
- first: 1,
31
- });
32
- hashTableLinearProbing.put('second', 2);
33
-
34
- expect(hashTableLinearProbing.data).toEqual({
35
- 36: new HashTableLinearProbingElement('second', 2),
36
- 52: new HashTableLinearProbingElement('first', 1),
37
- });
38
- expect(hashTableLinearProbing.size).toBe(2);
39
- });
40
-
41
- it('Should return the inserted element', () => {
42
- const hashTableLinearProbing = new HashTableLinearProbing({
43
- first: 1,
44
- });
45
- const returned = hashTableLinearProbing.put('second', 2);
46
-
47
- expect(returned).toBe(2);
48
- });
49
-
50
- it('Should change nothing when the hash table linear probing is full', () => {
51
- const hashTableLinearProbing = new HashTableLinearProbing({
52
- first: 1,
53
- second: 2,
54
- }, {
55
- maxSize: 2,
56
- });
57
- hashTableLinearProbing.put('third', 3);
58
-
59
- expect(hashTableLinearProbing.data).toEqual({
60
- 0: new HashTableLinearProbingElement('first', 1),
61
- 1: new HashTableLinearProbingElement('second', 2),
62
- });
63
- expect(hashTableLinearProbing.size).toBe(2);
64
- });
65
-
66
- it('Should return null when the hash table linear probing is full', () => {
67
- const hashTableLinearProbing = new HashTableLinearProbing({
68
- first: 1,
69
- second: 2,
70
- }, {
71
- maxSize: 2,
72
- });
73
- const returned = hashTableLinearProbing.put('third', 3);
74
-
75
- expect(returned).toBeNull();
76
- });
77
-
78
- it('Should put the new element in the next available position in collision case', () => {
79
- const hashTableLinearProbing = new HashTableLinearProbing({
80
- first: 1,
81
- });
82
- hashTableLinearProbing.put('tsrif', -1);
83
-
84
- expect(hashTableLinearProbing.data).toEqual({
85
- 52: new HashTableLinearProbingElement('first', 1),
86
- 53: new HashTableLinearProbingElement('tsrif', -1),
87
- });
88
- expect(hashTableLinearProbing.size).toBe(2);
89
- });
90
- });
91
-
92
- describe('.get()', () => {
93
- it('Should return the value of the key', () => {
94
- const hashTableLinearProbing = new HashTableLinearProbing({
95
- first: 1,
96
- second: 2,
97
- });
98
- const returned = hashTableLinearProbing.get('second');
99
-
100
- expect(returned).toBe(2);
101
- });
102
-
103
- it('Should return the correct value in case of collisions', () => {
104
- const hashTableLinearProbing = new HashTableLinearProbing({
105
- first: 1,
106
- tsrif: -1,
107
- });
108
- const returned = hashTableLinearProbing.get('tsrif');
109
-
110
- expect(returned).toBe(-1);
111
- });
112
-
113
- it('Should return null when the key is not in the hash table linear probing', () => {
114
- const hashTableLinearProbing = new HashTableLinearProbing({
115
- first: 1,
116
- second: 2,
117
- });
118
- const returned = hashTableLinearProbing.get('third');
119
-
120
- expect(returned).toBeNull();
121
- });
122
-
123
- it('Should return null when the key is not in a full hash table linear probing', () => {
124
- const hashTableLinearProbing = new HashTableLinearProbing({
125
- first: 1,
126
- second: 2,
127
- }, {
128
- maxSize: 2,
129
- });
130
- const returned = hashTableLinearProbing.get('third');
131
-
132
- expect(returned).toBeNull();
133
- });
134
-
135
- it('Should return null for any key in empty hash table linear probing', () => {
136
- const hashTableLinearProbing = new HashTableLinearProbing();
137
- const returned = hashTableLinearProbing.get('first');
138
-
139
- expect(returned).toBeNull();
140
- });
141
- });
142
-
143
- describe('.remove()', () => {
144
- it('Should remove an element', () => {
145
- const hashTableLinearProbing = new HashTableLinearProbing({
146
- first: 1,
147
- second: 2,
148
- });
149
- hashTableLinearProbing.remove('second');
150
-
151
- expect(hashTableLinearProbing.data).toEqual({
152
- 52: new HashTableLinearProbingElement('first', 1),
153
- });
154
- expect(hashTableLinearProbing.size).toBe(1);
155
- });
156
-
157
- it('Should return the removed value', () => {
158
- const hashTableLinearProbing = new HashTableLinearProbing({
159
- first: 1,
160
- second: 2,
161
- });
162
- const returned = hashTableLinearProbing.remove('second');
163
-
164
- expect(returned).toBe(2);
165
- });
166
-
167
- it('Should change nothing if the key is not in the hash table linear probing', () => {
168
- const hashTableLinearProbing = new HashTableLinearProbing({
169
- first: 1,
170
- second: 2,
171
- });
172
- hashTableLinearProbing.remove('third');
173
-
174
- expect(hashTableLinearProbing.data).toEqual({
175
- 36: new HashTableLinearProbingElement('second', 2),
176
- 52: new HashTableLinearProbingElement('first', 1),
177
- });
178
- expect(hashTableLinearProbing.size).toBe(2);
179
- });
180
-
181
- it('Should return null if the key is not in the hash table linear probing', () => {
182
- const hashTableLinearProbing = new HashTableLinearProbing({
183
- first: 1,
184
- second: 2,
185
- });
186
- const returned = hashTableLinearProbing.remove('third');
187
-
188
- expect(returned).toBeNull();
189
- });
190
-
191
- it('Should move all the subsequent elements that have the same hash code to the left', () => {
192
- const hashTableLinearProbing = new HashTableLinearProbing({
193
- first: 1,
194
- tsrif: -1,
195
- irstf: -2,
196
- srift: -3,
197
- });
198
- hashTableLinearProbing.remove('tsrif');
199
-
200
- expect(hashTableLinearProbing.data).toEqual({
201
- 52: new HashTableLinearProbingElement('first', 1),
202
- 53: new HashTableLinearProbingElement('irstf', -2),
203
- 54: new HashTableLinearProbingElement('srift', -3),
204
- });
205
- expect(hashTableLinearProbing.size).toBe(3);
206
- });
207
-
208
- it('Should move all the subsequent elements that have the same hash code to the left when remove the first position', () => {
209
- const hashTableLinearProbing = new HashTableLinearProbing({
210
- first: 1,
211
- tsrif: 0,
212
- irstf: -1,
213
- }, {
214
- maxSize: 3,
215
- });
216
- hashTableLinearProbing.remove('first');
217
-
218
- expect(hashTableLinearProbing.data).toEqual({
219
- 0: new HashTableLinearProbingElement('tsrif', 0),
220
- 1: new HashTableLinearProbingElement('irstf', -1),
221
- });
222
- expect(hashTableLinearProbing.size).toBe(2);
223
- });
224
-
225
- it('Should move all the subsequent elements that have the same hash code to the left when remove a middle position', () => {
226
- const hashTableLinearProbing = new HashTableLinearProbing({
227
- third: 3,
228
- driht: -3,
229
- rihtd: 0,
230
- }, {
231
- maxSize: 3,
232
- });
233
- hashTableLinearProbing.remove('third');
234
-
235
- expect(hashTableLinearProbing.data).toEqual({
236
- 0: new HashTableLinearProbingElement('rihtd', 0),
237
- 2: new HashTableLinearProbingElement('driht', -3),
238
- });
239
- expect(hashTableLinearProbing.size).toBe(2);
240
- });
241
- });
242
-
243
- describe('Conversion to primitive', () => {
244
- it('Should return the elements separated by comma in string conversion', () => {
245
- const hashTableLinearProbing = new HashTableLinearProbing({
246
- first: 1,
247
- second: 2,
248
- third: 3,
249
- });
250
- const string = String(hashTableLinearProbing);
251
-
252
- expect(string).toBe('[ { second => 2 }, { third => 3 }, { first => 1 } ]');
253
- });
254
-
255
- it('Should return the size in number conversion', () => {
256
- const hashTableLinearProbing = new HashTableLinearProbing({
257
- first: 1,
258
- second: 2,
259
- third: 3,
260
- });
261
- const number = Number(hashTableLinearProbing);
262
-
263
- expect(number).toBe(3);
264
- });
265
- });
266
- });
@@ -1,128 +0,0 @@
1
- import HashTableLinearProbingElement from './HashTableLinearProbingElement';
2
- import IHashTableLinearProbing from './IHashTableLinearProbing';
3
- import IHashTableLinearProbingInputs from './IHashTableLinearProbingInputs';
4
- import IHashTableLinearProbingOptions from './IHashTableLinearProbingOptions';
5
-
6
- export default class HashTableLinearProbing<T = number> implements IHashTableLinearProbing<T> {
7
- private _maxSize: number = 0;
8
- private _data: HashTableLinearProbingElement<T>[] = [];
9
-
10
- constructor(
11
- inputs: IHashTableLinearProbingInputs<T> = {},
12
- {maxSize = 100}: IHashTableLinearProbingOptions = {maxSize: 100},
13
- ) {
14
- this._maxSize = maxSize;
15
-
16
- for (const [key, value] of Object.entries(inputs)) {
17
- this.put(key, value);
18
- }
19
- }
20
-
21
- get data(): { [index: number]: HashTableLinearProbingElement<T>; } {
22
- return this._data
23
- .map((value, index) => ({index, value}))
24
- .reduce((accumulator, current) => ({
25
- ...accumulator,
26
- [current.index]: current.value,
27
- }), {});
28
- }
29
-
30
- get size(): number {
31
- return Reflect.ownKeys(this.data).length;
32
- }
33
-
34
- public put(key: string, value: T): T | null {
35
- if (this.size === this._maxSize) {
36
- return null;
37
- }
38
-
39
- const element = new HashTableLinearProbingElement(key, value);
40
- let position = this._hashCode(key);
41
-
42
- if (this._data[position]) {
43
- do {
44
- position = this._nextPositionOf(position);
45
- } while (this._data[position]);
46
- }
47
-
48
- this._data[position] = element;
49
-
50
- return value;
51
- }
52
-
53
- public get(key: string): T | null {
54
- const position = this._hashCode(key);
55
- let i = position;
56
-
57
- do {
58
- if (this._data[i]?.key === key) {
59
- return this._data[i].value;
60
- }
61
-
62
- i = this._nextPositionOf(i);
63
- } while (this._data[i] && i !== position);
64
-
65
- return null;
66
- }
67
-
68
- public remove(key: string): T | null {
69
- if (this.get(key)) {
70
- let position = this._hashCode(key);
71
-
72
- while (this._data[position].key !== key) {
73
- position = this._nextPositionOf(position);
74
- }
75
-
76
- const {value} = this._data[position];
77
- Reflect.deleteProperty(this._data, position);
78
-
79
- let i: number;
80
- for (
81
- i = position;
82
- this._data[this._nextPositionOf(i)]
83
- && this._hashCode(this._data[this._nextPositionOf(i)].key) === this._hashCode(key)
84
- && this._nextPositionOf(i) !== position;
85
- i = this._nextPositionOf(i)
86
- ) {
87
- this._data[i] = this._data[this._nextPositionOf(i)];
88
- }
89
-
90
- if (i !== position) {
91
- Reflect.deleteProperty(this._data, i);
92
- }
93
-
94
- return value;
95
- }
96
-
97
- return null;
98
- }
99
-
100
- private _hashCode(key: string): number {
101
- const code = key
102
- .split('')
103
- .map(character => character.charCodeAt(0))
104
- .reduce((previous, current) => previous + current, 0);
105
-
106
- return code % this._maxSize;
107
- }
108
-
109
- private _nextPositionOf(position: number) {
110
- return (position + 1) % this._maxSize;
111
- }
112
-
113
- private _previousPositionOf(position: number) {
114
- return (position + this.size - 1) % this._maxSize;
115
- }
116
-
117
- private [Symbol.toPrimitive](type: string): string | number | null {
118
- if (type === 'string') {
119
- return `[ ${Object.values(this.data).join(', ')} ]`;
120
- }
121
-
122
- if (type === 'number') {
123
- return this.size;
124
- }
125
-
126
- return null;
127
- }
128
- }
@@ -1,21 +0,0 @@
1
- /* eslint-disable no-useless-constructor */
2
- /* eslint-disable no-unused-vars */
3
-
4
- export default class HashTableSeparateChainingElement<T> {
5
- constructor(
6
- public key: string,
7
- public value: T,
8
- ) {}
9
-
10
- private[Symbol.toPrimitive](type: string): string | number | null {
11
- if (type === 'string') {
12
- return `{ ${this.key} => ${this.value}}`;
13
- }
14
-
15
- if (type === 'number') {
16
- return 2;
17
- }
18
-
19
- return null;
20
- }
21
- }
@@ -1,12 +0,0 @@
1
- import HashTableSeparateChainingElement from './HashTableSeparateChainingElement';
2
- import IHashTableSeparateChainingData from './IHashTableSeparateChainingData';
3
-
4
- interface IHashTableSeparateChaining<T> {
5
- get data(): IHashTableSeparateChainingData<T>;
6
- get size(): number;
7
- put(_key: string, _value: T): T;
8
- get(_key: string): HashTableSeparateChainingElement<T> | null;
9
- remove(_key: string): HashTableSeparateChainingElement<T> | null;
10
- }
11
-
12
- export default IHashTableSeparateChaining;
@@ -1,7 +0,0 @@
1
- import LinkedList from '../LinkedList';
2
-
3
- interface IHashTableSeparateChainingData<T> {
4
- [key: number]: LinkedList<T>;
5
- }
6
-
7
- export default IHashTableSeparateChainingData;
@@ -1,5 +0,0 @@
1
- interface IHashTableSeparateChainingInputs<T> {
2
- [key: string]: T;
3
- }
4
-
5
- export default IHashTableSeparateChainingInputs;
@@ -1,5 +0,0 @@
1
- interface IHashTableSeparateChainingOptions {
2
- maxSize: number;
3
- }
4
-
5
- export default IHashTableSeparateChainingOptions;