jc-structure 0.0.6 → 0.0.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.
- package/dist/jc-structure.js +316 -8
- package/dist/jc-structure.umd.cjs +2 -1
- package/index.d.ts +232 -0
- package/package.json +1 -1
- package/types/dictionary.d.ts +50 -0
- package/types/global.d.ts +6 -0
- package/types/graph.d.ts +24 -0
- package/types/heap.d.ts +18 -0
- package/types/linkedList.d.ts +47 -0
package/dist/jc-structure.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class f {
|
|
2
2
|
items = {};
|
|
3
3
|
count = 0;
|
|
4
4
|
lowestCount = 0;
|
|
@@ -10,8 +10,8 @@ class e {
|
|
|
10
10
|
return delete this.items[this.lowestCount], this.lowestCount++, t;
|
|
11
11
|
}
|
|
12
12
|
enqueue(...t) {
|
|
13
|
-
t.forEach((
|
|
14
|
-
this.items[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 e {
|
|
|
30
30
|
return this.isEmpty() ? "" : `Queue(size: ${this.size()}):[${this.items[this.lowestCount]},...rest]`;
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
-
class
|
|
33
|
+
class g {
|
|
34
34
|
items = {};
|
|
35
35
|
count = 0;
|
|
36
36
|
constructor() {
|
|
@@ -42,8 +42,8 @@ class o {
|
|
|
42
42
|
return delete this.items[this.count], t;
|
|
43
43
|
}
|
|
44
44
|
push(...t) {
|
|
45
|
-
t.forEach((
|
|
46
|
-
this.items[this.count] =
|
|
45
|
+
t.forEach((e) => {
|
|
46
|
+
this.items[this.count] = e, this.count++;
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
49
|
peek() {
|
|
@@ -62,7 +62,315 @@ class o {
|
|
|
62
62
|
return this.isEmpty() ? "" : `Stack(count: ${this.count}):[${this.items[this.count - 1]},...rest]`;
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
+
function o(s, t) {
|
|
66
|
+
return s === t ? 0 : s < t ? -1 : 1;
|
|
67
|
+
}
|
|
68
|
+
class n {
|
|
69
|
+
heap = [];
|
|
70
|
+
compareFn;
|
|
71
|
+
constructor(t = o) {
|
|
72
|
+
this.compareFn = t;
|
|
73
|
+
}
|
|
74
|
+
static getLeftIndex(t) {
|
|
75
|
+
return 2 * t + 1;
|
|
76
|
+
}
|
|
77
|
+
static getRightIndex(t) {
|
|
78
|
+
return 2 * t + 2;
|
|
79
|
+
}
|
|
80
|
+
static getParentIndex(t) {
|
|
81
|
+
return t === 0 ? void 0 : Math.floor((t - 1) / 2);
|
|
82
|
+
}
|
|
83
|
+
static swap(t, e, i) {
|
|
84
|
+
[t[e], t[i]] = [t[i], t[e]];
|
|
85
|
+
}
|
|
86
|
+
find() {
|
|
87
|
+
return this.isEmpty() ? void 0 : this.heap[0];
|
|
88
|
+
}
|
|
89
|
+
size() {
|
|
90
|
+
return this.heap.length;
|
|
91
|
+
}
|
|
92
|
+
isEmpty() {
|
|
93
|
+
return this.size() === 0;
|
|
94
|
+
}
|
|
95
|
+
clear() {
|
|
96
|
+
this.heap = [];
|
|
97
|
+
}
|
|
98
|
+
toString() {
|
|
99
|
+
return this.heap.toString();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
class l extends n {
|
|
103
|
+
insert(t) {
|
|
104
|
+
return t ? !1 : (this.heap.push(t), this.siftUp(this.heap.length - 1), !0);
|
|
105
|
+
}
|
|
106
|
+
extract() {
|
|
107
|
+
if (this.isEmpty()) return;
|
|
108
|
+
if (this.heap.length === 1) return this.heap.shift();
|
|
109
|
+
const t = this.heap[0];
|
|
110
|
+
return this.heap[0] = this.heap.pop(), this.siftDown(0), t;
|
|
111
|
+
}
|
|
112
|
+
siftUp(t) {
|
|
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
|
+
}
|
|
116
|
+
siftDown(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
|
+
}
|
|
121
|
+
}
|
|
122
|
+
class m extends l {
|
|
123
|
+
constructor(t = (e, i) => o(i, e)) {
|
|
124
|
+
super(t);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function c(s) {
|
|
128
|
+
return { value: s };
|
|
129
|
+
}
|
|
130
|
+
class v {
|
|
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 = c(e), this.length++, this.prepend(i), this.trimCache(), this.lookup.set(t, i), this.reverseLookup);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
class u {
|
|
164
|
+
value;
|
|
165
|
+
next = void 0;
|
|
166
|
+
}
|
|
167
|
+
class x {
|
|
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 u();
|
|
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 u();
|
|
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 p {
|
|
247
|
+
key;
|
|
248
|
+
value;
|
|
249
|
+
constructor(t, e) {
|
|
250
|
+
this.key = t, this.value = e;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function a(s, t = { emptyString: !1, zeroNumber: !1 }) {
|
|
254
|
+
return s == null ? !(t.emptyString && s === "" || t.zeroNumber && s === 0) : !1;
|
|
255
|
+
}
|
|
256
|
+
class d {
|
|
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 (a(t))
|
|
268
|
+
throw new Error("key is required");
|
|
269
|
+
if (a(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 p(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 d();
|
|
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 E(s) {
|
|
354
|
+
let t;
|
|
355
|
+
const e = new Proxy(s, {
|
|
356
|
+
construct(i, r, h) {
|
|
357
|
+
return t || (t = Reflect.construct(
|
|
358
|
+
i,
|
|
359
|
+
r,
|
|
360
|
+
h
|
|
361
|
+
)), t;
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
return s.prototype.constructor = e, e;
|
|
365
|
+
}
|
|
65
366
|
export {
|
|
66
|
-
|
|
67
|
-
|
|
367
|
+
d as Dictionary,
|
|
368
|
+
y as Graph,
|
|
369
|
+
v as LRU,
|
|
370
|
+
x as LinkedList,
|
|
371
|
+
m as MaxHeap,
|
|
372
|
+
l as MinHeap,
|
|
373
|
+
f as Queue,
|
|
374
|
+
g as Stack,
|
|
375
|
+
E as singleton
|
|
68
376
|
};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(h,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(h=typeof globalThis<"u"?globalThis:h||self,a(h["jc-structure"]={}))})(this,(function(h){"use strict";class a{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 f{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(s,t){return s===t?0:s<t?-1:1}class u{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 l extends u{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=u.getLeftIndex(e),r=u.getRightIndex(e),n=this.size();i<n&&this.compareFn(this.heap[e],this.heap[i])===-1&&(e=i),r<n&&this.compareFn(this.heap[e],this.heap[r])===1&&(e=r),e!==t&&(u.swap(this.heap,t,e),this.siftUp(e))}siftDown(t){let e=u.getParentIndex(t);for(;t>0&&e&&this.compareFn(this.heap[e],this.heap[t])===1;)u.swap(this.heap,e,t),t=e,e=u.getParentIndex(t)}}class g extends l{constructor(t=(e,i)=>o(i,e)){super(t)}}function m(s){return{value:s}}class v{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=m(e),this.length++,this.prepend(i),this.trimCache(),this.lookup.set(t,i),this.reverseLookup)}}class c{value;next=void 0}class y{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 c;if(i.value=t,e>this.count||e<0)throw new Error("index error");this.count++;let r,n;if(e===0){i.next=this.head,this.head=i;return}r=this.getElementAt(e-1),n=r.next,r.next=i,i.next=n}push(t){let e=new c;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 E{key;value;constructor(t,e){this.key=t,this.value=e}}function p(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(p(t))throw new Error("key is required");if(p(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 E(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 w{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 x(s){let t;const e=new Proxy(s,{construct(i,r,n){return t||(t=Reflect.construct(i,r,n)),t}});return s.prototype.constructor=e,e}h.Dictionary=d,h.Graph=w,h.LRU=v,h.LinkedList=y,h.MaxHeap=g,h.MinHeap=l,h.Queue=a,h.Stack=f,h.singleton=x,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})}));
|
package/index.d.ts
CHANGED
|
@@ -35,6 +35,9 @@ declare module "jc-structure" {
|
|
|
35
35
|
clear(): void;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
/**
|
|
39
|
+
* 队列类,采用object实现
|
|
40
|
+
*/
|
|
38
41
|
export class Queue<T> implements IQueue<T> {
|
|
39
42
|
constructor();
|
|
40
43
|
/**
|
|
@@ -67,4 +70,233 @@ declare module "jc-structure" {
|
|
|
67
70
|
*/
|
|
68
71
|
clear(): void;
|
|
69
72
|
}
|
|
73
|
+
|
|
74
|
+
// export function compareFn(a: any, b: any): CompareResult;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 最小堆
|
|
78
|
+
*/
|
|
79
|
+
export class MinHeap<T> {
|
|
80
|
+
constructor(fn?: (a: T, b: T) => CompareResult);
|
|
81
|
+
/**
|
|
82
|
+
* 向堆中添加元素
|
|
83
|
+
* @param args 要添加的元素,可以是多个或一个元素。
|
|
84
|
+
*/
|
|
85
|
+
insert(...args: Array<T>): void;
|
|
86
|
+
/**
|
|
87
|
+
* #### 移除最小值或最大值,返回该值
|
|
88
|
+
*/
|
|
89
|
+
extract(): T | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* 查找一个值
|
|
92
|
+
* @param value 要查找的值
|
|
93
|
+
*/
|
|
94
|
+
find(): T | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* 判断数据结构是否为空
|
|
97
|
+
*/
|
|
98
|
+
isEmpty(): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* 数据结构元素长度
|
|
101
|
+
*/
|
|
102
|
+
size(): number;
|
|
103
|
+
/**
|
|
104
|
+
* 重写 toString 方法
|
|
105
|
+
*/
|
|
106
|
+
toString(): string;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 最大堆
|
|
111
|
+
*/
|
|
112
|
+
export class MaxHeap<T> extends MinHeap<T> {
|
|
113
|
+
constructor(fn?: (a: T, b: T) => CompareResult);
|
|
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
|
+
* @param classCtor 类构造函数
|
|
298
|
+
*/
|
|
299
|
+
export function singleton<T extends new (...args: any[]) => object>(
|
|
300
|
+
classCtor: T
|
|
301
|
+
): T;
|
|
70
302
|
}
|
package/package.json
CHANGED
|
@@ -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
|
+
}
|
package/types/global.d.ts
CHANGED
package/types/graph.d.ts
ADDED
|
@@ -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
|
+
}
|
package/types/heap.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface IHeap<T> extends Structure {
|
|
2
|
+
/**
|
|
3
|
+
* #### 插入一个值,返回一个布尔值
|
|
4
|
+
* @param value 要插入的值
|
|
5
|
+
*/
|
|
6
|
+
insert(value: T): boolean;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* #### 移除最小值或最大值,返回该值
|
|
10
|
+
*/
|
|
11
|
+
extract(): T | undefined;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 查找一个值
|
|
15
|
+
* @param value 要查找的值
|
|
16
|
+
*/
|
|
17
|
+
find(): T | undefined;
|
|
18
|
+
}
|
|
@@ -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
|
+
}
|