Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. class LRUCache {
  2.  
  3. /*
  4. Our internal definition of a doubly linked list
  5. node, put in this class for convenience since
  6. this is submitted in one file on Leetcode
  7. */
  8. private class DNode {
  9. int key;
  10. int value;
  11. DNode prev;
  12. DNode next;
  13. }
  14.  
  15. private Map<Integer, DNode> hashtable = new HashMap<Integer, DNode>();
  16. private DNode head, tail;
  17. private int totalItemsInCache;
  18. private int maxCapacity;
  19.  
  20. public LRUCache(int maxCapacity) {
  21.  
  22. // Cache starts empty and capacity is set by client
  23. totalItemsInCache = 0;
  24. this.maxCapacity = maxCapacity;
  25.  
  26. // Initialize the dummy head of the cache
  27. head = new DNode();
  28. head.prev = null;
  29.  
  30. // Init the dummy tail of the cache
  31. tail = new DNode();
  32. tail.next = null;
  33.  
  34. // Wire the head and tail together
  35. head.next = tail;
  36. tail.prev = head;
  37. }
  38.  
  39. /*
  40. Retrieve an item from the cache
  41. */
  42. public int get(int key) {
  43.  
  44. DNode node = hashtable.get(key);
  45. boolean itemFoundInCache = node != null;
  46.  
  47. // Empty state check. Should raise exception here.
  48. if(!itemFoundInCache){
  49. return -1;
  50. }
  51.  
  52. // Item has been accessed. Move to the front of the list.
  53. moveToHead(node);
  54.  
  55. return node.value;
  56. }
  57.  
  58. /*
  59. Add an item to the cache if it is not already there,
  60. if it is already there update the value and move it
  61. to the front of the cache
  62. */
  63. public void put(int key, int value) {
  64.  
  65. DNode node = hashtable.get(key);
  66. boolean itemFoundInCache = node != null;
  67.  
  68. if(!itemFoundInCache){
  69.  
  70. // Create a new node
  71. DNode newNode = new DNode();
  72. newNode.key = key;
  73. newNode.value = value;
  74.  
  75. // Add to the hashtable and the doubly linked list
  76. hashtable.put(key, newNode);
  77. addNode(newNode);
  78.  
  79. // We just added an item to the cache
  80. totalItemsInCache++;
  81.  
  82. // If over capacity evict an item with LRU cache eviction policy
  83. if(totalItemsInCache > maxCapacity){
  84. removeLRUEntryFromStructure();
  85. }
  86.  
  87. } else {
  88. // If item is in cache just update and move it to the head
  89. node.value = value;
  90. moveToHead(node);
  91. }
  92.  
  93. }
  94.  
  95. /*
  96. Remove the least used entry from the doubly linked
  97. list as well as the hashtable. Hence it is evicted
  98. from the whole LRUCache structure
  99. */
  100. private void removeLRUEntryFromStructure() {
  101. DNode tail = popTail();
  102. hashtable.remove(tail.key);
  103. --totalItemsInCache;
  104. }
  105.  
  106. /*
  107. Insertions to the doubly linked list will always
  108. be right after the dummy head
  109. */
  110. private void addNode(DNode node){
  111.  
  112. // Wire the node being inserted
  113. node.prev = head;
  114. node.next = head.next;
  115.  
  116. // Re-wire the head's old next
  117. head.next.prev = node;
  118.  
  119. // Re-wire the head to point to the inserted node
  120. head.next = node;
  121. }
  122.  
  123. /*
  124. Remove the given node from the doubly linked list
  125. */
  126. private void removeNode(DNode node){
  127.  
  128. // Grab reference to the prev and next of the node
  129. DNode savedPrev = node.prev;
  130. DNode savedNext = node.next;
  131.  
  132. // Cut out going forwards
  133. savedPrev.next = savedNext;
  134.  
  135. // Cut out going backards
  136. savedNext.prev = savedPrev;
  137. }
  138.  
  139. /*
  140. Move a node to the head of the doubly linked lsit
  141. */
  142. private void moveToHead(DNode node){
  143. removeNode(node);
  144. addNode(node);
  145. }
  146.  
  147. /*
  148. Pop the last item from the structure
  149. */
  150. private DNode popTail(){
  151. DNode itemBeingRemoved = tail.prev;
  152. removeNode(itemBeingRemoved);
  153. return itemBeingRemoved;
  154. }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement