All files lru.ts

38.1% Statements 64/168
28.57% Branches 16/56
18.52% Functions 5/27
38.32% Lines 64/167

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318                                        3x 3x   3x 357x           357x 357x 357x 357x   357x                 257x 257x 257x 257x     3x 338x   154x           184x 184x 4x   184x   184x 180x   184x 184x 184x 184x   184x     3x                                             3x   597x 597x 259x     338x 338x     3x 257x   257x               257x   257x   195x 195x     62x       257x 257x 257x         257x     3x                                                   3x         3x       3x                                                         3x                   3x     3x                         3x     3x                         3x     3x                   3x       3x       3x       3x       3x                             3x                       3x                        
/**
 * A doubly linked list-based Least Recently Used (LRU) cache. Will keep most
 * recently used items while discarding least recently used items when its limit
 * is reached.
 *
 * Licensed under MIT. Copyright (c) 2010 Rasmus Andersson <http://hunch.se/>
 * See README.md for details.
 *
 * Illustration of the design:
 *
 *       entry             entry             entry             entry
 *       ______            ______            ______            ______
 *      | head |.newer => |      |.newer => |      |.newer => | tail |
 *      |  A   |          |  B   |          |  C   |          |  D   |
 *      |______| <= older.|______| <= older.|______| <= older.|______|
 *
 *  removed  <--  <--  <--  <--  <--  <--  <--  <--  <--  <--  <--  added
 */
/* eslint-disable */
 
const NEWER = Symbol('newer')
const OLDER = Symbol('older')
 
export function LRUMap(limit: number, entries?: any) {
  Iif (typeof limit !== 'number') {
    // called as (entries)
    entries = limit
    limit = 0
  }
 
  this.size = 0
  this.limit = limit
  this.oldest = this.newest = undefined
  this._keymap = new Map()
 
  Iif (entries) {
    this.assign(entries)
    if (limit < 1) {
      this.limit = this.size
    }
  }
}
 
function Entry(key: any, value: any) {
  this.key = key
  this.value = value
  this[NEWER] = undefined
  this[OLDER] = undefined
}
 
LRUMap.prototype._markEntryAsUsed = function(entry: any) {
  if (entry === this.newest) {
    // Already the most recenlty used entry, so no need to update the list
    return
  }
  // HEAD--------------TAIL
  //   <.older   .newer>
  //  <--- add direction --
  //   A  B  C  <D>  E
  Eif (entry[NEWER]) {
    if (entry === this.oldest) {
      this.oldest = entry[NEWER]
    }
    entry[NEWER][OLDER] = entry[OLDER] // C <-- E.
  }
  if (entry[OLDER]) {
    entry[OLDER][NEWER] = entry[NEWER] // C. --> E
  }
  entry[NEWER] = undefined // D --x
  entry[OLDER] = this.newest // D. --> E
  Eif (this.newest) {
    this.newest[NEWER] = entry // E. <-- D
  }
  this.newest = entry
}
 
LRUMap.prototype.assign = function(entries: any) {
  let entry: any
  let limit = this.limit || Number.MAX_VALUE
  this._keymap.clear()
  const it = entries[Symbol.iterator]()
  for (let itv = it.next(); !itv.done; itv = it.next()) {
    const e = new Entry(itv.value[0], itv.value[1])
    this._keymap.set(e.key, e)
    if (!entry) {
      this.oldest = e
    } else {
      entry[NEWER] = e
      e[OLDER] = entry
    }
    entry = e
    if (limit-- === 0) {
      throw new Error('overflow')
    }
  }
  this.newest = entry
  this.size = this._keymap.size
}
 
LRUMap.prototype.get = function(key: any) {
  // First, find our cache entry
  const entry = this._keymap.get(key)
  if (!entry) {
    return
  } // Not cached. Sorry.
  // As <key> was found in the cache, register it as being requested recently
  this._markEntryAsUsed(entry)
  return entry.value
}
 
LRUMap.prototype.set = function(key: any, value: any) {
  let entry = this._keymap.get(key)
 
  Iif (entry) {
    // update existing
    entry.value = value
    this._markEntryAsUsed(entry)
    return this
  }
 
  // new entry
  this._keymap.set(key, (entry = new Entry(key, value)))
 
  if (this.newest) {
    // link previous tail to the new tail (entry)
    this.newest[NEWER] = entry
    entry[OLDER] = this.newest
  } else {
    // we're first in -- yay
    this.oldest = entry
  }
 
  // add new entry to the end of the linked list -- it's now the freshest entry.
  this.newest = entry
  ++this.size
  Iif (this.size > this.limit) {
    // we hit the limit -- remove the head
    this.shift()
  }
 
  return this
}
 
LRUMap.prototype.shift = function() {
  // todo: handle special case when limit == 1
  const entry = this.oldest
  if (entry) {
    if (this.oldest[NEWER]) {
      // advance the list
      this.oldest = this.oldest[NEWER]
      this.oldest[OLDER] = undefined
    } else {
      // the cache is exhausted
      this.oldest = undefined
      this.newest = undefined
    }
    // Remove last strong reference to <entry> and remove links from the purged
    // entry being returned:
    entry[NEWER] = entry[OLDER] = undefined
    this._keymap.delete(entry.key)
    --this.size
    return [entry.key, entry.value]
  }
}
 
// ----------------------------------------------------------------------------
// Following code is optional and can be removed without breaking the core
// functionality.
 
LRUMap.prototype.find = function(key: any) {
  const e = this._keymap.get(key)
  return e ? e.value : undefined
}
 
LRUMap.prototype.has = function(key: any) {
  return this._keymap.has(key)
}
 
LRUMap.prototype.delete = function(key: any) {
  const entry = this._keymap.get(key)
  if (!entry) {
    return
  }
  this._keymap.delete(entry.key)
  if (entry[NEWER] && entry[OLDER]) {
    // relink the older entry with the newer entry
    entry[OLDER][NEWER] = entry[NEWER]
    entry[NEWER][OLDER] = entry[OLDER]
  } else if (entry[NEWER]) {
    // remove the link to us
    entry[NEWER][OLDER] = undefined
    // link the newer entry to head
    this.oldest = entry[NEWER]
  } else if (entry[OLDER]) {
    // remove the link to us
    entry[OLDER][NEWER] = undefined
    // link the newer entry to head
    this.newest = entry[OLDER]
  } else {
    // if(entry[OLDER] === undefined && entry.newer === undefined) {
    this.oldest = this.newest = undefined
  }
 
  this.size--
  return entry.value
}
 
LRUMap.prototype.clear = function() {
  // Not clearing links should be safe, as we don't expose live links to user
  this.oldest = this.newest = undefined
  this.size = 0
  this._keymap.clear()
}
 
function EntryIterator(oldestEntry: any) {
  this.entry = oldestEntry
}
EntryIterator.prototype[Symbol.iterator] = function() {
  return this
}
EntryIterator.prototype.next = function() {
  const ent = this.entry
  if (ent) {
    this.entry = ent[NEWER]
    return { done: false, value: [ent.key, ent.value] }
  } else {
    return { done: true, value: undefined }
  }
}
 
function KeyIterator(oldestEntry) {
  this.entry = oldestEntry
}
KeyIterator.prototype[Symbol.iterator] = function() {
  return this
}
KeyIterator.prototype.next = function() {
  const ent = this.entry
  if (ent) {
    this.entry = ent[NEWER]
    return { done: false, value: ent.key }
  } else {
    return { done: true, value: undefined }
  }
}
 
function ValueIterator(oldestEntry) {
  this.entry = oldestEntry
}
ValueIterator.prototype[Symbol.iterator] = function() {
  return this
}
ValueIterator.prototype.next = function() {
  const ent = this.entry
  if (ent) {
    this.entry = ent[NEWER]
    return { done: false, value: ent.value }
  } else {
    return { done: true, value: undefined }
  }
}
 
LRUMap.prototype.keys = function() {
  return new KeyIterator(this.oldest)
}
 
LRUMap.prototype.values = function() {
  return new ValueIterator(this.oldest)
}
 
LRUMap.prototype.entries = function() {
  return this
}
 
LRUMap.prototype[Symbol.iterator] = function() {
  return new EntryIterator(this.oldest)
}
 
LRUMap.prototype.forEach = function(
  fun: (value: any, key: any, ctx: object) => void,
  thisObj: any
) {
  if (typeof thisObj !== 'object') {
    thisObj = this
  }
  let entry = this.oldest
  while (entry) {
    fun.call(thisObj, entry.value, entry.key, this)
    entry = entry[NEWER]
  }
}
 
/** Returns a JSON (array) representation */
LRUMap.prototype.toJSON = function() {
  const s = new Array(this.size)
  let i = 0
  let entry = this.oldest
  while (entry) {
    s[i++] = { key: entry.key, value: entry.value }
    entry = entry[NEWER]
  }
  return s
}
 
/** Returns a String representation */
LRUMap.prototype.toString = function() {
  let s = ''
  let entry = this.oldest
  while (entry) {
    s += String(entry.key) + ':' + entry.value
    entry = entry[NEWER]
    if (entry) {
      s += ' < '
    }
  }
  return s
}