設計可以變更的緩存結構

題目

設計一種緩存結構,該結構在構造時確定大小,假設大小爲K,並由兩個功能:

  • set(key,value):將記錄(key,value)插入該結構
  • get(key):返回key對應的value值

要求

1.set和get方法的時間複雜度O(1)。
2.某個key的set或get操作一旦發生,認爲這個key的記錄成了最經常使用的。
3.當緩存的大小超過K時,移除最不經常使用的記錄,即set或get最久遠的。

JAVA代碼實現

package otherquestions;

/**
 * 雙向鏈表節點的結構
 * @author HDian
 *
 * @param <V>
 */
public class Node<V> {

	public V value;
	public Node<V> last;
	public Node<V> next;
	
	public Node(V value) {
		this.value = value;
	}
}

package otherquestions;

/**
 * 基於雙向鏈表節點實現雙向鏈表,實現按照“訪問經常度”排序節點
 * @author HDian
 *
 * @param <V>
 */
public class NodeDoubleLinkedList1217<V> {

	//定義雙向鏈表的頭節點和尾節點
	private Node<V> head;
	private Node<V> tail;
	
	/*
	 * 構造方法重載
	 */
	public NodeDoubleLinkedList1217() {
		this.head = null;
		this.tail = null;
	}
	
	/*
	 * 雙向鏈表中添加新節點
	 */
	public void addNode(Node<V> newNode) {
		if (newNode == null) {
			return;
		}
		if (this.head == null) {
			this.head = newNode;
			this.tail = newNode;
		} else {
			this.head.next = newNode;
			newNode.last = this.tail;
			this.tail = newNode;
		}
	}
	
	
	/*
	 * 最常用的節點放到尾節點處
	 */
	public void moveNodeToTail(Node<V> node) {
		if (this.tail == node) {
			return;
		}
		if (this.head == node) {
			this.head = node.next;
			node.next.last = node.last;
		}
		node.last = this.tail;
		node.next = null;
		this.tail.next = node;
		this.tail = node;
	}
	
	/*
	 * 頭節點刪除
	 */
	public Node<V> removeHead() {
		if (this.head == null) {
			return null;
		}
		Node<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;
	}
}

package otherquestions;

import java.util.HashMap;

public class MyCache1217<K, V> {

	//key到node的映射
	private HashMap<K, Node<V>> keyNodeMap;
	//node到key的映射
	private HashMap<Node<V>, K> nodeKeyMap;
	private NodeDoubleLinkedList1217<V> nodeList;
	private int capacity;
	
	//構造方法重載
	public MyCache1217(int capacity) {
		if (capacity < 1) {
			throw new RuntimeException("should be more than 0.");
		}
		this.keyNodeMap = new HashMap<>();
		this.nodeKeyMap = new HashMap<>();
		this.nodeList = new NodeDoubleLinkedList1217<>();
		this.capacity = capacity;
	}
	
	public V get(K key) {
		if (this.keyNodeMap.containsKey(key)) {
			Node<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<V> node = this.keyNodeMap.get(key);
			node.value = value;
			this.nodeList.moveNodeToTail(node);
		} else {
			Node<V> newNode = new Node<V>(value);
			this.keyNodeMap.put(key, newNode);
			this.nodeKeyMap.put(newNode, key);
			this.nodeList.addNode(newNode);
			if (this.keyNodeMap.size() == this.capacity + 1) {
				this.removeMostUnusedCache();
			}
		}
	}
	
	
	private void removeMostUnusedCache() {
		Node<V> removeNode = this.nodeList.removeHead();
		K removeKey = this.nodeKeyMap.get(removeNode);
		this.nodeKeyMap.remove(removeKey);
		this.keyNodeMap.remove(removeKey);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章