add:添加简单LRU工具类
This commit is contained in:
15
src/utils/LRU.ts
Normal file
15
src/utils/LRU.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
export function pushToStack(stack: string[], id: string): void {
|
||||||
|
if (stack.includes(id)) {
|
||||||
|
// 如果已经存在于栈中,先移除再插入到栈顶
|
||||||
|
stack.splice(stack.indexOf(id), 1);
|
||||||
|
}
|
||||||
|
stack.push(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function popFromStack(stack: string[]): string | undefined {
|
||||||
|
return stack.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function topFromStack(stack: string[]): string | undefined {
|
||||||
|
return stack.at(-1); // 使用 .at() 方法访问最后一个元素
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user