Blame view

node_modules/zrender/src/core/LRU.ts 3.42 KB
bd028579   易尊强   2/28
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
  import { Dictionary } from './types';
  
  // Simple LRU cache use doubly linked list
  // @module zrender/core/LRU
  
  export class Entry<T> {
  
      value: T
  
      key: string | number
  
      next: Entry<T>
  
      prev: Entry<T>
  
      constructor(val: T) {
          this.value = val;
      }
  }
  /**
   * Simple double linked list. Compared with array, it has O(1) remove operation.
   * @constructor
   */
  export class LinkedList<T> {
  
      head: Entry<T>
      tail: Entry<T>
  
      private _len = 0
  
      /**
       * Insert a new value at the tail
       */
      insert(val: T): Entry<T> {
          const entry = new Entry(val);
          this.insertEntry(entry);
          return entry;
      }
  
      /**
       * Insert an entry at the tail
       */
      insertEntry(entry: Entry<T>) {
          if (!this.head) {
              this.head = this.tail = entry;
          }
          else {
              this.tail.next = entry;
              entry.prev = this.tail;
              entry.next = null;
              this.tail = entry;
          }
          this._len++;
      }
  
      /**
       * Remove entry.
       */
      remove(entry: Entry<T>) {
          const prev = entry.prev;
          const next = entry.next;
          if (prev) {
              prev.next = next;
          }
          else {
              // Is head
              this.head = next;
          }
          if (next) {
              next.prev = prev;
          }
          else {
              // Is tail
              this.tail = prev;
          }
          entry.next = entry.prev = null;
          this._len--;
      }
  
      /**
       * Get length
       */
      len(): number {
          return this._len;
      }
  
      /**
       * Clear list
       */
      clear() {
          this.head = this.tail = null;
          this._len = 0;
      }
  
  }
  
  /**
   * LRU Cache
   */
  export default class LRU<T> {
  
      private _list = new LinkedList<T>()
  
      private _maxSize = 10
  
      private _lastRemovedEntry: Entry<T>
  
      private _map: Dictionary<Entry<T>> = {}
  
      constructor(maxSize: number) {
          this._maxSize = maxSize;
      }
  
      /**
       * @return Removed value
       */
      put(key: string | number, value: T): T {
          const list = this._list;
          const map = this._map;
          let removed = null;
          if (map[key] == null) {
              const len = list.len();
              // Reuse last removed entry
              let entry = this._lastRemovedEntry;
  
              if (len >= this._maxSize && len > 0) {
                  // Remove the least recently used
                  const leastUsedEntry = list.head;
                  list.remove(leastUsedEntry);
                  delete map[leastUsedEntry.key];
  
                  removed = leastUsedEntry.value;
                  this._lastRemovedEntry = leastUsedEntry;
              }
  
              if (entry) {
                  entry.value = value;
              }
              else {
                  entry = new Entry(value);
              }
              entry.key = key;
              list.insertEntry(entry);
              map[key] = entry;
          }
  
          return removed;
      }
  
      get(key: string | number): T {
          const entry = this._map[key];
          const list = this._list;
          if (entry != null) {
              // Put the latest used entry in the tail
              if (entry !== list.tail) {
                  list.remove(entry);
                  list.insertEntry(entry);
              }
  
              return entry.value;
          }
      }
  
      /**
       * Clear the cache
       */
      clear() {
          this._list.clear();
          this._map = {};
      }
  
      len() {
          return this._list.len();
      }
  }