GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/LRUCache.java at master · mengli/leetcode Expand file tree/ Copy pathLRUCache.javaMore file actions 143 lines (123 loc) · 3.7 KB / Copy pathLRUCache.javaFile metadata and controls143 lines (123 loc) · 3.7 KB 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 import java.util.HashMap; import java.util.Map; /** * Design and implement a data structure for Least Recently Used (LRU) cache. It should support the * following operations: get and set. * * <p>get(key) - Get the value (will always be positive) of the key if the key exists in the cache, * otherwise return -1. set(key, value) - Set or insert the value if the key is not already present. * When the cache reached its capacity, it should invalidate the least recently used item before * inserting a new item. */ public class LRUCache { public class CacheItem<K, V> { private K key; private V value; private CacheItem<K, V> prev; private CacheItem<K, V> next; public CacheItem<K, V> getPrev() { return prev; } public void setPrev(CacheItem<K, V> prev) { this.prev = prev; } public CacheItem<K, V> getNext() { return next; } public void setNext(CacheItem<K, V> next) { this.next = next; } public CacheItem(K key, V value) { this.key = key; this.value = value; } } public class BidirectionalList<K, V> { private CacheItem<K, V> head = new CacheItem<K, V>(null, null); private CacheItem<K, V> tail = head; public void insertTail(CacheItem<K, V> node) { tail.next = node; node.prev = tail; node.next = null; tail = node; } public void delete(CacheItem<K, V> node) { if (head.next == null) return; if (node == head.next) { deleteHead(); } else if (node == tail) { deleteTail(); } else { node.prev.next = node.next; node.next.prev = node.prev; node.next = null; node.prev = null; } } public CacheItem<K, V> deleteTail() { if (head.next == null) return null; CacheItem<K, V> ret = tail; tail = tail.prev; tail.next = null; ret.prev = null; ret.next = null; return ret; } public CacheItem<K, V> deleteHead() { if (head.next == null) return null; if (head.next == tail) { tail = tail.prev; } CacheItem<K, V> ret = head.next; head.next = ret.next; if (ret.next != null) { ret.next.prev = head; } ret.next = null; ret.prev = null; return ret; } } private Map<Integer, CacheItem<Integer, Integer>> cacheMap = new HashMap<Integer, CacheItem<Integer, Integer>>(); private BidirectionalList<Integer, Integer> cacheList = new BidirectionalList<Integer, Integer>(); private int cacheSize = 0; private int capacity; public LRUCache(int capacity) { this.capacity = capacity; } public int get(int key) { CacheItem<Integer, Integer> itm = cacheMap.get(key); if (itm == null) return -1; cacheList.delete(itm); cacheList.insertTail(itm); return itm.value.intValue(); } public void set(int key, int value) { if (cacheMap.containsKey(key)) { updateItem(key, value); } else if (cacheSize == capacity && removeLeastUsed()) { cacheSize--; addItem(key, value); } else { addItem(key, value); } } private void addItem(int key, int value) { CacheItem<Integer, Integer> newCacheItem = new CacheItem<Integer, Integer>(key, value); cacheList.insertTail(newCacheItem); cacheMap.put(key, newCacheItem); cacheSize++; } private void updateItem(int key, int value) { CacheItem<Integer, Integer> exitedKey = cacheMap.get(key); exitedKey.value = value; cacheList.delete(exitedKey); cacheList.insertTail(exitedKey); } private boolean removeLeastUsed() { CacheItem<Integer, Integer> removedKey = cacheList.deleteHead(); cacheMap.remove(removedKey.key); return removedKey == null ? false : true; } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |