MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / Put

Method Put

csharp/Linked Lists/LRUCache.cs:57–75  ·  view source on GitHub ↗
(int key, int value)

Source from the content-addressed store, hash-verified

55 }
56
57 public void Put(int key, int value)
58 {
59 // If a node with this key already exists, remove it from the linked list.
60 if (_hashMap.ContainsKey(key))
61 removeNode(_hashMap[key]);
62
63 DoublyLinkedListNode node = new DoublyLinkedListNode(key, value);
64 _hashMap[key] = node;
65
66 // Remove the least recently used node from the cache if adding
67 // this new node will result in an overflow.
68 if (_hashMap.Count > _capacity)
69 {
70 _hashMap.Remove(_head.Next.Key);
71 removeNode(_head.Next);
72 }
73
74 addToTail(node);
75 }
76
77 private void addToTail(DoublyLinkedListNode node)
78 {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected