data-structure-typed 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/.idea/data-structure-typed.iml +12 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/vcs.xml +6 -0
  4. package/README.md +2 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +6 -0
  6. package/dist/data-structures/binary-tree/avl-tree.js +231 -0
  7. package/dist/data-structures/binary-tree/b-tree.js +6 -0
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +31 -0
  9. package/dist/data-structures/binary-tree/binary-tree.js +992 -0
  10. package/dist/data-structures/binary-tree/bst.js +431 -0
  11. package/dist/data-structures/binary-tree/index.js +20 -0
  12. package/dist/data-structures/binary-tree/rb-tree.js +6 -0
  13. package/dist/data-structures/binary-tree/segment-tree.js +151 -0
  14. package/dist/data-structures/binary-tree/splay-tree.js +6 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.js +16 -0
  16. package/dist/data-structures/binary-tree/two-three-tree.js +6 -0
  17. package/dist/data-structures/graph/abstract-graph.js +648 -0
  18. package/dist/data-structures/graph/directed-graph.js +268 -0
  19. package/dist/data-structures/graph/index.js +19 -0
  20. package/dist/data-structures/graph/undirected-graph.js +142 -0
  21. package/dist/data-structures/hash/coordinate-map.js +24 -0
  22. package/dist/data-structures/hash/coordinate-set.js +21 -0
  23. package/dist/data-structures/hash/hash-table.js +2 -0
  24. package/dist/data-structures/hash/index.js +17 -0
  25. package/dist/data-structures/hash/pair.js +2 -0
  26. package/dist/data-structures/hash/tree-map.js +2 -0
  27. package/dist/data-structures/hash/tree-set.js +2 -0
  28. package/dist/data-structures/heap/heap.js +114 -0
  29. package/dist/data-structures/heap/index.js +19 -0
  30. package/dist/data-structures/heap/max-heap.js +22 -0
  31. package/dist/data-structures/heap/min-heap.js +22 -0
  32. package/dist/data-structures/index.js +25 -0
  33. package/dist/data-structures/linked-list/doubly-linked-list.js +259 -0
  34. package/dist/data-structures/linked-list/index.js +18 -0
  35. package/dist/data-structures/linked-list/singly-linked-list.js +660 -0
  36. package/dist/data-structures/linked-list/skip-linked-list.js +2 -0
  37. package/dist/data-structures/matrix/index.js +19 -0
  38. package/dist/data-structures/matrix/matrix.js +14 -0
  39. package/dist/data-structures/matrix/matrix2d.js +119 -0
  40. package/dist/data-structures/matrix/navigator.js +78 -0
  41. package/dist/data-structures/matrix/vector2d.js +161 -0
  42. package/dist/data-structures/priority-queue/index.js +19 -0
  43. package/dist/data-structures/priority-queue/max-priority-queue.js +15 -0
  44. package/dist/data-structures/priority-queue/min-priority-queue.js +15 -0
  45. package/dist/data-structures/priority-queue/priority-queue.js +174 -0
  46. package/dist/data-structures/queue/deque.js +132 -0
  47. package/dist/data-structures/queue/index.js +17 -0
  48. package/dist/data-structures/queue/queue.js +113 -0
  49. package/dist/data-structures/stack/index.js +17 -0
  50. package/dist/data-structures/stack/stack.js +97 -0
  51. package/dist/data-structures/trampoline.js +52 -0
  52. package/dist/data-structures/trie/index.js +17 -0
  53. package/dist/data-structures/trie/trie.js +141 -0
  54. package/dist/index.js +17 -0
  55. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +2 -0
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +21 -0
  57. package/dist/types/data-structures/binary-tree/b-tree.d.ts +2 -0
  58. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  59. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +140 -0
  60. package/dist/types/data-structures/binary-tree/bst.d.ts +32 -0
  61. package/dist/types/data-structures/binary-tree/index.d.ts +4 -0
  62. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -0
  63. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +33 -0
  64. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +2 -0
  65. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +11 -0
  66. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +2 -0
  67. package/dist/types/data-structures/graph/abstract-graph.d.ts +126 -0
  68. package/dist/types/data-structures/graph/directed-graph.d.ts +51 -0
  69. package/dist/types/data-structures/graph/index.d.ts +3 -0
  70. package/dist/types/data-structures/graph/undirected-graph.d.ts +24 -0
  71. package/dist/types/data-structures/hash/coordinate-map.d.ts +8 -0
  72. package/dist/types/data-structures/hash/coordinate-set.d.ts +7 -0
  73. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  74. package/dist/types/data-structures/hash/index.d.ts +1 -0
  75. package/dist/types/data-structures/hash/pair.d.ts +1 -0
  76. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  77. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  78. package/dist/types/data-structures/heap/heap.d.ts +72 -0
  79. package/dist/types/data-structures/heap/index.d.ts +3 -0
  80. package/dist/types/data-structures/heap/max-heap.d.ts +14 -0
  81. package/dist/types/data-structures/heap/min-heap.d.ts +14 -0
  82. package/dist/types/data-structures/index.d.ts +9 -0
  83. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +59 -0
  84. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  85. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +358 -0
  86. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  87. package/dist/types/data-structures/matrix/index.d.ts +3 -0
  88. package/dist/types/data-structures/matrix/matrix.d.ts +9 -0
  89. package/dist/types/data-structures/matrix/matrix2d.d.ts +25 -0
  90. package/dist/types/data-structures/matrix/navigator.d.ts +31 -0
  91. package/dist/types/data-structures/matrix/vector2d.d.ts +74 -0
  92. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  93. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +4 -0
  94. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +4 -0
  95. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +36 -0
  96. package/dist/types/data-structures/queue/deque.d.ts +37 -0
  97. package/dist/types/data-structures/queue/index.d.ts +1 -0
  98. package/dist/types/data-structures/queue/queue.d.ts +76 -0
  99. package/dist/types/data-structures/stack/index.d.ts +1 -0
  100. package/dist/types/data-structures/stack/stack.d.ts +69 -0
  101. package/dist/types/data-structures/trampoline.d.ts +25 -0
  102. package/dist/types/data-structures/trie/index.d.ts +1 -0
  103. package/dist/types/data-structures/trie/trie.d.ts +28 -0
  104. package/dist/types/index.d.ts +1 -0
  105. package/dist/types/index.js +17 -0
  106. package/dist/types/types/index.d.ts +1 -0
  107. package/dist/types/types/utils.d.ts +46 -0
  108. package/dist/types/utils.d.ts +122 -0
  109. package/dist/types/utils.js +53 -0
  110. package/dist/utils.js +569 -0
  111. package/package.json +75 -0
  112. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  113. package/src/data-structures/binary-tree/avl-tree.ts +232 -0
  114. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  115. package/src/data-structures/binary-tree/binary-indexed-tree.ts +33 -0
  116. package/src/data-structures/binary-tree/binary-tree.ts +1088 -0
  117. package/src/data-structures/binary-tree/bst.ts +404 -0
  118. package/src/data-structures/binary-tree/index.ts +4 -0
  119. package/src/data-structures/binary-tree/rb-tree.ts +3 -0
  120. package/src/data-structures/binary-tree/segment-tree.ts +164 -0
  121. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  122. package/src/data-structures/binary-tree/tree-multiset.ts +21 -0
  123. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  124. package/src/data-structures/graph/abstract-graph.ts +789 -0
  125. package/src/data-structures/graph/directed-graph.ts +322 -0
  126. package/src/data-structures/graph/index.ts +3 -0
  127. package/src/data-structures/graph/undirected-graph.ts +154 -0
  128. package/src/data-structures/hash/coordinate-map.ts +24 -0
  129. package/src/data-structures/hash/coordinate-set.ts +20 -0
  130. package/src/data-structures/hash/hash-table.ts +1 -0
  131. package/src/data-structures/hash/index.ts +1 -0
  132. package/src/data-structures/hash/pair.ts +1 -0
  133. package/src/data-structures/hash/tree-map.ts +1 -0
  134. package/src/data-structures/hash/tree-set.ts +1 -0
  135. package/src/data-structures/heap/heap.ts +136 -0
  136. package/src/data-structures/heap/index.ts +3 -0
  137. package/src/data-structures/heap/max-heap.ts +22 -0
  138. package/src/data-structures/heap/min-heap.ts +24 -0
  139. package/src/data-structures/index.ts +10 -0
  140. package/src/data-structures/linked-list/doubly-linked-list.ts +258 -0
  141. package/src/data-structures/linked-list/index.ts +2 -0
  142. package/src/data-structures/linked-list/singly-linked-list.ts +750 -0
  143. package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
  144. package/src/data-structures/matrix/index.ts +3 -0
  145. package/src/data-structures/matrix/matrix.ts +13 -0
  146. package/src/data-structures/matrix/matrix2d.ts +125 -0
  147. package/src/data-structures/matrix/navigator.ts +99 -0
  148. package/src/data-structures/matrix/vector2d.ts +189 -0
  149. package/src/data-structures/priority-queue/index.ts +3 -0
  150. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -0
  151. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -0
  152. package/src/data-structures/priority-queue/priority-queue.ts +208 -0
  153. package/src/data-structures/queue/deque.ts +139 -0
  154. package/src/data-structures/queue/index.ts +1 -0
  155. package/src/data-structures/queue/queue.ts +123 -0
  156. package/src/data-structures/stack/index.ts +1 -0
  157. package/src/data-structures/stack/stack.ts +104 -0
  158. package/src/data-structures/trampoline.ts +91 -0
  159. package/src/data-structures/trie/index.ts +1 -0
  160. package/src/data-structures/trie/trie.ts +153 -0
  161. package/src/index.ts +1 -0
  162. package/src/types/index.ts +1 -0
  163. package/src/types/patches/index.d.ts +0 -0
  164. package/src/types/utils.ts +158 -0
  165. package/src/utils.ts +605 -0
  166. package/tsconfig.json +52 -0
@@ -0,0 +1,139 @@
1
+ import {DoublyLinkedList} from '../linked-list';
2
+
3
+ // O(n) time complexity of obtaining the value
4
+ // O(1) time complexity of adding at the beginning and the end
5
+ export class Deque<T> extends DoublyLinkedList<T> {
6
+
7
+ }
8
+
9
+ // O(1) time complexity of obtaining the value
10
+ // O(n) time complexity of adding at the beginning and the end
11
+ // todo tested slowest one
12
+ export class ObjectDeque<T> {
13
+ protected _nodes: { [key: number]: T } = {};
14
+ protected _capacity = Number.MAX_SAFE_INTEGER;
15
+ protected _first: number = -1;
16
+ protected _last: number = -1;
17
+ protected _size: number = 0;
18
+
19
+ constructor(capacity?: number) {
20
+ if (capacity !== undefined) this._capacity = capacity;
21
+ }
22
+
23
+ size() {
24
+ return this._size;
25
+ }
26
+
27
+ offerFirst(value: T) {
28
+ if (this._size === 0) {
29
+ const mid = Math.floor(this._capacity / 2);
30
+ this._first = mid;
31
+ this._last = mid;
32
+ } else {
33
+ this._first--;
34
+ }
35
+ this._nodes[this._first] = value;
36
+ this._size++;
37
+ }
38
+
39
+ offerLast(value: T) {
40
+ if (this._size === 0) {
41
+ const mid = Math.floor(this._capacity / 2);
42
+ this._first = mid;
43
+ this._last = mid;
44
+ } else {
45
+ this._last++;
46
+ }
47
+ this._nodes[this._last] = value;
48
+ this._size++;
49
+ }
50
+
51
+ pollFirst() {
52
+ if (!this._size) return;
53
+ let value = this.peekFirst();
54
+ delete this._nodes[this._first];
55
+ this._first++;
56
+ this._size--;
57
+ return value;
58
+ }
59
+
60
+ peekFirst() {
61
+ if (this._size) return this._nodes[this._first];
62
+ }
63
+
64
+ pollLast() {
65
+ if (!this._size) return;
66
+ let value = this.peekLast();
67
+ delete this._nodes[this._last];
68
+ this._last--;
69
+ this._size--;
70
+
71
+ return value;
72
+ }
73
+
74
+ peekLast() {
75
+ if (this._size) return this._nodes[this._last];
76
+ }
77
+
78
+ get(index: number) {
79
+ return this._nodes[this._first + index] || null;
80
+ }
81
+
82
+ isEmpty() {
83
+ return this._size <= 0;
84
+ }
85
+ }
86
+
87
+ // O(1) time complexity of obtaining the value
88
+ // O(n) time complexity of adding at the beginning and the end
89
+ export class ArrayDeque<T> {
90
+ protected _nodes: T[] = [];
91
+
92
+ get size() {
93
+ return this._nodes.length;
94
+ }
95
+
96
+ offerLast(value: T) {
97
+ return this._nodes.push(value);
98
+ }
99
+
100
+ pollLast(): T | null {
101
+ return this._nodes.pop() ?? null;
102
+ }
103
+
104
+ pollFirst(): T | null {
105
+ return this._nodes.shift() ?? null;
106
+ }
107
+
108
+ offerFirst(value: T) {
109
+ return this._nodes.unshift(value);
110
+ }
111
+
112
+ peekFirst(): T | null {
113
+ return this._nodes[0] ?? null;
114
+ }
115
+
116
+ peekLast(): T | null {
117
+ return this._nodes[this._nodes.length - 1] ?? null;
118
+ }
119
+
120
+ get(index: number): T | null {
121
+ return this._nodes[index] ?? null;
122
+ }
123
+
124
+ set(index: number, value: T) {
125
+ return this._nodes[index] = value;
126
+ }
127
+
128
+ insert(index: number, value: T) {
129
+ return this._nodes.splice(index, 0, value);
130
+ }
131
+
132
+ remove(index: number) {
133
+ return this._nodes.splice(index, 1);
134
+ }
135
+
136
+ isEmpty() {
137
+ return this._nodes.length === 0;
138
+ }
139
+ }
@@ -0,0 +1 @@
1
+ export * from './queue';
@@ -0,0 +1,123 @@
1
+ /**
2
+ * @license MIT
3
+ * @copyright 2020 Pablo
4
+ *
5
+ * @class
6
+ */
7
+ export class Queue<T> {
8
+ protected _nodes: T[];
9
+ protected _offset: number;
10
+
11
+ /**
12
+ * Creates a queue.
13
+ * @param {array} [elements]
14
+ */
15
+ constructor(elements?: T[]) {
16
+ this._nodes = elements || [];
17
+ this._offset = 0;
18
+ }
19
+
20
+ /**
21
+ * Adds an element at the back of the queue.
22
+ * @public
23
+ * @param {any} element
24
+ */
25
+ offer(element: T): Queue<T> {
26
+ this._nodes.push(element);
27
+ return this;
28
+ }
29
+
30
+ /**
31
+ * Dequeues the front element in the queue.
32
+ * @public
33
+ * @returns {any}
34
+ */
35
+ poll(): T | null {
36
+ if (this.size() === 0) return null;
37
+
38
+ const first = this.peek();
39
+ this._offset += 1;
40
+
41
+ if (this._offset * 2 < this._nodes.length) return first;
42
+
43
+ // only remove dequeued elements when reaching half size
44
+ // to decrease latency of shifting elements.
45
+ this._nodes = this._nodes.slice(this._offset);
46
+ this._offset = 0;
47
+ return first;
48
+ }
49
+
50
+ /**
51
+ * Returns the front element of the queue.
52
+ * @public
53
+ * @returns {any}
54
+ */
55
+ peek(): T | null {
56
+ return this.size() > 0 ? this._nodes[this._offset] : null;
57
+ }
58
+
59
+ /**
60
+ * Returns the back element of the queue.
61
+ * @public
62
+ * @returns {any}
63
+ */
64
+ peekLast(): T | null {
65
+ return this.size() > 0 ? this._nodes[this._nodes.length - 1] : null;
66
+ }
67
+
68
+ /**
69
+ * Returns the number of elements in the queue.
70
+ * @public
71
+ * @returns {number}
72
+ */
73
+ size(): number {
74
+ return this._nodes.length - this._offset;
75
+ }
76
+
77
+ /**
78
+ * Checks if the queue is empty.
79
+ * @public
80
+ * @returns {boolean}
81
+ */
82
+ isEmpty(): boolean {
83
+ return this.size() === 0;
84
+ }
85
+
86
+ /**
87
+ * Returns the remaining elements in the queue as an array.
88
+ * @public
89
+ * @returns {array}
90
+ */
91
+ toArray(): T[] {
92
+ return this._nodes.slice(this._offset);
93
+ }
94
+
95
+ /**
96
+ * Clears the queue.
97
+ * @public
98
+ */
99
+ clear(): void {
100
+ this._nodes = [];
101
+ this._offset = 0;
102
+ }
103
+
104
+ /**
105
+ * Creates a shallow copy of the queue.
106
+ * @public
107
+ * @return {Queue}
108
+ */
109
+ clone(): Queue<T> {
110
+ return new Queue(this._nodes.slice(this._offset));
111
+ }
112
+
113
+ /**
114
+ * Creates a queue from an existing array.
115
+ * @public
116
+ * @static
117
+ * @param {array} elements
118
+ * @return {Queue}
119
+ */
120
+ static fromArray<T>(elements: T[]): Queue<T> {
121
+ return new Queue(elements);
122
+ }
123
+ }
@@ -0,0 +1 @@
1
+ export * from './stack';
@@ -0,0 +1,104 @@
1
+ /**
2
+ * @license MIT
3
+ * @copyright 2020 Pablo Rios <zrwusa@gmail.com>
4
+ *
5
+ * @class
6
+ */
7
+ export class Stack<T> {
8
+ protected _elements: T[];
9
+
10
+ /**
11
+ * Creates a stack.
12
+ * @param {array} [elements]
13
+ */
14
+ constructor(elements?: T[]) {
15
+ this._elements = Array.isArray(elements) ? elements : [];
16
+ }
17
+
18
+ /**
19
+ * Checks if the stack is empty.
20
+ * @public
21
+ * @returns {boolean}
22
+ */
23
+ isEmpty(): boolean {
24
+ return this._elements.length === 0;
25
+ }
26
+
27
+ /**
28
+ * Returns the number of elements in the stack.
29
+ * @public
30
+ * @returns {number}
31
+ */
32
+ size(): number {
33
+ return this._elements.length;
34
+ }
35
+
36
+ /**
37
+ * Returns the top element in the stack.
38
+ * @public
39
+ * @returns {object}
40
+ */
41
+ peek(): T | null {
42
+ if (this.isEmpty()) return null;
43
+
44
+ return this._elements[this._elements.length - 1];
45
+ }
46
+
47
+ /**
48
+ * Adds an element to the top of the stack.
49
+ * @public
50
+ * @param {object} element
51
+ */
52
+ push(element: T): Stack<T> {
53
+ this._elements.push(element);
54
+ return this;
55
+ }
56
+
57
+ /**
58
+ * Removes and returns the top element in the stack.
59
+ * @public
60
+ * @returns {object}
61
+ */
62
+ pop(): T | null {
63
+ if (this.isEmpty()) return null;
64
+
65
+ return this._elements.pop() || null;
66
+ }
67
+
68
+ /**
69
+ * Returns the remaining elements as an array.
70
+ * @public
71
+ * @returns {array}
72
+ */
73
+ toArray(): T[] {
74
+ return this._elements.slice();
75
+ }
76
+
77
+ /**
78
+ * Clears all elements from the stack.
79
+ * @public
80
+ */
81
+ clear(): void {
82
+ this._elements = [];
83
+ }
84
+
85
+ /**
86
+ * Creates a shallow copy from the stack.
87
+ * @public
88
+ * @return {Stack}
89
+ */
90
+ clone(): Stack<T> {
91
+ return new Stack(this._elements.slice());
92
+ }
93
+
94
+ /**
95
+ * Creates a stack from an existing array
96
+ * @public
97
+ * @static
98
+ * @param {array} [elements]
99
+ * @return {Stack}
100
+ */
101
+ static fromArray<T>(elements: T[]): Stack<T> {
102
+ return new Stack(elements);
103
+ }
104
+ }
@@ -0,0 +1,91 @@
1
+ export type ArgumentTypes<T extends (...args: any[]) => any> =
2
+ T extends (...args: infer A) => any
3
+ ? A
4
+ : never;
5
+
6
+ export const THUNK_SYMBOL: unique symbol = Symbol('thunk');
7
+
8
+ export interface Thunk<T> extends Function {
9
+ __THUNK__: typeof THUNK_SYMBOL;
10
+
11
+ (): T;
12
+ }
13
+
14
+ export type ThunkOrValue<T> = T | Thunk<T>;
15
+
16
+ export type UnwrapThunkDeep<T> = {
17
+ 0: T extends Thunk<infer U> ? UnwrapThunkDeep<U> : T;
18
+ }[
19
+ T extends ThunkOrValue<T> ? 0 : never
20
+ ];
21
+
22
+ export const isThunk = <T>(value: any): value is Thunk<T> => {
23
+ return typeof value === 'function' && value.__THUNK__ === THUNK_SYMBOL;
24
+ };
25
+
26
+ export const toThunk = <R>(fn: () => R): Thunk<R> => {
27
+ const thunk = () => fn();
28
+ thunk.__THUNK__ = THUNK_SYMBOL;
29
+ return thunk;
30
+ };
31
+ export type UnwrapPromise<T> = T extends Promise<infer U> ? Exclude<U, Promise<T>> : T;
32
+
33
+ export type Unbox<T> = UnwrapThunkDeep<UnwrapPromise<T>>;
34
+
35
+ export type Cont<A extends any[], R> = (...args: A) => Thunk<Unbox<R>>;
36
+
37
+ export interface Trampoline<F extends ((...args: any[]) => any)> {
38
+ (...args: ArgumentTypes<F>): Unbox<ReturnType<F>>;
39
+
40
+ cont: Cont<ArgumentTypes<F>, ReturnType<F>>;
41
+ }
42
+
43
+ export interface TrampolineAsync<F extends ((...args: any[]) => any)> {
44
+ (...args: ArgumentTypes<F>): Promise<Unbox<ReturnType<F>>>;
45
+
46
+ cont: Cont<ArgumentTypes<F>, ReturnType<F>>;
47
+ }
48
+
49
+ export const trampoline = <F extends ((...args: any[]) => any)>(fn: F): Trampoline<F> => {
50
+ const cont = (...args: ArgumentTypes<F>) => toThunk(() => fn(...args));
51
+
52
+ return Object.assign(
53
+ (...args: ArgumentTypes<F>): Unbox<ReturnType<F>> => {
54
+ let result: ThunkOrValue<ReturnType<F>> = fn(...args);
55
+
56
+ while (isThunk<ReturnType<F>>(result)) {
57
+ result = result();
58
+ }
59
+
60
+ return result;
61
+ },
62
+ {cont},
63
+ );
64
+ };
65
+
66
+ export const trampolineAsync = <F extends ((...args: any[]) => any)>(fn: F): TrampolineAsync<F> => {
67
+ const cont = (...args: ArgumentTypes<F>) => toThunk(() => fn(...args));
68
+
69
+ return Object.assign(
70
+ async (...args: ArgumentTypes<F>): Promise<Unbox<ReturnType<F>>> => {
71
+ let result: ThunkOrValue<ReturnType<F>> = await fn(...args);
72
+
73
+ while (isThunk<ReturnType<F>>(result)) {
74
+ result = await result();
75
+ }
76
+
77
+ return result;
78
+ },
79
+ {cont},
80
+ );
81
+ };
82
+
83
+
84
+ const factorial = trampoline((n: number, acc: number = 1): ThunkOrValue<number> => {
85
+ return n
86
+ // Note: calling factorial.cont instead of factorial directly
87
+ ? factorial.cont(n - 1, acc * n)
88
+ : acc;
89
+ });
90
+
91
+ // factorial(32768)
@@ -0,0 +1 @@
1
+ export * from './trie';
@@ -0,0 +1,153 @@
1
+ export class TrieNode {
2
+ protected _children: Map<string, TrieNode> = new Map<string, TrieNode>();
3
+
4
+ get children(): Map<string, TrieNode> {
5
+ return this._children;
6
+ }
7
+
8
+ set children(v: Map<string, TrieNode>) {
9
+ this._children = v;
10
+ }
11
+
12
+ protected _isEnd = false;
13
+
14
+ get isEnd(): boolean {
15
+ return this._isEnd;
16
+ }
17
+
18
+ set isEnd(v: boolean) {
19
+ this._isEnd = v;
20
+ }
21
+ }
22
+
23
+ export class Trie {
24
+ protected _root: TrieNode;
25
+ get root() {
26
+ return this._root;
27
+ }
28
+
29
+ set root(v: TrieNode) {
30
+ this._root = v;
31
+ }
32
+
33
+ constructor() {
34
+ this._root = new TrieNode();
35
+ }
36
+
37
+ put(input: string): boolean {
38
+ let cur = this._root;
39
+ for (const c of input) {
40
+ let nodeC = cur.children.get(c);
41
+ if (!nodeC) {
42
+ nodeC = new TrieNode();
43
+ cur.children.set(c, nodeC);
44
+ }
45
+ cur = nodeC;
46
+ }
47
+ cur.isEnd = true;
48
+ return true;
49
+ }
50
+
51
+
52
+ has(input: string): boolean {
53
+ let cur = this._root;
54
+ for (const c of input) {
55
+ const nodeC = cur.children.get(c);
56
+ if (!nodeC) return false;
57
+ cur = nodeC;
58
+ }
59
+ return cur.isEnd;
60
+ }
61
+
62
+ remove(word: string) {
63
+ let isDeleted = false;
64
+ const dfs = (cur: TrieNode, i: number): boolean => {
65
+ const char = word[i];
66
+ const child = cur.children.get(char);
67
+ if (child) {
68
+ if (i === word.length - 1) {
69
+ if (child.isEnd) {
70
+ if (child.children.size > 0) {
71
+ child.isEnd = false;
72
+ } else {
73
+ cur.children.delete(char);
74
+ }
75
+ isDeleted = true;
76
+ return true;
77
+ }
78
+ return false;
79
+ }
80
+ const res = dfs(child, i + 1);
81
+ if (res && !cur.isEnd && child.children.size === 0) {
82
+ cur.children.delete(char);
83
+ return true;
84
+ }
85
+ return false;
86
+ }
87
+ return false;
88
+ }
89
+
90
+ dfs(this.root, 0);
91
+ return isDeleted;
92
+ }
93
+
94
+ // --- start additional methods ---
95
+ /**
96
+ * Only can present as a prefix, not a word
97
+ * @param input
98
+ */
99
+ isAbsPrefix(input: string): boolean {
100
+ let cur = this._root;
101
+ for (const c of input) {
102
+ const nodeC = cur.children.get(c);
103
+ if (!nodeC) return false;
104
+ cur = nodeC;
105
+ }
106
+ return !cur.isEnd;
107
+ }
108
+
109
+ /**
110
+ * Can present as a prefix or word
111
+ * @param input
112
+ */
113
+ isPrefix(input: string): boolean {
114
+ let cur = this._root;
115
+ for (const c of input) {
116
+ const nodeC = cur.children.get(c);
117
+ if (!nodeC) return false;
118
+ cur = nodeC;
119
+ }
120
+ return true;
121
+ }
122
+
123
+
124
+ getAll(prefix = ''): string[] {
125
+ const words: string[] = [];
126
+
127
+ function dfs(node: TrieNode, word: string) {
128
+ for (const char of node.children.keys()) {
129
+ const charNode = node.children.get(char);
130
+ if (charNode !== undefined) {
131
+ dfs(charNode, word.concat(char));
132
+ }
133
+ }
134
+ if (node.isEnd) {
135
+ words.push(word);
136
+ }
137
+ }
138
+
139
+ let startNode = this._root;
140
+
141
+ if (prefix) {
142
+ for (const c of prefix) {
143
+ const nodeC = startNode.children.get(c);
144
+ if (nodeC) startNode = nodeC;
145
+ }
146
+ }
147
+
148
+ dfs(startNode, prefix);
149
+ return words;
150
+ }
151
+
152
+ // --- end additional methods ---
153
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './data-structures';
@@ -0,0 +1 @@
1
+ export * from './utils';
File without changes