设计可以变更的缓存结构

题目

设计一种缓存结构,该结构在构造时确定大小,假设大小为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);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值