1. 設計緩存結構LRU
注:哈希表中,key或value如果存的是簡單類型(基本數據類型和String),則哈希表中存的是簡單類型的值。如果要往哈希表中添加對象,存的是該對象的地址。
思路:
先設計一個節點Node<key, value>,(key,value的類型可以使用泛型)根據這個節點設計一個雙向鏈表(由頭到尾,優先級由低到高),裏面存的是Node節點設計雙向鏈表的幾個方法:添加節點、刪除節點、將某節點移到尾部。
創建一個HashMap<String, Node<key, value>>,可以通過get(i)的方式獲取中鏈表中該節點的地址。如果雙向鏈表出現添加或刪除節點操作的話,處理哈希表中對應節點。
import java.util.HashMap;
public class LRU {
//節點類型,有頭指針和尾指針
public static class Node<K,V> {
public K key;
public V value;
public Node<K, V> last;
public Node<K, V> next;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
}
//設計雙向鏈表
public static class NodeDoubleLinkedList<K, V> {
private Node<K, V> head;
private Node<K, V> tail;
public NodeDoubleLinkedList() {
this.head = null;
this.tail = null;
}
public void addNode(Node<K, V> newNode) {
if (newNode == null) {
return;
}
if (this.head == null) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
newNode.last = this.tail;
this.tail = newNode;
}
}
//每一次操作node節點後,都需要將node節點優先級調到最高
public void moveNodeToTail(Node<K, V> node) {
if (this.tail == node) {
return;
}
if (this.head == node) {
this.head = node.next;
this.head.last = null;
} else {
node.last.next = node.next;
node.next.last = node.last;
}
node.last = this.tail;
node.next = null;
this.tail.next = node;
this.tail = node;
}
public Node<K, V> removeHead() {
if (this.head == null) {
return null;
}
Node<K, V> res = this.head;
if (this.head == this.tail) {
this.head = null;
this.tail = null;
} else {
this.head = res.next;
res.next = null;
this.head.last = null;
}
return res;
}
}
public static class MyCache<K, V> {
private HashMap<K, Node<K, V>> keyNodeMap;
private NodeDoubleLinkedList<K, V> nodeList;
private int capacity;
public MyCache(int capacity) {
if (capacity < 1) {
throw new RuntimeException("should be more than 0.");
}
this.keyNodeMap = new HashMap<K, Node<K, V>>();
this.nodeList = new NodeDoubleLinkedList<K, V>();
this.capacity = capacity;
}
//從內存中取節點
public V get(K key) {
if (this.keyNodeMap.containsKey(key)) {
Node<K, V> res = this.keyNodeMap.get(key);
this.nodeList.moveNodeToTail(res);
return res.value;
}
return null;
}
//往內存中添加節點
public void set(K key, V value) {
if (this.keyNodeMap.containsKey(key)) {
Node<K, V> node = this.keyNodeMap.get(key);
node.value = value;
this.nodeList.moveNodeToTail(node);
} else {
Node<K, V> newNode = new Node<K,V>(key, value);
this.keyNodeMap.put(key, newNode);
this.nodeList.addNode(newNode);
if (this.keyNodeMap.size() == this.capacity + 1) {
this.removeMostUnusedCache();
}
}
}
//容量超標是,刪除最久未使用的節點
private void removeMostUnusedCache() {
Node<K, V> removeNode = this.nodeList.removeHead();
K removeKey = removeNode.key;
this.keyNodeMap.remove(removeKey);
}
}
public static void main(String[] args) {
MyCache<String, Integer> testCache = new MyCache<String, Integer>(3);
testCache.set("A", 1);
testCache.set("B", 2);
testCache.set("C", 3);
System.out.println(testCache.get("B"));
System.out.println(testCache.get("A"));
testCache.set("D", 4);
System.out.println(testCache.get("D"));
System.out.println(testCache.get("C"));
}
}
2. 設計可以變更的緩存結構(LFU)
設計思路:對於使用次數相同的節點,放到同一個雙向鏈表裏。然後這些雙向鏈表之間相連
2.1 設計節點
public static class Node {
public Integer key;
public Integer value;
public Integer times;
public Node up;//上指針
public Node down;//下指針
public Node(int key, int value, int times) {
this.key = key;
this.value = value;
this.times = times;
}
}
節點的結構如下圖
2.2 設計雙向鏈表
public static class NodeList {
public Node head;
public Node tail;
public NodeList last;
public NodeList next;
public NodeList(Node node) {
head = node;
tail = node;
}
...
}
NodeList的結構如下圖
2.3 設計緩存結構
public static class LFUCache {
public static class NodeList {
...
}
private int capacity;//最大容量
private int size;//當前總節點個數
private HashMap<Integer, Node> records;//將所有節點保存在哈希表中,key值爲節點的key屬性
private HashMap<Node, NodeList> heads;//將所有鏈表存入哈希表中,key值爲鏈表的頭
private NodeList headList;//指向第一個鏈表
public LFUCache(int capacity) {
this.capacity = capacity;
this.size = 0;
this.records = new HashMap<>();
this.heads = new HashMap<>();
headList = null;
}
...
}
如下圖:每一個NodeList中,存放的都是出現次數相同的節點。可以選擇尾節點優先級最低。如果某NodeList中沒有節點了,刪除此NodeList
import java.util.HashMap;
public class Code_03_LFU {
public static class Node {
public Integer key;
public Integer value;
public Integer times;
public Node up;
public Node down;
public Node(int key, int value, int times) {
this.key = key;
this.value = value;
this.times = times;
}
}
public static class LFUCache {
public static class NodeList {
public Node head;
public Node tail;
public NodeList last;
public NodeList next;
public NodeList(Node node) {
head = node;
tail = node;
}
public void addNodeFromHead(Node newHead) {
newHead.down = head;
head.up = newHead;
head = newHead;
}
public boolean isEmpty() {
return head == null;
}
public void deleteNode(Node node) {
if (head == tail) {
head = null;
tail = null;
} else {
if (node == head) {
head = node.down;
head.up = null;
} else if (node == tail) {
tail = node.up;
tail.down = null;
} else {
node.up.down = node.down;
node.down.up = node.up;
}
}
node.up = null;
node.down = null;
}
}
private int capacity;
private int size;
private HashMap<Integer, Node> records;
private HashMap<Node, NodeList> heads;
private NodeList headList;
public LFUCache(int capacity) {
this.capacity = capacity;
this.size = 0;
this.records = new HashMap<>();
this.heads = new HashMap<>();
headList = null;
}
public void set(int key, int value) {
if (records.containsKey(key)) {
Node node = records.get(key);
node.value = value;
node.times++;
NodeList curNodeList = heads.get(node);
move(node, curNodeList);
} else {
if (size == capacity) {
Node node = headList.tail;
headList.deleteNode(node);
modifyHeadList(headList);
records.remove(node.key);
heads.remove(node);
size--;
}
Node node = new Node(key, value, 1);
if (headList == null) {
headList = new NodeList(node);
} else {
if (headList.head.times.equals(node.times)) {
headList.addNodeFromHead(node);
} else {
NodeList newList = new NodeList(node);
newList.next = headList;
headList.last = newList;
headList = newList;
}
}
records.put(key, node);
heads.put(node, headList);
size++;
}
}
private void move(Node node, NodeList oldNodeList) {
oldNodeList.deleteNode(node);
NodeList preList = modifyHeadList(oldNodeList) ? oldNodeList.last
: oldNodeList;
NodeList nextList = oldNodeList.next;
if (nextList == null) {
NodeList newList = new NodeList(node);
if (preList != null) {
preList.next = newList;
}
newList.last = preList;
if (headList == null) {
headList = newList;
}
heads.put(node, newList);
} else {
if (nextList.head.times.equals(node.times)) {
nextList.addNodeFromHead(node);
heads.put(node, nextList);
} else {
NodeList newList = new NodeList(node);
if (preList != null) {
preList.next = newList;
}
newList.last = preList;
newList.next = nextList;
nextList.last = newList;
if (headList == nextList) {
headList = newList;
}
heads.put(node, newList);
}
}
}
// return whether delete this head
private boolean modifyHeadList(NodeList nodeList) {
if (nodeList.isEmpty()) {
if (headList == nodeList) {
headList = nodeList.next;
if (headList != null) {
headList.last = null;
}
} else {
nodeList.last.next = nodeList.next;
if (nodeList.next != null) {
nodeList.next.last = nodeList.last;
}
}
return true;
}
return false;
}
public int get(int key) {
if (!records.containsKey(key)) {
return -1;
}
Node node = records.get(key);
node.times++;
NodeList curNodeList = heads.get(node);
move(node, curNodeList);
return node.value;
}
}
}