1. 程式人生 > >HashMap+雙向鏈表手寫LRU緩存算法/頁面置換算法

HashMap+雙向鏈表手寫LRU緩存算法/頁面置換算法

rri color over 鏈表 put null nbsp pri ext

import java.util.Hashtable;
//https://zhuanlan.zhihu.com/p/34133067
class DLinkedList {
    String key; //
    int value;    //
    DLinkedList pre; //雙向鏈表前驅
    DLinkedList next; //雙向鏈表後繼
}
public class LRUCache {
    private Hashtable<String,DLinkedList> cache = new Hashtable<String,DLinkedList>(); 
    
private int count; private int capacity; private DLinkedList head, tail; public LRUCache(int capacity) { this.count = 0; this.capacity = capacity; head = new DLinkedList(); head.pre = null; tail = new DLinkedList(); tail.next = null
; head.next = tail; tail.pre = head; } public int get(String key) { DLinkedList node = cache.get(key); if(node == null) return -1; this.moveToHead(node); return node.value; } public void set(String key,int value) { DLinkedList node
= cache.get(key); if(node == null) { DLinkedList newNode = new DLinkedList(); newNode.key = key; newNode.value = value; this.cache.put(key, newNode); this.addNode(newNode); ++count; if(count>capacity) { DLinkedList tail = this.popTail(); this.cache.remove(tail.key); --count; } } else { node.value = value; this.moveToHead(node); } } private void addNode(DLinkedList node) { node.pre = head; node.next = head.next; head.next.pre = node; head.next = node; } private void removeNode(DLinkedList node) { DLinkedList pre = node.pre; DLinkedList next = node.next; pre.next = next; next.pre = pre; } private void moveToHead(DLinkedList node) { this.removeNode(node); this.addNode(node); } private DLinkedList popTail() { DLinkedList res = tail.pre; this.removeNode(res); return res; } @Override public String toString() { StringBuilder sb = new StringBuilder(); DLinkedList node = head; while(node != null){ sb.append(String.format("%s:%s ", node.key,node.value)); node = node.next; } return sb.toString(); } public static void main(String[] args) { LRUCache lru = new LRUCache(3); lru.set("1", 7); System.out.println(lru.toString()); lru.set("2", 0); System.out.println(lru.toString()); lru.set("3", 1); System.out.println(lru.toString()); lru.set("4", 2); System.out.println(lru.toString()); lru.get("2"); System.out.println(lru.toString()); lru.set("5", 3); System.out.println(lru.toString()); lru.get("2"); System.out.println(lru.toString()); lru.set("6", 4); System.out.println(lru.toString()); /* 0ull:0 1:7 null:0 null:0 2:0 1:7 null:0 null:0 3:1 2:0 1:7 null:0 null:0 4:2 3:1 2:0 null:0 null:0 2:0 4:2 3:1 null:0 null:0 5:3 2:0 4:2 null:0 null:0 2:0 5:3 4:2 null:0 null:0 6:4 2:0 5:3 null:0 */ } }

HashMap+雙向鏈表手寫LRU緩存算法/頁面置換算法