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; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。