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,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayDeque = exports.ObjectDeque = exports.Deque = void 0;
4
+ const linked_list_1 = require("../linked-list");
5
+ // O(n) time complexity of obtaining the value
6
+ // O(1) time complexity of adding at the beginning and the end
7
+ class Deque extends linked_list_1.DoublyLinkedList {
8
+ }
9
+ exports.Deque = Deque;
10
+ // O(1) time complexity of obtaining the value
11
+ // O(n) time complexity of adding at the beginning and the end
12
+ // todo tested slowest one
13
+ class ObjectDeque {
14
+ constructor(capacity) {
15
+ this._nodes = {};
16
+ this._capacity = Number.MAX_SAFE_INTEGER;
17
+ this._first = -1;
18
+ this._last = -1;
19
+ this._size = 0;
20
+ if (capacity !== undefined)
21
+ this._capacity = capacity;
22
+ }
23
+ size() {
24
+ return this._size;
25
+ }
26
+ offerFirst(value) {
27
+ if (this._size === 0) {
28
+ const mid = Math.floor(this._capacity / 2);
29
+ this._first = mid;
30
+ this._last = mid;
31
+ }
32
+ else {
33
+ this._first--;
34
+ }
35
+ this._nodes[this._first] = value;
36
+ this._size++;
37
+ }
38
+ offerLast(value) {
39
+ if (this._size === 0) {
40
+ const mid = Math.floor(this._capacity / 2);
41
+ this._first = mid;
42
+ this._last = mid;
43
+ }
44
+ else {
45
+ this._last++;
46
+ }
47
+ this._nodes[this._last] = value;
48
+ this._size++;
49
+ }
50
+ pollFirst() {
51
+ if (!this._size)
52
+ return;
53
+ let value = this.peekFirst();
54
+ delete this._nodes[this._first];
55
+ this._first++;
56
+ this._size--;
57
+ return value;
58
+ }
59
+ peekFirst() {
60
+ if (this._size)
61
+ return this._nodes[this._first];
62
+ }
63
+ pollLast() {
64
+ if (!this._size)
65
+ return;
66
+ let value = this.peekLast();
67
+ delete this._nodes[this._last];
68
+ this._last--;
69
+ this._size--;
70
+ return value;
71
+ }
72
+ peekLast() {
73
+ if (this._size)
74
+ return this._nodes[this._last];
75
+ }
76
+ get(index) {
77
+ return this._nodes[this._first + index] || null;
78
+ }
79
+ isEmpty() {
80
+ return this._size <= 0;
81
+ }
82
+ }
83
+ exports.ObjectDeque = ObjectDeque;
84
+ // O(1) time complexity of obtaining the value
85
+ // O(n) time complexity of adding at the beginning and the end
86
+ class ArrayDeque {
87
+ constructor() {
88
+ this._nodes = [];
89
+ }
90
+ get size() {
91
+ return this._nodes.length;
92
+ }
93
+ offerLast(value) {
94
+ return this._nodes.push(value);
95
+ }
96
+ pollLast() {
97
+ var _a;
98
+ return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null;
99
+ }
100
+ pollFirst() {
101
+ var _a;
102
+ return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null;
103
+ }
104
+ offerFirst(value) {
105
+ return this._nodes.unshift(value);
106
+ }
107
+ peekFirst() {
108
+ var _a;
109
+ return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null;
110
+ }
111
+ peekLast() {
112
+ var _a;
113
+ return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
114
+ }
115
+ get(index) {
116
+ var _a;
117
+ return (_a = this._nodes[index]) !== null && _a !== void 0 ? _a : null;
118
+ }
119
+ set(index, value) {
120
+ return this._nodes[index] = value;
121
+ }
122
+ insert(index, value) {
123
+ return this._nodes.splice(index, 0, value);
124
+ }
125
+ remove(index) {
126
+ return this._nodes.splice(index, 1);
127
+ }
128
+ isEmpty() {
129
+ return this._nodes.length === 0;
130
+ }
131
+ }
132
+ exports.ArrayDeque = ArrayDeque;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./queue"), exports);
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Queue = void 0;
4
+ /**
5
+ * @license MIT
6
+ * @copyright 2020 Pablo
7
+ *
8
+ * @class
9
+ */
10
+ class Queue {
11
+ /**
12
+ * Creates a queue.
13
+ * @param {array} [elements]
14
+ */
15
+ constructor(elements) {
16
+ this._nodes = elements || [];
17
+ this._offset = 0;
18
+ }
19
+ /**
20
+ * Adds an element at the back of the queue.
21
+ * @public
22
+ * @param {any} element
23
+ */
24
+ offer(element) {
25
+ this._nodes.push(element);
26
+ return this;
27
+ }
28
+ /**
29
+ * Dequeues the front element in the queue.
30
+ * @public
31
+ * @returns {any}
32
+ */
33
+ poll() {
34
+ if (this.size() === 0)
35
+ return null;
36
+ const first = this.peek();
37
+ this._offset += 1;
38
+ if (this._offset * 2 < this._nodes.length)
39
+ return first;
40
+ // only remove dequeued elements when reaching half size
41
+ // to decrease latency of shifting elements.
42
+ this._nodes = this._nodes.slice(this._offset);
43
+ this._offset = 0;
44
+ return first;
45
+ }
46
+ /**
47
+ * Returns the front element of the queue.
48
+ * @public
49
+ * @returns {any}
50
+ */
51
+ peek() {
52
+ return this.size() > 0 ? this._nodes[this._offset] : null;
53
+ }
54
+ /**
55
+ * Returns the back element of the queue.
56
+ * @public
57
+ * @returns {any}
58
+ */
59
+ peekLast() {
60
+ return this.size() > 0 ? this._nodes[this._nodes.length - 1] : null;
61
+ }
62
+ /**
63
+ * Returns the number of elements in the queue.
64
+ * @public
65
+ * @returns {number}
66
+ */
67
+ size() {
68
+ return this._nodes.length - this._offset;
69
+ }
70
+ /**
71
+ * Checks if the queue is empty.
72
+ * @public
73
+ * @returns {boolean}
74
+ */
75
+ isEmpty() {
76
+ return this.size() === 0;
77
+ }
78
+ /**
79
+ * Returns the remaining elements in the queue as an array.
80
+ * @public
81
+ * @returns {array}
82
+ */
83
+ toArray() {
84
+ return this._nodes.slice(this._offset);
85
+ }
86
+ /**
87
+ * Clears the queue.
88
+ * @public
89
+ */
90
+ clear() {
91
+ this._nodes = [];
92
+ this._offset = 0;
93
+ }
94
+ /**
95
+ * Creates a shallow copy of the queue.
96
+ * @public
97
+ * @return {Queue}
98
+ */
99
+ clone() {
100
+ return new Queue(this._nodes.slice(this._offset));
101
+ }
102
+ /**
103
+ * Creates a queue from an existing array.
104
+ * @public
105
+ * @static
106
+ * @param {array} elements
107
+ * @return {Queue}
108
+ */
109
+ static fromArray(elements) {
110
+ return new Queue(elements);
111
+ }
112
+ }
113
+ exports.Queue = Queue;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./stack"), exports);
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Stack = void 0;
4
+ /**
5
+ * @license MIT
6
+ * @copyright 2020 Pablo Rios <zrwusa@gmail.com>
7
+ *
8
+ * @class
9
+ */
10
+ class Stack {
11
+ /**
12
+ * Creates a stack.
13
+ * @param {array} [elements]
14
+ */
15
+ constructor(elements) {
16
+ this._elements = Array.isArray(elements) ? elements : [];
17
+ }
18
+ /**
19
+ * Checks if the stack is empty.
20
+ * @public
21
+ * @returns {boolean}
22
+ */
23
+ isEmpty() {
24
+ return this._elements.length === 0;
25
+ }
26
+ /**
27
+ * Returns the number of elements in the stack.
28
+ * @public
29
+ * @returns {number}
30
+ */
31
+ size() {
32
+ return this._elements.length;
33
+ }
34
+ /**
35
+ * Returns the top element in the stack.
36
+ * @public
37
+ * @returns {object}
38
+ */
39
+ peek() {
40
+ if (this.isEmpty())
41
+ return null;
42
+ return this._elements[this._elements.length - 1];
43
+ }
44
+ /**
45
+ * Adds an element to the top of the stack.
46
+ * @public
47
+ * @param {object} element
48
+ */
49
+ push(element) {
50
+ this._elements.push(element);
51
+ return this;
52
+ }
53
+ /**
54
+ * Removes and returns the top element in the stack.
55
+ * @public
56
+ * @returns {object}
57
+ */
58
+ pop() {
59
+ if (this.isEmpty())
60
+ return null;
61
+ return this._elements.pop() || null;
62
+ }
63
+ /**
64
+ * Returns the remaining elements as an array.
65
+ * @public
66
+ * @returns {array}
67
+ */
68
+ toArray() {
69
+ return this._elements.slice();
70
+ }
71
+ /**
72
+ * Clears all elements from the stack.
73
+ * @public
74
+ */
75
+ clear() {
76
+ this._elements = [];
77
+ }
78
+ /**
79
+ * Creates a shallow copy from the stack.
80
+ * @public
81
+ * @return {Stack}
82
+ */
83
+ clone() {
84
+ return new Stack(this._elements.slice());
85
+ }
86
+ /**
87
+ * Creates a stack from an existing array
88
+ * @public
89
+ * @static
90
+ * @param {array} [elements]
91
+ * @return {Stack}
92
+ */
93
+ static fromArray(elements) {
94
+ return new Stack(elements);
95
+ }
96
+ }
97
+ exports.Stack = Stack;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.trampolineAsync = exports.trampoline = exports.toThunk = exports.isThunk = exports.THUNK_SYMBOL = void 0;
13
+ exports.THUNK_SYMBOL = Symbol('thunk');
14
+ const isThunk = (value) => {
15
+ return typeof value === 'function' && value.__THUNK__ === exports.THUNK_SYMBOL;
16
+ };
17
+ exports.isThunk = isThunk;
18
+ const toThunk = (fn) => {
19
+ const thunk = () => fn();
20
+ thunk.__THUNK__ = exports.THUNK_SYMBOL;
21
+ return thunk;
22
+ };
23
+ exports.toThunk = toThunk;
24
+ const trampoline = (fn) => {
25
+ const cont = (...args) => (0, exports.toThunk)(() => fn(...args));
26
+ return Object.assign((...args) => {
27
+ let result = fn(...args);
28
+ while ((0, exports.isThunk)(result)) {
29
+ result = result();
30
+ }
31
+ return result;
32
+ }, { cont });
33
+ };
34
+ exports.trampoline = trampoline;
35
+ const trampolineAsync = (fn) => {
36
+ const cont = (...args) => (0, exports.toThunk)(() => fn(...args));
37
+ return Object.assign((...args) => __awaiter(void 0, void 0, void 0, function* () {
38
+ let result = yield fn(...args);
39
+ while ((0, exports.isThunk)(result)) {
40
+ result = yield result();
41
+ }
42
+ return result;
43
+ }), { cont });
44
+ };
45
+ exports.trampolineAsync = trampolineAsync;
46
+ const factorial = (0, exports.trampoline)((n, acc = 1) => {
47
+ return n
48
+ // Note: calling factorial.cont instead of factorial directly
49
+ ? factorial.cont(n - 1, acc * n)
50
+ : acc;
51
+ });
52
+ // factorial(32768)
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./trie"), exports);
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Trie = exports.TrieNode = void 0;
4
+ class TrieNode {
5
+ constructor() {
6
+ this._children = new Map();
7
+ this._isEnd = false;
8
+ }
9
+ get children() {
10
+ return this._children;
11
+ }
12
+ set children(v) {
13
+ this._children = v;
14
+ }
15
+ get isEnd() {
16
+ return this._isEnd;
17
+ }
18
+ set isEnd(v) {
19
+ this._isEnd = v;
20
+ }
21
+ }
22
+ exports.TrieNode = TrieNode;
23
+ class Trie {
24
+ get root() {
25
+ return this._root;
26
+ }
27
+ set root(v) {
28
+ this._root = v;
29
+ }
30
+ constructor() {
31
+ this._root = new TrieNode();
32
+ }
33
+ put(input) {
34
+ let cur = this._root;
35
+ for (const c of input) {
36
+ let nodeC = cur.children.get(c);
37
+ if (!nodeC) {
38
+ nodeC = new TrieNode();
39
+ cur.children.set(c, nodeC);
40
+ }
41
+ cur = nodeC;
42
+ }
43
+ cur.isEnd = true;
44
+ return true;
45
+ }
46
+ has(input) {
47
+ let cur = this._root;
48
+ for (const c of input) {
49
+ const nodeC = cur.children.get(c);
50
+ if (!nodeC)
51
+ return false;
52
+ cur = nodeC;
53
+ }
54
+ return cur.isEnd;
55
+ }
56
+ remove(word) {
57
+ let isDeleted = false;
58
+ const dfs = (cur, i) => {
59
+ const char = word[i];
60
+ const child = cur.children.get(char);
61
+ if (child) {
62
+ if (i === word.length - 1) {
63
+ if (child.isEnd) {
64
+ if (child.children.size > 0) {
65
+ child.isEnd = false;
66
+ }
67
+ else {
68
+ cur.children.delete(char);
69
+ }
70
+ isDeleted = true;
71
+ return true;
72
+ }
73
+ return false;
74
+ }
75
+ const res = dfs(child, i + 1);
76
+ if (res && !cur.isEnd && child.children.size === 0) {
77
+ cur.children.delete(char);
78
+ return true;
79
+ }
80
+ return false;
81
+ }
82
+ return false;
83
+ };
84
+ dfs(this.root, 0);
85
+ return isDeleted;
86
+ }
87
+ // --- start additional methods ---
88
+ /**
89
+ * Only can present as a prefix, not a word
90
+ * @param input
91
+ */
92
+ isAbsPrefix(input) {
93
+ let cur = this._root;
94
+ for (const c of input) {
95
+ const nodeC = cur.children.get(c);
96
+ if (!nodeC)
97
+ return false;
98
+ cur = nodeC;
99
+ }
100
+ return !cur.isEnd;
101
+ }
102
+ /**
103
+ * Can present as a prefix or word
104
+ * @param input
105
+ */
106
+ isPrefix(input) {
107
+ let cur = this._root;
108
+ for (const c of input) {
109
+ const nodeC = cur.children.get(c);
110
+ if (!nodeC)
111
+ return false;
112
+ cur = nodeC;
113
+ }
114
+ return true;
115
+ }
116
+ getAll(prefix = '') {
117
+ const words = [];
118
+ function dfs(node, word) {
119
+ for (const char of node.children.keys()) {
120
+ const charNode = node.children.get(char);
121
+ if (charNode !== undefined) {
122
+ dfs(charNode, word.concat(char));
123
+ }
124
+ }
125
+ if (node.isEnd) {
126
+ words.push(word);
127
+ }
128
+ }
129
+ let startNode = this._root;
130
+ if (prefix) {
131
+ for (const c of prefix) {
132
+ const nodeC = startNode.children.get(c);
133
+ if (nodeC)
134
+ startNode = nodeC;
135
+ }
136
+ }
137
+ dfs(startNode, prefix);
138
+ return words;
139
+ }
140
+ }
141
+ exports.Trie = Trie;
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./data-structures"), exports);
@@ -0,0 +1,2 @@
1
+ export declare class AaTree {
2
+ }
@@ -0,0 +1,21 @@
1
+ import { BST, BSTNode } from './bst';
2
+ import { BinaryTreeNodeId } from './binary-tree';
3
+ export interface AVLTreeDeleted<T> {
4
+ deleted: AVLTreeNode<T> | null;
5
+ needBalanced: AVLTreeNode<T> | null;
6
+ }
7
+ export declare class AVLTreeNode<T> extends BSTNode<T> {
8
+ clone(): AVLTreeNode<T>;
9
+ }
10
+ export declare class AVLTree<T> extends BST<T> {
11
+ createNode(id: BinaryTreeNodeId, val: T, count?: number): AVLTreeNode<T>;
12
+ put(id: BinaryTreeNodeId, val: T | null, count?: number): AVLTreeNode<T> | null;
13
+ remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): AVLTreeDeleted<T>[];
14
+ balanceFactor(node: AVLTreeNode<T>): number;
15
+ updateHeight(node: AVLTreeNode<T>): void;
16
+ balancePath(node: AVLTreeNode<T>): void;
17
+ balanceLL(A: AVLTreeNode<T>): void;
18
+ balanceLR(A: AVLTreeNode<T>): void;
19
+ balanceRR(A: AVLTreeNode<T>): void;
20
+ balanceRL(A: AVLTreeNode<T>): void;
21
+ }
@@ -0,0 +1,2 @@
1
+ export declare class BTree {
2
+ }
@@ -0,0 +1,8 @@
1
+ export declare class BinaryIndexedTree {
2
+ private readonly _sumTree;
3
+ constructor(n: number);
4
+ update(i: number, delta: number): void;
5
+ getPrefixSum(i: number): number;
6
+ getRangeSum(start: number, end: number): number;
7
+ static lowBit(x: number): number;
8
+ }