jc-structure 0.2.7 → 0.2.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.
@@ -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,296 +1,119 @@
1
1
  /// <reference path="./types/index.d.ts" />
2
2
 
3
3
  declare module "jc-structure" {
4
- import * as Types from "./types";
5
- export = Types;
6
-
7
4
  //#region data structure
8
- /**
9
- * #### 数据结构接口
10
- */
11
- // interface DataStructure {
12
- // /**
13
- // * 判断数据结构是否为空
14
- // */
15
- // isEmpty(): boolean;
16
- // /**
17
- // * 数据结构元素长度
18
- // */
19
- // size(): number;
20
- // /**
21
- // * 清除数据结构元素
22
- // */
23
- // clear(): void;
24
- // /**
25
- // * 重写 toString 方法
26
- // */
27
- // toString(): string;
28
- // }
29
-
30
5
  /**
31
6
  * 栈类,采用object实现
32
7
  */
33
8
  export class Stack<T> implements IStack<T> {
34
9
  constructor();
10
+ pop(): T | undefined;
11
+ push(...args: (T | T[])[]): this;
12
+ peek(): T | undefined;
13
+ isEmpty(): boolean;
14
+ size(): number;
15
+ clear(): void;
16
+ toString(): string;
35
17
  }
36
- // interface Stack<T> extends DataStructure {
37
- // /**
38
- // * #### 移除栈顶元素并返回该元素
39
- // */
40
- // pop(): T | undefined;
41
-
42
- // /**
43
- // * #### 向栈顶添加元素
44
- // * @param args 要添加的元素,key可以是多个或一个元素。
45
- // */
46
- // push(item: T): this;
47
- // push(items: Array<T>): this;
48
- // push(...items: T[]): this;
49
- // push(itemOrItems: T | Array<T>): this;
50
-
51
- // /**
52
- // * #### 返回栈顶元素,但不移除
53
- // */
54
- // peek(): T | undefined;
55
- // }
56
- // interface StackConstructor {
57
- // new <T>(): Stack<T>;
58
- // readonly prototype: Stack<T>;
59
- // }
60
- // var Stack: StackConstructor;
61
18
 
62
19
  /**
63
20
  * 队列类,采用object实现
64
21
  */
65
- interface Queue<T> extends DataStructure {
66
- /**
67
- * #### 移除队列头部元素并返回该元素
68
- */
22
+ export class Queue<T> implements IQueue<T> {
23
+ constructor();
69
24
  dequeue(): T | undefined;
70
-
71
- /**
72
- * #### 向队列末尾添加元素
73
- * @param args 要添加的元素
74
- */
75
- enqueue(items: T | Array<T>): this;
76
- enqueue(...args: Array<T>): this;
77
- /**
78
- * #### 返回队列头部元素,但不移除
79
- */
25
+ enqueue(...args: (T | T[])[]): this;
80
26
  front(): T | undefined;
27
+ isEmpty(): boolean;
28
+ size(): number;
29
+ clear(): void;
30
+ toString(): string;
81
31
  }
82
- interface QueueConstructor {
83
- new <T>(): Queue<T>;
84
- readonly prototype: Queue<T>;
85
- }
86
- var Queue: QueueConstructor;
87
-
88
- type CompareResult = -1 | 0 | 1;
89
32
 
90
- interface CompareFn<T> {
91
- (a: T, b: T): CompareResult;
33
+ export class JcNode<T> {
34
+ value: T;
35
+ next: JcNode<T> | null;
36
+ constructor(value: T, next: JcNode<T> | null = null): void;
92
37
  }
93
38
 
94
- interface Obj<V = any> {
95
- [key: string]: V;
96
- }
97
39
  /**
98
- * 最小堆
40
+ * 双向链表类
99
41
  */
100
- interface MinHeap<T> extends DataStructure {
101
- /**
102
- * #### 插入一个值,返回一个布尔值
103
- * @param value 要插入的值
104
- */
105
- insert(value: T): boolean;
42
+ export class LinkedList<T> implements ILinkedList<T> {
43
+ constructor(elements?: T[]): LinkedList<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
+ }
106
58
 
107
- /**
108
- * #### 移除最小值或最大值,返回该值
109
- */
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;
110
63
  extract(): T | undefined;
111
-
112
- /**
113
- * 查找一个值
114
- * @param value 要查找的值
115
- */
116
64
  find(): T | undefined;
65
+ isEmpty(): boolean;
66
+ size(): number;
67
+ clear(): void;
68
+ toString(): string;
117
69
  }
118
- interface MinHeapConstructor {
119
- new <T>(fn?: CompareFn<T>): MinHeap<T>;
120
- readonly prototype: MinHeap;
121
- }
122
- var MinHeap: MinHeapConstructor;
123
70
 
124
- /**
125
- * 最大堆
126
- */
127
- interface MaxConstructor {
128
- new <T>(fn?: CompareFn<T>): MinHeap<T>;
129
- readonly prototype: MinHeap<T>;
71
+ export class MaxHeap<T> extends MinHeap<T> {
72
+ constructor(compareFn?: CompareFn<T>);
130
73
  }
131
- var MaxHeap: MaxConstructor;
132
74
 
133
- /**
134
- * LRU缓存类
135
- */
136
- interface LRU<K, V> {
137
- /**
138
- * 获取缓存的值
139
- * @param key 键值
140
- */
75
+ export class LRU<K, V> implements ILru<K, V> {
76
+ constructor(capacity?: number);
141
77
  get(key: K): V | undefined;
142
- /**
143
- * 更新缓存
144
- * @param key 键值
145
- * @param value 缓存的值
146
- */
147
78
  update(key: K, value: V): void;
148
79
  }
149
- interface LRUConstructor {
150
- new <K, V>(capacity?: number): LRU<K, V>;
151
- readonly prototype: LRU;
152
- }
153
- var LRU: LRUConstructor;
154
-
155
- // export class JcNode<T> {
156
- // value: T;
157
- // next: JcNode<T> | null;
158
- // constructor(value: T, next: JcNode<T> | null = null): void;
159
- // }
160
-
161
- // export interface ILinkedList<T> {
162
- // indexOf(element: T): number;
163
- // getElementAt(index: number): JcNode<T> | null;
164
- // getValueAt(index: number): T | undefined;
165
- // insert(element: T, index: number): boolean;
166
- // push(element: T): void;
167
- // remove(element: T): T | null;
168
- // removeAt(index: number): T | null;
169
- // isEmpty(): boolean;
170
- // size(): number;
171
- // clear(): void;
172
- // toString(): string;
173
- // toArray(): T[];
174
- // [Symbol.iterator](): Iterator<T>;
175
- // }
176
-
177
- // export class LinkedList<T> implements ILinkedList<T> {
178
- // constructor(elements?: T[]);
179
- // }
180
80
 
181
- export type NodeType<T> = JcNode<T>;
182
- export type ListType<T> = LinkedList<T>;
81
+ export class ValuePair<K, V> {
82
+ key: K;
83
+ value: V;
84
+ constructor(key: K, value: V);
85
+ }
183
86
 
184
- /**
185
- * 字典类
186
- */
187
- export class Dictionary<K, V> {
188
- constructor();
189
- /**
190
- * #### 设置键值对
191
- * @param key 键
192
- * @param value 值
193
- */
87
+ export class Dictionary<K, V> implements IDictionary<K, V> {
194
88
  set(key: K, value: V): void;
195
-
196
- /**
197
- * #### 删除键值对
198
- * @param key 键
199
- */
200
89
  remove(key: K): ValuePair<K, V> | undefined;
201
-
202
- /**
203
- * #### 判断键是否存在
204
- * @param key 键
205
- */
206
90
  has(key: K): boolean;
207
-
208
- /**
209
- * #### 获取键值对
210
- * @param key 键
211
- */
212
91
  get(key: K): ValuePair<K, V> | undefined;
213
-
214
- /**
215
- * #### 获取键数组
216
- */
217
- keys(): Array<K>;
218
-
219
- /**
220
- * #### 获取值数组
221
- */
222
- values(): Array<V>;
223
-
224
- /**
225
- * #### 获取键值对数组
226
- */
92
+ keys(): K[];
93
+ values(): V[];
227
94
  keyValues(): Array<[K, V]>;
228
-
229
- /**
230
- * 遍历键值对
231
- * @param callbackFunc 回调函数
232
- */
233
95
  forEach(callbackFunc: (key: K, value: V) => boolean | void): void;
234
-
235
- /**
236
- * 判断数据结构是否为空
237
- */
238
96
  isEmpty(): boolean;
239
- /**
240
- * 数据结构元素长度
241
- */
242
97
  size(): number;
243
- /**
244
- * 重写 toString 方法
245
- */
246
- toString(): string;
247
- /**
248
- * 清除数据结构元素
249
- */
250
98
  clear(): void;
99
+ toString(): string;
251
100
  }
252
101
 
253
- /**
254
- * 图类
255
- */
256
102
  export class Graph<T> implements IGraph<T> {
257
103
  constructor(isDirected?: boolean);
258
- /**
259
- * #### 添加顶点的方法
260
- * @param v 顶点
261
- */
262
104
  addVertex(v: T): void;
263
- /**
264
- * #### 添加边的方法
265
- * @param v 顶点
266
- * @param w
267
- */
268
105
  addEdge(v: T, w: T): void;
269
- /**
270
- * #### 获取顶点集合的方法
271
- */
272
- getVertices(): Array<T>;
273
- /**
274
- * #### 获取邻接表的方法
275
- */
276
- getAdjacencyList(): IDictionary<T, Array<T>>;
106
+ getVertices(): T[];
107
+ getAdjacencyList(): Dictionary<T, T[]>;
108
+ toString(): string;
277
109
  }
278
110
 
279
- interface Point {
280
- /**
281
- * #### 点x坐标
282
- */
283
- readonly x: number;
284
- /**
285
- * #### 点y坐标
286
- */
287
- readonly y: number;
288
- /**
289
- * #### 计算点与点之间的距离,实例方法
290
- * @param p 点p
291
- */
111
+ export class Point {
112
+ constructor(x: number, y: number);
113
+ static distance(p1: Point, p2: Point): number;
292
114
  distanceTo(p: Point): number;
293
115
  }
116
+
294
117
  interface PointConstructor {
295
118
  new (x: number, y: number): Point;
296
119
  /**
@@ -1033,7 +856,7 @@ declare module "jc-structure" {
1033
856
  * @param visited
1034
857
  * @returns
1035
858
  */
1036
- deepClone(obj: unknown, visited?: WeakMap): unknown;
859
+ // deepClone(obj: unknown, visited?: WeakMap): unknown;
1037
860
  }
1038
861
  var Obj: ObjConstructor;
1039
862
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "jc-structure",
3
3
  "private": false,
4
- "version": "0.2.7",
4
+ "version": "0.2.9",
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
+ }