data-structure-typed 2.2.6 → 2.2.8

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 (72) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/CONTRIBUTING.md +47 -1
  3. package/README.md +19 -8
  4. package/README_CN.md +119 -275
  5. package/benchmark/report.html +1 -1
  6. package/benchmark/report.json +20 -324
  7. package/dist/cjs/index.cjs +109 -107
  8. package/dist/cjs/index.cjs.map +1 -1
  9. package/dist/cjs-legacy/index.cjs +109 -107
  10. package/dist/cjs-legacy/index.cjs.map +1 -1
  11. package/dist/esm/index.mjs +109 -107
  12. package/dist/esm/index.mjs.map +1 -1
  13. package/dist/esm-legacy/index.mjs +109 -107
  14. package/dist/esm-legacy/index.mjs.map +1 -1
  15. package/dist/leetcode/avl-tree-counter.mjs +2957 -0
  16. package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
  17. package/dist/leetcode/avl-tree.mjs +2720 -0
  18. package/dist/leetcode/binary-tree.mjs +1594 -0
  19. package/dist/leetcode/bst.mjs +2398 -0
  20. package/dist/leetcode/deque.mjs +683 -0
  21. package/dist/leetcode/directed-graph.mjs +1733 -0
  22. package/dist/leetcode/doubly-linked-list.mjs +709 -0
  23. package/dist/leetcode/hash-map.mjs +493 -0
  24. package/dist/leetcode/heap.mjs +542 -0
  25. package/dist/leetcode/max-heap.mjs +375 -0
  26. package/dist/leetcode/max-priority-queue.mjs +383 -0
  27. package/dist/leetcode/min-heap.mjs +363 -0
  28. package/dist/leetcode/min-priority-queue.mjs +371 -0
  29. package/dist/leetcode/priority-queue.mjs +363 -0
  30. package/dist/leetcode/queue.mjs +943 -0
  31. package/dist/leetcode/red-black-tree.mjs +2765 -0
  32. package/dist/leetcode/singly-linked-list.mjs +754 -0
  33. package/dist/leetcode/stack.mjs +217 -0
  34. package/dist/leetcode/tree-counter.mjs +3039 -0
  35. package/dist/leetcode/tree-multi-map.mjs +2913 -0
  36. package/dist/leetcode/trie.mjs +413 -0
  37. package/dist/leetcode/undirected-graph.mjs +1650 -0
  38. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
  39. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  40. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
  41. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -23
  42. package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
  43. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  44. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
  45. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  46. package/dist/umd/data-structure-typed.js +105 -103
  47. package/dist/umd/data-structure-typed.js.map +1 -1
  48. package/dist/umd/data-structure-typed.min.js +2 -2
  49. package/dist/umd/data-structure-typed.min.js.map +1 -1
  50. package/package.json +48 -171
  51. package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
  52. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
  53. package/src/data-structures/binary-tree/avl-tree.ts +15 -15
  54. package/src/data-structures/binary-tree/binary-tree.ts +53 -55
  55. package/src/data-structures/binary-tree/bst.ts +21 -22
  56. package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
  57. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  58. package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
  59. package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
  60. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
  61. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
  62. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
  63. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
  64. package/test/unit/data-structures/binary-tree/bst.test.ts +99 -99
  65. package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
  66. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
  67. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +37 -37
  68. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
  69. package/tsup.config.js +50 -21
  70. package/tsup.leetcode.config.js +1 -1
  71. package/tsup.umd.config.js +29 -0
  72. package/tsup.node.config.js +0 -83
@@ -0,0 +1,217 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ var IterableElementBase = class {
4
+ static {
5
+ __name(this, "IterableElementBase");
6
+ }
7
+ constructor(options) {
8
+ if (options) {
9
+ const { toElementFn } = options;
10
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
11
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
12
+ }
13
+ }
14
+ _toElementFn;
15
+ get toElementFn() {
16
+ return this._toElementFn;
17
+ }
18
+ *[Symbol.iterator](...args) {
19
+ yield* this._getIterator(...args);
20
+ }
21
+ *values() {
22
+ for (const item of this) yield item;
23
+ }
24
+ every(predicate, thisArg) {
25
+ let index = 0;
26
+ for (const item of this) {
27
+ if (thisArg === void 0) {
28
+ if (!predicate(item, index++, this)) return false;
29
+ } else {
30
+ const fn = predicate;
31
+ if (!fn.call(thisArg, item, index++, this)) return false;
32
+ }
33
+ }
34
+ return true;
35
+ }
36
+ some(predicate, thisArg) {
37
+ let index = 0;
38
+ for (const item of this) {
39
+ if (thisArg === void 0) {
40
+ if (predicate(item, index++, this)) return true;
41
+ } else {
42
+ const fn = predicate;
43
+ if (fn.call(thisArg, item, index++, this)) return true;
44
+ }
45
+ }
46
+ return false;
47
+ }
48
+ forEach(callbackfn, thisArg) {
49
+ let index = 0;
50
+ for (const item of this) {
51
+ if (thisArg === void 0) {
52
+ callbackfn(item, index++, this);
53
+ } else {
54
+ const fn = callbackfn;
55
+ fn.call(thisArg, item, index++, this);
56
+ }
57
+ }
58
+ }
59
+ find(predicate, thisArg) {
60
+ let index = 0;
61
+ for (const item of this) {
62
+ if (thisArg === void 0) {
63
+ if (predicate(item, index++, this)) return item;
64
+ } else {
65
+ const fn = predicate;
66
+ if (fn.call(thisArg, item, index++, this)) return item;
67
+ }
68
+ }
69
+ return;
70
+ }
71
+ has(element) {
72
+ for (const ele of this) if (ele === element) return true;
73
+ return false;
74
+ }
75
+ reduce(callbackfn, initialValue) {
76
+ let index = 0;
77
+ const iter = this[Symbol.iterator]();
78
+ let acc;
79
+ if (arguments.length >= 2) {
80
+ acc = initialValue;
81
+ } else {
82
+ const first = iter.next();
83
+ if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
84
+ acc = first.value;
85
+ index = 1;
86
+ }
87
+ for (const value of iter) {
88
+ acc = callbackfn(acc, value, index++, this);
89
+ }
90
+ return acc;
91
+ }
92
+ toArray() {
93
+ return [...this];
94
+ }
95
+ toVisual() {
96
+ return [...this];
97
+ }
98
+ print() {
99
+ console.log(this.toVisual());
100
+ }
101
+ };
102
+ var Stack = class extends IterableElementBase {
103
+ static {
104
+ __name(this, "Stack");
105
+ }
106
+ _equals = Object.is;
107
+ constructor(elements = [], options) {
108
+ super(options);
109
+ this.pushMany(elements);
110
+ }
111
+ _elements = [];
112
+ get elements() {
113
+ return this._elements;
114
+ }
115
+ get size() {
116
+ return this.elements.length;
117
+ }
118
+ static fromArray(elements, options) {
119
+ return new this(elements, options);
120
+ }
121
+ isEmpty() {
122
+ return this.elements.length === 0;
123
+ }
124
+ peek() {
125
+ return this.isEmpty() ? void 0 : this.elements[this.elements.length - 1];
126
+ }
127
+ push(element) {
128
+ this.elements.push(element);
129
+ return true;
130
+ }
131
+ pop() {
132
+ return this.isEmpty() ? void 0 : this.elements.pop();
133
+ }
134
+ pushMany(elements) {
135
+ const ans = [];
136
+ for (const el of elements) {
137
+ if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
138
+ else ans.push(this.push(el));
139
+ }
140
+ return ans;
141
+ }
142
+ delete(element) {
143
+ const idx = this._indexOfByEquals(element);
144
+ return this.deleteAt(idx);
145
+ }
146
+ deleteAt(index) {
147
+ if (index < 0 || index >= this.elements.length) return false;
148
+ const spliced = this.elements.splice(index, 1);
149
+ return spliced.length === 1;
150
+ }
151
+ deleteWhere(predicate) {
152
+ for (let i = 0; i < this.elements.length; i++) {
153
+ if (predicate(this.elements[i], i, this)) {
154
+ this.elements.splice(i, 1);
155
+ return true;
156
+ }
157
+ }
158
+ return false;
159
+ }
160
+ clear() {
161
+ this._elements = [];
162
+ }
163
+ clone() {
164
+ const out = this._createInstance({ toElementFn: this.toElementFn });
165
+ for (const v of this) out.push(v);
166
+ return out;
167
+ }
168
+ filter(predicate, thisArg) {
169
+ const out = this._createInstance({ toElementFn: this.toElementFn });
170
+ let index = 0;
171
+ for (const v of this) {
172
+ if (predicate.call(thisArg, v, index, this)) out.push(v);
173
+ index++;
174
+ }
175
+ return out;
176
+ }
177
+ mapSame(callback, thisArg) {
178
+ const out = this._createInstance({ toElementFn: this.toElementFn });
179
+ let index = 0;
180
+ for (const v of this) {
181
+ const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
182
+ out.push(mv);
183
+ }
184
+ return out;
185
+ }
186
+ map(callback, options, thisArg) {
187
+ const out = this._createLike([], { ...options ?? {} });
188
+ let index = 0;
189
+ for (const v of this) {
190
+ out.push(thisArg === void 0 ? callback(v, index, this) : callback.call(thisArg, v, index, this));
191
+ index++;
192
+ }
193
+ return out;
194
+ }
195
+ setEquality(equals) {
196
+ this._equals = equals;
197
+ return this;
198
+ }
199
+ _indexOfByEquals(target) {
200
+ for (let i = 0; i < this.elements.length; i++) if (this._equals(this.elements[i], target)) return i;
201
+ return -1;
202
+ }
203
+ _createInstance(options) {
204
+ const Ctor = this.constructor;
205
+ return new Ctor([], options);
206
+ }
207
+ _createLike(elements = [], options) {
208
+ const Ctor = this.constructor;
209
+ return new Ctor(elements, options);
210
+ }
211
+ *_getIterator() {
212
+ for (let i = 0; i < this.elements.length; i++) yield this.elements[i];
213
+ }
214
+ };
215
+ export {
216
+ Stack
217
+ };