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.
Files changed (48) hide show
  1. package/README.md +278 -179
  2. package/dist/data-structures/binary-tree/binary-tree.d.ts +46 -1
  3. package/dist/data-structures/binary-tree/binary-tree.js +67 -0
  4. package/dist/data-structures/binary-tree/segment-tree.d.ts +29 -0
  5. package/dist/data-structures/binary-tree/segment-tree.js +45 -0
  6. package/dist/data-structures/graph/abstract-graph.d.ts +40 -5
  7. package/dist/data-structures/graph/abstract-graph.js +47 -7
  8. package/dist/data-structures/graph/directed-graph.d.ts +8 -0
  9. package/dist/data-structures/graph/directed-graph.js +14 -2
  10. package/dist/data-structures/graph/undirected-graph.d.ts +10 -0
  11. package/dist/data-structures/graph/undirected-graph.js +23 -1
  12. package/dist/data-structures/hash/coordinate-map.d.ts +7 -1
  13. package/dist/data-structures/hash/coordinate-map.js +16 -0
  14. package/dist/data-structures/hash/coordinate-set.d.ts +7 -1
  15. package/dist/data-structures/hash/coordinate-set.js +16 -0
  16. package/dist/data-structures/heap/heap.d.ts +16 -0
  17. package/dist/data-structures/heap/heap.js +38 -0
  18. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +30 -7
  19. package/dist/data-structures/linked-list/doubly-linked-list.js +71 -4
  20. package/dist/data-structures/linked-list/singly-linked-list.d.ts +262 -328
  21. package/dist/data-structures/linked-list/singly-linked-list.js +258 -273
  22. package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -1
  23. package/dist/data-structures/priority-queue/priority-queue.js +18 -2
  24. package/dist/data-structures/queue/deque.d.ts +18 -7
  25. package/dist/data-structures/queue/deque.js +50 -3
  26. package/dist/data-structures/types/abstract-graph.d.ts +2 -2
  27. package/dist/utils/types/utils.d.ts +0 -49
  28. package/dist/utils/types/utils.js +14 -52
  29. package/dist/utils/utils.d.ts +1 -97
  30. package/dist/utils/utils.js +197 -546
  31. package/package.json +4 -3
  32. package/src/data-structures/binary-tree/aa-tree.ts +1 -1
  33. package/src/data-structures/binary-tree/binary-tree.ts +84 -14
  34. package/src/data-structures/binary-tree/segment-tree.ts +45 -13
  35. package/src/data-structures/graph/abstract-graph.ts +58 -15
  36. package/src/data-structures/graph/directed-graph.ts +14 -5
  37. package/src/data-structures/graph/undirected-graph.ts +23 -6
  38. package/src/data-structures/hash/coordinate-map.ts +13 -1
  39. package/src/data-structures/hash/coordinate-set.ts +13 -1
  40. package/src/data-structures/heap/heap.ts +31 -0
  41. package/src/data-structures/linked-list/doubly-linked-list.ts +68 -11
  42. package/src/data-structures/linked-list/singly-linked-list.ts +312 -334
  43. package/src/data-structures/priority-queue/priority-queue.ts +15 -2
  44. package/src/data-structures/queue/deque.ts +38 -8
  45. package/src/data-structures/types/abstract-graph.ts +3 -3
  46. package/src/utils/types/utils.ts +165 -167
  47. package/src/utils/utils.ts +209 -480
  48. 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.nodes = Array.isArray(nodes) ? [...nodes] : [];
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
- protected _nodes: { [key: number]: T } = {};
21
- protected _capacity = Number.MAX_SAFE_INTEGER;
22
- protected _first: number = -1;
23
- protected _last: number = -1;
24
- protected _size: number = 0;
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
- constructor(capacity?: number) {
27
- if (capacity !== undefined) this._capacity = capacity;
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
- size() {
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
- containsVertex(vertexOrId: V | VertexId): boolean;
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
- containsEdge(src: V | VertexId, dest: V | VertexId): boolean;
26
+ hasEdge(src: V | VertexId, dest: V | VertexId): boolean;
27
27
 
28
- // containsEdge(e: E): boolean;
28
+ // hasEdge(e: E): boolean;
29
29
 
30
30
  getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
31
31
 
@@ -1,174 +1,161 @@
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
-
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
- 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;
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';