首页 > 代码库 > LRU cache.
LRU cache.
See http://blog.csdn.net/hexinuaa/article/details/6630384
Node<T> { T data; Node<T> next; Node<T> pre; } Node<T> swapToHead(Node<T> head, Node<T> n) { if (n == head) return n; Node<T> temp = n.next; n.next = temp; if (temp != null) temp.pre = n.pre; n.pre = null; n.next = head; return n; } class LRU<K, T> { private Map<K, Node<T>> map; private Node<T> head; private Node<T> tail; int size; T search(K key) { Node<T> n = map.get(key); if (n != null) { head = swapToHead(head, n); return head.data; } else { T = getOutFromCache(K); Node newNode = new Node(T); newNode.next = head; head = newNode; if (size == MAX_SIZE) { Node newTail = tail.pre; newTail.next = null; tail.pre = null; tail = null; tail = newTail; } else { size++; } } return T; } }
LRU cache.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。