jc-structure 0.0.6 → 0.0.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.
@@ -1,4 +1,4 @@
1
- class e {
1
+ class p {
2
2
  items = {};
3
3
  count = 0;
4
4
  lowestCount = 0;
@@ -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 o {
33
+ class a {
34
34
  items = {};
35
35
  count = 0;
36
36
  constructor() {
@@ -62,7 +62,71 @@ class o {
62
62
  return this.isEmpty() ? "" : `Stack(count: ${this.count}):[${this.items[this.count - 1]},...rest]`;
63
63
  }
64
64
  }
65
+ function o(i, t) {
66
+ return i === t ? 0 : i < t ? -1 : 1;
67
+ }
68
+ class h {
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, s, e) {
84
+ [t[s], t[e]] = [t[e], t[s]];
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 u extends h {
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 s = t, e = h.getLeftIndex(s), r = h.getRightIndex(s), n = this.size();
114
+ e < n && this.compareFn(this.heap[s], this.heap[e]) === -1 && (s = e), r < n && this.compareFn(this.heap[s], this.heap[r]) === 1 && (s = r), s !== t && (h.swap(this.heap, t, s), this.siftUp(s));
115
+ }
116
+ siftDown(t) {
117
+ let s = h.getParentIndex(t);
118
+ for (; t > 0 && s && this.compareFn(this.heap[s], this.heap[t]) === 1; )
119
+ h.swap(this.heap, s, t), t = s, s = h.getParentIndex(t);
120
+ }
121
+ }
122
+ class c extends u {
123
+ constructor(t = (s, e) => o(e, s)) {
124
+ super(t);
125
+ }
126
+ }
65
127
  export {
66
- e as Queue,
67
- o as Stack
128
+ c as MaxHeap,
129
+ u as MinHeap,
130
+ p as Queue,
131
+ a as Stack
68
132
  };
@@ -1 +1 @@
1
- (function(t,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(t=typeof globalThis<"u"?globalThis:t||self,e(t["jc-structure"]={}))})(this,(function(t){"use strict";class e{items={};count=0;lowestCount=0;constructor(){}dequeue(){if(this.isEmpty())return;const s=this.items[this.lowestCount];return delete this.items[this.lowestCount],this.lowestCount++,s}enqueue(...s){s.forEach(i=>{this.items[this.count]=i,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 u{items={};count=0;constructor(){}pop(){if(this.isEmpty())return;this.count--;const s=this.items[this.count];return delete this.items[this.count],s}push(...s){s.forEach(i=>{this.items[this.count]=i,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]`}}t.Queue=e,t.Stack=u,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(s,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(s=typeof globalThis<"u"?globalThis:s||self,r(s["jc-structure"]={}))})(this,(function(s){"use strict";class r{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 a{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(n,t){return n===t?0:n<t?-1:1}class h{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 p extends h{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=h.getLeftIndex(e),u=h.getRightIndex(e),c=this.size();i<c&&this.compareFn(this.heap[e],this.heap[i])===-1&&(e=i),u<c&&this.compareFn(this.heap[e],this.heap[u])===1&&(e=u),e!==t&&(h.swap(this.heap,t,e),this.siftUp(e))}siftDown(t){let e=h.getParentIndex(t);for(;t>0&&e&&this.compareFn(this.heap[e],this.heap[t])===1;)h.swap(this.heap,e,t),t=e,e=h.getParentIndex(t)}}class f extends p{constructor(t=(e,i)=>o(i,e)){super(t)}}s.MaxHeap=f,s.MinHeap=p,s.Queue=r,s.Stack=a,Object.defineProperty(s,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,46 @@ 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
+ }
70
115
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "jc-structure",
3
3
  "private": false,
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist",
package/types/global.d.ts CHANGED
@@ -16,3 +16,9 @@ interface Structure {
16
16
  */
17
17
  toString(): string;
18
18
  }
19
+
20
+ type CompareResult = -1 | 0 | 1;
21
+
22
+ interface CompareFn<T> {
23
+ (a: T, b: T): CompareResult;
24
+ }
@@ -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
+ }