data-structure-typed 1.35.0 → 1.40.0-rc
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/ci.yml +3 -0
- package/CHANGELOG.md +3 -1
- package/CONTRIBUTING.md +18 -0
- package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +527 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +323 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
- package/dist/data-structures/binary-tree/avl-tree.js +90 -3
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -0
- package/dist/data-structures/binary-tree/binary-tree.js +21 -0
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +133 -0
- package/dist/data-structures/binary-tree/bst.js +114 -0
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/index.d.ts +12 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
- package/dist/data-structures/binary-tree/segment-tree.js +45 -0
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
- package/dist/data-structures/graph/abstract-graph.js +269 -4
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.d.ts +200 -0
- package/dist/data-structures/graph/directed-graph.js +167 -0
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +54 -0
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
- package/dist/data-structures/graph/undirected-graph.js +105 -0
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
- package/dist/data-structures/hash/coordinate-map.js +35 -0
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
- package/dist/data-structures/hash/coordinate-set.js +28 -0
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +56 -0
- package/dist/data-structures/hash/hash-map.js +29 -1
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +106 -0
- package/dist/data-structures/hash/hash-table.js +88 -6
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/index.d.ts +7 -0
- package/dist/data-structures/hash/pair.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/heap/heap.d.ts +83 -0
- package/dist/data-structures/heap/heap.js +62 -0
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/max-heap.d.ts +23 -0
- package/dist/data-structures/heap/max-heap.js +16 -0
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.d.ts +24 -0
- package/dist/data-structures/heap/min-heap.js +17 -0
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +15 -0
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
- package/dist/data-structures/matrix/matrix2d.js +91 -2
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.d.ts +201 -0
- package/dist/data-structures/matrix/vector2d.js +188 -1
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +18 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +19 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.d.ts +180 -0
- package/dist/data-structures/priority-queue/priority-queue.js +141 -0
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.d.ts +165 -0
- package/dist/data-structures/queue/deque.js +124 -0
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/queue.d.ts +107 -0
- package/dist/data-structures/queue/queue.js +80 -0
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/stack.d.ts +63 -0
- package/dist/data-structures/stack/stack.js +50 -0
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +1 -0
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/trie.d.ts +61 -0
- package/dist/data-structures/trie/trie.js +36 -0
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
- package/dist/interfaces/abstract-graph.d.ts +5 -0
- package/dist/interfaces/avl-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +6 -0
- package/dist/interfaces/directed-graph.d.ts +3 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/rb-tree.d.ts +6 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/tree-multiset.d.ts +6 -0
- package/dist/interfaces/undirected-graph.d.ts +3 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +6 -0
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/hash.d.ts +1 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/index.d.ts +16 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/utils.d.ts +19 -0
- package/package.json +11 -7
- package/test/integration/avl-tree.test.ts +4 -4
- package/test/integration/bst.test.ts +8 -8
- package/test/unit/data-structures/graph/directed-graph.test.ts +5 -5
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/graph/index.ts +0 -2
- package/test/unit/data-structures/linked-list/index.ts +0 -4
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
4
11
|
class SinglyLinkedListNode {
|
|
12
|
+
/**
|
|
13
|
+
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
14
|
+
* @param {E} val - The "val" parameter is of type E, which means it can be any data type. It represents the value that
|
|
15
|
+
* will be stored in the node of a linked list.
|
|
16
|
+
*/
|
|
5
17
|
constructor(val) {
|
|
6
18
|
this._val = val;
|
|
7
19
|
this._next = null;
|
|
@@ -21,6 +33,9 @@ class SinglyLinkedListNode {
|
|
|
21
33
|
}
|
|
22
34
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
23
35
|
class SinglyLinkedList {
|
|
36
|
+
/**
|
|
37
|
+
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
38
|
+
*/
|
|
24
39
|
constructor() {
|
|
25
40
|
this._head = null;
|
|
26
41
|
this._tail = null;
|
|
@@ -41,6 +56,12 @@ class SinglyLinkedList {
|
|
|
41
56
|
get length() {
|
|
42
57
|
return this._length;
|
|
43
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
61
|
+
* array.
|
|
62
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
63
|
+
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
64
|
+
*/
|
|
44
65
|
static fromArray(data) {
|
|
45
66
|
const singlyLinkedList = new SinglyLinkedList();
|
|
46
67
|
for (const item of data) {
|
|
@@ -51,6 +72,11 @@ class SinglyLinkedList {
|
|
|
51
72
|
getLength() {
|
|
52
73
|
return this._length;
|
|
53
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* The `push` function adds a new node with the given data to the end of a singly linked list.
|
|
77
|
+
* @param {E} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
|
|
78
|
+
* any type (E) as specified in the generic type declaration of the class or function.
|
|
79
|
+
*/
|
|
54
80
|
push(data) {
|
|
55
81
|
const newNode = new SinglyLinkedListNode(data);
|
|
56
82
|
if (!this.head) {
|
|
@@ -63,6 +89,12 @@ class SinglyLinkedList {
|
|
|
63
89
|
}
|
|
64
90
|
this._length++;
|
|
65
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
94
|
+
* pointers accordingly.
|
|
95
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
96
|
+
* the linked list is empty, it returns `null`.
|
|
97
|
+
*/
|
|
66
98
|
pop() {
|
|
67
99
|
if (!this.head)
|
|
68
100
|
return undefined;
|
|
@@ -83,6 +115,10 @@ class SinglyLinkedList {
|
|
|
83
115
|
this._length--;
|
|
84
116
|
return val;
|
|
85
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
120
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
121
|
+
*/
|
|
86
122
|
shift() {
|
|
87
123
|
if (!this.head)
|
|
88
124
|
return undefined;
|
|
@@ -91,6 +127,11 @@ class SinglyLinkedList {
|
|
|
91
127
|
this._length--;
|
|
92
128
|
return removedNode.val;
|
|
93
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
132
|
+
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
133
|
+
* linked list.
|
|
134
|
+
*/
|
|
94
135
|
unshift(val) {
|
|
95
136
|
const newNode = new SinglyLinkedListNode(val);
|
|
96
137
|
if (!this.head) {
|
|
@@ -103,6 +144,13 @@ class SinglyLinkedList {
|
|
|
103
144
|
}
|
|
104
145
|
this._length++;
|
|
105
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
149
|
+
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
150
|
+
* retrieve from the list.
|
|
151
|
+
* @returns The method `getAt(index: number): E | null` returns the value at the specified index in the linked list, or
|
|
152
|
+
* `null` if the index is out of bounds.
|
|
153
|
+
*/
|
|
106
154
|
getAt(index) {
|
|
107
155
|
if (index < 0 || index >= this.length)
|
|
108
156
|
return undefined;
|
|
@@ -112,6 +160,13 @@ class SinglyLinkedList {
|
|
|
112
160
|
}
|
|
113
161
|
return current.val;
|
|
114
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
165
|
+
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
166
|
+
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
|
167
|
+
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<E>` object if the node at the
|
|
168
|
+
* specified index exists, or `null` if the index is out of bounds.
|
|
169
|
+
*/
|
|
115
170
|
getNodeAt(index) {
|
|
116
171
|
let current = this.head;
|
|
117
172
|
for (let i = 0; i < index; i++) {
|
|
@@ -119,6 +174,13 @@ class SinglyLinkedList {
|
|
|
119
174
|
}
|
|
120
175
|
return current;
|
|
121
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
179
|
+
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
180
|
+
* data structure. It is of type number.
|
|
181
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
|
182
|
+
* bounds.
|
|
183
|
+
*/
|
|
122
184
|
deleteAt(index) {
|
|
123
185
|
if (index < 0 || index >= this.length)
|
|
124
186
|
return undefined;
|
|
@@ -132,6 +194,13 @@ class SinglyLinkedList {
|
|
|
132
194
|
this._length--;
|
|
133
195
|
return removedNode.val;
|
|
134
196
|
}
|
|
197
|
+
/**
|
|
198
|
+
* The delete function removes a node with a specific value from a singly linked list.
|
|
199
|
+
* @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
|
|
200
|
+
* or a `SinglyLinkedListNode<E>` object.
|
|
201
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
202
|
+
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
203
|
+
*/
|
|
135
204
|
delete(valueOrNode) {
|
|
136
205
|
let value;
|
|
137
206
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
@@ -163,6 +232,15 @@ class SinglyLinkedList {
|
|
|
163
232
|
}
|
|
164
233
|
return false;
|
|
165
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
237
|
+
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
238
|
+
* linked list. It is of type number.
|
|
239
|
+
* @param {E} val - The `val` parameter represents the value that you want to insert into the linked list at the
|
|
240
|
+
* specified index.
|
|
241
|
+
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
242
|
+
* if the index is out of bounds.
|
|
243
|
+
*/
|
|
166
244
|
insertAt(index, val) {
|
|
167
245
|
if (index < 0 || index > this.length)
|
|
168
246
|
return false;
|
|
@@ -181,14 +259,26 @@ class SinglyLinkedList {
|
|
|
181
259
|
this._length++;
|
|
182
260
|
return true;
|
|
183
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
264
|
+
* whether it is empty or not.
|
|
265
|
+
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
266
|
+
*/
|
|
184
267
|
isEmpty() {
|
|
185
268
|
return this.length === 0;
|
|
186
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
|
272
|
+
*/
|
|
187
273
|
clear() {
|
|
188
274
|
this._head = null;
|
|
189
275
|
this._tail = null;
|
|
190
276
|
this._length = 0;
|
|
191
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* The `toArray` function converts a linked list into an array.
|
|
280
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
281
|
+
*/
|
|
192
282
|
toArray() {
|
|
193
283
|
const array = [];
|
|
194
284
|
let current = this.head;
|
|
@@ -198,6 +288,10 @@ class SinglyLinkedList {
|
|
|
198
288
|
}
|
|
199
289
|
return array;
|
|
200
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
293
|
+
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
294
|
+
*/
|
|
201
295
|
reverse() {
|
|
202
296
|
if (!this.head || this.head === this.tail)
|
|
203
297
|
return;
|
|
@@ -212,6 +306,13 @@ class SinglyLinkedList {
|
|
|
212
306
|
}
|
|
213
307
|
[this.head, this.tail] = [this.tail, this.head];
|
|
214
308
|
}
|
|
309
|
+
/**
|
|
310
|
+
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
311
|
+
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
312
|
+
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
313
|
+
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
314
|
+
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
315
|
+
*/
|
|
215
316
|
find(callback) {
|
|
216
317
|
let current = this.head;
|
|
217
318
|
while (current) {
|
|
@@ -222,6 +323,12 @@ class SinglyLinkedList {
|
|
|
222
323
|
}
|
|
223
324
|
return null;
|
|
224
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
328
|
+
* @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
329
|
+
* @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
|
|
330
|
+
* value is not found, it returns -1.
|
|
331
|
+
*/
|
|
225
332
|
indexOf(value) {
|
|
226
333
|
let index = 0;
|
|
227
334
|
let current = this.head;
|
|
@@ -234,6 +341,13 @@ class SinglyLinkedList {
|
|
|
234
341
|
}
|
|
235
342
|
return -1;
|
|
236
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
346
|
+
* null.
|
|
347
|
+
* @param {E} value - The value parameter is the value that we want to search for in the linked list.
|
|
348
|
+
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
349
|
+
* the specified value is found, the function returns `null`.
|
|
350
|
+
*/
|
|
237
351
|
findNode(value) {
|
|
238
352
|
let current = this.head;
|
|
239
353
|
while (current) {
|
|
@@ -244,6 +358,14 @@ class SinglyLinkedList {
|
|
|
244
358
|
}
|
|
245
359
|
return null;
|
|
246
360
|
}
|
|
361
|
+
/**
|
|
362
|
+
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
|
|
363
|
+
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
364
|
+
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
365
|
+
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
|
366
|
+
* @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
367
|
+
* inserted before the existing value, and `false` otherwise.
|
|
368
|
+
*/
|
|
247
369
|
insertBefore(existingValueOrNode, newValue) {
|
|
248
370
|
if (!this.head)
|
|
249
371
|
return false;
|
|
@@ -271,6 +393,14 @@ class SinglyLinkedList {
|
|
|
271
393
|
}
|
|
272
394
|
return false;
|
|
273
395
|
}
|
|
396
|
+
/**
|
|
397
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
398
|
+
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
399
|
+
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
400
|
+
* @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
401
|
+
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
402
|
+
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
403
|
+
*/
|
|
274
404
|
insertAfter(existingValueOrNode, newValue) {
|
|
275
405
|
let existingNode;
|
|
276
406
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
@@ -291,6 +421,11 @@ class SinglyLinkedList {
|
|
|
291
421
|
}
|
|
292
422
|
return false;
|
|
293
423
|
}
|
|
424
|
+
/**
|
|
425
|
+
* The function counts the number of occurrences of a given value in a linked list.
|
|
426
|
+
* @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
427
|
+
* @returns The count of occurrences of the given value in the linked list.
|
|
428
|
+
*/
|
|
294
429
|
countOccurrences(value) {
|
|
295
430
|
let count = 0;
|
|
296
431
|
let current = this.head;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"singly-linked-list.js","sourceRoot":"","sources":["../../../src/data-structures/linked-list/singly-linked-list.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"singly-linked-list.js","sourceRoot":"","sources":["../../../src/data-structures/linked-list/singly-linked-list.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,MAAa,oBAAoB;IAC/B;;;;OAIG;IACH,YAAY,GAAM;QAChB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAID,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,GAAG,CAAC,KAAQ;QACd,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IACpB,CAAC;IAID,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI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AC,CAAC;YACnD,OAAO,CAAC,IAAI,GAAG,YAAY,CAAC,IAAI,CAAC;YACjC,YAAY,CAAC,IAAI,GAAG,OAAO,CAAC;YAC5B,IAAI,YAAY,KAAK,IAAI,CAAC,IAAI,EAAE;gBAC9B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;aACrB;YACD,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,OAAO,IAAI,CAAC;SACb;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;OAIG;IACH,gBAAgB,CAAC,KAAQ;QACvB,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,IAAI,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC;QAExB,OAAO,OAAO,EAAE;YACd,IAAI,OAAO,CAAC,GAAG,KAAK,KAAK,EAAE;gBACzB,KAAK,EAAE,CAAC;aACT;YACD,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;SACxB;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC;QAChB,IAAI,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC;QAExB,OAAO,OAAO,EAAE;YACd,MAAM,OAAO,CAAC,GAAG,CAAC;YAClB,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;SACxB;IACH,CAAC;CACF;AA7cD,4CA6cC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
export declare class SkipListNode<K, V> {
|
|
9
|
+
key: K;
|
|
10
|
+
value: V;
|
|
11
|
+
forward: SkipListNode<K, V>[];
|
|
12
|
+
constructor(key: K, value: V, level: number);
|
|
13
|
+
}
|
|
14
|
+
export declare class SkipList<K, V> {
|
|
15
|
+
get probability(): number;
|
|
16
|
+
set probability(value: number);
|
|
17
|
+
get maxLevel(): number;
|
|
18
|
+
set maxLevel(value: number);
|
|
19
|
+
get level(): number;
|
|
20
|
+
set level(value: number);
|
|
21
|
+
get head(): SkipListNode<K, V>;
|
|
22
|
+
set head(value: SkipListNode<K, V>);
|
|
23
|
+
private _head;
|
|
24
|
+
private _level;
|
|
25
|
+
private _maxLevel;
|
|
26
|
+
private _probability;
|
|
27
|
+
/**
|
|
28
|
+
* The constructor initializes a SkipList with a specified maximum level and probability.
|
|
29
|
+
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
|
|
30
|
+
* the maximum number of levels that can be created in the skip list.
|
|
31
|
+
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher
|
|
32
|
+
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
33
|
+
*/
|
|
34
|
+
constructor(maxLevel?: number, probability?: number);
|
|
35
|
+
/**
|
|
36
|
+
* The function "randomLevel" generates a random level based on a given probability and maximum level.
|
|
37
|
+
* @returns the level, which is a number.
|
|
38
|
+
*/
|
|
39
|
+
private randomLevel;
|
|
40
|
+
/**
|
|
41
|
+
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
42
|
+
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
43
|
+
* @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
|
|
44
|
+
* List.
|
|
45
|
+
*/
|
|
46
|
+
add(key: K, value: V): void;
|
|
47
|
+
/**
|
|
48
|
+
* The function `get` retrieves the value associated with a given key from a skip list data structure.
|
|
49
|
+
* @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
|
|
50
|
+
* @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
|
|
51
|
+
* otherwise it returns `undefined`.
|
|
52
|
+
*/
|
|
53
|
+
get(key: K): V | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* The `remove` function removes a node with a specific key from a Skip List data structure.
|
|
56
|
+
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
57
|
+
* @returns The `remove` method returns a boolean value. It returns `true` if the key was successfully removed from the
|
|
58
|
+
* skip list, and `false` if the key was not found in the skip list.
|
|
59
|
+
*/
|
|
60
|
+
remove(key: K): boolean;
|
|
61
|
+
}
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.SkipList = exports.SkipListNode = void 0;
|
|
4
11
|
class SkipListNode {
|
|
@@ -34,12 +41,23 @@ class SkipList {
|
|
|
34
41
|
set head(value) {
|
|
35
42
|
this._head = value;
|
|
36
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* The constructor initializes a SkipList with a specified maximum level and probability.
|
|
46
|
+
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
|
|
47
|
+
* the maximum number of levels that can be created in the skip list.
|
|
48
|
+
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher
|
|
49
|
+
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
50
|
+
*/
|
|
37
51
|
constructor(maxLevel = 16, probability = 0.5) {
|
|
38
52
|
this._head = new SkipListNode(null, null, maxLevel);
|
|
39
53
|
this._level = 0;
|
|
40
54
|
this._maxLevel = maxLevel;
|
|
41
55
|
this._probability = probability;
|
|
42
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* The function "randomLevel" generates a random level based on a given probability and maximum level.
|
|
59
|
+
* @returns the level, which is a number.
|
|
60
|
+
*/
|
|
43
61
|
randomLevel() {
|
|
44
62
|
let level = 1;
|
|
45
63
|
while (Math.random() < this.probability && level < this.maxLevel) {
|
|
@@ -47,6 +65,12 @@ class SkipList {
|
|
|
47
65
|
}
|
|
48
66
|
return level;
|
|
49
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
70
|
+
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
71
|
+
* @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
|
|
72
|
+
* List.
|
|
73
|
+
*/
|
|
50
74
|
add(key, value) {
|
|
51
75
|
const newNode = new SkipListNode(key, value, this.randomLevel());
|
|
52
76
|
const update = new Array(this.maxLevel).fill(this.head);
|
|
@@ -65,6 +89,12 @@ class SkipList {
|
|
|
65
89
|
this.level = Math.max(this.level, newNode.forward.length);
|
|
66
90
|
}
|
|
67
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* The function `get` retrieves the value associated with a given key from a skip list data structure.
|
|
94
|
+
* @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
|
|
95
|
+
* @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
|
|
96
|
+
* otherwise it returns `undefined`.
|
|
97
|
+
*/
|
|
68
98
|
get(key) {
|
|
69
99
|
let current = this.head;
|
|
70
100
|
for (let i = this.level - 1; i >= 0; i--) {
|
|
@@ -78,6 +108,12 @@ class SkipList {
|
|
|
78
108
|
}
|
|
79
109
|
return undefined;
|
|
80
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* The `remove` function removes a node with a specific key from a Skip List data structure.
|
|
113
|
+
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
114
|
+
* @returns The `remove` method returns a boolean value. It returns `true` if the key was successfully removed from the
|
|
115
|
+
* skip list, and `false` if the key was not found in the skip list.
|
|
116
|
+
*/
|
|
81
117
|
remove(key) {
|
|
82
118
|
const update = new Array(this.maxLevel).fill(this.head);
|
|
83
119
|
let current = this.head;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"skip-linked-list.js","sourceRoot":"","sources":["../../../src/data-structures/linked-list/skip-linked-list.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"skip-linked-list.js","sourceRoot":"","sources":["../../../src/data-structures/linked-list/skip-linked-list.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,MAAa,YAAY;IAKvB,YAAY,GAAM,EAAE,KAAQ,EAAE,KAAa;QACzC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,OAAO,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC;CACF;AAVD,oCAUC;AAED,MAAa,QAAQ;IACnB,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED,IAAI,WAAW,CAAC,KAAa;QAC3B,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;IAC5B,CAAC;IACD,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,IAAI,QAAQ,CAAC,KAAa;QACxB,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;IACzB,CAAC;IACD,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,KAAK,CAAC,KAAa;QACrB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IACD,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI,IAAI,CAAC,KAAyB;QAChC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAMD;;;;;;OAMG;IACH,YAAY,QAAQ,GAAG,EAAE,EAAE,WAAW,GAAG,GAAG;QAC1C,IAAI,CAAC,KAAK,GAAG,IAAI,YAAY,CAAO,IAAW,EAAE,IAAW,EAAE,QAAQ,CAAC,CAAC;QACxE,IAAI,CAAC,MA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C,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,EAAE;gBACzD,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;aAC9B;YACD,MAAM,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;SACrB;QAED,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QAE7B,IAAI,OAAO,IAAI,OAAO,CAAC,GAAG,KAAK,GAAG,EAAE;YAClC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE;gBACnC,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,EAAE;oBACpC,MAAM;iBACP;gBACD,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;aAC3C;YACD,OAAO,IAAI,CAAC,KAAK,GAAG,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,IAAI,EAAE;gBACnE,IAAI,CAAC,KAAK,EAAE,CAAC;aACd;YACD,OAAO,IAAI,CAAC;SACb;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAjJD,4BAiJC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
export declare class MatrixNTI2D<V = any> {
|
|
9
|
+
private readonly _matrix;
|
|
10
|
+
/**
|
|
11
|
+
* The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
|
|
12
|
+
* given initial value or 0 if not provided.
|
|
13
|
+
* @param options - An object containing the following properties:
|
|
14
|
+
*/
|
|
15
|
+
constructor(options: {
|
|
16
|
+
row: number;
|
|
17
|
+
col: number;
|
|
18
|
+
initialVal?: V;
|
|
19
|
+
});
|
|
20
|
+
toArray(): Array<Array<V>>;
|
|
21
|
+
}
|
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MatrixNTI2D = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
11
|
+
// todo need to be improved
|
|
4
12
|
class MatrixNTI2D {
|
|
13
|
+
/**
|
|
14
|
+
* The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
|
|
15
|
+
* given initial value or 0 if not provided.
|
|
16
|
+
* @param options - An object containing the following properties:
|
|
17
|
+
*/
|
|
5
18
|
constructor(options) {
|
|
6
19
|
const { row, col, initialVal } = options;
|
|
7
20
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
8
21
|
}
|
|
22
|
+
/* The `toArray` method returns the matrix as a two-dimensional array. It converts the internal representation of the
|
|
23
|
+
matrix, which is an array of arrays, into a format that is more commonly used in JavaScript. */
|
|
9
24
|
toArray() {
|
|
10
25
|
return this._matrix;
|
|
11
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matrix.js","sourceRoot":"","sources":["../../../src/data-structures/matrix/matrix.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"matrix.js","sourceRoot":"","sources":["../../../src/data-structures/matrix/matrix.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,2BAA2B;AAC3B,MAAa,WAAW;IAGtB;;;;OAIG;IACH,YAAY,OAAmD;QAC7D,MAAM,EAAC,GAAG,EAAE,GAAG,EAAE,UAAU,EAAC,GAAG,OAAO,CAAC;QACvC,IAAI,CAAC,OAAO,GAAG,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC,CAAC,CAAC;IAChG,CAAC;IAED;mGAC+F;IAC/F,OAAO;QACL,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF;AAlBD,kCAkBC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import Vector2D from './vector2d';
|
|
9
|
+
export declare class Matrix2D {
|
|
10
|
+
private readonly _matrix;
|
|
11
|
+
/**
|
|
12
|
+
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
13
|
+
* or Vector2D object.
|
|
14
|
+
* @param {number[][] | Vector2D} [value] - The `value` parameter can be either a 2D array of numbers (`number[][]`) or
|
|
15
|
+
* an instance of the `Vector2D` class.
|
|
16
|
+
*/
|
|
17
|
+
constructor(value?: number[][] | Vector2D);
|
|
18
|
+
/**
|
|
19
|
+
* The function returns a 2D array with three empty arrays.
|
|
20
|
+
* @returns An empty 2-dimensional array with 3 empty arrays inside.
|
|
21
|
+
*/
|
|
22
|
+
static get empty(): number[][];
|
|
23
|
+
/**
|
|
24
|
+
* The above function returns a 3x3 identity matrix.
|
|
25
|
+
* @returns The method is returning a 2-dimensional array of numbers representing the identity matrix.
|
|
26
|
+
*/
|
|
27
|
+
static get identity(): number[][];
|
|
28
|
+
/**
|
|
29
|
+
* The function returns a two-dimensional array of numbers.
|
|
30
|
+
* @returns The getter method is returning the value of the private variable `_matrix`, which is a two-dimensional
|
|
31
|
+
* array of numbers.
|
|
32
|
+
*/
|
|
33
|
+
get m(): number[][];
|
|
34
|
+
/**
|
|
35
|
+
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
36
|
+
* _matrix array.
|
|
37
|
+
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
38
|
+
* the first column of the matrix.
|
|
39
|
+
*/
|
|
40
|
+
toVector(): Vector2D;
|
|
41
|
+
/**
|
|
42
|
+
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
43
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
44
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
|
|
45
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
46
|
+
*/
|
|
47
|
+
static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
48
|
+
/**
|
|
49
|
+
* The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
|
|
50
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
|
|
51
|
+
* @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
|
|
52
|
+
* representing the matrix elements.
|
|
53
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
54
|
+
*/
|
|
55
|
+
static subtract(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
56
|
+
/**
|
|
57
|
+
* The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
|
|
58
|
+
* @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
|
|
59
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
|
|
60
|
+
* @returns a new instance of the Matrix2D class, created using the result array.
|
|
61
|
+
*/
|
|
62
|
+
static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
63
|
+
/**
|
|
64
|
+
* The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
|
|
65
|
+
* @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
|
|
66
|
+
* matrix. It contains a property `m` that is a 2D array representing the matrix elements.
|
|
67
|
+
* @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
|
|
68
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
69
|
+
*/
|
|
70
|
+
static multiplyByValue(matrix: Matrix2D, value: number): Matrix2D;
|
|
71
|
+
/**
|
|
72
|
+
* The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
|
|
73
|
+
* @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
|
|
74
|
+
* @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
|
|
75
|
+
* @returns a Vector2D.
|
|
76
|
+
*/
|
|
77
|
+
static multiplyByVector(matrix: Matrix2D, vector: Vector2D): Vector2D;
|
|
78
|
+
/**
|
|
79
|
+
* The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
|
|
80
|
+
* @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
|
|
81
|
+
* specifies the width in pixels or any other unit of measurement.
|
|
82
|
+
* @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
|
|
83
|
+
* calculate the centerY value, which is the vertical center of the view.
|
|
84
|
+
* @returns a Matrix2D object.
|
|
85
|
+
*/
|
|
86
|
+
static view(width: number, height: number): Matrix2D;
|
|
87
|
+
/**
|
|
88
|
+
* The function scales a matrix by a given factor.
|
|
89
|
+
* @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
|
|
90
|
+
* should be scaled.
|
|
91
|
+
* @returns the result of multiplying a new instance of Matrix2D by the given factor.
|
|
92
|
+
*/
|
|
93
|
+
static scale(factor: number): Matrix2D;
|
|
94
|
+
/**
|
|
95
|
+
* The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
|
|
96
|
+
* @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
|
|
97
|
+
* @returns The code is returning a new instance of a Matrix2D object.
|
|
98
|
+
*/
|
|
99
|
+
static rotate(radians: number): Matrix2D;
|
|
100
|
+
/**
|
|
101
|
+
* The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
|
|
102
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
|
|
103
|
+
* and y, and an optional w component.
|
|
104
|
+
* @returns The method is returning a new instance of the Matrix2D class.
|
|
105
|
+
*/
|
|
106
|
+
static translate(vector: Vector2D): Matrix2D;
|
|
107
|
+
}
|
|
108
|
+
export default Matrix2D;
|