add:添加简单LRU工具类

This commit is contained in:
lhj
2024-11-16 14:43:26 +08:00
parent 57020eb9ca
commit f7a4a80a23

15
src/utils/LRU.ts Normal file
View 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() 方法访问最后一个元素
}