data-structure-typed 1.12.21 → 1.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +278 -179
- package/dist/data-structures/binary-tree/binary-tree.d.ts +46 -1
- package/dist/data-structures/binary-tree/binary-tree.js +67 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +29 -0
- package/dist/data-structures/binary-tree/segment-tree.js +45 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +40 -5
- package/dist/data-structures/graph/abstract-graph.js +47 -7
- package/dist/data-structures/graph/directed-graph.d.ts +8 -0
- package/dist/data-structures/graph/directed-graph.js +14 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +10 -0
- package/dist/data-structures/graph/undirected-graph.js +23 -1
- package/dist/data-structures/hash/coordinate-map.d.ts +7 -1
- package/dist/data-structures/hash/coordinate-map.js +16 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +7 -1
- package/dist/data-structures/hash/coordinate-set.js +16 -0
- package/dist/data-structures/heap/heap.d.ts +16 -0
- package/dist/data-structures/heap/heap.js +38 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +30 -7
- package/dist/data-structures/linked-list/doubly-linked-list.js +71 -4
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +262 -328
- package/dist/data-structures/linked-list/singly-linked-list.js +258 -273
- package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -1
- package/dist/data-structures/priority-queue/priority-queue.js +18 -2
- package/dist/data-structures/queue/deque.d.ts +18 -7
- package/dist/data-structures/queue/deque.js +50 -3
- package/dist/data-structures/types/abstract-graph.d.ts +2 -2
- package/dist/utils/types/utils.d.ts +0 -49
- package/dist/utils/types/utils.js +14 -52
- package/dist/utils/utils.d.ts +1 -97
- package/dist/utils/utils.js +197 -546
- package/package.json +4 -3
- package/src/data-structures/binary-tree/aa-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +84 -14
- package/src/data-structures/binary-tree/segment-tree.ts +45 -13
- package/src/data-structures/graph/abstract-graph.ts +58 -15
- package/src/data-structures/graph/directed-graph.ts +14 -5
- package/src/data-structures/graph/undirected-graph.ts +23 -6
- package/src/data-structures/hash/coordinate-map.ts +13 -1
- package/src/data-structures/hash/coordinate-set.ts +13 -1
- package/src/data-structures/heap/heap.ts +31 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +68 -11
- package/src/data-structures/linked-list/singly-linked-list.ts +312 -334
- package/src/data-structures/priority-queue/priority-queue.ts +15 -2
- package/src/data-structures/queue/deque.ts +38 -8
- package/src/data-structures/types/abstract-graph.ts +3 -3
- package/src/utils/types/utils.ts +165 -167
- package/src/utils/utils.ts +209 -480
- package/tests/unit/data-structures/graph/directed-graph.test.ts +431 -8
|
@@ -8,8 +8,21 @@
|
|
|
8
8
|
import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions} from '../types';
|
|
9
9
|
|
|
10
10
|
export class PriorityQueue<T = number> {
|
|
11
|
-
protected nodes: T[] = [];
|
|
12
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
|
+
|
|
13
26
|
/**
|
|
14
27
|
* The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
|
|
15
28
|
* function.
|
|
@@ -21,7 +34,7 @@ export class PriorityQueue<T = number> {
|
|
|
21
34
|
|
|
22
35
|
if (nodes && nodes instanceof Array && nodes.length > 0) {
|
|
23
36
|
// TODO support distinct
|
|
24
|
-
this.
|
|
37
|
+
this._nodes = Array.isArray(nodes) ? [...nodes] : [];
|
|
25
38
|
isFix && this._fix();
|
|
26
39
|
}
|
|
27
40
|
}
|
|
@@ -17,19 +17,49 @@ 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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
protected
|
|
20
|
+
private _nodes: { [key: number]: T } = {};
|
|
21
|
+
get nodes(): { [p: number]: T } {
|
|
22
|
+
return this._nodes;
|
|
23
|
+
}
|
|
24
|
+
protected set nodes(value: { [p: number]: T }) {
|
|
25
|
+
this._nodes = value;
|
|
26
|
+
}
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
private _capacity = Number.MAX_SAFE_INTEGER;
|
|
29
|
+
get capacity(): number {
|
|
30
|
+
return this._capacity;
|
|
31
|
+
}
|
|
32
|
+
set capacity(value: number) {
|
|
33
|
+
this._capacity = value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private _first: number = -1;
|
|
37
|
+
get first(): number {
|
|
38
|
+
return this._first;
|
|
39
|
+
}
|
|
40
|
+
set first(value: number) {
|
|
41
|
+
this._first = value;
|
|
28
42
|
}
|
|
29
43
|
|
|
30
|
-
|
|
44
|
+
private _last: number = -1;
|
|
45
|
+
get last(): number {
|
|
46
|
+
return this._last;
|
|
47
|
+
}
|
|
48
|
+
set last(value: number) {
|
|
49
|
+
this._last = value;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private _size: number = 0;
|
|
53
|
+
get size(): number {
|
|
31
54
|
return this._size;
|
|
32
55
|
}
|
|
56
|
+
protected set size(value: number) {
|
|
57
|
+
this._size = value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
constructor(capacity?: number) {
|
|
61
|
+
if (capacity !== undefined) this._capacity = capacity;
|
|
62
|
+
}
|
|
33
63
|
|
|
34
64
|
addFirst(value: T) {
|
|
35
65
|
if (this._size === 0) {
|
|
@@ -5,7 +5,7 @@ export type DijkstraResult<V> =
|
|
|
5
5
|
|
|
6
6
|
export interface IGraph<V, E> {
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
hasVertex(vertexOrId: V | VertexId): boolean;
|
|
9
9
|
|
|
10
10
|
getVertex(vertexOrId: VertexId | V): V | null;
|
|
11
11
|
|
|
@@ -23,9 +23,9 @@ export interface IGraph<V, E> {
|
|
|
23
23
|
|
|
24
24
|
edgesOf(vertexOrId: V | VertexId): E[];
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
hasEdge(src: V | VertexId, dest: V | VertexId): boolean;
|
|
27
27
|
|
|
28
|
-
//
|
|
28
|
+
// hasEdge(e: E): boolean;
|
|
29
29
|
|
|
30
30
|
getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
31
31
|
|
package/src/utils/types/utils.ts
CHANGED
|
@@ -1,174 +1,161 @@
|
|
|
1
|
-
export type JSONSerializable = {
|
|
2
|
-
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export type
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const arr = ['1', 2, 4, 5, 6] as const;
|
|
61
|
-
type Range = typeof arr[number];
|
|
62
|
-
const a: Range = 2;
|
|
63
|
-
|
|
1
|
+
// export type JSONSerializable = {
|
|
2
|
+
// [key: string]: any
|
|
3
|
+
// }
|
|
4
|
+
|
|
5
|
+
// export type JSONValue = string | number | boolean | undefined | JSONObject;
|
|
6
|
+
//
|
|
7
|
+
// export interface JSONObject {
|
|
8
|
+
// [key: string]: JSONValue;
|
|
9
|
+
// }
|
|
10
|
+
//
|
|
11
|
+
// export type AnyFunction<A extends any[] = any[], R = any> = (...args: A) => R;
|
|
12
|
+
|
|
13
|
+
// export type Primitive =
|
|
14
|
+
// | number
|
|
15
|
+
// | string
|
|
16
|
+
// | boolean
|
|
17
|
+
// | symbol
|
|
18
|
+
// | undefined
|
|
19
|
+
// | null
|
|
20
|
+
// | void
|
|
21
|
+
// | AnyFunction
|
|
22
|
+
// | Date;
|
|
23
|
+
|
|
24
|
+
// export type Cast<T, TComplex> = { [M in keyof TComplex]: T };
|
|
25
|
+
|
|
26
|
+
// export type DeepLeavesWrap<T, TComplex> =
|
|
27
|
+
// T extends string ? Cast<string, TComplex>
|
|
28
|
+
// : T extends number ? Cast<number, TComplex>
|
|
29
|
+
// : T extends boolean ? Cast<boolean, TComplex>
|
|
30
|
+
// : T extends undefined ? Cast<undefined, TComplex>
|
|
31
|
+
// : T extends null ? Cast<null, TComplex>
|
|
32
|
+
// : T extends void ? Cast<void, TComplex>
|
|
33
|
+
// : T extends symbol ? Cast<symbol, TComplex>
|
|
34
|
+
// : T extends AnyFunction ? Cast<AnyFunction, TComplex>
|
|
35
|
+
// : T extends Date ? Cast<Date, TComplex>
|
|
36
|
+
// : {
|
|
37
|
+
// [K in keyof T]:
|
|
38
|
+
// T[K] extends (infer U)[] ? DeepLeavesWrap<U, TComplex>[]
|
|
39
|
+
// : DeepLeavesWrap<T[K], TComplex>;
|
|
40
|
+
// }
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
// type Json = null | string | number | boolean | Json [] | { [name: string]: Json }
|
|
44
|
+
|
|
45
|
+
// export type TypeName<T> = T extends string
|
|
46
|
+
// ? 'string'
|
|
47
|
+
// : T extends number
|
|
48
|
+
// ? 'number'
|
|
49
|
+
// : T extends boolean
|
|
50
|
+
// ? 'boolean'
|
|
51
|
+
// : T extends undefined
|
|
52
|
+
// ? 'undefined'
|
|
53
|
+
// : T extends AnyFunction
|
|
54
|
+
// ? 'function'
|
|
55
|
+
// : 'object';
|
|
56
|
+
|
|
57
|
+
// export type JsonKeys<T> = keyof {
|
|
58
|
+
// [P in keyof T]: number
|
|
59
|
+
// }
|
|
64
60
|
|
|
65
61
|
/**
|
|
66
62
|
* A function that emits a side effect and does not return anything.
|
|
67
63
|
*/
|
|
68
|
-
export type Procedure = (...args: any[]) => void;
|
|
69
|
-
|
|
70
|
-
export type DebounceOptions = {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
export interface DebouncedFunction<F extends Procedure> {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export type MonthKey =
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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;
|
|
64
|
+
// export type Procedure = (...args: any[]) => void;
|
|
65
|
+
|
|
66
|
+
// export type DebounceOptions = {
|
|
67
|
+
// isImmediate?: boolean;
|
|
68
|
+
// maxWait?: number;
|
|
69
|
+
// };
|
|
70
|
+
|
|
71
|
+
// export interface DebouncedFunction<F extends Procedure> {
|
|
72
|
+
// cancel: () => void;
|
|
73
|
+
//
|
|
74
|
+
// (this: ThisParameterType<F>, ...args: [...Parameters<F>]): void;
|
|
75
|
+
// }
|
|
76
|
+
|
|
77
|
+
// export type MonthKey =
|
|
78
|
+
// 'January' |
|
|
79
|
+
// 'February' |
|
|
80
|
+
// 'March' |
|
|
81
|
+
// 'April' |
|
|
82
|
+
// 'May' |
|
|
83
|
+
// 'June' |
|
|
84
|
+
// 'July' |
|
|
85
|
+
// 'August' |
|
|
86
|
+
// 'September' |
|
|
87
|
+
// 'October' |
|
|
88
|
+
// 'November' |
|
|
89
|
+
// 'December';
|
|
90
|
+
|
|
91
|
+
// export type Month = { [key in MonthKey]: string }
|
|
92
|
+
|
|
93
|
+
// export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
94
|
+
|
|
95
|
+
// export class TreeNode<T> {
|
|
96
|
+
// id: string;
|
|
97
|
+
// name?: string | undefined;
|
|
98
|
+
// value?: T | undefined;
|
|
99
|
+
// children?: TreeNode<T>[] | undefined;
|
|
100
|
+
//
|
|
101
|
+
// constructor(id: string, name?: string, value?: T, children?: TreeNode<T>[]) {
|
|
102
|
+
// this.id = id;
|
|
103
|
+
// this.name = name || '';
|
|
104
|
+
// this.value = value || undefined;
|
|
105
|
+
// this.children = children || [];
|
|
106
|
+
// }
|
|
107
|
+
//
|
|
108
|
+
// addChildren(children: TreeNode<T> | TreeNode<T> []) {
|
|
109
|
+
// if (!this.children) {
|
|
110
|
+
// this.children = [];
|
|
111
|
+
// }
|
|
112
|
+
// if (children instanceof TreeNode) {
|
|
113
|
+
// this.children.push(children);
|
|
114
|
+
// } else {
|
|
115
|
+
// this.children = this.children.concat(children);
|
|
116
|
+
// }
|
|
117
|
+
// }
|
|
118
|
+
//
|
|
119
|
+
// getHeight() {
|
|
120
|
+
// // eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
121
|
+
// const beginRoot = this;
|
|
122
|
+
// let maxDepth = 1;
|
|
123
|
+
// if (beginRoot) {
|
|
124
|
+
// const bfs = (node: TreeNode<T>, level: number) => {
|
|
125
|
+
// if (level > maxDepth) {
|
|
126
|
+
// maxDepth = level;
|
|
127
|
+
// }
|
|
128
|
+
// const {children} = node;
|
|
129
|
+
// if (children) {
|
|
130
|
+
// for (let i = 0, len = children.length; i < len; i++) {
|
|
131
|
+
// bfs(children[i], level + 1);
|
|
132
|
+
// }
|
|
133
|
+
// }
|
|
134
|
+
// };
|
|
135
|
+
// bfs(beginRoot, 1);
|
|
136
|
+
// }
|
|
137
|
+
// return maxDepth;
|
|
138
|
+
// }
|
|
139
|
+
//
|
|
140
|
+
// }
|
|
141
|
+
|
|
142
|
+
// export type OrderType = 'InOrder' | 'PreOrder' | 'PostOrder'
|
|
143
|
+
|
|
144
|
+
// export type DeepProxy<T> = T extends (...args: any[]) => infer R
|
|
145
|
+
// ? (...args: [...Parameters<T>]) => DeepProxy<R>
|
|
146
|
+
// : T extends object
|
|
147
|
+
// ? { [K in keyof T]: DeepProxy<T[K]> }
|
|
148
|
+
// : T;
|
|
149
|
+
|
|
150
|
+
// export type DeepProxyOnChange = (target: any, property: string | symbol, value: any, receiver: any, descriptor: any, result: any) => void;
|
|
151
|
+
|
|
152
|
+
// export type DeepProxyOnGet = (target: any, property: string | symbol, value: any, receiver: any, descriptor: any, result: any) => void;
|
|
153
|
+
|
|
154
|
+
// export type CurryFunc<T> = T extends (...args: infer Args) => infer R
|
|
155
|
+
// ? Args extends [infer Arg, ...infer RestArgs]
|
|
156
|
+
// ? (arg: Arg) => CurryFunc<(...args: RestArgs) => R>
|
|
157
|
+
// : R
|
|
158
|
+
// : T;
|
|
172
159
|
|
|
173
160
|
|
|
174
161
|
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
@@ -176,3 +163,14 @@ export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: Symbol };
|
|
|
176
163
|
export type TrlFn = (...args: any[]) => any;
|
|
177
164
|
export type TrlAsyncFn = (...args: any[]) => any;
|
|
178
165
|
|
|
166
|
+
// export type CaseType =
|
|
167
|
+
// 'camel'
|
|
168
|
+
// | 'snake'
|
|
169
|
+
// | 'pascal'
|
|
170
|
+
// | 'constant'
|
|
171
|
+
// | 'kebab'
|
|
172
|
+
// | 'lower'
|
|
173
|
+
// | 'title'
|
|
174
|
+
// | 'sentence'
|
|
175
|
+
// | 'path'
|
|
176
|
+
// | 'dot';
|