data-structure-typed 1.15.0 → 1.15.2
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/README.md +378 -7
- package/dist/data-structures/binary-tree/binary-tree.d.ts +30 -30
- package/dist/data-structures/binary-tree/binary-tree.js +55 -55
- package/dist/data-structures/binary-tree/segment-tree.d.ts +17 -17
- package/dist/data-structures/binary-tree/segment-tree.js +30 -30
- package/dist/data-structures/graph/abstract-graph.d.ts +6 -6
- package/dist/data-structures/graph/abstract-graph.js +6 -6
- package/dist/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/data-structures/graph/directed-graph.js +6 -6
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/data-structures/hash/coordinate-map.d.ts +2 -2
- package/dist/data-structures/hash/coordinate-set.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +14 -14
- package/dist/data-structures/heap/heap.js +12 -12
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +9 -9
- package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +7 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -7
- package/dist/data-structures/priority-queue/priority-queue.js +6 -6
- package/dist/data-structures/queue/deque.d.ts +1 -1
- package/dist/utils/types/utils.d.ts +0 -52
- package/dist/utils/types/utils.js +0 -52
- package/dist/utils/utils.d.ts +1 -97
- package/dist/utils/utils.js +1 -547
- package/package.json +3 -4
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/binary-tree.ts +83 -76
- package/src/data-structures/binary-tree/segment-tree.ts +55 -36
- package/src/data-structures/graph/abstract-graph.ts +21 -19
- package/src/data-structures/graph/directed-graph.ts +23 -18
- package/src/data-structures/graph/undirected-graph.ts +16 -11
- package/src/data-structures/hash/coordinate-map.ts +11 -8
- package/src/data-structures/hash/coordinate-set.ts +11 -8
- package/src/data-structures/heap/heap.ts +34 -28
- package/src/data-structures/linked-list/doubly-linked-list.ts +40 -26
- package/src/data-structures/linked-list/singly-linked-list.ts +32 -23
- package/src/data-structures/priority-queue/priority-queue.ts +17 -14
- package/src/data-structures/queue/deque.ts +14 -4
- package/src/utils/types/utils.ts +1 -175
- package/src/utils/utils.ts +1 -484
- package/tests/unit/data-structures/binary-tree/bst.test.ts +40 -31
- package/tests/unit/data-structures/graph/directed-graph.test.ts +31 -34
|
@@ -6,24 +6,27 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export class CoordinateSet extends Set {
|
|
9
|
+
constructor(joint?: string) {
|
|
10
|
+
super();
|
|
11
|
+
if (joint !== undefined) this._joint = joint;
|
|
12
|
+
}
|
|
13
|
+
|
|
9
14
|
protected _joint: string = '_';
|
|
15
|
+
|
|
10
16
|
get joint(): string {
|
|
11
17
|
return this._joint;
|
|
12
18
|
}
|
|
19
|
+
|
|
20
|
+
protected set joint(v: string) {
|
|
21
|
+
this._joint = v;
|
|
22
|
+
}
|
|
23
|
+
|
|
13
24
|
/**
|
|
14
25
|
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
15
26
|
*/
|
|
16
27
|
getJoint(): string {
|
|
17
28
|
return this._joint;
|
|
18
29
|
}
|
|
19
|
-
protected set joint(v: string) {
|
|
20
|
-
this._joint = v;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
constructor(joint?: string) {
|
|
24
|
-
super();
|
|
25
|
-
if (joint !== undefined) this._joint = joint;
|
|
26
|
-
}
|
|
27
30
|
|
|
28
31
|
/**
|
|
29
32
|
* The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
|
|
@@ -9,34 +9,6 @@ import {PriorityQueue} from '../priority-queue';
|
|
|
9
9
|
import type {HeapItem, HeapOptions} from '../types';
|
|
10
10
|
|
|
11
11
|
export abstract class Heap<T> {
|
|
12
|
-
protected abstract _pq: PriorityQueue<HeapItem<T>>;
|
|
13
|
-
get pq() {
|
|
14
|
-
return this._pq;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
18
|
-
*/
|
|
19
|
-
getPq() {
|
|
20
|
-
return this._pq;
|
|
21
|
-
}
|
|
22
|
-
protected set pq(v: PriorityQueue<HeapItem<T>>) {
|
|
23
|
-
this._pq = v;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
protected _priorityCb: (element: T) => number;
|
|
27
|
-
get priorityCb() {
|
|
28
|
-
return this._priorityCb;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
32
|
-
*/
|
|
33
|
-
getPriorityCb() {
|
|
34
|
-
return this._priorityCb;
|
|
35
|
-
}
|
|
36
|
-
protected set priorityCb(v: (element: T) => number) {
|
|
37
|
-
this._priorityCb = v;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
12
|
/**
|
|
41
13
|
* The function is a constructor for a class that initializes a priority callback function based on the
|
|
42
14
|
* options provided.
|
|
@@ -54,6 +26,25 @@ export abstract class Heap<T> {
|
|
|
54
26
|
}
|
|
55
27
|
}
|
|
56
28
|
|
|
29
|
+
protected abstract _pq: PriorityQueue<HeapItem<T>>;
|
|
30
|
+
|
|
31
|
+
get pq() {
|
|
32
|
+
return this._pq;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
protected set pq(v: PriorityQueue<HeapItem<T>>) {
|
|
36
|
+
this._pq = v;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected _priorityCb: (element: T) => number;
|
|
40
|
+
get priorityCb() {
|
|
41
|
+
return this._priorityCb;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected set priorityCb(v: (element: T) => number) {
|
|
45
|
+
this._priorityCb = v;
|
|
46
|
+
}
|
|
47
|
+
|
|
57
48
|
/**
|
|
58
49
|
* The function returns the size of a priority queue.
|
|
59
50
|
* @returns The size of the priority queue.
|
|
@@ -61,6 +52,21 @@ export abstract class Heap<T> {
|
|
|
61
52
|
get size(): number {
|
|
62
53
|
return this._pq.size;
|
|
63
54
|
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
58
|
+
*/
|
|
59
|
+
getPq() {
|
|
60
|
+
return this._pq;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
65
|
+
*/
|
|
66
|
+
getPriorityCb() {
|
|
67
|
+
return this._priorityCb;
|
|
68
|
+
}
|
|
69
|
+
|
|
64
70
|
/**
|
|
65
71
|
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
66
72
|
*/
|
|
@@ -8,48 +8,56 @@
|
|
|
8
8
|
import type {DoublyLinkedListGetBy} from '../types';
|
|
9
9
|
|
|
10
10
|
export class DoublyLinkedListNode<T> {
|
|
11
|
+
constructor(nodeValue: T) {
|
|
12
|
+
this._val = nodeValue;
|
|
13
|
+
this._next = null;
|
|
14
|
+
this._prev = null;
|
|
15
|
+
}
|
|
16
|
+
|
|
11
17
|
protected _val: T;
|
|
18
|
+
|
|
12
19
|
get val(): T {
|
|
13
20
|
return this._val;
|
|
14
21
|
}
|
|
22
|
+
|
|
15
23
|
set val(v: T) {
|
|
16
24
|
this._val = v;
|
|
17
25
|
}
|
|
18
26
|
|
|
19
27
|
protected _next: DoublyLinkedListNode<T> | null;
|
|
28
|
+
|
|
20
29
|
get next(): DoublyLinkedListNode<T> | null {
|
|
21
30
|
return this._next;
|
|
22
31
|
}
|
|
32
|
+
|
|
23
33
|
set next(v: DoublyLinkedListNode<T> | null) {
|
|
24
34
|
this._next = v;
|
|
25
35
|
}
|
|
26
36
|
|
|
27
37
|
protected _prev: DoublyLinkedListNode<T> | null;
|
|
38
|
+
|
|
28
39
|
get prev(): DoublyLinkedListNode<T> | null {
|
|
29
40
|
return this._prev;
|
|
30
41
|
}
|
|
42
|
+
|
|
31
43
|
set prev(v: DoublyLinkedListNode<T> | null) {
|
|
32
44
|
this._prev = v;
|
|
33
45
|
}
|
|
34
|
-
|
|
35
|
-
constructor(nodeValue: T) {
|
|
36
|
-
this._val = nodeValue;
|
|
37
|
-
this._next = null;
|
|
38
|
-
this._prev = null;
|
|
39
|
-
}
|
|
40
46
|
}
|
|
41
47
|
|
|
42
48
|
export class DoublyLinkedList<T> {
|
|
49
|
+
constructor() {
|
|
50
|
+
this._first = null;
|
|
51
|
+
this._last = null;
|
|
52
|
+
this._size = 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
43
55
|
protected _first: DoublyLinkedListNode<T> | null;
|
|
56
|
+
|
|
44
57
|
get first(): DoublyLinkedListNode<T> | null {
|
|
45
58
|
return this._first;
|
|
46
59
|
}
|
|
47
|
-
|
|
48
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
49
|
-
*/
|
|
50
|
-
getFirst(): DoublyLinkedListNode<T> | null {
|
|
51
|
-
return this._first;
|
|
52
|
-
}
|
|
60
|
+
|
|
53
61
|
protected set first(v: DoublyLinkedListNode<T> | null) {
|
|
54
62
|
this._first = v;
|
|
55
63
|
}
|
|
@@ -58,34 +66,40 @@ export class DoublyLinkedList<T> {
|
|
|
58
66
|
get last(): DoublyLinkedListNode<T> | null {
|
|
59
67
|
return this._last;
|
|
60
68
|
}
|
|
61
|
-
|
|
62
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
63
|
-
*/
|
|
64
|
-
getLast(): DoublyLinkedListNode<T> | null {
|
|
65
|
-
return this._last;
|
|
66
|
-
}
|
|
69
|
+
|
|
67
70
|
protected set last(v: DoublyLinkedListNode<T> | null) {
|
|
68
71
|
this._last = v;
|
|
69
72
|
}
|
|
70
73
|
|
|
71
74
|
protected _size: number;
|
|
75
|
+
|
|
72
76
|
get size(): number {
|
|
73
77
|
return this._size;
|
|
74
78
|
}
|
|
79
|
+
|
|
80
|
+
protected set size(v: number) {
|
|
81
|
+
this._size = v;
|
|
82
|
+
}
|
|
83
|
+
|
|
75
84
|
/**
|
|
76
85
|
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
77
86
|
*/
|
|
78
|
-
|
|
79
|
-
return this.
|
|
87
|
+
getFirst(): DoublyLinkedListNode<T> | null {
|
|
88
|
+
return this._first;
|
|
80
89
|
}
|
|
81
|
-
|
|
82
|
-
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
93
|
+
*/
|
|
94
|
+
getLast(): DoublyLinkedListNode<T> | null {
|
|
95
|
+
return this._last;
|
|
83
96
|
}
|
|
84
97
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
100
|
+
*/
|
|
101
|
+
getSize(): number {
|
|
102
|
+
return this._size;
|
|
89
103
|
}
|
|
90
104
|
|
|
91
105
|
/**
|
|
@@ -10,7 +10,15 @@
|
|
|
10
10
|
/* The SinglyLinkedListNode class represents a node in a singly linked list and provides methods for inserting, removing,
|
|
11
11
|
and accessing nodes. */
|
|
12
12
|
export class SinglyLinkedListNode<NodeVal = any> {
|
|
13
|
+
constructor(val: NodeVal, prev?: SinglyLinkedListNode<NodeVal> | null, next?: SinglyLinkedListNode<NodeVal> | null, list?: SinglyLinkedList<NodeVal> | null) {
|
|
14
|
+
this._val = val;
|
|
15
|
+
this._prev = prev || null;
|
|
16
|
+
this._next = next || null;
|
|
17
|
+
this._list = list || null;
|
|
18
|
+
}
|
|
19
|
+
|
|
13
20
|
protected _val: NodeVal;
|
|
21
|
+
|
|
14
22
|
get val(): NodeVal {
|
|
15
23
|
return this._val;
|
|
16
24
|
}
|
|
@@ -20,6 +28,7 @@ export class SinglyLinkedListNode<NodeVal = any> {
|
|
|
20
28
|
}
|
|
21
29
|
|
|
22
30
|
protected _prev: SinglyLinkedListNode<NodeVal> | null;
|
|
31
|
+
|
|
23
32
|
get prev(): SinglyLinkedListNode<NodeVal> | null {
|
|
24
33
|
return this._prev;
|
|
25
34
|
}
|
|
@@ -29,6 +38,7 @@ export class SinglyLinkedListNode<NodeVal = any> {
|
|
|
29
38
|
}
|
|
30
39
|
|
|
31
40
|
protected _next: SinglyLinkedListNode<NodeVal> | null
|
|
41
|
+
|
|
32
42
|
get next(): SinglyLinkedListNode<NodeVal> | null {
|
|
33
43
|
return this._next;
|
|
34
44
|
}
|
|
@@ -38,6 +48,7 @@ export class SinglyLinkedListNode<NodeVal = any> {
|
|
|
38
48
|
}
|
|
39
49
|
|
|
40
50
|
protected _list: SinglyLinkedList<NodeVal> | null
|
|
51
|
+
|
|
41
52
|
get list(): SinglyLinkedList<NodeVal> | null {
|
|
42
53
|
return this._list;
|
|
43
54
|
}
|
|
@@ -46,13 +57,6 @@ export class SinglyLinkedListNode<NodeVal = any> {
|
|
|
46
57
|
this._list = value;
|
|
47
58
|
}
|
|
48
59
|
|
|
49
|
-
constructor(val: NodeVal, prev?: SinglyLinkedListNode<NodeVal> | null, next?: SinglyLinkedListNode<NodeVal> | null, list?: SinglyLinkedList<NodeVal> | null) {
|
|
50
|
-
this._val = val;
|
|
51
|
-
this._prev = prev || null;
|
|
52
|
-
this._next = next || null;
|
|
53
|
-
this._list = list || null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
60
|
get index() {
|
|
57
61
|
if (!this.list) {
|
|
58
62
|
return undefined;
|
|
@@ -97,46 +101,51 @@ export class SinglyLinkedListNode<NodeVal = any> {
|
|
|
97
101
|
|
|
98
102
|
export class SinglyLinkedList<NodeVal = any> {
|
|
99
103
|
|
|
104
|
+
/**
|
|
105
|
+
* The constructor initializes a linked list with the given arguments as nodes.
|
|
106
|
+
* @param {NodeVal[]} args - args is a rest parameter that allows the constructor to accept an arbitrary number of
|
|
107
|
+
* arguments of type NodeVal.
|
|
108
|
+
*/
|
|
109
|
+
constructor(...args: NodeVal[]) {
|
|
110
|
+
this._head = null;
|
|
111
|
+
this._tail = null;
|
|
112
|
+
this._size = 0;
|
|
113
|
+
|
|
114
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
115
|
+
this.append(args[i]);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
100
119
|
protected _head: SinglyLinkedListNode<NodeVal> | null;
|
|
120
|
+
|
|
101
121
|
get head(): SinglyLinkedListNode<NodeVal> | null {
|
|
102
122
|
return this._head;
|
|
103
123
|
}
|
|
124
|
+
|
|
104
125
|
set head(value: SinglyLinkedListNode<NodeVal> | null) {
|
|
105
126
|
this._head = value;
|
|
106
127
|
}
|
|
107
128
|
|
|
108
|
-
|
|
109
129
|
protected _tail: SinglyLinkedListNode<NodeVal> | null;
|
|
130
|
+
|
|
110
131
|
get tail(): SinglyLinkedListNode<NodeVal> | null {
|
|
111
132
|
return this._tail;
|
|
112
133
|
}
|
|
134
|
+
|
|
113
135
|
set tail(value: SinglyLinkedListNode<NodeVal> | null) {
|
|
114
136
|
this._tail = value;
|
|
115
137
|
}
|
|
116
138
|
|
|
117
139
|
protected _size: number;
|
|
140
|
+
|
|
118
141
|
get size(): number {
|
|
119
142
|
return this._size;
|
|
120
143
|
}
|
|
144
|
+
|
|
121
145
|
set size(value: number) {
|
|
122
146
|
this._size = value;
|
|
123
147
|
}
|
|
124
148
|
|
|
125
|
-
/**
|
|
126
|
-
* The constructor initializes a linked list with the given arguments as nodes.
|
|
127
|
-
* @param {NodeVal[]} args - args is a rest parameter that allows the constructor to accept an arbitrary number of
|
|
128
|
-
* arguments of type NodeVal.
|
|
129
|
-
*/
|
|
130
|
-
constructor(...args: NodeVal[]) {
|
|
131
|
-
this._head = null;
|
|
132
|
-
this._tail = null;
|
|
133
|
-
this._size = 0;
|
|
134
|
-
|
|
135
|
-
for (let i = 0; i < arguments.length; i++) {
|
|
136
|
-
this.append(args[i]);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
149
|
/**
|
|
141
150
|
* The `from` function in TypeScript creates a new SinglyLinkedList instance from an iterable object.
|
|
142
151
|
* @param iterable - The `iterable` parameter is an object that can be iterated over, such as an array or a string. It
|
|
@@ -9,20 +9,6 @@ import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueu
|
|
|
9
9
|
|
|
10
10
|
export class PriorityQueue<T = number> {
|
|
11
11
|
|
|
12
|
-
protected _nodes: T[] = [];
|
|
13
|
-
get nodes(): T[] {
|
|
14
|
-
return this._nodes;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
18
|
-
*/
|
|
19
|
-
getNodes(): T[] {
|
|
20
|
-
return this._nodes;
|
|
21
|
-
}
|
|
22
|
-
protected set nodes(value: T[]) {
|
|
23
|
-
this._nodes = value;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
12
|
/**
|
|
27
13
|
* The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
|
|
28
14
|
* function.
|
|
@@ -39,6 +25,16 @@ export class PriorityQueue<T = number> {
|
|
|
39
25
|
}
|
|
40
26
|
}
|
|
41
27
|
|
|
28
|
+
protected _nodes: T[] = [];
|
|
29
|
+
|
|
30
|
+
get nodes(): T[] {
|
|
31
|
+
return this._nodes;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
protected set nodes(value: T[]) {
|
|
35
|
+
this._nodes = value;
|
|
36
|
+
}
|
|
37
|
+
|
|
42
38
|
get size(): number {
|
|
43
39
|
return this.nodes.length;
|
|
44
40
|
}
|
|
@@ -67,6 +63,13 @@ export class PriorityQueue<T = number> {
|
|
|
67
63
|
return new PriorityQueue({...options, isFix: true}).isValid();
|
|
68
64
|
}
|
|
69
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
68
|
+
*/
|
|
69
|
+
getNodes(): T[] {
|
|
70
|
+
return this._nodes;
|
|
71
|
+
}
|
|
72
|
+
|
|
70
73
|
/**
|
|
71
74
|
* The "add" function adds a node to the heap and ensures that the heap property is maintained.
|
|
72
75
|
* @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
|
|
@@ -17,50 +17,60 @@ export class Deque<T> extends DoublyLinkedList<T> {
|
|
|
17
17
|
// O(n) time complexity of adding at the beginning and the end
|
|
18
18
|
// todo tested slowest one
|
|
19
19
|
export class ObjectDeque<T> {
|
|
20
|
+
constructor(capacity?: number) {
|
|
21
|
+
if (capacity !== undefined) this._capacity = capacity;
|
|
22
|
+
}
|
|
23
|
+
|
|
20
24
|
private _nodes: { [key: number]: T } = {};
|
|
25
|
+
|
|
21
26
|
get nodes(): { [p: number]: T } {
|
|
22
27
|
return this._nodes;
|
|
23
28
|
}
|
|
29
|
+
|
|
24
30
|
protected set nodes(value: { [p: number]: T }) {
|
|
25
31
|
this._nodes = value;
|
|
26
32
|
}
|
|
27
33
|
|
|
28
34
|
private _capacity = Number.MAX_SAFE_INTEGER;
|
|
35
|
+
|
|
29
36
|
get capacity(): number {
|
|
30
37
|
return this._capacity;
|
|
31
38
|
}
|
|
39
|
+
|
|
32
40
|
set capacity(value: number) {
|
|
33
41
|
this._capacity = value;
|
|
34
42
|
}
|
|
35
43
|
|
|
36
44
|
private _first: number = -1;
|
|
45
|
+
|
|
37
46
|
get first(): number {
|
|
38
47
|
return this._first;
|
|
39
48
|
}
|
|
49
|
+
|
|
40
50
|
set first(value: number) {
|
|
41
51
|
this._first = value;
|
|
42
52
|
}
|
|
43
53
|
|
|
44
54
|
private _last: number = -1;
|
|
55
|
+
|
|
45
56
|
get last(): number {
|
|
46
57
|
return this._last;
|
|
47
58
|
}
|
|
59
|
+
|
|
48
60
|
set last(value: number) {
|
|
49
61
|
this._last = value;
|
|
50
62
|
}
|
|
51
63
|
|
|
52
64
|
private _size: number = 0;
|
|
65
|
+
|
|
53
66
|
get size(): number {
|
|
54
67
|
return this._size;
|
|
55
68
|
}
|
|
69
|
+
|
|
56
70
|
protected set size(value: number) {
|
|
57
71
|
this._size = value;
|
|
58
72
|
}
|
|
59
73
|
|
|
60
|
-
constructor(capacity?: number) {
|
|
61
|
-
if (capacity !== undefined) this._capacity = capacity;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
74
|
addFirst(value: T) {
|
|
65
75
|
if (this._size === 0) {
|
|
66
76
|
const mid = Math.floor(this._capacity / 2);
|
package/src/utils/types/utils.ts
CHANGED
|
@@ -1,178 +1,4 @@
|
|
|
1
|
-
export type JSONSerializable = {
|
|
2
|
-
[key: string]: any
|
|
3
|
-
}
|
|
4
|
-
export type JSONValue = string | number | boolean | undefined | JSONObject;
|
|
5
|
-
|
|
6
|
-
export interface JSONObject {
|
|
7
|
-
[key: string]: JSONValue;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export type AnyFunction<A extends any[] = any[], R = any> = (...args: A) => R;
|
|
11
|
-
export type Primitive =
|
|
12
|
-
| number
|
|
13
|
-
| string
|
|
14
|
-
| boolean
|
|
15
|
-
| symbol
|
|
16
|
-
| undefined
|
|
17
|
-
| null
|
|
18
|
-
| void
|
|
19
|
-
| AnyFunction
|
|
20
|
-
| Date;
|
|
21
|
-
|
|
22
|
-
export type Cast<T, TComplex> = { [M in keyof TComplex]: T };
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
export type DeepLeavesWrap<T, TComplex> =
|
|
26
|
-
T extends string ? Cast<string, TComplex>
|
|
27
|
-
: T extends number ? Cast<number, TComplex>
|
|
28
|
-
: T extends boolean ? Cast<boolean, TComplex>
|
|
29
|
-
: T extends undefined ? Cast<undefined, TComplex>
|
|
30
|
-
: T extends null ? Cast<null, TComplex>
|
|
31
|
-
: T extends void ? Cast<void, TComplex>
|
|
32
|
-
: T extends symbol ? Cast<symbol, TComplex>
|
|
33
|
-
: T extends AnyFunction ? Cast<AnyFunction, TComplex>
|
|
34
|
-
: T extends Date ? Cast<Date, TComplex>
|
|
35
|
-
: {
|
|
36
|
-
[K in keyof T]:
|
|
37
|
-
T[K] extends (infer U)[] ? DeepLeavesWrap<U, TComplex>[]
|
|
38
|
-
: DeepLeavesWrap<T[K], TComplex>;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
type Json = null | string | number | boolean | Json [] | { [name: string]: Json }
|
|
43
|
-
|
|
44
|
-
export type TypeName<T> = T extends string
|
|
45
|
-
? 'string'
|
|
46
|
-
: T extends number
|
|
47
|
-
? 'number'
|
|
48
|
-
: T extends boolean
|
|
49
|
-
? 'boolean'
|
|
50
|
-
: T extends undefined
|
|
51
|
-
? 'undefined'
|
|
52
|
-
: T extends AnyFunction
|
|
53
|
-
? 'function'
|
|
54
|
-
: 'object';
|
|
55
|
-
|
|
56
|
-
export type JsonKeys<T> = keyof {
|
|
57
|
-
[P in keyof T]: number
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const arr = ['1', 2, 4, 5, 6] as const;
|
|
61
|
-
type Range = typeof arr[number];
|
|
62
|
-
const a: Range = 2;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* A function that emits a side effect and does not return anything.
|
|
67
|
-
*/
|
|
68
|
-
export type Procedure = (...args: any[]) => void;
|
|
69
|
-
|
|
70
|
-
export type DebounceOptions = {
|
|
71
|
-
isImmediate?: boolean;
|
|
72
|
-
maxWait?: number;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
export interface DebouncedFunction<F extends Procedure> {
|
|
76
|
-
cancel: () => void;
|
|
77
|
-
|
|
78
|
-
(this: ThisParameterType<F>, ...args: [...Parameters<F>]): void;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export type MonthKey =
|
|
82
|
-
'January' |
|
|
83
|
-
'February' |
|
|
84
|
-
'March' |
|
|
85
|
-
'April' |
|
|
86
|
-
'May' |
|
|
87
|
-
'June' |
|
|
88
|
-
'July' |
|
|
89
|
-
'August' |
|
|
90
|
-
'September' |
|
|
91
|
-
'October' |
|
|
92
|
-
'November' |
|
|
93
|
-
'December';
|
|
94
|
-
|
|
95
|
-
export type Month = { [key in MonthKey]: string }
|
|
96
|
-
|
|
97
|
-
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
98
|
-
|
|
99
|
-
export class TreeNode<T> {
|
|
100
|
-
id: string;
|
|
101
|
-
name?: string | undefined;
|
|
102
|
-
value?: T | undefined;
|
|
103
|
-
children?: TreeNode<T>[] | undefined;
|
|
104
|
-
|
|
105
|
-
constructor(id: string, name?: string, value?: T, children?: TreeNode<T>[]) {
|
|
106
|
-
this.id = id;
|
|
107
|
-
this.name = name || '';
|
|
108
|
-
this.value = value || undefined;
|
|
109
|
-
this.children = children || [];
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// TODO get set
|
|
113
|
-
// get name (): string | undefined {
|
|
114
|
-
// return this.name;
|
|
115
|
-
// }
|
|
116
|
-
//
|
|
117
|
-
// set name (name: string | undefined) {
|
|
118
|
-
// this.name = name;
|
|
119
|
-
// }
|
|
120
|
-
|
|
121
|
-
addChildren(children: TreeNode<T> | TreeNode<T> []) {
|
|
122
|
-
if (!this.children) {
|
|
123
|
-
this.children = [];
|
|
124
|
-
}
|
|
125
|
-
if (children instanceof TreeNode) {
|
|
126
|
-
this.children.push(children);
|
|
127
|
-
} else {
|
|
128
|
-
this.children = this.children.concat(children);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
getHeight() {
|
|
133
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
134
|
-
const beginRoot = this;
|
|
135
|
-
let maxDepth = 1;
|
|
136
|
-
if (beginRoot) {
|
|
137
|
-
const bfs = (node: TreeNode<T>, level: number) => {
|
|
138
|
-
if (level > maxDepth) {
|
|
139
|
-
maxDepth = level;
|
|
140
|
-
}
|
|
141
|
-
const {children} = node;
|
|
142
|
-
if (children) {
|
|
143
|
-
for (let i = 0, len = children.length; i < len; i++) {
|
|
144
|
-
bfs(children[i], level + 1);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
bfs(beginRoot, 1);
|
|
149
|
-
}
|
|
150
|
-
return maxDepth;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export type OrderType = 'InOrder' | 'PreOrder' | 'PostOrder'
|
|
156
|
-
|
|
157
|
-
export type DeepProxy<T> = T extends (...args: any[]) => infer R
|
|
158
|
-
? (...args: [...Parameters<T>]) => DeepProxy<R>
|
|
159
|
-
: T extends object
|
|
160
|
-
? { [K in keyof T]: DeepProxy<T[K]> }
|
|
161
|
-
: T;
|
|
162
|
-
|
|
163
|
-
export type DeepProxyOnChange = (target: any, property: string | symbol, value: any, receiver: any, descriptor: any, result: any) => void;
|
|
164
|
-
|
|
165
|
-
export type DeepProxyOnGet = (target: any, property: string | symbol, value: any, receiver: any, descriptor: any, result: any) => void;
|
|
166
|
-
|
|
167
|
-
export type CurryFunc<T> = T extends (...args: infer Args) => infer R
|
|
168
|
-
? Args extends [infer Arg, ...infer RestArgs]
|
|
169
|
-
? (arg: Arg) => CurryFunc<(...args: RestArgs) => R>
|
|
170
|
-
: R
|
|
171
|
-
: T;
|
|
172
|
-
|
|
173
|
-
|
|
174
1
|
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
175
2
|
export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: Symbol };
|
|
176
3
|
export type TrlFn = (...args: any[]) => any;
|
|
177
|
-
export type TrlAsyncFn = (...args: any[]) => any;
|
|
178
|
-
|
|
4
|
+
export type TrlAsyncFn = (...args: any[]) => any;
|