MCPcopy
hub / github.com/trekhleb/javascript-algorithms / append

Method append

src/data-structures/lru-cache/LRUCache.js:89–112  ·  view source on GitHub ↗

* Appends a new node to the end of the cache linked list. * @param {LinkedListNode} node

(node)

Source from the content-addressed store, hash-verified

87 * @param {LinkedListNode} node
88 */
89 append(node) {
90 this.nodesMap[node.key] = node;
91
92 if (!this.head.next) {
93 // First node to append.
94 this.head.next = node;
95 this.tail.prev = node;
96 node.prev = this.head;
97 node.next = this.tail;
98 } else {
99 // Append to an existing tail.
100 const oldTail = this.tail.prev;
101 oldTail.next = node;
102 node.prev = oldTail;
103 node.next = this.tail;
104 this.tail.prev = node;
105 }
106
107 this.size += 1;
108
109 if (this.size > this.capacity) {
110 this.evict(this.head.next);
111 }
112 }
113
114 /**
115 * Evicts (removes) the node from cache linked list.

Callers 7

setMethod · 0.95
promoteMethod · 0.95
enqueueMethod · 0.45
setMethod · 0.45
addEdgeMethod · 0.45
traversal.test.jsFile · 0.45

Calls 1

evictMethod · 0.95

Tested by

no test coverage detected