123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.LruCache = void 0;
- class LruCache {
- constructor(limit = 1000) {
- this.limit = limit;
- this.head = undefined;
- this.tail = undefined;
- this.map = Object.create(null);
- this.capacity = limit | 0;
- }
- get size() {
- return this.limit - this.capacity;
- }
- set(key, value) {
- const node = this.map[key];
- if (node) {
- this.pop(node);
- node.v = value;
- this.push(node);
- }
- else {
- if (!this.capacity) {
- const head = this.head;
- if (head) {
- this.pop(head);
- delete this.map[head.k];
- this.capacity++;
- }
- }
- this.capacity--;
- const node = new LruNode(key, value);
- this.map[key] = node;
- this.push(node);
- }
- }
- get(key) {
- const node = this.map[key];
- if (!node)
- return;
- if (this.tail !== node) {
- this.pop(node);
- this.push(node);
- }
- return node.v;
- }
- peek(key) {
- const node = this.map[key];
- return node instanceof LruNode ? node.v : undefined;
- }
- has(key) {
- return key in this.map;
- }
- clear() {
- this.head = undefined;
- this.tail = undefined;
- this.map = Object.create(null);
- this.capacity = this.limit;
- }
- keys() {
- return Object.keys(this.map);
- }
- del(key) {
- const node = this.map[key];
- if (node instanceof LruNode) {
- this.pop(node);
- delete this.map[key];
- ++this.capacity;
- return true;
- }
- return false;
- }
- pop(node) {
- const l = node.l;
- const r = node.r;
- if (this.head === node)
- this.head = r;
- else
- l.r = r;
- if (this.tail === node)
- this.tail = l;
- else
- r.l = l;
- // node.l = undefined;
- // node.r = undefined;
- }
- push(node) {
- const tail = this.tail;
- if (tail) {
- tail.r = node;
- node.l = tail;
- }
- else
- this.head = node;
- this.tail = node;
- }
- }
- exports.LruCache = LruCache;
- class LruNode {
- constructor(k, v) {
- this.k = k;
- this.v = v;
- this.l = undefined;
- this.r = undefined;
- }
- }
|