码迷,mamicode.com
首页 > 编程语言 > 详细

HashMap+双向链表手写LRU缓存算法/页面置换算法

时间:2019-01-31 01:31:35      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:port   cap   imp   class   aci   count   mat   .com   页面置换   

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缓存算法/页面置换算法

标签:port   cap   imp   class   aci   count   mat   .com   页面置换   

原文地址:https://www.cnblogs.com/Roni-i/p/10340300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!