jc-structure 0.2.8 → 0.2.10

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.
@@ -236,7 +236,6 @@ class G {
236
236
  count = 0;
237
237
  head = null;
238
238
  tail = null;
239
- // 添加尾指针优化
240
239
  constructor(t) {
241
240
  t && t.forEach((e) => this.push(e));
242
241
  }
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /// <reference path="./types/index.d.ts" />
2
2
 
3
3
  declare module "jc-structure" {
4
+ //#region data structure
4
5
  /**
5
6
  * 栈类,采用object实现
6
7
  */
@@ -28,393 +29,238 @@ declare module "jc-structure" {
28
29
  clear(): void;
29
30
  toString(): string;
30
31
  }
31
- // interface Queue<T> extends DataStructure {
32
- // /**
33
- // * #### 移除队列头部元素并返回该元素
34
- // */
35
- // dequeue(): T | undefined;
36
32
 
37
- // /**
38
- // * #### 向队列末尾添加元素
39
- // * @param args 要添加的元素
40
- // */
41
- // enqueue(items: T | Array<T>): this;
42
- // enqueue(...args: Array<T>): this;
43
- // /**
44
- // * #### 返回队列头部元素,但不移除
45
- // */
46
- // front(): T | undefined;
47
- // }
48
- // interface QueueConstructor {
49
- // new <T>(): Queue<T>;
50
- // readonly prototype: Queue<T>;
51
- // }
52
- // var Queue: QueueConstructor;
53
-
54
- // type CompareResult = -1 | 0 | 1;
55
-
56
- // interface CompareFn<T> {
57
- // (a: T, b: T): CompareResult;
58
- // }
59
-
60
- // interface Obj<V = any> {
61
- // [key: string]: V;
62
- // }
63
- // /**
64
- // * 最小堆
65
- // */
66
- // interface MinHeap<T> extends DataStructure {
67
- // /**
68
- // * #### 插入一个值,返回一个布尔值
69
- // * @param value 要插入的值
70
- // */
71
- // insert(value: T): boolean;
72
-
73
- // /**
74
- // * #### 移除最小值或最大值,返回该值
75
- // */
76
- // extract(): T | undefined;
77
-
78
- // /**
79
- // * 查找一个值
80
- // * @param value 要查找的值
81
- // */
82
- // find(): T | undefined;
83
- // }
84
- // interface MinHeapConstructor {
85
- // new <T>(fn?: CompareFn<T>): MinHeap<T>;
86
- // readonly prototype: MinHeap;
87
- // }
88
- // var MinHeap: MinHeapConstructor;
89
-
90
- // /**
91
- // * 最大堆
92
- // */
93
- // interface MaxConstructor {
94
- // new <T>(fn?: CompareFn<T>): MinHeap<T>;
95
- // readonly prototype: MinHeap<T>;
96
- // }
97
- // var MaxHeap: MaxConstructor;
98
-
99
- // /**
100
- // * LRU缓存类
101
- // */
102
- // interface LRU<K, V> {
103
- // /**
104
- // * 获取缓存的值
105
- // * @param key 键值
106
- // */
107
- // get(key: K): V | undefined;
108
- // /**
109
- // * 更新缓存
110
- // * @param key 键值
111
- // * @param value 缓存的值
112
- // */
113
- // update(key: K, value: V): void;
114
- // }
115
- // interface LRUConstructor {
116
- // new <K, V>(capacity?: number): LRU<K, V>;
117
- // readonly prototype: LRU;
118
- // }
119
- // var LRU: LRUConstructor;
120
-
121
- // export class JcNode<T> {
122
- // value: T;
123
- // next: JcNode<T> | null;
124
- // constructor(value: T, next: JcNode<T> | null = null): void;
125
- // }
126
-
127
- // export interface ILinkedList<T> {
128
- // indexOf(element: T): number;
129
- // getElementAt(index: number): JcNode<T> | null;
130
- // getValueAt(index: number): T | undefined;
131
- // insert(element: T, index: number): boolean;
132
- // push(element: T): void;
133
- // remove(element: T): T | null;
134
- // removeAt(index: number): T | null;
135
- // isEmpty(): boolean;
136
- // size(): number;
137
- // clear(): void;
138
- // toString(): string;
139
- // toArray(): T[];
140
- // [Symbol.iterator](): Iterator<T>;
141
- // }
142
-
143
- // export class LinkedList<T> implements ILinkedList<T> {
144
- // constructor(elements?: T[]);
145
- // }
146
-
147
- // export type NodeType<T> = JcNode<T>;
148
- // export type ListType<T> = LinkedList<T>;
33
+ export class JcNode<T> {
34
+ value: T;
35
+ next: JcNode<T> | null;
36
+ constructor(value: T, next?: JcNode<T> | null);
37
+ }
149
38
 
150
39
  /**
151
- * 字典类
40
+ * 双向链表类
152
41
  */
153
- // export class Dictionary<K, V> {
154
- // constructor();
155
- // /**
156
- // * #### 设置键值对
157
- // * @param key
158
- // * @param value
159
- // */
160
- // set(key: K, value: V): void;
161
-
162
- // /**
163
- // * #### 删除键值对
164
- // * @param key 键
165
- // */
166
- // remove(key: K): ValuePair<K, V> | undefined;
167
-
168
- // /**
169
- // * #### 判断键是否存在
170
- // * @param key 键
171
- // */
172
- // has(key: K): boolean;
173
-
174
- // /**
175
- // * #### 获取键值对
176
- // * @param key 键
177
- // */
178
- // get(key: K): ValuePair<K, V> | undefined;
42
+ export class LinkedList<T> implements ILinkedList<T> {
43
+ constructor(elements?: T[]);
44
+ indexOf(element: T): number;
45
+ getElementAt(index: number): JcNode<T> | null;
46
+ getValueAt(index: number): T | undefined;
47
+ insert(element: T, index: number): boolean;
48
+ push(element: T): void;
49
+ remove(element: T): T | null;
50
+ removeAt(index: number): T | null;
51
+ isEmpty(): boolean;
52
+ size(): number;
53
+ clear(): void;
54
+ toString(): string;
55
+ toArray(): T[];
56
+ [Symbol.iterator](): Iterator<T>;
57
+ }
179
58
 
180
- // /**
181
- // * #### 获取键数组
182
- // */
183
- // keys(): Array<K>;
59
+ export class MinHeap<T> implements IHeap<T> {
60
+ constructor(compareFn?: CompareFn<T>);
61
+ static swap<T>(arr: T[], index1: number, index2: number): void;
62
+ insert(value: T): boolean;
63
+ extract(): T | undefined;
64
+ find(): T | undefined;
65
+ isEmpty(): boolean;
66
+ size(): number;
67
+ clear(): void;
68
+ toString(): string;
69
+ }
184
70
 
185
- // /**
186
- // * #### 获取值数组
187
- // */
188
- // values(): Array<V>;
71
+ export class MaxHeap<T> extends MinHeap<T> {
72
+ constructor(compareFn?: CompareFn<T>);
73
+ }
189
74
 
190
- // /**
191
- // * #### 获取键值对数组
192
- // */
193
- // keyValues(): Array<[K, V]>;
75
+ export class LRU<K, V> implements ILru<K, V> {
76
+ constructor(capacity?: number);
77
+ get(key: K): V | undefined;
78
+ update(key: K, value: V): void;
79
+ }
194
80
 
195
- // /**
196
- // * 遍历键值对
197
- // * @param callbackFunc 回调函数
198
- // */
199
- // forEach(callbackFunc: (key: K, value: V) => boolean | void): void;
81
+ export class ValuePair<K, V> {
82
+ key: K;
83
+ value: V;
84
+ constructor(key: K, value: V);
85
+ }
200
86
 
201
- // /**
202
- // * 判断数据结构是否为空
203
- // */
204
- // isEmpty(): boolean;
205
- // /**
206
- // * 数据结构元素长度
207
- // */
208
- // size(): number;
209
- // /**
210
- // * 重写 toString 方法
211
- // */
212
- // toString(): string;
213
- // /**
214
- // * 清除数据结构元素
215
- // */
216
- // clear(): void;
217
- // }
87
+ export class Dictionary<K, V> implements IDictionary<K, V> {
88
+ set(key: K, value: V): void;
89
+ remove(key: K): ValuePair<K, V> | undefined;
90
+ has(key: K): boolean;
91
+ get(key: K): ValuePair<K, V> | undefined;
92
+ keys(): K[];
93
+ values(): V[];
94
+ keyValues(): Array<[K, V]>;
95
+ forEach(callbackFunc: (key: K, value: V) => boolean | void): void;
96
+ isEmpty(): boolean;
97
+ size(): number;
98
+ clear(): void;
99
+ toString(): string;
100
+ }
218
101
 
219
- /**
220
- * 图类
221
- */
222
- // export class Graph<T> implements IGraph<T> {
223
- // constructor(isDirected?: boolean);
224
- // /**
225
- // * #### 添加顶点的方法
226
- // * @param v 顶点
227
- // */
228
- // addVertex(v: T): void;
229
- // /**
230
- // * #### 添加边的方法
231
- // * @param v 顶点
232
- // * @param w
233
- // */
234
- // addEdge(v: T, w: T): void;
235
- // /**
236
- // * #### 获取顶点集合的方法
237
- // */
238
- // getVertices(): Array<T>;
239
- // /**
240
- // * #### 获取邻接表的方法
241
- // */
242
- // getAdjacencyList(): IDictionary<T, Array<T>>;
243
- // }
102
+ export class Graph<T> implements IGraph<T> {
103
+ constructor(isDirected?: boolean);
104
+ addVertex(v: T): void;
105
+ addEdge(v: T, w: T): void;
106
+ getVertices(): T[];
107
+ getAdjacencyList(): Dictionary<T, T[]>;
108
+ toString(): string;
109
+ }
244
110
 
245
- // interface Point {
246
- // /**
247
- // * #### 点x坐标
248
- // */
249
- // readonly x: number;
250
- // /**
251
- // * #### 点y坐标
252
- // */
253
- // readonly y: number;
254
- // /**
255
- // * #### 计算点与点之间的距离,实例方法
256
- // * @param p 点p
257
- // */
258
- // distanceTo(p: Point): number;
259
- // }
260
- // interface PointConstructor {
261
- // new (x: number, y: number): Point;
262
- // /**
263
- // * #### 计算两点之间的距离
264
- // * @param p1 点1
265
- // * @param p2 点2
266
- // */
267
- // distance(p1: Point, p2: Point): number;
268
- // }
269
- // var Point: PointConstructor;
111
+ export class Point {
112
+ constructor(x: number, y: number);
113
+ static distance(p1: Point, p2: Point): number;
114
+ distanceTo(p: Point): number;
115
+ }
270
116
 
271
- // interface Line {
272
- // /**
273
- // * #### 线段长度
274
- // */
275
- // get length(): number;
276
- // /**
277
- // * #### 线段中点
278
- // */
279
- // get midpoint(): Point;
280
- // /**
281
- // * #### 线段角度(弧度)
282
- // */
283
- // get angle(): number;
284
- // /**
285
- // * #### 获取线段的方向向量
286
- // */
287
- // get direction(): Point;
288
- // /**
289
- // * #### 获取线段的起点
290
- // */
291
- // get start(): Point;
292
- // /**
293
- // * #### 获取线段的终点
294
- // */
295
- // get end(): Point;
296
- // /**
297
- // * #### 判断点是否在线段上
298
- // * @param point
299
- // * @param tolerance 容差,默认1e-10
300
- // */
301
- // containsPoint(point: Point, tolerance?: number): boolean;
302
- // }
303
- // interface LineConstructor {
304
- // new (p1: Point, p2: Point): Line;
305
- // readonly prototype: Line;
117
+ interface Line {
118
+ /**
119
+ * #### 线段长度
120
+ */
121
+ get length(): number;
122
+ /**
123
+ * #### 线段中点
124
+ */
125
+ get midpoint(): Point;
126
+ /**
127
+ * #### 线段角度(弧度)
128
+ */
129
+ get angle(): number;
130
+ /**
131
+ * #### 获取线段的方向向量
132
+ */
133
+ get direction(): Point;
134
+ /**
135
+ * #### 获取线段的起点
136
+ */
137
+ get start(): Point;
138
+ /**
139
+ * #### 获取线段的终点
140
+ */
141
+ get end(): Point;
142
+ /**
143
+ * #### 判断点是否在线段上
144
+ * @param point
145
+ * @param tolerance 容差,默认1e-10
146
+ */
147
+ containsPoint(point: Point, tolerance?: number): boolean;
148
+ }
149
+ interface LineConstructor {
150
+ new (p1: Point, p2: Point): Line;
151
+ readonly prototype: Line;
306
152
 
307
- // /**
308
- // * #### 判断线段是否平行
309
- // * @param line 线段
310
- // * @param tolerance 容差,默认1e-10
311
- // */
312
- // sloped(line: Line, tolerance?: number): number | null;
153
+ /**
154
+ * #### 判断线段是否平行
155
+ * @param line 线段
156
+ * @param tolerance 容差,默认1e-10
157
+ */
158
+ sloped(line: Line, tolerance?: number): number | null;
313
159
 
314
- // /**
315
- // * #### 判断线段是否平行
316
- // * @param line1 线段1
317
- // * @param line2 线段2
318
- // * @param tolerance 容差,默认1e-10
319
- // */
320
- // isParallel(line1: Line, line2: Line, tolerance?: number): boolean;
160
+ /**
161
+ * #### 判断线段是否平行
162
+ * @param line1 线段1
163
+ * @param line2 线段2
164
+ * @param tolerance 容差,默认1e-10
165
+ */
166
+ isParallel(line1: Line, line2: Line, tolerance?: number): boolean;
321
167
 
322
- // /**
323
- // * #### 判断线段是否相交
324
- // * @param line1 线段1
325
- // * @param line2 线段2
326
- // */
327
- // isIntersecting(line1: Line, line2: Line): boolean;
168
+ /**
169
+ * #### 判断线段是否相交
170
+ * @param line1 线段1
171
+ * @param line2 线段2
172
+ */
173
+ isIntersecting(line1: Line, line2: Line): boolean;
328
174
 
329
- // /**
330
- // * #### 获取两线段交点
331
- // * @param line1 线段1
332
- // * @param line2 线段2
333
- // * @param tolerance 容差,默认1e-10
334
- // */
335
- // getIntersection(line1: Line, line2: Line, tolerance?: number): Point | null;
175
+ /**
176
+ * #### 获取两线段交点
177
+ * @param line1 线段1
178
+ * @param line2 线段2
179
+ * @param tolerance 容差,默认1e-10
180
+ */
181
+ getIntersection(line1: Line, line2: Line, tolerance?: number): Point | null;
336
182
 
337
- // /**
338
- // * #### 获取线段到点的距离
339
- // * @param line1 线段1
340
- // * @param line2 线段2
341
- // * @param tolerance 容差,默认1e-10
342
- // */
343
- // distanceToPoint(line: Line, point: Point, tolerance?: number): number;
344
- // }
345
- // var Line: LineConstructor;
183
+ /**
184
+ * #### 获取线段到点的距离
185
+ * @param line1 线段1
186
+ * @param line2 线段2
187
+ * @param tolerance 容差,默认1e-10
188
+ */
189
+ distanceToPoint(line: Line, point: Point, tolerance?: number): number;
190
+ }
191
+ var Line: LineConstructor;
346
192
 
347
- // enum TriangleType {
348
- // Equilateral = "equilateral", // 等边
349
- // Isosceles = "isosceles", // 等腰
350
- // Scalene = "scalene", // 不等边
351
- // }
352
- // /**
353
- // * ### 三角形类
354
- // */
355
- // interface Triangle {
356
- // readonly p1: Point;
357
- // readonly p2: Point;
358
- // readonly p3: Point;
359
- // /**
360
- // * #### 获取三角形的边长
361
- // */
362
- // get side(): [number, number, number];
363
- // /**
364
- // * #### 获取三角形的内心点
365
- // */
366
- // get centroid(): Point;
367
- // /**
368
- // * #### 获取三角形的外切园心点
369
- // */
370
- // get circumcenter(): Point;
371
- // /**
372
- // * #### 获取三角形的内切园心点
373
- // */
374
- // get incenter(): Point;
375
- // /**
376
- // * #### 获取三角形的周长
377
- // */
378
- // perimeter(): number;
379
- // /**
380
- // * #### 获取三角形的面积
381
- // */
382
- // area(): number;
383
- // /**
384
- // * #### 判断点是否在三角形内
385
- // * @param point
386
- // */
387
- // containsPoint(point: Point): boolean;
388
- // }
389
- // interface TriangleConstructor {
390
- // new (p1: Point, p2: Point, p3: Point): Triangle;
391
- // readonly prototype: Triangle;
193
+ enum TriangleType {
194
+ Equilateral = "equilateral", // 等边
195
+ Isosceles = "isosceles", // 等腰
196
+ Scalene = "scalene", // 不等边
197
+ }
198
+ /**
199
+ * ### 三角形类
200
+ */
201
+ interface Triangle {
202
+ readonly p1: Point;
203
+ readonly p2: Point;
204
+ readonly p3: Point;
205
+ /**
206
+ * #### 获取三角形的边长
207
+ */
208
+ get side(): [number, number, number];
209
+ /**
210
+ * #### 获取三角形的内心点
211
+ */
212
+ get centroid(): Point;
213
+ /**
214
+ * #### 获取三角形的外切园心点
215
+ */
216
+ get circumcenter(): Point;
217
+ /**
218
+ * #### 获取三角形的内切园心点
219
+ */
220
+ get incenter(): Point;
221
+ /**
222
+ * #### 获取三角形的周长
223
+ */
224
+ perimeter(): number;
225
+ /**
226
+ * #### 获取三角形的面积
227
+ */
228
+ area(): number;
229
+ /**
230
+ * #### 判断点是否在三角形内
231
+ * @param point
232
+ */
233
+ containsPoint(point: Point): boolean;
234
+ }
235
+ interface TriangleConstructor {
236
+ new (p1: Point, p2: Point, p3: Point): Triangle;
237
+ readonly prototype: Triangle;
392
238
 
393
- // /**
394
- // * #### 判断三条边是否能构成三角形
395
- // * @param a
396
- // * @param b
397
- // * @param c
398
- // */
399
- // isValid(a: number, b: number, c: number): boolean;
400
- // /**
401
- // * #### 计算三角形的面积,采用秦九昭公式或叫海伦面积公式
402
- // * @param a
403
- // * @param b
404
- // * @param c
405
- // */
406
- // area(a: number, b: number, c: number): number;
407
- // /**
408
- // * #### 获取三角形的角度
409
- // * @param a
410
- // * @param b
411
- // * @param c
412
- // */
413
- // getAngles(a: number, b: number, c: number): [number, number, number];
239
+ /**
240
+ * #### 判断三条边是否能构成三角形
241
+ * @param a
242
+ * @param b
243
+ * @param c
244
+ */
245
+ isValid(a: number, b: number, c: number): boolean;
246
+ /**
247
+ * #### 计算三角形的面积,采用秦九昭公式或叫海伦面积公式
248
+ * @param a
249
+ * @param b
250
+ * @param c
251
+ */
252
+ area(a: number, b: number, c: number): number;
253
+ /**
254
+ * #### 获取三角形的角度
255
+ * @param a
256
+ * @param b
257
+ * @param c
258
+ */
259
+ getAngles(a: number, b: number, c: number): [number, number, number];
414
260
 
415
- // getTYpe(a: number, b: number, c: number): TriangleType;
416
- // }
417
- // var Triangle: TriangleConstructor;
261
+ getTYpe(a: number, b: number, c: number): TriangleType;
262
+ }
263
+ var Triangle: TriangleConstructor;
418
264
 
419
265
  /**
420
266
  * ### 向量类
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "jc-structure",
3
3
  "private": false,
4
- "version": "0.2.8",
4
+ "version": "0.2.10",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist",
package/types/index.d.ts CHANGED
@@ -2,3 +2,7 @@ export * from "./global";
2
2
  export * from "./linkedList";
3
3
  export * from "./stack";
4
4
  export * from "./queue";
5
+ export * from "./heap";
6
+ export * from "./lru";
7
+ export * from "./dictionary";
8
+ export * from "./graph";
@@ -1,28 +1,65 @@
1
- class JcNode<T> {
2
- value: T;
3
- next: JcNode<T> | null;
4
- constructor(value: T, next: JcNode<T> | null = null): void;
5
- }
6
-
7
1
  /**
8
- * #### 链表接口
2
+ * #### 双向链表接口
9
3
  */
10
4
  interface ILinkedList<T> {
5
+ /**
6
+ * #### 查找元素位置
7
+ * @param element 元素
8
+ */
11
9
  indexOf(element: T): number;
10
+ /**
11
+ * #### 获取指定位置的元素
12
+ * @param index 索引
13
+ */
12
14
  getElementAt(index: number): JcNode<T> | null;
15
+ /**
16
+ * #### 获取指定位置的值
17
+ * @param index 索引
18
+ */
13
19
  getValueAt(index: number): T | undefined;
20
+ /**
21
+ * #### 在指定位置插入元素
22
+ * @param element 元素
23
+ * @param index 索引
24
+ */
14
25
  insert(element: T, index: number): boolean;
26
+ /**
27
+ * #### 在链表尾部添加元素
28
+ * @param element 元素
29
+ */
15
30
  push(element: T): void;
31
+ /**
32
+ * #### 删除指定元素
33
+ * @param element 元素
34
+ */
16
35
  remove(element: T): T | null;
36
+ /**
37
+ * #### 删除指定位置的元素
38
+ * @param index 索引
39
+ */
17
40
  removeAt(index: number): T | null;
41
+ /**
42
+ * #### 判断链表是否为空
43
+ */
18
44
  isEmpty(): boolean;
45
+ /**
46
+ * #### 获取链表长度
47
+ */
19
48
  size(): number;
49
+ /**
50
+ * #### 清空链表
51
+ */
20
52
  clear(): void;
53
+ /**
54
+ * #### 将链表转换为字符串
55
+ */
21
56
  toString(): string;
57
+ /**
58
+ * #### 将链表转换为数组
59
+ */
22
60
  toArray(): T[];
61
+ /**
62
+ * #### 实现了 [Symbol.iterator] 方法,使链表可以被 for...of 循环遍历
63
+ */
23
64
  [Symbol.iterator](): Iterator<T>;
24
65
  }
25
-
26
- class LinkedList<T> implements ILinkedList<T> {
27
- constructor(elements?: T[]);
28
- }
package/types/lru.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ interface LRU_Node<T> {
2
+ value: T;
3
+ prev?: LRU_Node<T>;
4
+ next?: LRU_Node<T>;
5
+ }
6
+
7
+ /**
8
+ * #### Least Recently Used Cache
9
+ */
10
+ interface ILru<K, V> {
11
+ /**
12
+ * #### Get the value of the key
13
+ * @param key
14
+ */
15
+ get(key: K): V | undefined;
16
+ /**
17
+ * #### Set the value of the key
18
+ * @param key
19
+ * @param value
20
+ */
21
+ update(key: K, value: V): void;
22
+ }