-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathArrayExt.ts
More file actions
279 lines (248 loc) · 7.14 KB
/
ArrayExt.ts
File metadata and controls
279 lines (248 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { Random } from './Random';
/**
* 数组扩展器和高效数据结构工具
* 提供栈、队列等数据结构的高效实现
*/
export class ArrayExt {
/**
* 将数组打乱顺序(Fisher-Yates洗牌算法)
* 时间复杂度: O(n),空间复杂度: O(1)
*
* @param list 要打乱的数组
* @throws {Error} 当数组为null或undefined时抛出错误
*/
public static shuffle<T>(list: Array<T>): void {
if (!list) {
throw new Error('数组不能为null或undefined');
}
// 优化:从后往前遍历,减少一次减法运算
for (let i = list.length - 1; i > 0; i--) {
const j = Random.integer(0, i);
// 使用解构赋值进行交换,更简洁
[list[i], list[j]] = [list[j]!, list[i]!];
}
}
/**
* 取出数组第一个项(不移除)
* @param list 目标数组
* @returns 第一个元素
* @throws {Error} 当数组为空时抛出错误
*/
public static peek<T>(list: Array<T>): T {
if (list.length === 0) {
throw new Error('无法从空数组中获取元素');
}
return list[0]!;
}
/**
* 向数组头部添加一个项
* @param list 目标数组
* @param item 要添加的项
*/
public static unshift<T>(list: Array<T>, item: T): void {
list.unshift(item);
}
/**
* 移除数组第一个项并返回它
* @param list 目标数组
* @returns 移除的元素,如果数组为空则返回undefined
*/
public static pop<T>(list: Array<T>): T | undefined {
return list.shift();
}
/**
* 向数组尾部添加一个项
* @param list 目标数组
* @param item 要添加的项
*/
public static append<T>(list: Array<T>, item: T): void {
list.push(item);
}
/**
* 移除数组最后一个项并返回它
* @param list 目标数组
* @returns 移除的元素,如果数组为空则返回undefined
*/
public static removeLast<T>(list: Array<T>): T | undefined {
return list.pop();
}
/**
* 检查数组是否为空
* @param list 目标数组
* @returns 是否为空
*/
public static isEmpty<T>(list: Array<T>): boolean {
return list.length === 0;
}
/**
* 获取数组大小
* @param list 目标数组
* @returns 数组长度
*/
public static size<T>(list: Array<T>): number {
return list.length;
}
/**
* 清空数组
* @param list 目标数组
*/
public static clear<T>(list: Array<T>): void {
list.length = 0;
}
}
/**
* 高效的双端队列实现
* 使用环形缓冲区,避免数组头部插入的性能问题
*
* @template T 队列中元素的类型
*
* @example
* ```typescript
* const deque = new Deque<number>(32);
* deque.push(1);
* deque.unshift(0);
* console.log(deque.peekFirst()); // 0
* console.log(deque.peekLast()); // 1
* ```
*/
export class Deque<T> {
private _buffer: (T | undefined)[];
private _head: number = 0;
private _tail: number = 0;
private _size: number = 0;
private _capacity: number;
/**
* 创建双端队列
* @param initialCapacity 初始容量,必须大于0,默认16
*/
constructor(initialCapacity: number = 16) {
if (initialCapacity <= 0) {
throw new Error('初始容量必须大于0');
}
this._capacity = Math.max(initialCapacity, 4);
this._buffer = new Array(this._capacity);
}
/**
* 向队列头部添加元素
* @param item 要添加的元素
*/
public unshift(item: T): void {
if (this._size === this._capacity) {
this._resize();
}
this._head = (this._head - 1 + this._capacity) % this._capacity;
this._buffer[this._head] = item;
this._size++;
}
/**
* 向队列尾部添加元素
* @param item 要添加的元素
*/
public push(item: T): void {
if (this._size === this._capacity) {
this._resize();
}
this._buffer[this._tail] = item;
this._tail = (this._tail + 1) % this._capacity;
this._size++;
}
/**
* 从队列头部移除元素
* @returns 移除的元素,如果队列为空则返回undefined
*/
public shift(): T | undefined {
if (this._size === 0) {
return undefined;
}
const item = this._buffer[this._head];
this._buffer[this._head] = undefined;
this._head = (this._head + 1) % this._capacity;
this._size--;
return item;
}
/**
* 从队列尾部移除元素
* @returns 移除的元素,如果队列为空则返回undefined
*/
public pop(): T | undefined {
if (this._size === 0) {
return undefined;
}
this._tail = (this._tail - 1 + this._capacity) % this._capacity;
const item = this._buffer[this._tail];
this._buffer[this._tail] = undefined;
this._size--;
return item;
}
/**
* 查看队列头部元素(不移除)
* @returns 头部元素,如果队列为空则返回undefined
*/
public peekFirst(): T | undefined {
return this._size > 0 ? this._buffer[this._head] : undefined;
}
/**
* 查看队列尾部元素(不移除)
* @returns 尾部元素,如果队列为空则返回undefined
*/
public peekLast(): T | undefined {
if (this._size === 0) {
return undefined;
}
const lastIndex = (this._tail - 1 + this._capacity) % this._capacity;
return this._buffer[lastIndex];
}
/**
* 获取队列大小
*/
public get size(): number {
return this._size;
}
/**
* 检查队列是否为空
*/
public get isEmpty(): boolean {
return this._size === 0;
}
/**
* 清空队列
*/
public clear(): void {
for (let i = 0; i < this._capacity; i++) {
this._buffer[i] = undefined;
}
this._head = 0;
this._tail = 0;
this._size = 0;
}
/**
* 扩容队列(内部使用)
* 当队列满时自动调用,容量翻倍
*/
private _resize(): void {
const newCapacity = this._capacity * 2;
const newBuffer = new Array<T | undefined>(newCapacity);
// 复制现有元素到新缓冲区
for (let i = 0; i < this._size; i++) {
newBuffer[i] = this._buffer[(this._head + i) % this._capacity];
}
this._buffer = newBuffer;
this._head = 0;
this._tail = this._size;
this._capacity = newCapacity;
}
/**
* 将队列转换为数组
* @returns 包含队列所有元素的数组(从头到尾的顺序)
*/
public toArray(): T[] {
const result: T[] = [];
for (let i = 0; i < this._size; i++) {
const item = this._buffer[(this._head + i) % this._capacity];
if (item !== undefined) {
result.push(item);
}
}
return result;
}
}