jc-structure 0.2.5 → 0.2.7
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/index.d.ts +49 -43
- package/package.json +2 -1
- package/types/dictionary.d.ts +50 -0
- package/types/global.d.ts +75 -0
- package/types/graph.d.ts +24 -0
- package/types/heap.d.ts +21 -0
- package/types/index.d.ts +4 -0
- package/types/linkedList.d.ts +28 -0
- package/types/queue.d.ts +20 -0
- package/types/shape.d.ts +11 -0
- package/types/stack.d.ts +24 -0
- package/types/vector.d.ts +20 -0
package/index.d.ts
CHANGED
|
@@ -1,57 +1,63 @@
|
|
|
1
|
+
/// <reference path="./types/index.d.ts" />
|
|
2
|
+
|
|
1
3
|
declare module "jc-structure" {
|
|
2
|
-
|
|
4
|
+
import * as Types from "./types";
|
|
5
|
+
export = Types;
|
|
3
6
|
|
|
4
7
|
//#region data structure
|
|
5
8
|
/**
|
|
6
9
|
* #### 数据结构接口
|
|
7
10
|
*/
|
|
8
|
-
interface DataStructure {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
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
|
+
// }
|
|
26
29
|
|
|
27
30
|
/**
|
|
28
31
|
* 栈类,采用object实现
|
|
29
32
|
*/
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
* #### 移除栈顶元素并返回该元素
|
|
33
|
-
*/
|
|
34
|
-
pop(): T | undefined;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* #### 向栈顶添加元素
|
|
38
|
-
* @param args 要添加的元素,key可以是多个或一个元素。
|
|
39
|
-
*/
|
|
40
|
-
push(item: T): this;
|
|
41
|
-
push(items: Array<T>): this;
|
|
42
|
-
push(...items: T[]): this;
|
|
43
|
-
push(itemOrItems: T | Array<T>): this;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* #### 返回栈顶元素,但不移除
|
|
47
|
-
*/
|
|
48
|
-
peek(): T | undefined;
|
|
49
|
-
}
|
|
50
|
-
interface StackConstructor {
|
|
51
|
-
new <T>(): Stack<T>;
|
|
52
|
-
readonly prototype: Stack<T>;
|
|
33
|
+
export class Stack<T> implements IStack<T> {
|
|
34
|
+
constructor();
|
|
53
35
|
}
|
|
54
|
-
|
|
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;
|
|
55
61
|
|
|
56
62
|
/**
|
|
57
63
|
* 队列类,采用object实现
|
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
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
interface Structure {
|
|
2
|
+
/**
|
|
3
|
+
* 判断数据结构是否为空
|
|
4
|
+
*/
|
|
5
|
+
isEmpty(): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* 数据结构元素长度
|
|
8
|
+
*/
|
|
9
|
+
size(): number;
|
|
10
|
+
/**
|
|
11
|
+
* 清除数据结构元素
|
|
12
|
+
*/
|
|
13
|
+
clear(): void;
|
|
14
|
+
/**
|
|
15
|
+
* 重写 toString 方法
|
|
16
|
+
*/
|
|
17
|
+
toString(): string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type CompareResult = -1 | 0 | 1;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* #### 比较函数接口
|
|
24
|
+
*/
|
|
25
|
+
interface CompareFn<T> {
|
|
26
|
+
(a: T, b: T): CompareResult;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* #### 对象接口,用于存储数据结构元素,值类型默认为 any
|
|
31
|
+
*/
|
|
32
|
+
interface Obj<V = any> {
|
|
33
|
+
[key: string]: V;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface RegExpConstructor {
|
|
37
|
+
/**
|
|
38
|
+
* #### 转义正则表达式中的特殊字符
|
|
39
|
+
* @param str 字符串
|
|
40
|
+
*/
|
|
41
|
+
escape(str: string): string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface Element {
|
|
45
|
+
/**
|
|
46
|
+
* #### 获取最深元素集合中的第一个元素
|
|
47
|
+
*/
|
|
48
|
+
readonly firstElement: Element | null;
|
|
49
|
+
/**
|
|
50
|
+
* #### 获取最深元素集合中的最后一个元素
|
|
51
|
+
*/
|
|
52
|
+
readonly lastElement: Element | null;
|
|
53
|
+
/**
|
|
54
|
+
* #### 最祖先的匹配元素
|
|
55
|
+
* @param this element
|
|
56
|
+
* @param selector 选择器
|
|
57
|
+
*/
|
|
58
|
+
farthest<E extends Element = Element>(this: E, selector: string): E | null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface Text {
|
|
62
|
+
/**
|
|
63
|
+
* #### 将文本节点包裹在指定标签中
|
|
64
|
+
* @param this Text 该文本节点
|
|
65
|
+
* @param selector 要包裹的元素标签,默认为:'strong'
|
|
66
|
+
* @param text 要包裹的文本,默认为:''
|
|
67
|
+
*/
|
|
68
|
+
surround(this: Text, selector?: string, text?: string): HTMLElement | null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface Array<T> {
|
|
72
|
+
groupBy<K extends string | number | symbol>(
|
|
73
|
+
generateKey: string | ((item: T, index: number, arr: T[]) => K)
|
|
74
|
+
): Record<K, T[]>;
|
|
75
|
+
}
|
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,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### 堆
|
|
3
|
+
*/
|
|
4
|
+
interface IHeap<T> extends Structure {
|
|
5
|
+
/**
|
|
6
|
+
* #### 插入一个值,返回一个布尔值
|
|
7
|
+
* @param value 要插入的值
|
|
8
|
+
*/
|
|
9
|
+
insert(value: T): boolean;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* #### 移除最小值或最大值,返回该值
|
|
13
|
+
*/
|
|
14
|
+
extract(): T | undefined;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 查找一个值
|
|
18
|
+
* @param value 要查找的值
|
|
19
|
+
*/
|
|
20
|
+
find(): T | undefined;
|
|
21
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
/**
|
|
8
|
+
* #### 链表接口
|
|
9
|
+
*/
|
|
10
|
+
interface ILinkedList<T> {
|
|
11
|
+
indexOf(element: T): number;
|
|
12
|
+
getElementAt(index: number): JcNode<T> | null;
|
|
13
|
+
getValueAt(index: number): T | undefined;
|
|
14
|
+
insert(element: T, index: number): boolean;
|
|
15
|
+
push(element: T): void;
|
|
16
|
+
remove(element: T): T | null;
|
|
17
|
+
removeAt(index: number): T | null;
|
|
18
|
+
isEmpty(): boolean;
|
|
19
|
+
size(): number;
|
|
20
|
+
clear(): void;
|
|
21
|
+
toString(): string;
|
|
22
|
+
toArray(): T[];
|
|
23
|
+
[Symbol.iterator](): Iterator<T>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class LinkedList<T> implements ILinkedList<T> {
|
|
27
|
+
constructor(elements?: T[]);
|
|
28
|
+
}
|
package/types/queue.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### 队列接口
|
|
3
|
+
*/
|
|
4
|
+
interface IQueue<T> extends Structure {
|
|
5
|
+
/**
|
|
6
|
+
* #### 移除队列头部元素并返回该元素
|
|
7
|
+
*/
|
|
8
|
+
dequeue(): T | undefined;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* #### 向队列末尾添加元素
|
|
12
|
+
* @param args 要添加的元素
|
|
13
|
+
*/
|
|
14
|
+
enqueue(...args: (T | T[])[]): this;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* #### 返回队列头部元素,但不移除
|
|
18
|
+
*/
|
|
19
|
+
front(): T | undefined;
|
|
20
|
+
}
|
package/types/shape.d.ts
ADDED
package/types/stack.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
interface StructureObj<T> {
|
|
2
|
+
[key: number]: T;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* #### 栈接口
|
|
7
|
+
*/
|
|
8
|
+
interface IStack<T> extends Structure {
|
|
9
|
+
/**
|
|
10
|
+
* #### 移除栈顶元素并返回该元素
|
|
11
|
+
*/
|
|
12
|
+
pop(): T | undefined;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* #### 添加一个或多个元素
|
|
16
|
+
* @param items
|
|
17
|
+
*/
|
|
18
|
+
push(...items: (T | T[])[]): this;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* #### 返回栈顶元素,但不移除
|
|
22
|
+
*/
|
|
23
|
+
peek(): T | undefined;
|
|
24
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface Vector {
|
|
2
|
+
get dimension(): number;
|
|
3
|
+
get norm(): number;
|
|
4
|
+
getItem(index: number): number;
|
|
5
|
+
normalize(): Vector;
|
|
6
|
+
add(vector: Vector): Vector;
|
|
7
|
+
sub(vector: Vector): Vector;
|
|
8
|
+
mul(scalar: number): Vector;
|
|
9
|
+
dot(vector: Vector): number;
|
|
10
|
+
pos(): Vector;
|
|
11
|
+
neg(): Vector;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface VectorConstructor {
|
|
15
|
+
new (...args: number[]): Vector;
|
|
16
|
+
readonly prototype: Vector;
|
|
17
|
+
zero(dimension: number): Vector;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare var Vector: VectorConstructor;
|