jc-structure 0.0.8 → 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.
- package/dist/jc-structure.js +70 -30
- package/dist/jc-structure.umd.cjs +2 -2
- package/index.d.ts +33 -0
- package/package.json +4 -1
- package/types/extend.d.ts +22 -0
package/dist/jc-structure.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class m {
|
|
2
2
|
items = {};
|
|
3
3
|
count = 0;
|
|
4
4
|
lowestCount = 0;
|
|
@@ -30,7 +30,7 @@ class f {
|
|
|
30
30
|
return this.isEmpty() ? "" : `Queue(size: ${this.size()}):[${this.items[this.lowestCount]},...rest]`;
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
-
class
|
|
33
|
+
class v {
|
|
34
34
|
items = {};
|
|
35
35
|
count = 0;
|
|
36
36
|
constructor() {
|
|
@@ -62,13 +62,13 @@ class g {
|
|
|
62
62
|
return this.isEmpty() ? "" : `Stack(count: ${this.count}):[${this.items[this.count - 1]},...rest]`;
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
-
function
|
|
65
|
+
function c(s, t) {
|
|
66
66
|
return s === t ? 0 : s < t ? -1 : 1;
|
|
67
67
|
}
|
|
68
68
|
class n {
|
|
69
69
|
heap = [];
|
|
70
70
|
compareFn;
|
|
71
|
-
constructor(t =
|
|
71
|
+
constructor(t = c) {
|
|
72
72
|
this.compareFn = t;
|
|
73
73
|
}
|
|
74
74
|
static getLeftIndex(t) {
|
|
@@ -99,7 +99,7 @@ class n {
|
|
|
99
99
|
return this.heap.toString();
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
|
-
class
|
|
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
|
}
|
|
@@ -119,15 +119,15 @@ class l extends n {
|
|
|
119
119
|
n.swap(this.heap, e, t), t = e, e = n.getParentIndex(t);
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
|
-
class
|
|
123
|
-
constructor(t = (e, i) =>
|
|
122
|
+
class w extends p {
|
|
123
|
+
constructor(t = (e, i) => c(i, e)) {
|
|
124
124
|
super(t);
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
|
-
function
|
|
127
|
+
function f(s) {
|
|
128
128
|
return { value: s };
|
|
129
129
|
}
|
|
130
|
-
class
|
|
130
|
+
class x {
|
|
131
131
|
capacity;
|
|
132
132
|
length = 0;
|
|
133
133
|
head = null;
|
|
@@ -157,14 +157,14 @@ class v {
|
|
|
157
157
|
}
|
|
158
158
|
update(t, e) {
|
|
159
159
|
let i = this.lookup.get(t);
|
|
160
|
-
i ? (this.detach(i), this.prepend(i), i.value = e) : (i =
|
|
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
161
|
}
|
|
162
162
|
}
|
|
163
|
-
class
|
|
163
|
+
class o {
|
|
164
164
|
value;
|
|
165
165
|
next = void 0;
|
|
166
166
|
}
|
|
167
|
-
class
|
|
167
|
+
class E {
|
|
168
168
|
count = 0;
|
|
169
169
|
head = void 0;
|
|
170
170
|
constructor() {
|
|
@@ -197,7 +197,7 @@ class x {
|
|
|
197
197
|
return this.getElementAt(t)?.value;
|
|
198
198
|
}
|
|
199
199
|
insert(t, e) {
|
|
200
|
-
let i = new
|
|
200
|
+
let i = new o();
|
|
201
201
|
if (i.value = t, e > this.count || e < 0)
|
|
202
202
|
throw new Error("index error");
|
|
203
203
|
this.count++;
|
|
@@ -209,7 +209,7 @@ class x {
|
|
|
209
209
|
r = this.getElementAt(e - 1), h = r.next, r.next = i, i.next = h;
|
|
210
210
|
}
|
|
211
211
|
push(t) {
|
|
212
|
-
let e = new
|
|
212
|
+
let e = new o();
|
|
213
213
|
if (e.value = t, this.count++, this.isEmpty()) {
|
|
214
214
|
this.head = e;
|
|
215
215
|
return;
|
|
@@ -243,17 +243,17 @@ class x {
|
|
|
243
243
|
return t = t.slice(0, -1), t;
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
|
-
class
|
|
246
|
+
class d {
|
|
247
247
|
key;
|
|
248
248
|
value;
|
|
249
249
|
constructor(t, e) {
|
|
250
250
|
this.key = t, this.value = e;
|
|
251
251
|
}
|
|
252
252
|
}
|
|
253
|
-
function
|
|
253
|
+
function l(s, t = { emptyString: !1, zeroNumber: !1 }) {
|
|
254
254
|
return s == null ? !(t.emptyString && s === "" || t.zeroNumber && s === 0) : !1;
|
|
255
255
|
}
|
|
256
|
-
class
|
|
256
|
+
class g {
|
|
257
257
|
table = [];
|
|
258
258
|
constructor() {
|
|
259
259
|
}
|
|
@@ -264,15 +264,15 @@ class d {
|
|
|
264
264
|
return -1;
|
|
265
265
|
}
|
|
266
266
|
set(t, e) {
|
|
267
|
-
if (
|
|
267
|
+
if (l(t))
|
|
268
268
|
throw new Error("key is required");
|
|
269
|
-
if (
|
|
269
|
+
if (l(e))
|
|
270
270
|
throw new Error("value is required");
|
|
271
271
|
if (this.has(t)) {
|
|
272
272
|
let i = this.getItemIndex(t);
|
|
273
273
|
this.table[i].value = e;
|
|
274
274
|
} else {
|
|
275
|
-
const i = new
|
|
275
|
+
const i = new d(t, e);
|
|
276
276
|
this.table.push(i);
|
|
277
277
|
}
|
|
278
278
|
}
|
|
@@ -328,7 +328,7 @@ class y {
|
|
|
328
328
|
vertices;
|
|
329
329
|
adjList;
|
|
330
330
|
constructor(t = !1) {
|
|
331
|
-
this.isDirected = t, this.vertices = [], this.adjList = new
|
|
331
|
+
this.isDirected = t, this.vertices = [], this.adjList = new g();
|
|
332
332
|
}
|
|
333
333
|
addVertex(t) {
|
|
334
334
|
this.vertices.includes(t) || (this.vertices.push(t), this.adjList.set(t, []));
|
|
@@ -350,7 +350,44 @@ class y {
|
|
|
350
350
|
return t;
|
|
351
351
|
}
|
|
352
352
|
}
|
|
353
|
-
function
|
|
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) {
|
|
354
391
|
let t;
|
|
355
392
|
const e = new Proxy(s, {
|
|
356
393
|
construct(i, r, h) {
|
|
@@ -364,13 +401,16 @@ function E(s) {
|
|
|
364
401
|
return s.prototype.constructor = e, e;
|
|
365
402
|
}
|
|
366
403
|
export {
|
|
367
|
-
|
|
404
|
+
g as Dictionary,
|
|
368
405
|
y as Graph,
|
|
369
|
-
|
|
370
|
-
x as
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
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
|
|
376
416
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
`;return t}}function x(s){let t;const e=new Proxy(s,{construct(i,r,
|
|
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
|
@@ -292,6 +292,28 @@ declare module "jc-structure" {
|
|
|
292
292
|
getAdjacencyList(): IDictionary<T, Array<T>>;
|
|
293
293
|
}
|
|
294
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
|
+
|
|
295
317
|
/**
|
|
296
318
|
* #### 单例模式
|
|
297
319
|
* @param classCtor 类构造函数
|
|
@@ -299,4 +321,15 @@ declare module "jc-structure" {
|
|
|
299
321
|
export function singleton<T extends new (...args: any[]) => object>(
|
|
300
322
|
classCtor: T
|
|
301
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>;
|
|
302
335
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jc-structure",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
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,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
|
+
}
|