jc-structure 0.0.7 → 0.0.9

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.
@@ -1,4 +1,4 @@
1
- class p {
1
+ class m {
2
2
  items = {};
3
3
  count = 0;
4
4
  lowestCount = 0;
@@ -10,8 +10,8 @@ class p {
10
10
  return delete this.items[this.lowestCount], this.lowestCount++, t;
11
11
  }
12
12
  enqueue(...t) {
13
- t.forEach((s) => {
14
- this.items[this.count] = s, this.count++;
13
+ t.forEach((e) => {
14
+ this.items[this.count] = e, this.count++;
15
15
  });
16
16
  }
17
17
  front() {
@@ -30,7 +30,7 @@ class p {
30
30
  return this.isEmpty() ? "" : `Queue(size: ${this.size()}):[${this.items[this.lowestCount]},...rest]`;
31
31
  }
32
32
  }
33
- class a {
33
+ class v {
34
34
  items = {};
35
35
  count = 0;
36
36
  constructor() {
@@ -42,8 +42,8 @@ class a {
42
42
  return delete this.items[this.count], t;
43
43
  }
44
44
  push(...t) {
45
- t.forEach((s) => {
46
- this.items[this.count] = s, this.count++;
45
+ t.forEach((e) => {
46
+ this.items[this.count] = e, this.count++;
47
47
  });
48
48
  }
49
49
  peek() {
@@ -62,13 +62,13 @@ class a {
62
62
  return this.isEmpty() ? "" : `Stack(count: ${this.count}):[${this.items[this.count - 1]},...rest]`;
63
63
  }
64
64
  }
65
- function o(i, t) {
66
- return i === t ? 0 : i < t ? -1 : 1;
65
+ function c(s, t) {
66
+ return s === t ? 0 : s < t ? -1 : 1;
67
67
  }
68
- class h {
68
+ class n {
69
69
  heap = [];
70
70
  compareFn;
71
- constructor(t = o) {
71
+ constructor(t = c) {
72
72
  this.compareFn = t;
73
73
  }
74
74
  static getLeftIndex(t) {
@@ -80,8 +80,8 @@ class h {
80
80
  static getParentIndex(t) {
81
81
  return t === 0 ? void 0 : Math.floor((t - 1) / 2);
82
82
  }
83
- static swap(t, s, e) {
84
- [t[s], t[e]] = [t[e], t[s]];
83
+ static swap(t, e, i) {
84
+ [t[e], t[i]] = [t[i], t[e]];
85
85
  }
86
86
  find() {
87
87
  return this.isEmpty() ? void 0 : this.heap[0];
@@ -99,7 +99,7 @@ class h {
99
99
  return this.heap.toString();
100
100
  }
101
101
  }
102
- class u extends h {
102
+ class p extends n {
103
103
  insert(t) {
104
104
  return t ? !1 : (this.heap.push(t), this.siftUp(this.heap.length - 1), !0);
105
105
  }
@@ -110,23 +110,307 @@ class u extends h {
110
110
  return this.heap[0] = this.heap.pop(), this.siftDown(0), t;
111
111
  }
112
112
  siftUp(t) {
113
- let s = t, e = h.getLeftIndex(s), r = h.getRightIndex(s), n = this.size();
114
- e < n && this.compareFn(this.heap[s], this.heap[e]) === -1 && (s = e), r < n && this.compareFn(this.heap[s], this.heap[r]) === 1 && (s = r), s !== t && (h.swap(this.heap, t, s), this.siftUp(s));
113
+ let e = t, i = n.getLeftIndex(e), r = n.getRightIndex(e), h = this.size();
114
+ i < h && this.compareFn(this.heap[e], this.heap[i]) === -1 && (e = i), r < h && this.compareFn(this.heap[e], this.heap[r]) === 1 && (e = r), e !== t && (n.swap(this.heap, t, e), this.siftUp(e));
115
115
  }
116
116
  siftDown(t) {
117
- let s = h.getParentIndex(t);
118
- for (; t > 0 && s && this.compareFn(this.heap[s], this.heap[t]) === 1; )
119
- h.swap(this.heap, s, t), t = s, s = h.getParentIndex(t);
117
+ let e = n.getParentIndex(t);
118
+ for (; t > 0 && e && this.compareFn(this.heap[e], this.heap[t]) === 1; )
119
+ n.swap(this.heap, e, t), t = e, e = n.getParentIndex(t);
120
120
  }
121
121
  }
122
- class c extends u {
123
- constructor(t = (s, e) => o(e, s)) {
122
+ class w extends p {
123
+ constructor(t = (e, i) => c(i, e)) {
124
124
  super(t);
125
125
  }
126
126
  }
127
+ function f(s) {
128
+ return { value: s };
129
+ }
130
+ class x {
131
+ capacity;
132
+ length = 0;
133
+ head = null;
134
+ tail = null;
135
+ lookup = /* @__PURE__ */ new Map();
136
+ reverseLookup = /* @__PURE__ */ new Map();
137
+ constructor(t = 10) {
138
+ this.capacity = t;
139
+ }
140
+ prepend(t) {
141
+ this.head ? (t.next = this.head, this.head.prev = t, this.head = t) : this.head = this.tail = t;
142
+ }
143
+ detach(t) {
144
+ t.prev && (t.prev.next = t.next), t.next && (t.next.prev = t.prev), this.head === t && (this.head = this.head.next || null), this.tail === t && (this.tail = this.tail.prev || null), t.next = void 0, t.prev = void 0;
145
+ }
146
+ trimCache() {
147
+ if (this.length <= this.capacity) return;
148
+ const t = this.tail;
149
+ this.detach(t);
150
+ const e = this.reverseLookup.get(t);
151
+ this.lookup.delete(e), this.reverseLookup.delete(t), this.length--;
152
+ }
153
+ get(t) {
154
+ const e = this.lookup.get(t);
155
+ if (e)
156
+ return this.detach(e), this.prepend(e), e.value;
157
+ }
158
+ update(t, e) {
159
+ let i = this.lookup.get(t);
160
+ i ? (this.detach(i), this.prepend(i), i.value = e) : (i = f(e), this.length++, this.prepend(i), this.trimCache(), this.lookup.set(t, i), this.reverseLookup);
161
+ }
162
+ }
163
+ class o {
164
+ value;
165
+ next = void 0;
166
+ }
167
+ class E {
168
+ count = 0;
169
+ head = void 0;
170
+ constructor() {
171
+ }
172
+ indexOf(t) {
173
+ let e = this.head, i = 0, r = !1;
174
+ for (; e; ) {
175
+ if (this.equals(e.value, t)) {
176
+ r = !0;
177
+ break;
178
+ }
179
+ i++, e = e.next;
180
+ }
181
+ return r ? i : -1;
182
+ }
183
+ equals(t, e) {
184
+ return !1;
185
+ }
186
+ getElementAt(t) {
187
+ if (t < 0 || t >= this.count)
188
+ return;
189
+ if (t === 0)
190
+ return this.head;
191
+ let e = this.head;
192
+ for (let i = 0; i < t; i++)
193
+ e = e?.next;
194
+ return e;
195
+ }
196
+ getValueAt(t) {
197
+ return this.getElementAt(t)?.value;
198
+ }
199
+ insert(t, e) {
200
+ let i = new o();
201
+ if (i.value = t, e > this.count || e < 0)
202
+ throw new Error("index error");
203
+ this.count++;
204
+ let r, h;
205
+ if (e === 0) {
206
+ i.next = this.head, this.head = i;
207
+ return;
208
+ }
209
+ r = this.getElementAt(e - 1), h = r.next, r.next = i, i.next = h;
210
+ }
211
+ push(t) {
212
+ let e = new o();
213
+ if (e.value = t, this.count++, this.isEmpty()) {
214
+ this.head = e;
215
+ return;
216
+ }
217
+ let i = this.getElementAt(this.count - 1);
218
+ i.next = e;
219
+ }
220
+ remove(t) {
221
+ const e = this.indexOf(t);
222
+ return e === -1 ? void 0 : this.removeAt(e);
223
+ }
224
+ removeAt(t) {
225
+ if (this.isEmpty() || t < 0 || t >= this.count)
226
+ return;
227
+ let e = this.getElementAt(t), i = this.getElementAt(t - 1), r = e?.next;
228
+ return t === 0 && (this.head = r), i && (i.next = r), this.count--, e?.value;
229
+ }
230
+ isEmpty() {
231
+ return this.count === 0;
232
+ }
233
+ size() {
234
+ return this.count;
235
+ }
236
+ clear() {
237
+ this.count = 0, this.head = void 0;
238
+ }
239
+ toString() {
240
+ let t = "", e = this.head;
241
+ for (; e; )
242
+ t += e.value, t += ",", e = e.next;
243
+ return t = t.slice(0, -1), t;
244
+ }
245
+ }
246
+ class d {
247
+ key;
248
+ value;
249
+ constructor(t, e) {
250
+ this.key = t, this.value = e;
251
+ }
252
+ }
253
+ function l(s, t = { emptyString: !1, zeroNumber: !1 }) {
254
+ return s == null ? !(t.emptyString && s === "" || t.zeroNumber && s === 0) : !1;
255
+ }
256
+ class g {
257
+ table = [];
258
+ constructor() {
259
+ }
260
+ getItemIndex(t) {
261
+ for (let e = 0, i = this.table.length; e < i; e++)
262
+ if (this.table[e].key === t)
263
+ return e;
264
+ return -1;
265
+ }
266
+ set(t, e) {
267
+ if (l(t))
268
+ throw new Error("key is required");
269
+ if (l(e))
270
+ throw new Error("value is required");
271
+ if (this.has(t)) {
272
+ let i = this.getItemIndex(t);
273
+ this.table[i].value = e;
274
+ } else {
275
+ const i = new d(t, e);
276
+ this.table.push(i);
277
+ }
278
+ }
279
+ remove(t) {
280
+ if (this.has(t)) {
281
+ let e = this.getItemIndex(t);
282
+ return this.table.splice(e, 1)[0];
283
+ }
284
+ }
285
+ has(t) {
286
+ return this.getItemIndex(t) !== -1;
287
+ }
288
+ get(t) {
289
+ if (this.has(t)) {
290
+ let e = this.getItemIndex(t);
291
+ return this.table[e];
292
+ }
293
+ }
294
+ keys() {
295
+ return this.table.map((t) => t.key);
296
+ }
297
+ values() {
298
+ return this.table.map((t) => t.value);
299
+ }
300
+ keyValues() {
301
+ return this.table.map((t) => [t.key, t.value]);
302
+ }
303
+ forEach(t) {
304
+ for (let e = 0, i = this.size(); e < i; e++) {
305
+ let r = this.table[e];
306
+ if (!t(r.key, r.value))
307
+ break;
308
+ }
309
+ }
310
+ isEmpty() {
311
+ return !this.size();
312
+ }
313
+ size() {
314
+ return this.table.length;
315
+ }
316
+ clear() {
317
+ this.table = [];
318
+ }
319
+ toString() {
320
+ let t = "";
321
+ for (let e = 0, i = this.table.length; e < i; e++)
322
+ t += this.table[e].toString(), t += ",";
323
+ return t = t.slice(0, -1), t;
324
+ }
325
+ }
326
+ class y {
327
+ isDirected;
328
+ vertices;
329
+ adjList;
330
+ constructor(t = !1) {
331
+ this.isDirected = t, this.vertices = [], this.adjList = new g();
332
+ }
333
+ addVertex(t) {
334
+ this.vertices.includes(t) || (this.vertices.push(t), this.adjList.set(t, []));
335
+ }
336
+ addEdge(t, e) {
337
+ this.adjList.get(t) || this.addVertex(t), this.adjList.get(e) || this.addVertex(e), this.adjList.get(t)?.value.indexOf(e) === -1 && this.adjList.get(t)?.value.push(e), this.isDirected || this.adjList.get(e)?.value.indexOf(t) === -1 && this.adjList.get(e)?.value.push(t);
338
+ }
339
+ getVertices() {
340
+ return this.vertices;
341
+ }
342
+ getAdjacencyList() {
343
+ return this.adjList;
344
+ }
345
+ toString() {
346
+ let t = "";
347
+ for (let e = 0; e < this.vertices.length; e++)
348
+ t += this.vertices[e] + "-->", t += this.adjList.get(this.vertices[e])?.toString() || "", t += `
349
+ `;
350
+ return t;
351
+ }
352
+ }
353
+ function b(s) {
354
+ return new Promise((t) => setTimeout(t, s));
355
+ }
356
+ function a(s) {
357
+ return s !== null && (typeof s == "object" || typeof s == "function");
358
+ }
359
+ class L {
360
+ map = /* @__PURE__ */ new Map();
361
+ weakMap = /* @__PURE__ */ new WeakMap();
362
+ set(t, e) {
363
+ a(t) ? this.weakMap.set(t, e) : this.map.set(t, e);
364
+ }
365
+ get(t) {
366
+ return a(t) ? this.weakMap.get(t) : this.map.get(t);
367
+ }
368
+ has(t) {
369
+ return a(t) ? this.weakMap.has(t) : this.map.has(t);
370
+ }
371
+ }
372
+ function k(s) {
373
+ if (!s.length) return [];
374
+ const t = [[s[0]]];
375
+ for (let i = 1, r = s.length; i < r; i++) {
376
+ const h = s[i];
377
+ e(h);
378
+ }
379
+ function e(i) {
380
+ for (let r = t.length - 1; r >= 0; r--) {
381
+ const h = t[r], u = h[t[r].length - 1];
382
+ if (u < i) {
383
+ t[r + 1] = [...h, i];
384
+ break;
385
+ } else u > i && r === 0 && (t[r] = [i]);
386
+ }
387
+ }
388
+ return t[t.length - 1];
389
+ }
390
+ function I(s) {
391
+ let t;
392
+ const e = new Proxy(s, {
393
+ construct(i, r, h) {
394
+ return t || (t = Reflect.construct(
395
+ i,
396
+ r,
397
+ h
398
+ )), t;
399
+ }
400
+ });
401
+ return s.prototype.constructor = e, e;
402
+ }
127
403
  export {
128
- c as MaxHeap,
129
- u as MinHeap,
130
- p as Queue,
131
- a as Stack
404
+ g as Dictionary,
405
+ y as Graph,
406
+ k as LIS,
407
+ x as LRU,
408
+ E as LinkedList,
409
+ w as MaxHeap,
410
+ L as MemoizeMap,
411
+ p as MinHeap,
412
+ m as Queue,
413
+ v as Stack,
414
+ I as singleton,
415
+ b as sleep
132
416
  };
@@ -1 +1,2 @@
1
- (function(s,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(s=typeof globalThis<"u"?globalThis:s||self,r(s["jc-structure"]={}))})(this,(function(s){"use strict";class r{items={};count=0;lowestCount=0;constructor(){}dequeue(){if(this.isEmpty())return;const t=this.items[this.lowestCount];return delete this.items[this.lowestCount],this.lowestCount++,t}enqueue(...t){t.forEach(e=>{this.items[this.count]=e,this.count++})}front(){return this.isEmpty()?void 0:this.items[this.lowestCount]}isEmpty(){return this.size()===0}size(){return this.count-this.lowestCount}clear(){this.items={},this.count=0,this.lowestCount=0}toString(){return this.isEmpty()?"":`Queue(size: ${this.size()}):[${this.items[this.lowestCount]},...rest]`}}class a{items={};count=0;constructor(){}pop(){if(this.isEmpty())return;this.count--;const t=this.items[this.count];return delete this.items[this.count],t}push(...t){t.forEach(e=>{this.items[this.count]=e,this.count++})}peek(){return this.isEmpty()?void 0:this.items[this.count-1]}isEmpty(){return this.count===0}size(){return this.count}clear(){this.items={},this.count=0}toString(){return this.isEmpty()?"":`Stack(count: ${this.count}):[${this.items[this.count-1]},...rest]`}}function o(n,t){return n===t?0:n<t?-1:1}class h{heap=[];compareFn;constructor(t=o){this.compareFn=t}static getLeftIndex(t){return 2*t+1}static getRightIndex(t){return 2*t+2}static getParentIndex(t){return t===0?void 0:Math.floor((t-1)/2)}static swap(t,e,i){[t[e],t[i]]=[t[i],t[e]]}find(){return this.isEmpty()?void 0:this.heap[0]}size(){return this.heap.length}isEmpty(){return this.size()===0}clear(){this.heap=[]}toString(){return this.heap.toString()}}class p extends h{insert(t){return t?!1:(this.heap.push(t),this.siftUp(this.heap.length-1),!0)}extract(){if(this.isEmpty())return;if(this.heap.length===1)return this.heap.shift();const t=this.heap[0];return this.heap[0]=this.heap.pop(),this.siftDown(0),t}siftUp(t){let e=t,i=h.getLeftIndex(e),u=h.getRightIndex(e),c=this.size();i<c&&this.compareFn(this.heap[e],this.heap[i])===-1&&(e=i),u<c&&this.compareFn(this.heap[e],this.heap[u])===1&&(e=u),e!==t&&(h.swap(this.heap,t,e),this.siftUp(e))}siftDown(t){let e=h.getParentIndex(t);for(;t>0&&e&&this.compareFn(this.heap[e],this.heap[t])===1;)h.swap(this.heap,e,t),t=e,e=h.getParentIndex(t)}}class f extends p{constructor(t=(e,i)=>o(i,e)){super(t)}}s.MaxHeap=f,s.MinHeap=p,s.Queue=r,s.Stack=a,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(n,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(n=typeof globalThis<"u"?globalThis:n||self,u(n["jc-structure"]={}))})(this,(function(n){"use strict";class u{items={};count=0;lowestCount=0;constructor(){}dequeue(){if(this.isEmpty())return;const t=this.items[this.lowestCount];return delete this.items[this.lowestCount],this.lowestCount++,t}enqueue(...t){t.forEach(e=>{this.items[this.count]=e,this.count++})}front(){return this.isEmpty()?void 0:this.items[this.lowestCount]}isEmpty(){return this.size()===0}size(){return this.count-this.lowestCount}clear(){this.items={},this.count=0,this.lowestCount=0}toString(){return this.isEmpty()?"":`Queue(size: ${this.size()}):[${this.items[this.lowestCount]},...rest]`}}class m{items={};count=0;constructor(){}pop(){if(this.isEmpty())return;this.count--;const t=this.items[this.count];return delete this.items[this.count],t}push(...t){t.forEach(e=>{this.items[this.count]=e,this.count++})}peek(){return this.isEmpty()?void 0:this.items[this.count-1]}isEmpty(){return this.count===0}size(){return this.count}clear(){this.items={},this.count=0}toString(){return this.isEmpty()?"":`Stack(count: ${this.count}):[${this.items[this.count-1]},...rest]`}}function l(s,t){return s===t?0:s<t?-1:1}class a{heap=[];compareFn;constructor(t=l){this.compareFn=t}static getLeftIndex(t){return 2*t+1}static getRightIndex(t){return 2*t+2}static getParentIndex(t){return t===0?void 0:Math.floor((t-1)/2)}static swap(t,e,i){[t[e],t[i]]=[t[i],t[e]]}find(){return this.isEmpty()?void 0:this.heap[0]}size(){return this.heap.length}isEmpty(){return this.size()===0}clear(){this.heap=[]}toString(){return this.heap.toString()}}class c extends a{insert(t){return t?!1:(this.heap.push(t),this.siftUp(this.heap.length-1),!0)}extract(){if(this.isEmpty())return;if(this.heap.length===1)return this.heap.shift();const t=this.heap[0];return this.heap[0]=this.heap.pop(),this.siftDown(0),t}siftUp(t){let e=t,i=a.getLeftIndex(e),r=a.getRightIndex(e),h=this.size();i<h&&this.compareFn(this.heap[e],this.heap[i])===-1&&(e=i),r<h&&this.compareFn(this.heap[e],this.heap[r])===1&&(e=r),e!==t&&(a.swap(this.heap,t,e),this.siftUp(e))}siftDown(t){let e=a.getParentIndex(t);for(;t>0&&e&&this.compareFn(this.heap[e],this.heap[t])===1;)a.swap(this.heap,e,t),t=e,e=a.getParentIndex(t)}}class v extends c{constructor(t=(e,i)=>l(i,e)){super(t)}}function w(s){return{value:s}}class y{capacity;length=0;head=null;tail=null;lookup=new Map;reverseLookup=new Map;constructor(t=10){this.capacity=t}prepend(t){this.head?(t.next=this.head,this.head.prev=t,this.head=t):this.head=this.tail=t}detach(t){t.prev&&(t.prev.next=t.next),t.next&&(t.next.prev=t.prev),this.head===t&&(this.head=this.head.next||null),this.tail===t&&(this.tail=this.tail.prev||null),t.next=void 0,t.prev=void 0}trimCache(){if(this.length<=this.capacity)return;const t=this.tail;this.detach(t);const e=this.reverseLookup.get(t);this.lookup.delete(e),this.reverseLookup.delete(t),this.length--}get(t){const e=this.lookup.get(t);if(e)return this.detach(e),this.prepend(e),e.value}update(t,e){let i=this.lookup.get(t);i?(this.detach(i),this.prepend(i),i.value=e):(i=w(e),this.length++,this.prepend(i),this.trimCache(),this.lookup.set(t,i),this.reverseLookup)}}class p{value;next=void 0}class E{count=0;head=void 0;constructor(){}indexOf(t){let e=this.head,i=0,r=!1;for(;e;){if(this.equals(e.value,t)){r=!0;break}i++,e=e.next}return r?i:-1}equals(t,e){return!1}getElementAt(t){if(t<0||t>=this.count)return;if(t===0)return this.head;let e=this.head;for(let i=0;i<t;i++)e=e?.next;return e}getValueAt(t){return this.getElementAt(t)?.value}insert(t,e){let i=new p;if(i.value=t,e>this.count||e<0)throw new Error("index error");this.count++;let r,h;if(e===0){i.next=this.head,this.head=i;return}r=this.getElementAt(e-1),h=r.next,r.next=i,i.next=h}push(t){let e=new p;if(e.value=t,this.count++,this.isEmpty()){this.head=e;return}let i=this.getElementAt(this.count-1);i.next=e}remove(t){const e=this.indexOf(t);return e===-1?void 0:this.removeAt(e)}removeAt(t){if(this.isEmpty()||t<0||t>=this.count)return;let e=this.getElementAt(t),i=this.getElementAt(t-1),r=e?.next;return t===0&&(this.head=r),i&&(i.next=r),this.count--,e?.value}isEmpty(){return this.count===0}size(){return this.count}clear(){this.count=0,this.head=void 0}toString(){let t="",e=this.head;for(;e;)t+=e.value,t+=",",e=e.next;return t=t.slice(0,-1),t}}class L{key;value;constructor(t,e){this.key=t,this.value=e}}function f(s,t={emptyString:!1,zeroNumber:!1}){return s==null?!(t.emptyString&&s===""||t.zeroNumber&&s===0):!1}class d{table=[];constructor(){}getItemIndex(t){for(let e=0,i=this.table.length;e<i;e++)if(this.table[e].key===t)return e;return-1}set(t,e){if(f(t))throw new Error("key is required");if(f(e))throw new Error("value is required");if(this.has(t)){let i=this.getItemIndex(t);this.table[i].value=e}else{const i=new L(t,e);this.table.push(i)}}remove(t){if(this.has(t)){let e=this.getItemIndex(t);return this.table.splice(e,1)[0]}}has(t){return this.getItemIndex(t)!==-1}get(t){if(this.has(t)){let e=this.getItemIndex(t);return this.table[e]}}keys(){return this.table.map(t=>t.key)}values(){return this.table.map(t=>t.value)}keyValues(){return this.table.map(t=>[t.key,t.value])}forEach(t){for(let e=0,i=this.size();e<i;e++){let r=this.table[e];if(!t(r.key,r.value))break}}isEmpty(){return!this.size()}size(){return this.table.length}clear(){this.table=[]}toString(){let t="";for(let e=0,i=this.table.length;e<i;e++)t+=this.table[e].toString(),t+=",";return t=t.slice(0,-1),t}}class b{isDirected;vertices;adjList;constructor(t=!1){this.isDirected=t,this.vertices=[],this.adjList=new d}addVertex(t){this.vertices.includes(t)||(this.vertices.push(t),this.adjList.set(t,[]))}addEdge(t,e){this.adjList.get(t)||this.addVertex(t),this.adjList.get(e)||this.addVertex(e),this.adjList.get(t)?.value.indexOf(e)===-1&&this.adjList.get(t)?.value.push(e),this.isDirected||this.adjList.get(e)?.value.indexOf(t)===-1&&this.adjList.get(e)?.value.push(t)}getVertices(){return this.vertices}getAdjacencyList(){return this.adjList}toString(){let t="";for(let e=0;e<this.vertices.length;e++)t+=this.vertices[e]+"-->",t+=this.adjList.get(this.vertices[e])?.toString()||"",t+=`
2
+ `;return t}}function k(s){return new Promise(t=>setTimeout(t,s))}function o(s){return s!==null&&(typeof s=="object"||typeof s=="function")}class x{map=new Map;weakMap=new WeakMap;set(t,e){o(t)?this.weakMap.set(t,e):this.map.set(t,e)}get(t){return o(t)?this.weakMap.get(t):this.map.get(t)}has(t){return o(t)?this.weakMap.has(t):this.map.has(t)}}function I(s){if(!s.length)return[];const t=[[s[0]]];for(let i=1,r=s.length;i<r;i++){const h=s[i];e(h)}function e(i){for(let r=t.length-1;r>=0;r--){const h=t[r],g=h[t[r].length-1];if(g<i){t[r+1]=[...h,i];break}else g>i&&r===0&&(t[r]=[i])}}return t[t.length-1]}function M(s){let t;const e=new Proxy(s,{construct(i,r,h){return t||(t=Reflect.construct(i,r,h)),t}});return s.prototype.constructor=e,e}n.Dictionary=d,n.Graph=b,n.LIS=I,n.LRU=y,n.LinkedList=E,n.MaxHeap=v,n.MemoizeMap=x,n.MinHeap=c,n.Queue=u,n.Stack=m,n.singleton=M,n.sleep=k,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
package/index.d.ts CHANGED
@@ -112,4 +112,224 @@ declare module "jc-structure" {
112
112
  export class MaxHeap<T> extends MinHeap<T> {
113
113
  constructor(fn?: (a: T, b: T) => CompareResult);
114
114
  }
115
+
116
+ /**
117
+ * LRU缓存类
118
+ */
119
+ export class LRU<K, V> {
120
+ constructor(capacity?: number);
121
+ /**
122
+ * 获取缓存的值
123
+ * @param key 键值
124
+ */
125
+ get(key: K): V | undefined;
126
+ /**
127
+ * 更新缓存
128
+ * @param key 键值
129
+ * @param value 缓存的值
130
+ */
131
+ update(key: K, value: V): void;
132
+ }
133
+
134
+ /**
135
+ * 链表节点类
136
+ */
137
+ export class LinkedList<T> {
138
+ constructor();
139
+ /**
140
+ * #### 向链表中添加元素
141
+ * @param element 要添加的元素
142
+ */
143
+ push(element: T): void;
144
+
145
+ /**
146
+ * #### 在指定位置插入元素
147
+ * @param element 要插入的元素
148
+ * @param index 插入的位置索引
149
+ */
150
+ insert(element: T, index: number): void;
151
+
152
+ /**
153
+ * #### 获取指定位置的节点
154
+ * @param index 指定位置索引
155
+ */
156
+ getElementAt(index: number): Node<T> | undefined;
157
+
158
+ /**
159
+ * #### 获取指定位置的值
160
+ * @param index 指定位置索引
161
+ */
162
+ getValueAt(index: number): T | undefined;
163
+
164
+ /**
165
+ * #### 移除指定位置的元素
166
+ * @param index 指定位置索引
167
+ */
168
+ removeAt(index: number): T | undefined;
169
+
170
+ /**
171
+ * #### 移除指定元素
172
+ * @param element 指定元素
173
+ */
174
+ remove(element: T): T | undefined;
175
+
176
+ /**
177
+ * #### 获取指定元素的索引
178
+ * @param element 指定元素
179
+ */
180
+ indexOf(element: T): number | undefined;
181
+
182
+ /**
183
+ * 判断数据结构是否为空
184
+ */
185
+ isEmpty(): boolean;
186
+ /**
187
+ * 数据结构元素长度
188
+ */
189
+ size(): number;
190
+ /**
191
+ * 重写 toString 方法
192
+ */
193
+ toString(): string;
194
+ /**
195
+ * 清除数据结构元素
196
+ */
197
+ clear(): void;
198
+ }
199
+
200
+ /**
201
+ * 字典类
202
+ */
203
+ export class Dictionary<K, V> {
204
+ constructor();
205
+ /**
206
+ * #### 设置键值对
207
+ * @param key 键
208
+ * @param value 值
209
+ */
210
+ set(key: K, value: V): void;
211
+
212
+ /**
213
+ * #### 删除键值对
214
+ * @param key 键
215
+ */
216
+ remove(key: K): ValuePair<K, V> | undefined;
217
+
218
+ /**
219
+ * #### 判断键是否存在
220
+ * @param key 键
221
+ */
222
+ has(key: K): boolean;
223
+
224
+ /**
225
+ * #### 获取键值对
226
+ * @param key 键
227
+ */
228
+ get(key: K): ValuePair<K, V> | undefined;
229
+
230
+ /**
231
+ * #### 获取键数组
232
+ */
233
+ keys(): Array<K>;
234
+
235
+ /**
236
+ * #### 获取值数组
237
+ */
238
+ values(): Array<V>;
239
+
240
+ /**
241
+ * #### 获取键值对数组
242
+ */
243
+ keyValues(): Array<[K, V]>;
244
+
245
+ /**
246
+ * 遍历键值对
247
+ * @param callbackFunc 回调函数
248
+ */
249
+ forEach(callbackFunc: (key: K, value: V) => boolean | void): void;
250
+
251
+ /**
252
+ * 判断数据结构是否为空
253
+ */
254
+ isEmpty(): boolean;
255
+ /**
256
+ * 数据结构元素长度
257
+ */
258
+ size(): number;
259
+ /**
260
+ * 重写 toString 方法
261
+ */
262
+ toString(): string;
263
+ /**
264
+ * 清除数据结构元素
265
+ */
266
+ clear(): void;
267
+ }
268
+
269
+ /**
270
+ * 图类
271
+ */
272
+ export class Graph<T> implements IGraph<T> {
273
+ constructor(isDirected?: boolean);
274
+ /**
275
+ * #### 添加顶点的方法
276
+ * @param v 顶点
277
+ */
278
+ addVertex(v: T): void;
279
+ /**
280
+ * #### 添加边的方法
281
+ * @param v 顶点
282
+ * @param w
283
+ */
284
+ addEdge(v: T, w: T): void;
285
+ /**
286
+ * #### 获取顶点集合的方法
287
+ */
288
+ getVertices(): Array<T>;
289
+ /**
290
+ * #### 获取邻接表的方法
291
+ */
292
+ getAdjacencyList(): IDictionary<T, Array<T>>;
293
+ }
294
+
295
+ /**
296
+ * ### 缓存类
297
+ */
298
+ export class MemoizeMap {
299
+ /**
300
+ * 根据键的类型,判断缓存在 Map 还是 WeakMap中
301
+ * @param key 键值
302
+ * @param value 值
303
+ */
304
+ set(key: unknown, value: unknown): void;
305
+ /**
306
+ * #### 根据键获取缓存的值
307
+ * @param key
308
+ */
309
+ get(key: unknown): any;
310
+ /**
311
+ * #### 判断缓存中是否存在该键
312
+ * @param key
313
+ */
314
+ has(key: unknown): boolean;
315
+ }
316
+
317
+ /**
318
+ * #### 单例模式
319
+ * @param classCtor 类构造函数
320
+ */
321
+ export function singleton<T extends new (...args: any[]) => object>(
322
+ classCtor: T
323
+ ): T;
324
+
325
+ /**
326
+ * #### 最长递增子序列
327
+ * @param nums
328
+ */
329
+ export function LIS(nums: Array<number>): Array<number>;
330
+ /**
331
+ * #### 睡眠函数
332
+ * @param ms 毫秒
333
+ */
334
+ export function sleep(ms: number): Promise<void>;
115
335
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "jc-structure",
3
3
  "private": false,
4
- "version": "0.0.7",
4
+ "version": "0.0.9",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist",
@@ -37,5 +37,8 @@
37
37
  "typescript": "~5.9.2",
38
38
  "vite": "^7.1.5",
39
39
  "vitest": "^4.0.15"
40
+ },
41
+ "dependencies": {
42
+ "es-toolkit": "^1.42.0"
40
43
  }
41
44
  }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * #### 字典接口
3
+ */
4
+ interface IDictionary<K, V> extends Structure {
5
+ /**
6
+ * #### 设置键值对
7
+ * @param key 键
8
+ * @param value 值
9
+ */
10
+ set(key: K, value: V): void;
11
+
12
+ /**
13
+ * #### 删除键值对
14
+ * @param key 键
15
+ */
16
+ remove(key: K): ValuePair<K, V> | undefined;
17
+
18
+ /**
19
+ * #### 判断键是否存在
20
+ * @param key 键
21
+ */
22
+ has(key: K): boolean;
23
+
24
+ /**
25
+ * #### 获取键值对
26
+ * @param key 键
27
+ */
28
+ get(key: K): ValuePair<K, V> | undefined;
29
+
30
+ /**
31
+ * #### 获取键数组
32
+ */
33
+ keys(): Array<K>;
34
+
35
+ /**
36
+ * #### 获取值数组
37
+ */
38
+ values(): Array<V>;
39
+
40
+ /**
41
+ * #### 获取键值对数组
42
+ */
43
+ keyValues(): Array<[K, V]>;
44
+
45
+ /**
46
+ * 遍历键值对
47
+ * @param callbackFunc 回调函数
48
+ */
49
+ forEach(callbackFunc: (key: K, value: V) => boolean | void): void;
50
+ }
@@ -0,0 +1,22 @@
1
+ interface String {
2
+ /**
3
+ * #### 获取字符串码点长度,1个码点对应1个码元或2个码元,譬如 "👍" 长度为2,"a" 长度为1
4
+ * - 计算字符串的码点长度,考虑到 Unicode 字符的特殊性
5
+ * @returns {number} 字符串的码元长度
6
+ */
7
+ pointLength(this: string): number;
8
+ /**
9
+ * #### 根据码点索引获取对应的字符
10
+ * - 处理 Unicode 字符,确保正确获取码点对应的字符
11
+ * @param index 码点索引
12
+ */
13
+ pointAt(this: string, index: number): string | undefined;
14
+
15
+ /**
16
+ * #### 根据码点索引切割字符串
17
+ * - 按照码点索引切割字符串,确保不会破坏 Unicode 字符
18
+ * @param start 开始索引
19
+ * @param end 结束索引
20
+ */
21
+ sliceByPoint(this: string, start: number, end?: number): string;
22
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * #### 图接口
3
+ */
4
+ interface IGraph<T> {
5
+ /**
6
+ * #### 添加顶点的方法
7
+ * @param v 顶点
8
+ */
9
+ addVertex(v: T): void;
10
+ /**
11
+ * #### 添加边的方法
12
+ * @param v 顶点
13
+ * @param w
14
+ */
15
+ addEdge(v: T, w: T): void;
16
+ /**
17
+ * #### 获取顶点集合的方法
18
+ */
19
+ getVertices(): Array<T>;
20
+ /**
21
+ * #### 获取邻接表的方法
22
+ */
23
+ getAdjacencyList(): IDictionary<T, Array<T>>;
24
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * #### 链表接口
3
+ */
4
+ interface ILinkedList<T> extends Structure {
5
+ /**
6
+ * #### 向链表中添加元素
7
+ * @param element 要添加的元素
8
+ */
9
+ push(element: T): void;
10
+
11
+ /**
12
+ * #### 在指定位置插入元素
13
+ * @param element 要插入的元素
14
+ * @param index 插入的位置索引
15
+ */
16
+ insert(element: T, index: number): void;
17
+
18
+ /**
19
+ * #### 获取指定位置的节点
20
+ * @param index 指定位置索引
21
+ */
22
+ getElementAt(index: number): Node<T> | undefined;
23
+
24
+ /**
25
+ * #### 获取指定位置的值
26
+ * @param index 指定位置索引
27
+ */
28
+ getValueAt(index: number): T | undefined;
29
+
30
+ /**
31
+ * #### 移除指定位置的元素
32
+ * @param index 指定位置索引
33
+ */
34
+ removeAt(index: number): T | undefined;
35
+
36
+ /**
37
+ * #### 移除指定元素
38
+ * @param element 指定元素
39
+ */
40
+ remove(element: T): T | undefined;
41
+
42
+ /**
43
+ * #### 获取指定元素的索引
44
+ * @param element 指定元素
45
+ */
46
+ indexOf(element: T): number | undefined;
47
+ }