loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.9602591458478096","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"CmNsYXNzIExSVW5vZGUgewogIGNvbnN0cnVjdG9yIChrZXksIHZhbHVlKSB7CiAgICB0aGlzLmtleSA9IGtleTsKICAgIHRoaXMudmFsdWUgPSB2YWx1ZTsKICAgIHRoaXMubmV4dCA9IG51bGw7CiAgICB0aGlzLnByZXYgPSBudWxsOwogIH0KfQoKY2xhc3MgTFJVY2FjaGUgewogIGNvbnN0cnVjdG9yIChsaW1pdCkgewogICAgdGhpcy5zaXplID0gMDsKICAgIHRoaXMubGltaXQgPSBsaW1pdCB8fCAxMDsKICAgIHRoaXMubWFwID0gbmV3IE1hcCgpOwogICAgdGhpcy5oZWFkID0gbnVsbDsKICAgIHRoaXMudGFpbCA9IG51bGw7CiAgfQogIG1ha2VIZWFkIChub2RlKSB7CiAgICBub2RlLm5leHQgPSB0aGlzLmhlYWQ7IC8vIGRlbW90ZSBjdXJyZW50IGhlYWQKICAgIG5vZGUucHJldiA9IG51bGw7CiAgICBpZih0aGlzLmhlYWQgIT09IG51bGwpeyAvLyBpcyBjYWNoZSBub3QgZW1wdHkKICAgICAgdGhpcy5oZWFkLnByZXYgPSBub2RlOwogICAgfQogICAgdGhpcy5oZWFkID0gbm9kZTsKICAgIGlmKHRoaXMudGFpbCA9PT0gbnVsbCl7IC8vIG9ubHkgd2hlbiBjYWNoZSBoYXMgb25lIG5vZGUgYXJlIHRoZSBoZWFkIGFuZCB0YWlsIHRoZSBzYW1lIG5vZGUKICAgICAgdGhpcy50YWlsID0gbm9kZTsKICAgIH0KICAgIHRoaXMuc2l6ZSsrOwogICAgdGhpcy5tYXAuc2V0KG5vZGUua2V5LCBub2RlKTsgLy8gbWFwIGZvciBsb29rdXAKICB9CiAgc2V0IChrZXksIHZhbHVlKSB7CiAgICBjb25zdCBub2RlID0gbmV3IExSVW5vZGUoa2V5LCB2YWx1Z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IC8vIGlzIG5vdCB0YWlsCiAgICAgIG5vZGUubmV4dC5wcmV2ID0gbm9kZS5wcmV2OwogICAgfSBlbHNlIHsgLy8gaXMgdGFpbAogICAgICB0aGlzLnRhaWwgPSB0aGlzLnRhaWwucHJldjsKICAgIH0KICAgIHRoaXMubWFwLmRlbGV0ZShrZXkpOwogICAgdGhpcy5zaXplLS07CiAgfQogIHRvQXJyYXkgKCkgewogICAgY29uc3QganNvbiA9IFtdOwogICAgbGV0IG5vZGUgPSB0aGlzLmhlYWQ7CiAgICB3aGlsZShub2RlKSB7IC8vIHRoaXMudGFpbC5uZXh0IHdpbGwgYWxsd2F5cyBiZSBudWxsCiAgICAgIGpzb24ucHVzaCh7a2V5OiBub2RlLmtleSwgdmFsdWU6IG5vZGUudmFsdWV9KTsKICAgICAgbm9kZSA9IG5vZGUubmV4dDsKICAgIH0KICAgIHJldHVybiBqc29uOwogIH0KfQoKY29uc3QgY2FjaGUgPSBuZXcgTFJVY2FjaGUoMyk7CgpjYWNoZS5zZXQoJ29uZScsICcxJyk7CmNhY2hlLnNldCgndHdvJywgJzInKTsKY2FjaGUuc2V0KCd0aHJlZScsICczJyk7CmNvbnNvbGUubG9nKGNhY2hlLnRvQXJyYXkoKSk7CmNhY2hlLnNldCgnZm91cicsICc0Jyk7CmNvbnNvbGUubG9nKGNhY2hlLnRvQXJyYXkoKSk7CmNhY2hlLnJlbW92ZSgndHdvJyk7CmNvbnNvbGUubG9nKGNhY2hlLnRhaWwpOwo="},"asBuffer":null},"loaded":true}}
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
class LRUnode {
  constructor (key, value) {
    this.key = key;
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class LRUcache {
  constructor (limit) {
    this.size = 0;
    this.limit = limit || 10;
    this.map = new Map();
    this.head = null;
    this.tail = null;
  }
  makeHead (node) {
    node.next = this.head; // demote current head
    node.prev = null;
    if(this.head !== null){ // is cache not empty
      this.head.prev = node;
    }
    this.head = node;
    if(this.tail === null){ // only when cache has one node are the head and tail the same node
      this.tail = node;
    }
    this.size++;
    this.map.set(node.key, node); // map for lookup
  }
  set (key, value) {
    const node = new LRUnode(key, value);
    if(this.map.get(key)){
      this.map.get(key).value = value;
      this.remove(node.key);
    } else {
      if(this.size >= this.limit){ // cull least recently used
        this.remove(this.tail.key);
      }
    }
    this.makeHead(node); // most recently used; promote to head
  }
  get (key) {
    if(this.map.get(key)){ 
      const value = this.map.get(key).value;
      const node = new LRUnode(key, value);
      this.remove(key);
      this.makeHead(node); // most recently used; promote to head
      return value;
    } else {
      console.log(`Key '${key}' does not exist`);
    }
  }
  remove (key) {
    const node = this.map.get(key);
    if(node.prev !== null){ // is not head
      node.prev.next = node.next;
    } else { // is head
      this.head = node.next; 
    }
    if(node.next !== null) { // is not tail
      node.next.prev = node.prev;
    } else { // is tail
      this.tail = this.tail.prev;
    }
    this.map.delete(key);
    this.size--;
  }
  toArray () {
    const json = [];
    let node = this.head;
    while(node) { // this.tail.next will allways be null
      json.push({key: node.key, value: node.value});
      node = node.next;
    }
    return json;
  }
}

const cache = new LRUcache(3);

cache.set('one', '1');
cache.set('two', '2');
cache.set('three', '3');
console.log(cache.toArray());
cache.set('four', '4');
console.log(cache.toArray());
cache.remove('two');
console.log(cache.tail);
node v10.16.0